ActivityMetaInfo { static Pair<Integer, List<String>> generate(final IOCBeanDef<?> beanDefinition) { AssociatedResources associatedResources = null; DynamicAnnotation dynAssociatedResources = null; Priority priority = null; DynamicAnnotation dynPriority = null; final Set<Annotation> annotations = beanDefinition.getQualifiers(); final boolean dynamic = beanDefinition.isDynamic(); for (Annotation a : annotations) { final DynamicAnnotation da = (dynamic) ? (DynamicAnnotation) a : null; if (a instanceof AssociatedResources) { associatedResources = (AssociatedResources) a; } else if (da != null && AssociatedResources.class.getName().equals(da.getName())) { dynAssociatedResources = da; } else if (a instanceof Priority) { priority = (Priority) a; } else if (da != null && Priority.class.getName().equals(da.getName())) { dynPriority = da; } } if (associatedResources == null && dynAssociatedResources == null) { return null; } final int priorityValue; if (priority == null && dynPriority == null) { priorityValue = 0; } else { if (dynamic) { priorityValue = Integer.valueOf(dynPriority.getMember("value")); } else { priorityValue = priority.value(); } } final List<String> types = new ArrayList<String>(); if (dynamic) { String resourceTypes = dynAssociatedResources.getMember("value"); resourceTypes = resourceTypes.substring(1, resourceTypes.length() - 1); types.addAll(Arrays.asList(resourceTypes.split(","))); } else { for (Class<? extends ClientResourceType> type : associatedResources.value()) { types.add(type.getName()); } } return Pair.newPair(priorityValue, types); }    }
@Test public void generateNotGenerateActivityMetaInfo() { IOCBeanDef<?> beanDefinition = mock(IOCBeanDef.class); when(beanDefinition.getQualifiers()).thenReturn(Collections.<Annotation>emptySet()); Pair<Integer, List<String>> nullGenerated = ActivityMetaInfo.generate(beanDefinition); assertNull(nullGenerated); }
@Test @SuppressWarnings("unchecked") public void generateActivityMetaInfo() { IOCBeanDef<?> beanDefinition = mock(IOCBeanDef.class); Priority priority = mock(Priority.class); Integer priorityValue = 1; when(priority.value()).thenReturn(priorityValue); Set<Annotation> qualifiers = new HashSet<Annotation>(); AssociatedResources associatedResources = mock(AssociatedResources.class); final List<Class<? extends ClientResourceType>> typesList = new ArrayList<Class<? extends ClientResourceType>>(); typesList.add(ClientResourceType.class); Class<? extends ClientResourceType>[] array = typesList.toArray(new Class[typesList.size()]); when(associatedResources.value()).thenReturn(array); qualifiers.add(associatedResources); qualifiers.add(priority); when(beanDefinition.getQualifiers()).thenReturn(qualifiers); Pair<Integer, List<String>> generated = ActivityMetaInfo.generate(beanDefinition); assertEquals(priorityValue, generated.getK1()); assertTrue(generated.getK2().contains(ClientResourceType.class.getName())); }
@Test public void generateActivityMetaInfoForDynamicActivity() { final String otherResourceType = "org.uberfire.OtherResourceType"; IOCBeanDef<?> beanDefinition = mock(IOCBeanDef.class); DynamicAnnotation priority = mock(DynamicAnnotation.class); when(priority.getName()).thenReturn(Priority.class.getName()); when(priority.getMember("value")).thenReturn("1"); Set<Annotation> qualifiers = new HashSet<Annotation>(); DynamicAnnotation associatedResources = mock(DynamicAnnotation.class); when(associatedResources.getName()).thenReturn(AssociatedResources.class.getName()); when(associatedResources.getMember("value")).thenReturn("[" + ClientResourceType.class.getName() + "," + otherResourceType + "]"); qualifiers.add(associatedResources); qualifiers.add(priority); when(beanDefinition.isDynamic()).thenReturn(true); when(beanDefinition.getQualifiers()).thenReturn(qualifiers); Pair<Integer, List<String>> generated = ActivityMetaInfo.generate(beanDefinition); assertEquals(Integer.valueOf(1), generated.getK1()); assertTrue(generated.getK2().contains(ClientResourceType.class.getName())); assertTrue(generated.getK2().contains("org.uberfire.OtherResourceType")); }