FieldLoader { public Object set( Object target, Field field, BackendRdfValue value, boolean extendExisting) throws LoaderException { Class<?> fieldType = field.getType(); if (Collection.class.isAssignableFrom(fieldType)) { return setCollection(target, field, value, extendExisting); } else if (isPrimitive(fieldType)) { FieldUtils.setValue(target, field, valueToPrimitive(fieldType, value)); } else if (fieldType.isEnum()) { FieldUtils.setValue(target, field, valueToEnum(fieldType, value)); } else if (fieldType.isArray()) { throw new LoaderException("Arrays are not supported."); } else { if (LangString.class.isAssignableFrom(fieldType)) { FieldUtils.setValue(target, field, valueToStringLang(fieldType, value)); return null; } if (extendExisting) { final Object currentObject = FieldUtils.getValue(target, field); if (currentObject != null) { return currentObject; } } Object newObject = createInstance(fieldType); FieldUtils.setValue(target, field, newObject); return newObject; } return null; }  Object set(
            Object target, Field field, BackendRdfValue value,
            boolean extendExisting);  }
@Test public void loadInteger() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("intValue"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asLong()).thenReturn(12L); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertEquals(12, instance.intValue); }
@Test public void loadString() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("stringValue"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("value"); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertEquals("value", instance.stringValue); }
@Test public void loadBoolean() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("booleanValue"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertEquals(true, instance.booleanValue); }
@Test public void loadReference() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("reference"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("http: Assert.assertNull(instance.reference); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertNotNull(instance.reference); }
@Test public void loadNewReference() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("reference"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("http: FieldLoader loader = new FieldLoader(); Object firstReference = loader.set(instance, field, value, true); Object secondReference = loader.set(instance, field, value, false); Assert.assertNotEquals(firstReference, secondReference); }
@Test public void extendReference() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("reference"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("http: FieldLoader loader = new FieldLoader(); Object firstReference = loader.set(instance, field, value, true); Object secondReference = loader.set(instance, field, value, true); Assert.assertEquals(firstReference, secondReference); }
@Test public void loadCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("list"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); loader.set(instance, field, value, true); Assert.assertEquals(2, instance.list.size()); }
@Test(expected = LoaderException.class) public void loadNullCollection() throws Exception { TestClass instance = new TestClass(); instance.list = null; Field field = TestClass.class.getDeclaredField("list"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); }
@Test public void loadClearLoadCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("list"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); loader.set(instance, field, value, false); Assert.assertEquals(1, instance.list.size()); }
@Test(expected = LoaderException.class) public void loadArray() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("array"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); }
@Test public void loadPrimitiveCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("intList"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); FieldLoader loader = new FieldLoader(); Mockito.when(value.asLong()).thenReturn(11L); loader.set(instance, field, value, true); Mockito.when(value.asLong()).thenReturn(12L); loader.set(instance, field, value, true); Assert.assertEquals(2, instance.intList.size()); Assert.assertTrue(11 == instance.intList.get(0)); Assert.assertTrue(12 == instance.intList.get(1)); }
@Test public void loadEnum() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("enumValue"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("VALUE_A"); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertEquals(TestEnum.VALUE_A, instance.enumValue); }
@Test public void loadLangString() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("langString"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("value"); Mockito.when(value.getLanguage()).thenReturn("cs"); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertNotNull(instance.langString); Assert.assertEquals("value", instance.langString.value); Assert.assertEquals("cs", instance.langString.language); }
@Test public void loadDate() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("dateValue"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("2010-02-20"); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(instance.dateValue); Assert.assertEquals(2010, calendar.get(Calendar.YEAR)); Assert.assertEquals(1, calendar.get(Calendar.MONTH)); Assert.assertEquals(20, calendar.get(Calendar.DAY_OF_MONTH)); }
@Test(expected = LoaderException.class) public void loadNestedCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("nestedCollection"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); }
@Test public void loadEnumCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("enumCollection"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("VALUE_A"); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertEquals(1, instance.enumCollection.size()); Assert.assertEquals(TestEnum.VALUE_A, instance.enumCollection.get(0)); }
@Test(expected = LoaderException.class) public void loadArrayCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("arrayCollection"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asBoolean()).thenReturn(true); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); }
@Test public void loadLangStringCollection() throws Exception { TestClass instance = new TestClass(); Field field = TestClass.class.getDeclaredField("langStringCollection"); BackendRdfValue value = Mockito.mock(BackendRdfValue.class); Mockito.when(value.asString()).thenReturn("value"); Mockito.when(value.getLanguage()).thenReturn("cs"); FieldLoader loader = new FieldLoader(); loader.set(instance, field, value, true); Assert.assertEquals(1, instance.langStringCollection.size()); TestLangString langString = instance.langStringCollection.get(0); Assert.assertEquals("value", langString.value); Assert.assertEquals("cs", langString.language); }