### Question:
FieldReferringClassFactory { public static ValueProvider create(Class<?> targetClass, String identifier, String propertyName) { String key = nameForType(targetClass, propertyName) + '$' + identifier; return valueProviders.computeIfAbsent(key, k -> createFieldReferringInstance(targetClass, identifier, propertyName)); } private  FieldReferringClassFactory(); static ValueProvider create(Class<?> targetClass, String identifier, String propertyName);  }### Answer:
@Test void shouldProperlyGenerateFieldBasedValueProvider() { TestClass testClass = new TestClass("justDoIt"); ValueProvider valueProvider = FieldReferringClassFactory.create(TestClass.class, "testClass", "aString"); Object result = valueProvider.get(new PredicateContext("any", new Object(), ImmutableMap.of("testClass", testClass), emptyMap(), null)); assertThat(result).isEqualTo("justDoIt"); }
@Test void shouldCallSimpleGetterForBooleanField() { SimpleGetterClass simpleGetterClass = new SimpleGetterClass(true); ValueProvider valueProvider = FieldReferringClassFactory.create(SimpleGetterClass.class, "simpleGetterClass", "flag"); Object result = valueProvider.get(new PredicateContext("any", new Object(), ImmutableMap.of("simpleGetterClass", simpleGetterClass), emptyMap(), null)); assertThat(result).isEqualTo(true); assertThat(simpleGetterClass.getterExecutionCounter).isEqualTo(1); }
@Test void shouldCallBooleanFieldGetterForBooleanField() { BooleanFieldGetterClass booleanFieldGetterClass = new BooleanFieldGetterClass(true); ValueProvider valueProvider = FieldReferringClassFactory.create(BooleanFieldGetterClass.class, "booleanFieldGetterClass", "flag"); Object result = valueProvider.get(new PredicateContext("any", new Object(), ImmutableMap.of("booleanFieldGetterClass", booleanFieldGetterClass), emptyMap(), null)); assertThat(result).isEqualTo(true); assertThat(booleanFieldGetterClass.getterExecutionCounter).isEqualTo(1); }
@Test void shouldCallSimpleFieldGetterForBooleanFieldWhenTwoGettersAvailable() { BothBooleanGettersClass bothBooleanGettersClass = new BothBooleanGettersClass(true); ValueProvider valueProvider = FieldReferringClassFactory.create(BothBooleanGettersClass.class, "bothBooleanGettersClass", "flag"); Object result = valueProvider.get(new PredicateContext("any", new Object(), ImmutableMap.of("bothBooleanGettersClass", bothBooleanGettersClass), emptyMap(), null)); assertThat(result).isEqualTo(true); assertThat(bothBooleanGettersClass.simpleGetterExecutionCounter).isEqualTo(1); assertThat(bothBooleanGettersClass.booleanFieldGetterExecutionCounter).isEqualTo(0); }
@Test void shouldCallSimpleFieldGetterForBooleanWrapperFieldWhenTwoGettersAvailable() { BooleanWrapperBothBooleanGettersClass booleanWrapperBothBooleanGettersClass = new BooleanWrapperBothBooleanGettersClass(true); ValueProvider valueProvider = FieldReferringClassFactory.create(BooleanWrapperBothBooleanGettersClass.class, "booleanWrapperBothBooleanGettersClass", "flag"); Object result = valueProvider.get(new PredicateContext("any", new Object(), ImmutableMap.of("booleanWrapperBothBooleanGettersClass", booleanWrapperBothBooleanGettersClass), emptyMap(), null)); assertThat(result).isEqualTo(true); assertThat(booleanWrapperBothBooleanGettersClass.simpleGetterExecutionCounter).isEqualTo(1); assertThat(booleanWrapperBothBooleanGettersClass.booleanFieldGetterExecutionCounter).isEqualTo(0); }