CompositeTreePermission implements TreePermission { static TreePermission create(@NotNull Tree rootTree, @NotNull TreeProvider treeProvider, @NotNull TreeTypeProvider typeProvider, @NotNull AggregatedPermissionProvider[] providers, @NotNull CompositionType compositionType) { switch (providers.length) { case 0 : return TreePermission.EMPTY; case 1 : return providers[0].getTreePermission(rootTree, TreeType.DEFAULT, TreePermission.EMPTY); default : int cnt = 0; TreePermission[] treePermissions = new TreePermission[providers.length]; for (int i = 0; i < providers.length; i++) { TreePermission tp = providers[i].getTreePermission(rootTree, TreeType.DEFAULT, TreePermission.EMPTY); if (!isValid(tp)) { cnt++; } treePermissions[i] = tp; } if (compositionType == AND) { return new CompositeTreePermissionAnd(rootTree, TreeType.DEFAULT, treeProvider, typeProvider, providers, treePermissions, cnt); } else { return new CompositeTreePermissionOr(rootTree, TreeType.DEFAULT, treeProvider, typeProvider, providers, treePermissions, cnt); } } } private  CompositeTreePermission(@NotNull Tree tree, @NotNull TreeType type,
                                    @NotNull TreeProvider treeProvider,
                                    @NotNull TreeTypeProvider typeProvider,
                                    @NotNull AggregatedPermissionProvider[] providers,
                                    @NotNull TreePermission[] treePermissions, int cnt); @NotNull @Override TreePermission getChildPermission(@NotNull final String childName, @NotNull final NodeState childState); @Override boolean canRead(); @Override boolean canRead(@NotNull PropertyState property); @Override boolean canReadAll(); @Override boolean canReadProperties(); @Override boolean isGranted(long permissions); @Override boolean isGranted(long permissions, @NotNull PropertyState property);  }
@Test public void testCreateWithTreeType() throws Exception { TreePermission mockTp = mock(TreePermission.class); AggregatedPermissionProvider mockPP = mock(AggregatedPermissionProvider.class); when(mockPP.getTreePermission(any(Tree.class), any(TreeType.class), any(TreePermission.class))).thenReturn(mockTp); TreePermission parent = createRootTreePermission(mockPP, fullScopeProvider); assertCompositeTreePermission(true, parent); Tree t = rootTree.getChild("test"); TreePermission tp = CompositeTreePermission.create(t, getTreeProvider(), (CompositeTreePermission) parent, TreeType.INTERNAL); assertCompositeTreePermission(true, tp); Field typeF = CompositeTreePermission.class.getDeclaredField("type"); typeF.setAccessible(true); assertSame(TreeType.INTERNAL, typeF.get(tp)); verify(mockPP, times(1)).getTreePermission(rootTree, TreeType.DEFAULT, TreePermission.EMPTY); verify(mockPP, times(1)).getTreePermission(t, TreeType.INTERNAL, mockTp); verify(mockPP, never()).getTreePermission(t, TreeType.DEFAULT, parent); }
@Test public void testCreateSingleWithInvalidParent() { AggregatedPermissionProvider mockPP = mock(AggregatedPermissionProvider.class); when(mockPP.getTreePermission(any(Tree.class), any(TreeType.class), any(TreePermission.class))).thenReturn(TreePermission.NO_RECOURSE); TreePermission parent = createRootTreePermission(mockPP, fullScopeProvider); Tree t = rootTree.getChild("test"); TreePermission tp = CompositeTreePermission.create(t, getTreeProvider(), (CompositeTreePermission) parent, TreeType.DEFAULT); assertCompositeTreePermission(false, tp); }
@Test public void testCreateMultipleWithInvalidParent() { AggregatedPermissionProvider mockPP = mock(AggregatedPermissionProvider.class); when(mockPP.getTreePermission(any(Tree.class), any(TreeType.class), any(TreePermission.class))).thenReturn(TreePermission.NO_RECOURSE); TreePermission parent = createRootTreePermission(mockPP, fullScopeProvider, fullScopeProvider); Tree t = rootTree.getChild("test"); TreePermission tp = CompositeTreePermission.create(t, getTreeProvider(), (CompositeTreePermission) parent, TreeType.DEFAULT); assertCompositeTreePermission(true, tp); }
@Test public void testCreateWithInvalidParent() { AggregatedPermissionProvider mockPP = mock(AggregatedPermissionProvider.class); when(mockPP.getTreePermission(any(Tree.class), any(TreeType.class), any(TreePermission.class))).thenReturn(TreePermission.NO_RECOURSE); TreePermission parent = createRootTreePermission(mockPP, mockPP, mockPP); Tree t = rootTree.getChild("test"); TreePermission tp = CompositeTreePermission.create(t, getTreeProvider(), (CompositeTreePermission) parent, TreeType.DEFAULT); assertSame(TreePermission.EMPTY, tp); }