001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management;
010:
011: import java.util.List;
012: import javax.management.MBeanServer;
013: import javax.management.MBeanServerFactory;
014:
015: import junit.framework.TestCase;
016:
017: /**
018: * @version $Revision: 1.7 $
019: */
020: public class MBeanServerFactoryTest extends TestCase {
021: public MBeanServerFactoryTest(String s) {
022: super (s);
023: }
024:
025: public void testCreate() throws Exception {
026: MBeanServer server1 = null;
027: try {
028: System.out.print("Creating MBeanServer 1 ... ");
029: server1 = MBeanServerFactory.createMBeanServer();
030: System.out.println("OK");
031: System.out.print("Finding MBeanServer... ");
032: List l = MBeanServerFactory.findMBeanServer(null);
033: if (l.size() != 1) {
034: fail("MBeanServer creation is not tracked");
035: }
036: if (!server1.equals(l.get(0))) {
037: fail("MBeanServer found is different");
038: }
039: System.out.println("Found, OK");
040: } finally {
041: MBeanServerFactory.releaseMBeanServer(server1);
042: }
043: }
044:
045: public void testNew() throws Exception {
046: MBeanServer server1 = null;
047: try {
048: System.out.print("Creating MBeanServer 1 ... ");
049: server1 = MBeanServerFactory.createMBeanServer();
050: System.out.println("OK");
051:
052: // newMBeanServer must not keep track of the MBeanServer it creates
053: System.out.print("Newing MBeanServer 2 ... ");
054: MBeanServer server2 = MBeanServerFactory.newMBeanServer();
055: System.out.println("OK");
056: System.out.print("Finding MBeanServer... ");
057: List l = MBeanServerFactory.findMBeanServer(null);
058: if (l.size() != 1) {
059: fail("newMBeanServer tracks creation");
060: }
061: if (!server1.equals(l.get(0))) {
062: fail("MBeanServer found is different");
063: }
064: System.out.println("Found number 1, OK");
065: } finally {
066: MBeanServerFactory.releaseMBeanServer(server1);
067: }
068: }
069:
070: public void testCreateWithDomain() {
071: MBeanServer server1 = null;
072: MBeanServer server3 = null;
073: try {
074: System.out.print("Creating MBeanServer 1 ... ");
075: server1 = MBeanServerFactory.createMBeanServer();
076: System.out.println("OK");
077:
078: String domain = "test";
079:
080: System.out.print("Creating MBeanServer 3 ... ");
081: server3 = MBeanServerFactory.createMBeanServer(domain);
082: System.out.println("OK");
083: System.out.print("Finding MBeanServer... ");
084: List l = MBeanServerFactory.findMBeanServer(null);
085: if (l.size() != 2) {
086: fail("MBeanServer creation is not tracked");
087: }
088: MBeanServer found1 = (MBeanServer) l.get(0);
089: if (!found1.getDefaultDomain().equals(
090: server1.getDefaultDomain())) {
091: fail("MBeanServer found is different");
092: }
093: if (!server3.equals(l.get(1))) {
094: fail("MBeanServer found is different");
095: }
096: if (!server3.getDefaultDomain().equals(domain)) {
097: fail("Domain is different");
098: }
099: System.out.println("Found number 1 & 3, OK");
100: } finally {
101: MBeanServerFactory.releaseMBeanServer(server1);
102: MBeanServerFactory.releaseMBeanServer(server3);
103: }
104: }
105:
106: public void testRelease() throws Exception {
107: System.out.print("Creating MBeanServer 1 ... ");
108: MBeanServer server1 = MBeanServerFactory.createMBeanServer();
109: System.out.println("OK");
110:
111: System.out.print("Creating MBeanServer 2 ... ");
112: String domain = "test";
113: MBeanServer server2 = MBeanServerFactory
114: .createMBeanServer(domain);
115: System.out.println("OK");
116:
117: System.out.print("Releasing MBeanServer 1 ... ");
118: List l = MBeanServerFactory.findMBeanServer(null);
119: MBeanServer found1 = (MBeanServer) l.get(0);
120: MBeanServerFactory.releaseMBeanServer(found1);
121: System.out.println("OK");
122:
123: System.out.print("Finding MBeanServer... ");
124: l = MBeanServerFactory.findMBeanServer(null);
125: if (l.size() != 1) {
126: fail("Removed MBeanServer still present");
127: }
128: MBeanServer found2 = (MBeanServer) l.get(0);
129: if (!found2.getDefaultDomain().equals(domain)) {
130: fail("Removed wrong MBeanServer");
131: }
132: System.out.println("Found number 2, OK");
133:
134: System.out.print("Releasing MBeanServer 2 ... ");
135: MBeanServerFactory.releaseMBeanServer(found2);
136: System.out.println("OK");
137:
138: System.out.print("Finding MBeanServer... ");
139: l = MBeanServerFactory.findMBeanServer(null);
140: if (l.size() != 0) {
141: fail("Removed MBeanServer still present");
142: }
143: System.out.println("Found none, OK");
144: }
145: }
|