[Major] Implemented InMemoryAuditQuery

This commit is contained in:
Robert von Burg 2014-08-28 21:45:57 +02:00
parent 581cf95e2c
commit 9a3d31ef35
9 changed files with 584 additions and 58 deletions

View File

@ -76,6 +76,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
private TransactionCloseStrategy closeStrategy;
private boolean suppressUpdates;
private boolean suppressAudits;
private TransactionResult txResult;
private List<Command> commands;
@ -163,6 +164,21 @@ public abstract class AbstractTransaction implements StrolchTransaction {
return this.suppressUpdates;
}
/**
* @param suppressAudits
* the suppressAudits to set
*/
public void setSuppressAudits(boolean suppressAudits) {
this.suppressAudits = suppressAudits;
}
/**
* @return the suppressAudits
*/
public boolean isSuppressAudits() {
return this.suppressAudits;
}
@Override
public <T extends StrolchRootElement> void lock(T element) {
this.realm.lock(element);
@ -499,6 +515,8 @@ public abstract class AbstractTransaction implements StrolchTransaction {
private long writeAuditTrail() {
if (!isAuditTrailEnabled())
return 0L;
if (isSuppressAudits())
return 0L;
long auditTrailStart = System.nanoTime();

View File

@ -24,6 +24,8 @@ import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.api.AuditDao;
import li.strolch.runtime.query.inmemory.InMemoryAuditQuery;
import li.strolch.runtime.query.inmemory.InMemoryAuditQueryVisitor;
import ch.eitchnet.utils.collections.DateRange;
import ch.eitchnet.utils.collections.MapOfMaps;
@ -155,8 +157,9 @@ public class InMemoryAuditDao implements AuditDao {
}
@Override
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor) {
// TODO Auto-generated method stub
return null;
public <U> List<U> doQuery(AuditQuery auditQuery, AuditVisitor<U> auditVisitor) {
InMemoryAuditQueryVisitor visitor = new InMemoryAuditQueryVisitor();
InMemoryAuditQuery<U> query = visitor.visit(auditQuery, auditVisitor);
return query.doQuery(this);
}
}

View File

@ -17,16 +17,13 @@ package li.strolch.runtime.query.inmemory;
import java.util.List;
import li.strolch.model.StrolchElement;
import li.strolch.persistence.api.StrolchDao;
import li.strolch.model.audit.Audit;
import li.strolch.persistence.api.AuditDao;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AnyNavigator<T extends StrolchElement> implements Navigator<T> {
public interface AuditNavigator {
@Override
public List<T> navigate(StrolchDao<T> dao) {
return dao.queryAll();
}
}
public abstract List<Audit> navigate(AuditDao dao);
}

View File

@ -0,0 +1,122 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.ActionSelection;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.ElementSelection;
import li.strolch.model.audit.IdentitySelection;
import li.strolch.model.query.StringSelection;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public abstract class AuditSelector {
public abstract boolean select(Audit audit);
public static AuditSelector selectorFor(ElementSelection selection) {
return new ElementSelector(selection);
}
public static AuditSelector selectorFor(IdentitySelection selection) {
return new IdentitySelector(selection);
}
public static AuditSelector selectorFor(ActionSelection selection) {
return new ActionSelector(selection);
}
private static class ElementSelector extends AuditSelector {
private StringSelection elementAccessedSelection;
public ElementSelector(ElementSelection selection) {
this.elementAccessedSelection = selection.getElementAccessedSelection();
}
@Override
public boolean select(Audit audit) {
return this.elementAccessedSelection.matches(audit.getElementAccessed());
}
}
public static class IdentitySelector extends AuditSelector {
private StringSelection firstnameSelection;
private StringSelection lastnameSelection;
private StringSelection usernameSelection;
public IdentitySelector(IdentitySelection selection) {
this.firstnameSelection = selection.getFirstnameSelection();
this.lastnameSelection = selection.getLastnameSelection();
this.usernameSelection = selection.getUsernameSelection();
}
@Override
public boolean select(Audit audit) {
if (this.firstnameSelection != null) {
if (!this.firstnameSelection.matches(audit.getFirstname()))
return false;
}
if (this.lastnameSelection != null) {
if (!this.lastnameSelection.matches(audit.getLastname()))
return false;
}
if (this.usernameSelection != null) {
if (!this.usernameSelection.matches(audit.getUsername()))
return false;
}
return true;
}
}
public static class ActionSelector extends AuditSelector {
private Set<AccessType> accessTypes;
private StringSelection actionSelection;
public ActionSelector(ActionSelection selection) {
if (selection.getAccessTypes() != null && selection.getAccessTypes().length != 0)
this.accessTypes = new HashSet<>(Arrays.asList(selection.getAccessTypes()));
this.actionSelection = selection.getActionSelection();
}
@Override
public boolean select(Audit audit) {
if (this.accessTypes != null) {
if (!this.accessTypes.contains(audit.getAccessType()))
return false;
}
if (this.actionSelection != null) {
if (!this.actionSelection.matches(audit.getAction()))
return false;
}
return true;
}
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import java.util.List;
import li.strolch.model.audit.Audit;
import li.strolch.persistence.api.AuditDao;
import ch.eitchnet.utils.collections.DateRange;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AuditTypeNavigator implements AuditNavigator {
private String type;
private DateRange dateRange;
public AuditTypeNavigator(String type, DateRange dateRange) {
this.type = type;
this.dateRange = dateRange;
}
@Override
public List<Audit> navigate(AuditDao dao) {
return dao.queryAll(this.type, this.dateRange);
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import java.util.ArrayList;
import java.util.List;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.persistence.inmemory.InMemoryAuditDao;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
* @param <U>
*/
public class InMemoryAuditQuery<U> {
private AuditTypeNavigator navigator;
private List<AuditSelector> selectors;
private AuditVisitor<U> auditVisitor;
/**
* @param navigator
* @param selectors
* @param auditVisitor
*/
public InMemoryAuditQuery(AuditTypeNavigator navigator, List<AuditSelector> selectors, AuditVisitor<U> auditVisitor) {
DBC.PRE.assertNotNull("Navigator must be set!", navigator);
DBC.PRE.assertNotNull("selectors must be set!", selectors);
DBC.PRE.assertNotNull("auditVisitor must be set!", auditVisitor);
this.navigator = navigator;
this.selectors = selectors;
this.auditVisitor = auditVisitor;
}
public List<U> doQuery(InMemoryAuditDao dao) {
List<U> result = new ArrayList<U>();
List<Audit> elements = this.navigator.navigate(dao);
for (Audit audit : elements) {
if (!this.selectors.isEmpty()) {
boolean nok = false;
for (AuditSelector selector : this.selectors) {
if (!selector.select(audit)) {
nok = true;
break;
}
}
if (nok)
continue;
}
U returnValue = this.auditVisitor.visitAudit(audit);
DBC.INTERIM.assertNotNull("Visitor may not return null in query!", returnValue); //$NON-NLS-1$
result.add(returnValue);
}
return result;
}
}

View File

@ -0,0 +1,72 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import java.util.ArrayList;
import java.util.List;
import li.strolch.model.audit.ActionSelection;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.AuditQueryVisitor;
import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.audit.ElementSelection;
import li.strolch.model.audit.IdentitySelection;
import ch.eitchnet.utils.collections.DateRange;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InMemoryAuditQueryVisitor implements AuditQueryVisitor {
private AuditTypeNavigator navigator;
private List<AuditSelector> selectors;
public <U> InMemoryAuditQuery<U> visit(AuditQuery auditQuery, AuditVisitor<U> auditVisitor) {
DBC.PRE.assertNotNull("auditVisitor may not be null!", auditVisitor); //$NON-NLS-1$
this.selectors = new ArrayList<>();
auditQuery.accept(this);
if (this.navigator == null) {
String msg = "Query is missing a navigation!"; //$NON-NLS-1$
throw new QueryException(msg);
}
return new InMemoryAuditQuery<U>(this.navigator, this.selectors, auditVisitor);
}
@Override
public void visit(ElementSelection selection) {
this.selectors.add(AuditSelector.selectorFor(selection));
}
@Override
public void visit(IdentitySelection selection) {
this.selectors.add(AuditSelector.selectorFor(selection));
}
@Override
public void visit(ActionSelection selection) {
this.selectors.add(AuditSelector.selectorFor(selection));
}
@Override
public void visit(AuditQuery auditQuery) {
String type = auditQuery.getElementTypeSelection();
DateRange dateRange = auditQuery.getDateRange();
this.navigator = new AuditTypeNavigator(type, dateRange);
}
}

View File

@ -0,0 +1,212 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.runtime.query.inmemory;
import static org.junit.Assert.assertEquals;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import li.strolch.model.ModelGenerator;
import li.strolch.model.Tags;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.persistence.inmemory.InMemoryAuditDao;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.eitchnet.utils.StringMatchMode;
import ch.eitchnet.utils.collections.DateRange;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AuditQueryTest {
private static Date past;
private static Date earlier;
private static Date current;
private static Date later;
private static Date future;
@BeforeClass
public static void beforeClass() throws SQLException {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(2000, 1, 1);
past = cal.getTime();
cal.set(2000, 4, 1);
earlier = cal.getTime();
cal.set(2000, 6, 1);
current = cal.getTime();
cal.set(2000, 8, 1);
later = cal.getTime();
cal.set(2000, 11, 1);
future = cal.getTime();
}
@Test
public void shouldQueryTypeAndDateRange() throws SQLException {
AuditQuery query = new AuditQuery(Tags.AUDIT, new DateRange().from(earlier, true).to(later, true));
performQuery(query, Arrays.asList(0L, 1L, 2L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(current, true).to(current, true));
performQuery(query, Arrays.asList(1L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(current, true));
performQuery(query, Arrays.asList(1L, 2L, 3L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().to(current, true));
performQuery(query, Arrays.asList(0L, 1L, 3L, 4L));
query = new AuditQuery(Tags.RESOURCE, new DateRange().from(past, true).to(future, true));
performQuery(query, Arrays.<Long> asList());
}
@Test
public void shouldQueryAudits() throws SQLException {
AuditQuery query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ);
performQuery(query, Arrays.asList(0L, 1L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE);
performQuery(query, Arrays.asList(0L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ)
.actions(StringMatchMode.EQUALS_CASE_SENSITIVE, "create", "read");
performQuery(query, Arrays.asList(0L, 1L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.action().accessTypes(AccessType.CREATE, AccessType.READ)
.actions(StringMatchMode.EQUALS_CASE_SENSITIVE, "read");
performQuery(query, Arrays.asList(1L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementsAccessed(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "crea");
performQuery(query, Arrays.asList(0L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementsAccessed(StringMatchMode.CONTAINS_CASE_SENSITIVE, "crea");
performQuery(query, Arrays.<Long> asList());
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.element().elementsAccessed(StringMatchMode.EQUALS_CASE_INSENSITIVE, "create");
performQuery(query, Arrays.asList(0L, 4L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier");
performQuery(query, Arrays.asList(0L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier", "later");
performQuery(query, Arrays.asList(0L, 2L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier")
.firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn");
performQuery(query, Arrays.asList(0L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().usernames(StringMatchMode.EQUALS_CASE_INSENSITIVE, "earlier")
.firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn")
.lastnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "kennedy");
performQuery(query, Arrays.asList(0L));
query = new AuditQuery(Tags.AUDIT, new DateRange().from(past, true).to(future, true));
query.identity().firstnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "enn")
.lastnames(StringMatchMode.CONTAINS_CASE_INSENSITIVE, "kennedy");
performQuery(query, Arrays.asList(0L, 1L, 2L, 3L, 4L));
}
private void performQuery(AuditQuery query, List<Long> expected) throws SQLException {
InMemoryAuditDao dao = new InMemoryAuditDao();
dao.saveAll(getAudits());
List<Audit> result = dao.doQuery(query, new NoStrategyAuditVisitor());
Set<Long> ids = new HashSet<>();
for (Audit audit : result) {
ids.add(audit.getId());
}
assertEquals(new HashSet<>(expected), new HashSet<>(ids));
}
private static List<Audit> getAudits() {
List<Audit> audits = new ArrayList<>();
int i = 0;
Audit randomAudit;
randomAudit = ModelGenerator.randomAudit();
randomAudit.setId(i++);
randomAudit.setUsername("earlier");
randomAudit.setDate(earlier);
randomAudit.setAccessType(AccessType.CREATE);
randomAudit.setAction("create");
randomAudit.setElementAccessed(randomAudit.getAccessType().name());
audits.add(randomAudit);
randomAudit = ModelGenerator.randomAudit();
randomAudit.setId(i++);
randomAudit.setDate(current);
randomAudit.setUsername("current");
randomAudit.setAccessType(AccessType.READ);
randomAudit.setAction("read");
randomAudit.setElementAccessed(randomAudit.getAccessType().name());
audits.add(randomAudit);
randomAudit = ModelGenerator.randomAudit();
randomAudit.setId(i++);
randomAudit.setDate(later);
randomAudit.setUsername("later");
randomAudit.setAccessType(AccessType.UPDATE);
randomAudit.setAction("update");
randomAudit.setElementAccessed(randomAudit.getAccessType().name());
audits.add(randomAudit);
randomAudit = ModelGenerator.randomAudit();
randomAudit.setId(i++);
randomAudit.setDate(current);
randomAudit.setUsername("current");
randomAudit.setAccessType(AccessType.DELETE);
randomAudit.setAction("delete");
randomAudit.setElementAccessed(randomAudit.getAccessType().name());
audits.add(randomAudit);
randomAudit = ModelGenerator.randomAudit();
randomAudit.setId(i++);
randomAudit.setDate(current);
randomAudit.setUsername("current");
randomAudit.setAccessType(AccessType.CREATE);
randomAudit.setAction("create");
randomAudit.setElementAccessed(randomAudit.getAccessType().name());
audits.add(randomAudit);
return audits;
}
}

View File

@ -29,7 +29,14 @@ import li.strolch.model.State;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.FloatParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.visitor.NoStrategyVisitor;
import li.strolch.model.query.IdSelection;
import li.strolch.model.query.NameSelection;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ParameterSelection;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.query.StrolchTypeNavigation;
import li.strolch.model.visitor.NoStrategyOrderVisitor;
import li.strolch.model.visitor.NoStrategyResourceVisitor;
import li.strolch.persistence.inmemory.InMemoryOrderDao;
import li.strolch.persistence.inmemory.InMemoryResourceDao;
@ -50,12 +57,9 @@ public class InMemoryQueryTest {
InMemoryOrderDao dao = new InMemoryOrderDao();
dao.saveAll(orders);
InMemoryQuery<Order, Order> orderQuery = new InMemoryQuery<>();
orderQuery.setNavigator(new AnyNavigator<Order>());
orderQuery.setSelector(new IdSelector<Order>("@1"));
orderQuery.setElementVisitor(new NoStrategyVisitor<Order>());
List<Order> result = orderQuery.doQuery(dao);
OrderQuery orderQuery = new OrderQuery(new StrolchTypeNavigation("MyType1"));
orderQuery.with(new IdSelection("@1"));
List<Order> result = dao.doQuery(orderQuery, new NoStrategyOrderVisitor());
assertEquals(1, result.size());
assertEquals("@1", result.get(0).getId());
}
@ -67,12 +71,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
InMemoryQuery<Resource, Resource> resourceQuery = new InMemoryQuery<>();
resourceQuery.setNavigator(new AnyNavigator<Resource>());
resourceQuery.setSelector(new IdSelector<Resource>("@1"));
resourceQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
resourceQuery.with(new IdSelection("@1"));
List<Resource> result = resourceQuery.doQuery(dao);
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
assertEquals(1, result.size());
assertEquals("@1", result.get(0).getId());
}
@ -84,14 +86,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
InMemoryQuery<Resource, Resource> resourceQuery = new InMemoryQuery<>();
resourceQuery.setNavigator(new AnyNavigator<Resource>());
resourceQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
BooleanSelector<Resource> andSelector = new OrSelector<>(new IdSelector<Resource>("@3"),
new IdSelector<Resource>("@4"));
resourceQuery.setSelector(andSelector);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
resourceQuery.or().with(new IdSelection("@3"), new IdSelection("@4"));
List<Resource> result = resourceQuery.doQuery(dao);
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
assertEquals(2, result.size());
assertEquals("@3", result.get(0).getId());
assertEquals("@4", result.get(1).getId());
@ -104,16 +102,11 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
InMemoryQuery<Resource, Resource> resourceQuery = new InMemoryQuery<>();
resourceQuery.setNavigator(new AnyNavigator<Resource>());
resourceQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
List<Selector<Resource>> andSelectors = new ArrayList<>();
andSelectors.add(new IdSelector<Resource>("@3"));
andSelectors.add(new NameSelector<Resource>("Res 3", StringMatchMode.EQUALS_CASE_SENSITIVE));
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
resourceQuery.setSelector(andSelector);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
resourceQuery.and().with(new IdSelection("@3"),
new NameSelection("Res 3", StringMatchMode.EQUALS_CASE_SENSITIVE));
List<Resource> result = resourceQuery.doQuery(dao);
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
assertEquals(1, result.size());
assertEquals("@3", result.get(0).getId());
}
@ -125,16 +118,10 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
InMemoryQuery<Resource, Resource> resourceQuery = new InMemoryQuery<>();
resourceQuery.setNavigator(new AnyNavigator<Resource>());
resourceQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
List<Selector<Resource>> andSelectors = new ArrayList<>();
andSelectors.add(new IdSelector<Resource>("@3"));
andSelectors.add(new NameSelector<Resource>("Res 4", StringMatchMode.EQUALS_CASE_SENSITIVE));
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
resourceQuery.setSelector(andSelector);
ResourceQuery resourceQuery = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
resourceQuery.and().with(new IdSelection("@3"), new NameSelection("@4", StringMatchMode.EQUALS_CASE_SENSITIVE));
List<Resource> result = resourceQuery.doQuery(dao);
List<Resource> result = dao.doQuery(resourceQuery, new NoStrategyResourceVisitor());
assertEquals(0, result.size());
}
@ -146,17 +133,15 @@ public class InMemoryQueryTest {
InMemoryResourceDao dao = new InMemoryResourceDao();
dao.saveAll(resources);
InMemoryQuery<Resource, Resource> ballQuery = new InMemoryQuery<>();
ballQuery.setNavigator(new AnyNavigator<Resource>());
ballQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
AndSelector<Resource> andSelector = new AndSelector<>();
andSelector.with(ParameterSelector.<Resource> stringSelector("parameters", "color", "red",
StringMatchMode.EQUALS_CASE_SENSITIVE));
andSelector.with(ParameterSelector.<Resource> booleanSelector("parameters", "forChildren", true));
andSelector.with(ParameterSelector.<Resource> floatSelector("parameters", "diameter", 22.0));
ballQuery.setSelector(andSelector);
ResourceQuery ballQuery = new ResourceQuery(new StrolchTypeNavigation("Ball"));
ballQuery
.and()
.with( //
ParameterSelection.stringSelection("parameters", "color", "red", StringMatchMode.EQUALS_CASE_SENSITIVE),
ParameterSelection.booleanSelection("parameters", "forChildren", true),
ParameterSelection.floatSelection("parameters", "diameter", 22.0));
List<Resource> result = ballQuery.doQuery(dao);
List<Resource> result = dao.doQuery(ballQuery, new NoStrategyResourceVisitor());
assertEquals(1, result.size());
}