public void addResourceActivity(ActivityAndMetaInfo activityAndMetaInfo) { getResourceActivities().add(activityAndMetaInfo); List<ClientResourceType> resourceTypes = getResourceTypes(activityAndMetaInfo); this.addAll(resourceTypes); }
/** * Returns the activated activity with the highest priority that can handle the given file. Returns null if no * activated activity can handle the path. * @param path the file to find a path-based activity for (probably a {@link WorkbenchEditorActivity}, but this cache * makes no guarantees). */ public SyncBeanDef<Activity> getActivity(final Path path) { Optional<ActivityAndMetaInfo> optional = resourceTypeManagerCache.getResourceActivities().stream() .filter(activityAndMetaInfo -> activitySupportsPath(activityAndMetaInfo, path)) .findAny(); if(optional.isPresent()) { return optional.get().getActivityBean(); } throw new EditorResourceTypeNotFound(); }
@Before public void setup() { when(authzManager.authorize(any(Resource.class), any(User.class))).thenReturn(true); when(activitiesAuthorizationManager.authorizeActivity(anyObject())).thenReturn(true); activatedActivity = mock(Activity.class); when(activatedActivity.getIdentifier()).thenReturn("activated activity"); when(activatedActivityBean.getInstance()).thenReturn(activatedActivity); when(activatedActivityBean.isActivated()).thenReturn(true); when(nonActivatedActivityBean.isActivated()).thenReturn(false); Collection<SyncBeanDef<Activity>> activityList = new ArrayList<SyncBeanDef<Activity>>(); activityList.add(activatedActivityBean); activityList.add(nonActivatedActivityBean); // This covers the case where the activity manager goes directly to the Errai bean manager. // The list includes all beans, active or otherwise, and the activity manager has to filter them. when(iocManager.lookupBeans(Activity.class)).thenReturn(activityList); // And this covers the case where the activity manager does the lookup via the ActivityBeansCache. // We set this up assuming ActivityBeansCache is well-behaved, and hides the existence of inactive beans. // (of course this assumption is verified in a separate test) ActivityAndMetaInfo activatedActivityAndMetaInfo = new ActivityAndMetaInfo(iocManager, activatedActivityBean, 0, Collections.<String>emptyList()); when(resourceTypeManagerCache.getResourceActivities()).thenReturn(singletonList(activatedActivityAndMetaInfo)); when(activityBeansCache.getActivity("activated activity")).thenReturn(activatedActivityBean); }
@Before public void setup() { when(authzManager.authorize(any(Resource.class), any(User.class))).thenReturn(true); when(activitiesAuthorizationManager.authorizeActivity(anyObject())).thenReturn(true); activatedActivity = mock(Activity.class); when(activatedActivity.getIdentifier()).thenReturn("activated activity"); when(activatedActivityBean.getInstance()).thenReturn(activatedActivity); when(activatedActivityBean.isActivated()).thenReturn(true); when(nonActivatedActivityBean.isActivated()).thenReturn(false); Collection<SyncBeanDef<Activity>> activityList = new ArrayList<SyncBeanDef<Activity>>(); activityList.add(activatedActivityBean); activityList.add(nonActivatedActivityBean); // This covers the case where the activity manager goes directly to the Errai bean manager. // The list includes all beans, active or otherwise, and the activity manager has to filter them. when(iocManager.lookupBeans(Activity.class)).thenReturn(activityList); // And this covers the case where the activity manager does the lookup via the ActivityBeansCache. // We set this up assuming ActivityBeansCache is well-behaved, and hides the existence of inactive beans. // (of course this assumption is verified in a separate test) ActivityAndMetaInfo activatedActivityAndMetaInfo = new ActivityAndMetaInfo(iocManager, activatedActivityBean, 0, Collections.<String>emptyList()); when(resourceTypeManagerCache.getResourceActivities()).thenReturn(singletonList(activatedActivityAndMetaInfo)); when(activityBeansCache.getActivity("activated activity")).thenReturn(activatedActivityBean); }
@Test public void cacheShouldNotReturnInactiveBeansFromGetResourceActivities() throws Exception { activityBeansCache.init(); List<ActivityAndMetaInfo> activityBeans = this.resourceTypeManagerCache.getResourceActivities(); assertEquals(1, activityBeans.size()); assertSame(activeResourceActivityBean, activityBeans.get(0).getActivityBean()); }
@Test public void cacheShouldNotReturnInactiveBeansFromGetResourceActivities() throws Exception { activityBeansCache.init(); List<ActivityAndMetaInfo> activityBeans = this.resourceTypeManagerCache.getResourceActivities(); assertEquals(1, activityBeans.size()); assertSame(activeResourceActivityBean, activityBeans.get(0).getActivityBean()); }
@Test public void addEditorActivityShouldSortResourcesByPriority() { String higherPriority = "20000"; String lowerPriority = "1"; Collection<SyncBeanDef> resourceTypeBeans = createResourceType("MODEL"); when(iocManager.lookupBeans(eq("resource"))).thenReturn(resourceTypeBeans); Collection<SyncBeanDef> resourceTypeBeans1 = createResourceType("MODEL"); when(iocManager.lookupBeans(eq("resource1"))).thenReturn(resourceTypeBeans1); Collection<SyncBeanDef> resourceTypeBeans2 = createResourceType("MODEL"); when(iocManager.lookupBeans(eq("resource2"))).thenReturn(resourceTypeBeans2); SyncBeanDef mock = mock(SyncBeanDef.class); when(mock.getName()).thenReturn("resource1"); cache.addNewEditorActivity(mock, lowerPriority, "resource"); SyncBeanDef mock1 = mock(SyncBeanDef.class); when(mock1.getName()).thenReturn("resource2"); cache.addNewEditorActivity(mock1, higherPriority, "resource1"); List<ActivityAndMetaInfo> resourceActivities = this.resourceTypeManagerCache.getResourceActivities(); assertEquals(resourceActivities.get(0).getPriority(), Integer.valueOf(higherPriority).intValue()); }
@Test public void addEditorActivityShouldSortResourcesByPriority() { String higherPriority = "20000"; String lowerPriority = "1"; Collection<SyncBeanDef> resourceTypeBeans = createResourceType("MODEL"); when(iocManager.lookupBeans(eq("resource"))).thenReturn(resourceTypeBeans); Collection<SyncBeanDef> resourceTypeBeans1 = createResourceType("MODEL"); when(iocManager.lookupBeans(eq("resource1"))).thenReturn(resourceTypeBeans1); Collection<SyncBeanDef> resourceTypeBeans2 = createResourceType("MODEL"); when(iocManager.lookupBeans(eq("resource2"))).thenReturn(resourceTypeBeans2); SyncBeanDef mock = mock(SyncBeanDef.class); when(mock.getName()).thenReturn("resource1"); cache.addNewEditorActivity(mock, lowerPriority, "resource"); SyncBeanDef mock1 = mock(SyncBeanDef.class); when(mock1.getName()).thenReturn("resource2"); cache.addNewEditorActivity(mock1, higherPriority, "resource1"); List<ActivityAndMetaInfo> resourceActivities = this.resourceTypeManagerCache.getResourceActivities(); assertEquals(resourceActivities.get(0).getPriority(), Integer.valueOf(higherPriority).intValue()); }
@Test public void initShouldCacheOtherActivities() throws Exception { ActivityBeansCacheUnitTestWrapper cache = new ActivityBeansCacheUnitTestWrapper(); cache.mockActivityBehaviour(); cache.init(); assertFalse(cache.getResourceTypeManagerCache().getResourceActivities().isEmpty()); }
@Test public void initShouldOrderActivityByPriority() throws Exception { ActivityBeansCacheUnitTestWrapper cache = new ActivityBeansCacheUnitTestWrapper(); int priorityActivityOne = Integer.MIN_VALUE; int priorityActivityTwo = Integer.MAX_VALUE; cache.createActivitiesAndMetaInfo(priorityActivityOne, priorityActivityTwo); ActivityAndMetaInfo firstActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(0); ActivityAndMetaInfo secondActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(1); assertEquals(priorityActivityOne, firstActivityOnList.getPriority()); assertEquals(priorityActivityTwo, secondActivityOnList.getPriority()); cache.getResourceTypeManagerCache().sortResourceActivitiesByPriority(); firstActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(0); secondActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(1); assertEquals(priorityActivityTwo, firstActivityOnList.getPriority()); assertEquals(priorityActivityOne, secondActivityOnList.getPriority()); }
@Test public void initShouldCacheOtherActivities() throws Exception { ActivityBeansCacheUnitTestWrapper cache = new ActivityBeansCacheUnitTestWrapper(); cache.mockActivityBehaviour(); cache.init(); assertFalse(cache.getResourceTypeManagerCache().getResourceActivities().isEmpty()); }
@Test public void initShouldOrderActivityByPriority() throws Exception { ActivityBeansCacheUnitTestWrapper cache = new ActivityBeansCacheUnitTestWrapper(); int priorityActivityOne = Integer.MIN_VALUE; int priorityActivityTwo = Integer.MAX_VALUE; cache.createActivitiesAndMetaInfo(priorityActivityOne, priorityActivityTwo); ActivityAndMetaInfo firstActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(0); ActivityAndMetaInfo secondActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(1); assertEquals(priorityActivityOne, firstActivityOnList.getPriority()); assertEquals(priorityActivityTwo, secondActivityOnList.getPriority()); cache.getResourceTypeManagerCache().sortResourceActivitiesByPriority(); firstActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(0); secondActivityOnList = cache.getResourceTypeManagerCache().getResourceActivities().get(1); assertEquals(priorityActivityTwo, firstActivityOnList.getPriority()); assertEquals(priorityActivityOne, secondActivityOnList.getPriority()); }