public void testPrivileged() throws Exception { // setting up String namespace_Priv = "previlege"; String namespace_UnPriv = "unprevilege"; String name1 = "one"; String name2 = "two"; String name3 = "three"; String name4 = "four"; Mockito.when(mci.isPrivilegedNamespace(namespace_Priv)).thenReturn(true); Mockito.when(mci.isPrivilegedNamespace(namespace_UnPriv)).thenReturn(false); // create empty stack, sanity check InstanceStack iStack = new InstanceStack(); iStack.setConfigAdapter(mci); assertFalse("stack should has topUnprivileged=null at the beginning", iStack.isUnprivileged()); // start pushing TestInstance one = new TestInstance(namespace_Priv, name1); iStack.pushInstance(one, one.getDescriptor()); assertFalse( "topUnprivileged is still null after pushing in one previleged instance:instance1", iStack.isUnprivileged()); TestInstance two = new TestInstance(namespace_UnPriv, name2); iStack.pushInstance(two, two.getDescriptor()); assertTrue( "topUnprivileged should become first unprivilege instance:instance2", iStack.isUnprivileged()); TestInstance three = new TestInstance(namespace_Priv, name3); iStack.pushInstance(three, three.getDescriptor()); assertTrue( "topUnprivileged should remain unchanged after pushing in a new privilege instance:instance3", iStack.isUnprivileged()); TestInstance four = new TestInstance(namespace_UnPriv, name4); iStack.pushInstance(four, four.getDescriptor()); assertTrue( "topUnprivileged should be unchanged after pushing in a new unprivilege instance:instance4", iStack.isUnprivileged()); // start poping iStack.popInstance(four); assertTrue( "topUnprivileged should be unchanged after poping out unprivilege instance:instance4", iStack.isUnprivileged()); iStack.popInstance(three); assertTrue( "topUnprivileged should be unchanged after poping out privilege instance:instance3", iStack.isUnprivileged()); iStack.popInstance(two); assertFalse( "topUnprivileged should become null after poping out first unprivilege instance:instance2", iStack.isUnprivileged()); iStack.popInstance(one); assertFalse( "topUnprivileged should be unchanged(null) after poping out instance1", iStack.isUnprivileged()); }
@Override public void registerComponent(BaseComponent<?, ?> component) { InstanceStack iStack = getInstanceStack(); if (iStack.isUnprivileged()) { if (componentCount++ > MAX_COMPONENT_COUNT) { // // This is bad, try to give the poor user an idea of what happened. // Action tmp = getCurrentAction(); StringBuffer sb = new StringBuffer(); if (tmp != null) { sb.append(tmp); sb.append("("); tmp.logParams(new SBKeyValueLogger(sb)); sb.append(")"); } else { sb.append("request"); } throw new SystemErrorException("Too many components for " + sb.toString()); } } iStack.registerComponent(component); }