AnnotatedObjectFactory { public <T> Map<RepositoryObjectKey, T> createObjects(Class<T> objectClass, Annotation annotation) { Map<String, T> rawObjectInstances; ObjectInstance[] instanceDescriptors = extractInstanceDescriptors(annotation); if (instanceDescriptors.length > 0) { rawObjectInstances = createObjectsFromInstanceDescriptors(objectClass, instanceDescriptors); } else { rawObjectInstances = createObject(objectClass, annotation); } int objectOrder = extractOrder(annotation); Map<RepositoryObjectKey, T> createdObjects = new HashMap<RepositoryObjectKey, T>(); for (Entry<String, T> objectEntry : rawObjectInstances.entrySet()) { createdObjects.put(new RepositoryObjectKey(objectEntry.getKey(), objectOrder), objectEntry.getValue()); } return createdObjects; }  Map<RepositoryObjectKey, T> createObjects(Class<T> objectClass, Annotation annotation); static final int DEFAULT_ORDER_VALUE; }
@Test public void shouldCreateSingleObjectWithGivenValueAsKey() { Annotation annotation = annotation().withValue("TEST_CODE").build(); Map<RepositoryObjectKey, Object> objects = annotatedObjectFactory.createObjects(Object.class, annotation); assertThatItemMap(objects).containsRepositoryKey("TEST_CODE").hasSize(1); }
@Test public void shouldCreateSingleInstanceAndReturnItUnderDifferentKeysWhenUsingValuesProperty() { Annotation annotation = annotation().withValues("TEST_CODE1", "TEST_CODE2").build(); Map<RepositoryObjectKey, Object> objects = annotatedObjectFactory.createObjects(Object.class, annotation); assertThatItemMap(objects).containsObjectsThatAreSame("TEST_CODE1", "TEST_CODE2").hasSize(2); }
@Test public void shouldCreateMultipleInstancesOfObjectWhenUsingInstanceDescriptors() { Annotation annotation = annotation().withInstanceDescriptor("TEST_CODE1", new String[]{}) .withInstanceDescriptor("TEST_CODE2", new String[]{"PROPERTY_VALUE"}).build(); Map<RepositoryObjectKey, ClassWithStringConstructor> objects = annotatedObjectFactory.createObjects(ClassWithStringConstructor.class, annotation); assertThatItemMap(objects).containsObjectsThatAreNotSame("TEST_CODE1", "TEST_CODE2").hasSize(2); assertThat(objects.get(RepositoryObjectKey.withKey("TEST_CODE2")).property).isSameAs("PROPERTY_VALUE"); }