public Result testNotWritable() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); /* Role is not readable */ RoleInfo[] rInfo = { new RoleInfo("role", classPath, true, false) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); try { rService.createRelation("relation", "relationType", unitRoles); return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public void borrowBooks(String relationId, String roleName, ObjectName bookToAdd) { Logger logger = getLogger(); try { // get the old values ArrayList oldRoleValue = getRoleValue(relationId, roleName); ArrayList newRoleValue = (ArrayList)oldRoleValue.clone(); newRoleValue.add(bookToAdd); // now we update the values Role role = new Role(roleName, newRoleValue); Object[] params1 = {relationId, role}; String[] signature1 = {"java.lang.String", "javax.management.relation.Role"}; m_server.invoke(m_relationObjectName, "setRole", params1, signature1); } catch (Exception ex) { logger.error("Unable to add a book"); ex.printStackTrace(); } }
private void testAllAccessQueries(String relationId) { Logger logger = getLogger(); // retrieve all roles try { Object[] params = {relationId}; String[] signature = {"java.lang.String"}; RoleResult roleResult = (RoleResult)(m_server.invoke(m_relationObjectName, "getAllRoles", params, signature)); RoleList roleList = roleResult.getRoles(); for (Iterator i = roleList.iterator(); i.hasNext();) { Role currentRole = (Role)i.next(); System.out.println(">>>> role name: " + currentRole.getRoleName()); System.out.println(">>>> role values: " + currentRole.getRoleValue().toString()); } System.out.println("No unresolved Roles roleUnresolved size: " + roleResult.getRolesUnresolved().size()); } catch (Exception ex) { logger.error("Exception printing the results from relationId: " + relationId); System.out.println("Printing the Exception message to validate exception: " + ex.getMessage()); } }
public Result testNotRegisteredException() throws Exception { /* Initialisation without registering */ MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); /* catch RelationServiceNotRegisteredException */ try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (RelationServiceNotRegisteredException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testNullRelationName() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); /* set null relation name */ try { rService.createRelation(null, "relationType", unitRoles); return failed(""); } catch (IllegalArgumentException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testNullRelationType() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); /* set null relation type */ try { rService.createRelation("relation", null, unitRoles); return failed(""); } catch (IllegalArgumentException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testUnknownRelationType() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); /* Try to create relation with unknown relation type */ try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (RelationTypeNotFoundException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testNumberOfMBeanLessThanNeed() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* The Number of MBeans is less than default (1) */ unitRoles.add(new Role("role", new ArrayList())); try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (InvalidRoleValueException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testMBeanNotRegistered() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* Referenced MBean hasn't registered */ unitRoles.add(new Role("role", Collections.singletonList(unitName))); try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (InvalidRoleValueException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testNotReadable() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); /* Role is not readable */ RoleInfo[] rInfo = { new RoleInfo("role", classPath, false, true) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); try { rService.createRelation("relation", "relationType", unitRoles); return passed(); } catch (Throwable t) { return failed(t.toString()); } }
/** * This test verifies that MBeans don't have to be registered before * creation of role. */ public Result testMBeansDoNotHaveTo() throws Exception { MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); // Register Relation Service RelationService relationService = new RelationService(true); ObjectName relationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=rs"); mBeanServer.registerMBean(relationService, relationServiceName); // Create Role Information Array RoleInfo[] roleInfo = new RoleInfo[1]; roleInfo[0] = new RoleInfo("unitRole", classPath, true, true, 1, 2, "Unit role"); // Creating an Internal Relation Type relationService.createRelationType("internalRelationType", roleInfo); ObjectName unit1Name = new ObjectName( "mBeanServer:type=Unit,name=Unit1"); // Create RoleLists for single unit and double unit relations ArrayList unitList = new ArrayList(); unitList.add(unit1Name); Role unitRole = new Role("unitRole", unitList); return !unitRole.getRoleValue().contains(unit1Name) ? failed("") : passed(); }
public Result testRoleNotFound() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* role name differs from role info contains */ unitRoles.add(new Role("another role", Collections .singletonList(unitName))); /* catch RoleNotFoundException */ try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (RoleNotFoundException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testIdAlreadyUsed() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); unitRoles.add(new Role("role", Collections.singletonList(unitName))); rService.createRelation("relation", "relationType", unitRoles); /* now try to create another relation with the same name */ /* create another relation type for clearance */ rService.createRelationType("another relationType", rInfo); try { rService.createRelation("relation", "another relationType", unitRoles); return failed(""); } catch (InvalidRelationIdException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testTheSameRoleName() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); ObjectName unitName2 = new ObjectName("a:b=c2"); server.registerMBean(new Unit(), unitName); server.registerMBean(new Unit(), unitName2); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* Add 2 roles with the same name */ unitRoles.add(new Role("role", Collections.singletonList(unitName))); unitRoles.add(new Role("role", Collections.singletonList(unitName2))); try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (InvalidRoleValueException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testNumberOfMBeanMoreThanNeed() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); ObjectName unitName2 = new ObjectName("a:b=c2"); server.registerMBean(new Unit(), unitName); server.registerMBean(new Unit(), unitName2); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* The Number of MBeans is more than default (1) */ ArrayList units = new ArrayList(); units.add(unitName); units.add(unitName2); unitRoles.add(new Role("role", units)); try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (InvalidRoleValueException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testDifferentTypeMBean() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); /* MBean class is NotUnit */ server.registerMBean(new NotUnit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* Referenced MBean hasn't registered */ unitRoles.add(new Role("role", Collections.singletonList(unitName))); try { rService.createRelation("relation", "relationType", unitRoles); return failed(""); } catch (InvalidRoleValueException e) { return passed(); } catch (Throwable t) { return failed(t.toString()); } }
public Result testExistenceAfterException() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName unitName = new ObjectName("a:b=c"); server.registerMBean(new Unit(), unitName); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); RoleInfo[] rInfo = { new RoleInfo("role", classPath), new RoleInfo("another role", classPath) }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); /* * "anotherRole is incorrect - the number of MBeans is less than * default" */ unitRoles.add(new Role("role", Collections.singletonList(unitName))); unitRoles.add(new Role("another role", new ArrayList())); try { rService.createRelation("relation", "relationType", unitRoles); System.out.println("existenceAfterExceptions test failed"); } catch (InvalidRoleValueException e) { /* this exception should be caught */ } catch (Throwable t) { System.out.println("existenceAfterExceptions test failed: " + t.toString()); } /* now verify is relation exists */ if (!rService.getAllRelationIds().contains("relation")) { return passed(); } return failed(""); }
public Result testPurgeRemains() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName[] unitNames = { new ObjectName("a:b=c"), new ObjectName("a:b=c2") }; server.registerMBean(new Unit(), unitNames[0]); server.registerMBean(new Unit(), unitNames[1]); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); /* This role remains after unregister one of unit */ RoleInfo[] rInfo = { new RoleInfo("role", classPath, true, true, 1, 2, "role") }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); Role role = new Role("role", Arrays.asList(unitNames)); unitRoles.add(role); rService.createRelation("relation", "relationType", unitRoles); /* now remove one of MBeans */ server.unregisterMBean(unitNames[0]); /* verify relation is exist */ if (!rService.getAllRelationIds().contains("relation")) { return failed(""); } return passed(); }
public Result testPurgeRemoves() throws Exception { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName[] unitNames = { new ObjectName("a:b=c"), new ObjectName("a:b=c2") }; server.registerMBean(new Unit(), unitNames[0]); server.registerMBean(new Unit(), unitNames[1]); RelationService rService = new RelationService(true); server.registerMBean(rService, new ObjectName("a:b=d")); /* This role remains after unregister one of unit */ RoleInfo[] rInfo = { new RoleInfo("role", classPath, true, true, 2, 2, "role") }; rService.createRelationType("relationType", rInfo); RoleList unitRoles = new RoleList(); Role role = new Role("role", Arrays.asList(unitNames)); unitRoles.add(role); rService.createRelation("relation", "relationType", unitRoles); /* now remove one of MBeans */ server.unregisterMBean(unitNames[0]); /* verify relation is exist */ if (rService.getAllRelationIds().contains("relation")) { return failed(""); } return passed(); }
/** * Step by step for verification that then MBean is unregistered Role * becomes incorrect * <ul> * <li>a. Create relation * <li>b. Unregister referenced MBean * <li>c. Verify that relation becomes incorrect * </ul> * */ public Result testInfluenceMBeanUnregToRole() throws Exception { MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); RelationService relationService = new RelationService(true); ObjectName relationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=rs"); mBeanServer.registerMBean(relationService, relationServiceName); UnitMBean unit1 = new Unit(); ObjectName unitName = new ObjectName("mBeanServer:type=Unit,name=Unit1"); mBeanServer.registerMBean(unit1, unitName); RoleInfo[] roleInfo = new RoleInfo[1]; roleInfo[0] = new RoleInfo("usualRole", classPath); relationService.createRelationType("internalRelationType", roleInfo); ArrayList unitList = new ArrayList(); unitList.add(unitName); Role usualRole = new Role("usualRole", unitList); mBeanServer.unregisterMBean(unitName); if (relationService.checkRoleWriting(usualRole, "internalRelationType", Boolean.TRUE).byteValue() != 7) { return failed("unregisterMBean influence to Role incorrect"); } return passed(); }
public static void main(String[] args) throws Exception { MBeanServer mbs = MBeanServerFactory.newMBeanServer(); ObjectName relSvcName = new ObjectName("a:type=relationService"); RelationServiceMBean relSvc = JMX.newMBeanProxy(mbs, relSvcName, RelationServiceMBean.class); mbs.createMBean("javax.management.relation.RelationService", relSvcName, new Object[] {Boolean.TRUE}, new String[] {"boolean"}); final BlockingQueue<Notification> q = new ArrayBlockingQueue<Notification>(100); NotificationListener qListener = new NotificationListener() { public void handleNotification(Notification notification, Object handback) { q.add(notification); } }; mbs.addNotificationListener(relSvcName, qListener, null, null); RoleInfo leftInfo = new RoleInfo("left", "javax.management.timer.TimerMBean"); RoleInfo rightInfo = new RoleInfo("right", "javax.management.timer.Timer"); relSvc.createRelationType("typeName", new RoleInfo[] {leftInfo, rightInfo}); ObjectName timer1 = new ObjectName("a:type=timer,number=1"); ObjectName timer2 = new ObjectName("a:type=timer,number=2"); mbs.createMBean("javax.management.timer.Timer", timer1); mbs.createMBean("javax.management.timer.Timer", timer2); Role leftRole = new Role("left", Arrays.asList(new ObjectName[] {timer1})); Role rightRole = new Role("right", Arrays.asList(new ObjectName[] {timer2})); RoleList roles = new RoleList(Arrays.asList(new Role[] {leftRole, rightRole})); final int NREPEAT = 10; for (int i = 0; i < NREPEAT; i++) { relSvc.createRelation("relationName", "typeName", roles); relSvc.removeRelation("relationName"); } Notification firstNotif = q.remove(); long seqNo = firstNotif.getSequenceNumber(); for (int i = 0; i < NREPEAT * 2 - 1; i++) { Notification n = q.remove(); long nSeqNo = n.getSequenceNumber(); if (nSeqNo != seqNo + 1) { throw new Exception( "TEST FAILED: expected seqNo " + (seqNo + 1) + "; got " + nSeqNo); } seqNo++; } System.out.println("TEST PASSED: got " + (NREPEAT * 2) + " notifications " + "with contiguous sequence numbers"); }
@Test public final void testProcessFormSubmission() { try { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); BindException aError = new BindException(new RoleCommand(), RoleCommand.ACTION_EDIT); this.testSetAgencyUserManager(); this.testSetAuthorityManager(); this.testSetMessageSource(); RoleCommand aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_NEW); aCommand.setOid(new Long(3000)); ModelAndView mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("AddRole")); List<Agency> agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_VIEW); aCommand.setOid(new Long(3000)); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("AddRole")); agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); RoleCommand newCommand = (RoleCommand)mav.getModel().get("command"); assertTrue(newCommand != null); assertTrue(newCommand.getViewOnlyMode()); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_EDIT); aCommand.setOid(new Long(3000)); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("AddRole")); agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); newCommand = (RoleCommand)mav.getModel().get("command"); assertTrue(newCommand != null); assertFalse(newCommand.getViewOnlyMode()); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_SAVE); aCommand.setRoleName("New Test Role"); aCommand.setAgency(AuthUtil.getRemoteUserObject().getAgency().getOid()); String[] scopedPrivileges = {}; aCommand.setScopedPrivileges(scopedPrivileges); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("Roles")); List<Role> roles = (List<Role>)mav.getModel().get("roles"); assertTrue(roles != null); assertTrue(roles.size() > 0); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_FILTER); String agencyFilter = "Dummy"; aCommand.setAgencyFilter(agencyFilter); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("Roles")); RoleCommand command = (RoleCommand)mav.getModel().get("command"); assertTrue(command != null); assertTrue(agencyFilter.equals(command.getAgencyFilter())); assertTrue(agencyFilter.equals((String)request.getSession().getAttribute(RoleCommand.PARAM_AGENCY_FILTER))); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
/** * This testcase verifies MBeanToUnregister field in notifications. */ public Result testRemovalNotification() throws Exception { incorrectNotificationFlag = false; MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); RelationService relationService = new RelationService(true); ObjectName relationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=rs"); mBeanServer.registerMBean(relationService, relationServiceName); NotificationFilterSupport filter = new NotificationFilterSupport(); filter.enableType("jmx.relation"); relationService.addNotificationListener(this, filter, null); checkNotifications = true; RoleInfo[] roleInfo = new RoleInfo[1]; roleInfo[0] = new RoleInfo("unitRole", classPath, true, true, 1, 2, "Unit role"); // Creating an Internal Relation Type relationService.createRelationType("internalRelationType", roleInfo); UnitMBean unit = new Unit(); ObjectName unitName = new ObjectName("mBeanServer:type=Unit,name=Unit"); mBeanServer.registerMBean(unit, unitName); ArrayList unitList = new ArrayList(); unitList.add(unitName); Role unitRole = new Role("unitRole", unitList); RoleList unitRoles = new RoleList(); unitRoles.add(unitRole); // Now Create relations between unit MBeans // Notification Data relationNotification = new RelationNotificationForCompare( "jmx.relation.creation.basic", relationService, 1, 10, "Creation of relation internalRelation", "internalRelation", "internalRelationType", null, null); relationService.createRelation("internalRelation", "internalRelationType", unitRoles); // Notification Data relationNotification = new RelationNotificationForCompare( "jmx.relation.removal.basic", relationService, 2, 10, "Removal of relation internalRelation", "internalRelation", "internalRelationType", null, new ArrayList()); relationService.removeRelation("internalRelation"); if (incorrectNotificationFlag) { return failed("IncorrectNotifications (see log for more information)"); } return passed(); }
/** * Step by step for verification that relation is removed as soon as its * relation type is removed. * <ul> * <li>a. Create relation * <li>b. Remove relation type * <li>c. Check that relation isn't present in relation service * </ul> */ public Result testRemoveRelationType() throws Exception { MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); RelationService relationService = new RelationService(true); ObjectName relationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=rs"); mBeanServer.registerMBean(relationService, relationServiceName); RoleInfo[] roleInfo = new RoleInfo[1]; roleInfo[0] = new RoleInfo("unitRole", classPath, true, true, 1, 2, "Unit role"); /* Creating an Internal Relation Type */ relationService.createRelationType("internalRelationType", roleInfo); /* 2 MBean objects to be associate */ UnitMBean unit1 = new Unit(); ObjectName unit1Name = new ObjectName( "mBeanServer:type=Unit,name=Unit1"); mBeanServer.registerMBean(unit1, unit1Name); /* Create Role */ ArrayList unitList = new ArrayList(); unitList.add(unit1Name); Role unitRole = new Role("unitRole", unitList); RoleList unitRoles = new RoleList(); unitRoles.add(unitRole); /* Now Create relations between unit MBeans */ relationService.createRelation("internalRelation", "internalRelationType", unitRoles); ERelation externalRelation = new ERelation("externalRelation", relationServiceName, mBeanServer, "internalRelationType", unitRoles); mBeanServer.registerMBean(externalRelation, externalRelation .getRelationON()); /* Relations created. Now Remove Relation type */ relationService.removeRelationType("internalRelationType"); /* Check relations are removed */ if (relationService.hasRelation("internalRelation").booleanValue() && relationService.hasRelation("externalRelation") .booleanValue()) { return failed("Relation type had been removed, but relation is not"); } return passed(); }
/** * Test creates 2 relation types with equals names and RoleInfos, but * referred to different relation services. Test checks that when first * relation type creates relation, second relation type is not affected */ public Result testTheSameRelationType() throws Exception { MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); RelationService firstRelationService = new RelationService(true); RelationService secondRelationService = new RelationService(true); ObjectName firstRelationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=first"); ObjectName secondRelationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=second"); mBeanServer.registerMBean(firstRelationService, firstRelationServiceName); mBeanServer.registerMBean(secondRelationService, secondRelationServiceName); RoleInfo roleInfo = new RoleInfo("usualRole", classPath, true, true, 1, 1, "usual role"); ERelationType eRelationType = new ERelationType("relationType", roleInfo); firstRelationService.addRelationType(eRelationType); secondRelationService.addRelationType(eRelationType); UnitMBean unit1 = new Unit(); ObjectName unit1Name = new ObjectName( "mBeanServer:type=Unit,name=Unit1"); mBeanServer.registerMBean(unit1, unit1Name); ArrayList unitList = new ArrayList(); unitList.add(unit1Name); Role singleUnitRole = new Role("usualRole", unitList); RoleList singleunitRoles = new RoleList(); singleunitRoles.add(singleUnitRole); firstRelationService.createRelation("relation", "relationType", singleunitRoles); if (secondRelationService.hasRelation("relation").booleanValue()) { return failed("RelationType Mismatch"); } return passed(); }
/** * In this test External relation unregistered MBeans by itself. After this * verified relation service consistency. */ public Result testSelfUnregistration() throws Exception { MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); RelationService relationService = new RelationService(true); ObjectName relationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=rs"); mBeanServer.registerMBean(relationService, relationServiceName); RoleInfo[] roleInfo = new RoleInfo[1]; roleInfo[0] = new RoleInfo("unitRole", classPath, true, true, 1, 2, "Unit role"); // Creating an Internal Relation Type relationService.createRelationType("internalRelationType", roleInfo); // Registering 2 MBean objects UnitMBean unit1 = new Unit(); ObjectName unit1Name = new ObjectName( "mBeanServer:type=Unit,name=Unit1"); mBeanServer.registerMBean(unit1, unit1Name); ArrayList unitList = new ArrayList(); unitList.add(unit1Name); Role unitRole = new Role("unitRole", unitList); RoleList unitRoles = new RoleList(); unitRoles.add(unitRole); ERelation externalRelation = new ERelation("externalRelation", relationServiceName, mBeanServer, "internalRelationType", unitRoles); mBeanServer.registerMBean(externalRelation, externalRelation .getRelationON()); relationService.addRelation(externalRelation.getRelationON()); externalRelation.unregisterMBean(unit1Name); if (relationService.hasRelation("externalRelation").booleanValue()) { return failed("Consistency Failed"); } return passed(); }
/** * This test verifies findReferencingRelations method */ public Result testRelationIdMap() throws Exception { MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer(); // Register Relation Service RelationService relationService = new RelationService(true); ObjectName relationServiceName = new ObjectName( "mBeanServer:type=RelationService,name=rs"); mBeanServer.registerMBean(relationService, relationServiceName); // Create Role Information Array RoleInfo[] roleInfo = new RoleInfo[1]; roleInfo[0] = new RoleInfo("unitRole", classPath, true, true, 1, 2, "Unit role"); // Creating an Internal Relation Type relationService.createRelationType("internalRelationType", roleInfo); // Registering 2 MBean objects UnitMBean unit1 = new Unit(); ObjectName unit2Name = new ObjectName( "mBeanServer:type=Unit,name=Unit2"); mBeanServer.registerMBean(unit1, unit2Name); ObjectName unit1Name = new ObjectName( "mBeanServer:type=Unit,name=Unit1"); mBeanServer.registerMBean(unit1, unit1Name); // Create RoleLists for single unit and double unit relations ArrayList unitList = new ArrayList(); unitList.add(unit1Name); Role singleUnitRole = new Role("unitRole", unitList); unitList.add(unit2Name); Role doubleUnitRole = new Role("unitRole", unitList); RoleList singleunitRoles = new RoleList(); singleunitRoles.add(singleUnitRole); RoleList doubleunitRoles = new RoleList(); doubleunitRoles.add(doubleUnitRole); // Create 2 Internal Relations relationService.createRelation("internalRelationSingle", "internalRelationType", singleunitRoles); relationService.createRelation("internalRelationDouble", "internalRelationType", doubleunitRoles); ERelation externalRelation = new ERelation("externalRelation", relationServiceName, mBeanServer, "internalRelationType", doubleunitRoles); mBeanServer.registerMBean(externalRelation, externalRelation .getRelationON()); relationService.addRelation(externalRelation.getRelationON()); if ((!relationService.findReferencingRelations(unit2Name, "internalRelationType", "unitRole").containsKey( "internalRelationDouble")) || (!relationService.findReferencingRelations(unit1Name, "internalRelationType", "unitRole").containsKey( "internalRelationDouble")) || (!relationService.findReferencingRelations(unit1Name, "internalRelationType", "unitRole").containsKey( "internalRelationSingle")) || (!relationService.findReferencingRelations(unit1Name, "internalRelationType", "unitRole").containsKey( "externalRelation")) || (!relationService.findReferencingRelations(unit2Name, "internalRelationType", "unitRole").containsKey( "externalRelation"))) { log.info(relationService.findReferencingRelations(unit1Name, "internalRelationType", "unitRole").toString()); return failed("findReferencingRelations method is incorrect"); } return passed(); }