DefaultQueryDescriptor implements QueryDescriptor { @Override public boolean matches(Object entity, Invocation invocation) { log.info("Matching " + entity + " against " + invocation); final List<List<Parameter>> branches = getBranches(); if (branches.isEmpty()) { return entity != null; } final BeanWrapper wrapper = new BeanWrapperImpl(entity); for (List<Parameter> branch : branches) { boolean matches = true; for (Parameter parameter : branch) { final boolean ignoreCase; ignoreCase = parameter.getModifiers().contains(Modifier.IGNORE_CASE); Object value = wrapper.getPropertyValue(parameter.getPath()); if (ignoreCase && value instanceof String) { value = ((String) value).toLowerCase(); } final Operator operator = parameter.getOperator(); final Object[] properties = new Object[operator.getOperands()]; for (int i = 0; i < operator.getOperands(); i++) { properties[i] = invocation.getArguments()[parameter.getIndices()[i]]; if (ignoreCase && properties[i] != null && properties[i] instanceof String) { properties[i] = ((String) properties[i]).toLowerCase(); } } if (!operator.getMatcher().matches(parameter, value, properties)) { matches = false; break; } } if (matches) { return true; } } return false; } DefaultQueryDescriptor(boolean distinct, String function, int limit, PageParameterExtractor pageExtractor,
                                  SortParameterExtractor sortExtractor, List<List<Parameter>> branches,
                                  RepositoryFactoryConfiguration configuration, RepositoryMetadata repositoryMetadata); @Override RepositoryFactoryConfiguration getConfiguration(); @Override RepositoryMetadata getRepositoryMetadata(); @Override boolean isDistinct(); @Override String getFunction(); @Override int getLimit(); @Override Page getPage(Invocation invocation); @Override Sort getSort(Invocation invocation); @Override List<List<Parameter>> getBranches(); @Override boolean matches(Object entity, Invocation invocation); @Override String toString();  }
@Test public void testMatchingWhenThereAreNotAnyConditions() { final List<List<Parameter>> branches = Collections.emptyList(); final DefaultQueryDescriptor descriptor = new DefaultQueryDescriptor(false, null, 0, null, null, branches, null, null); assertThat(descriptor.matches(null, new ImmutableInvocation(null, null)), is(false)); assertThat(descriptor.matches(new Object(), new ImmutableInvocation(null, null)), is(true)); }
@Test public void testThatEachBranchIsConjunctive() { final List<List<Parameter>> branches = new ArrayList<>(); final ArrayList<Parameter> branch = new ArrayList<>(); branches.add(branch); branch.add(new ImmutableParameter("firstName", Collections.emptySet(), new int[]{0}, operatorContext.getBySuffix("Equals"))); branch.add(new ImmutableParameter("lastName", Collections.emptySet(), new int[]{1}, operatorContext.getBySuffix("IsNot"))); final DefaultQueryDescriptor descriptor = new DefaultQueryDescriptor(false, null, 0, null, null, branches, null, null); final Person person = new Person().setFirstName("X").setLastName("Y"); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"A", "Y"})), is(false)); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"X", "Y"})), is(false)); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"A", "B"})), is(false)); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"X", "B"})), is(true)); }
@Test public void testThatBranchesAreDisjunctive() { final List<List<Parameter>> branches = new ArrayList<>(); final ArrayList<Parameter> first = new ArrayList<>(); final ArrayList<Parameter> second = new ArrayList<>(); final ArrayList<Parameter> third = new ArrayList<>(); branches.add(first); branches.add(second); branches.add(third); first.add(new ImmutableParameter("firstName", Collections.emptySet(), new int[]{0}, operatorContext.getBySuffix("Equals"))); first.add(new ImmutableParameter("lastName", Collections.emptySet(), new int[]{1}, operatorContext.getBySuffix("Equals"))); second.add(new ImmutableParameter("id", Collections.emptySet(), new int[]{2}, operatorContext.getBySuffix("Equals"))); third.add(new ImmutableParameter("address.city", Collections.emptySet(), new int[]{3}, operatorContext.getBySuffix("Equals"))); third.add(new ImmutableParameter("address.street", Collections.emptySet(), new int[]{4}, operatorContext.getBySuffix("Equals"))); final DefaultQueryDescriptor descriptor = new DefaultQueryDescriptor(false, null, 0, null, null, branches, null, null); final Person person = new Person().setId("1").setFirstName("X").setLastName("Y").setAddress( new Address().setCity("Shiraz").setStreet("Chaharbagh")); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"X", "Y", "2", "Mashad", "Reza"})), is(true)); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"A", "B", "1", "Mashad", "Reza"})), is(true)); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"A", "B", "3", "Shiraz", "Chaharbagh"})), is(true)); assertThat(descriptor.matches(person, new ImmutableInvocation(null, new Object[]{"A", "B", "3", "Tehran", "Tajrish"})), is(false)); }