package test.compliance.loading;
import java.net.URL;
import java.util.Set;
import javax.management.Attribute;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.ServiceNotFoundException;
import javax.management.loading.MLet;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
public class MLetTEST extends TestCase
{
private MBeanServer server;
public MLetTEST(String s)
{
super(s);
}
protected void setUp() throws Exception
{
super.setUp();
server = MBeanServerFactory.createMBeanServer();
}
public void testCreateAndRegister() throws Exception
{
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
}
finally
{
server.unregisterMBean(name);
}
}
public void testMLetLoadClassFromURLInConstructor() throws Exception
{
final String MBEANS_URL = "file:./output/etc/test/compliance/loading/MyMBeans.jar";
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial");
fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
}
catch (ClassNotFoundException e)
{
}
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("test:name=mlet");
MLet mlet = new MLet(new URL[] { new URL(MBEANS_URL)} );
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial");
fail("class test.compliance.loading.support.Trivial found in CL repository after MLet construction.");
}
catch (ClassNotFoundException e)
{
}
try
{
server.registerMBean(mlet, name);
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial");
}
finally
{
server.unregisterMBean(name);
}
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial");
fail("class test.compliance.loading.support.Trivial was still found in CL repository.");
}
catch (ClassNotFoundException e)
{
}
}
public void testBasicMLetFileLoad() throws Exception
{
final String MLET_URL = "file:./output/etc/test/compliance/loading/BasicConfig.mlet";
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial");
fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
}
catch (ClassNotFoundException e)
{
}
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial2");
fail("class test.compliance.loading.support.Trivial2 was already found in CL repository.");
}
catch (ClassNotFoundException e)
{
}
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
server.registerMBean(mlet, name);
Set mbeans = (Set)server.invoke(name, "getMBeansFromURL",
new Object[] { MLET_URL },
new String[] { String.class.getName() }
);
try
{
assertTrue(server.isRegistered(new ObjectName("test:name=Trivial")));
assertTrue(server.isRegistered(new ObjectName("test:name=Trivial2")));
}
catch (AssertionFailedError e)
{
URL[] urls = mlet.getURLs();
URL url = null;
if (urls != null && urls.length > 0)
url = urls[0];
fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" +
MLET_URL + "' resulting into MLET codebase URL '" + url + "' and therefore fails " +
"to load the required classes from the Java archive (MyMBeans.jar)");
}
assertTrue(server.getMBeanInfo(new ObjectName("test:name=Trivial")) != null);
assertTrue(server.getMBeanInfo(new ObjectName("test:name=Trivial2")) != null);
assertTrue(server.getAttribute(new ObjectName("test:name=Trivial2"), "Something").equals("foo"));
server.invoke(new ObjectName("test:name=Trivial"), "doOperation",
new Object[] { "Test" },
new String[] { String.class.getName() }
);
server.invoke(new ObjectName("test:name=Trivial2"), "doOperation",
new Object[] { "Test" },
new String[] { String.class.getName() }
);
}
public void testConflictingMLetFileLoad() throws Exception
{
final String MLET_URL1 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet";
final String MLET_URL2 = "file:./output/etc/test/compliance/loading/BasicConfig2.mlet";
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial3");
fail("class test.compliance.loading.support.Trivial was already found in CL repository.");
}
catch (ClassNotFoundException e)
{
}
try
{
server.getClassLoaderRepository().loadClass("test.compliance.loading.support.Trivial4");
fail("class test.compliance.loading.support.Trivial2 was already found in CL repository.");
}
catch (ClassNotFoundException e)
{
}
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
server.registerMBean(mlet, name);
Set mbeans = (Set)server.invoke(name, "getMBeansFromURL",
new Object[] { MLET_URL1 },
new String[] { String.class.getName() }
);
ObjectName oname = new ObjectName("test:name=Trivial2");
server.setAttribute(oname, new Attribute("Something", "Something"));
mlet = new MLet();
name = new ObjectName("test:name=mlet2");
server.registerMBean(mlet, name);
mbeans = (Set)server.invoke(name, "getMBeansFromURL",
new Object[] { MLET_URL2 },
new String[] { String.class.getName() }
);
oname = new ObjectName("test:name=Trivial2");
String value = (String)server.getAttribute(oname, "Something");
assertTrue(value.equals("Something"));
}
public void testMalformedURLLoad() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { "output/etc/test/compliance/loading/BasicConfig.mlet" },
new String[] { String.class.getName() }
);
fail("FAILS IN RI: Malformed URL in getMBeansURL() should result in ServiceNotFoundException thrown.");
}
catch (AssertionFailedError e)
{
throw e;
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testMissingMLetTagInLoad() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { "file:./output/etc/test/compliance/loading/MissingMLET.mlet" },
new String[] { String.class.getName() }
);
fail("MLet text file missing the MLET tag should result in ServiceNotFoundException thrown.");
}
catch (AssertionFailedError e)
{
throw e;
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testMissingMandatoryArchiveTagInLoad() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryArchive.mlet" },
new String[] { String.class.getName() }
);
fail("MLet text file missing mandatory ARCHIVE attribute should result in ServiceNotFoundException thrown.");
}
catch (AssertionFailedError e)
{
throw e;
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testMissingMandatoryCodeTagInLoad() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { "file:./output/etc/test/compliance/loading/MissingMandatoryCode.mlet" },
new String[] { String.class.getName() }
);
fail("MLet text file missing mandatory CODE attribute should result in ServiceNotFoundException thrown.");
}
catch (AssertionFailedError e)
{
throw e;
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testArchiveListInMLet() throws Exception
{
final String MLET_URL = "file:./output/etc/test/compliance/loading/ArchiveList.mlet";
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { MLET_URL },
new String[] { String.class.getName() }
);
Class c = null;
try
{
c = mlet.loadClass("test.compliance.loading.support.AClass");
}
catch (ClassNotFoundException e)
{
URL[] urls = mlet.getURLs();
fail("FAILS IN RI: SUN JMX RI builds a malformed URL from an MLet text file URL '" +
MLET_URL + "' resulting into MLET codebase URL '" + urls[0] + "' and therefore fails " +
"to load the required classes from the Java archive.");
}
Object o = c.newInstance();
server.setAttribute(new ObjectName("test:name=AnotherTrivial"), new Attribute("Something", o));
o = server.getAttribute(new ObjectName("test:name=AnotherTrivial"), "Something");
assertTrue(o.getClass().isAssignableFrom(c));
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testUnexpectedEndOfFile() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { "file:./output/etc/test/compliance/loading/UnexpectedEnd.mlet" },
new String[] { String.class.getName() }
);
fail("Unexpected end of file from mlet text file did not cause an exception.");
}
catch (AssertionFailedError e)
{
throw e;
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
public void testMissingEndMLetTag() throws Exception
{
MBeanServer server = MBeanServerFactory.createMBeanServer();
MLet mlet = new MLet();
ObjectName name = new ObjectName("test:name=mlet");
try
{
server.registerMBean(mlet, name);
server.invoke(name, "getMBeansFromURL",
new Object[] { "file:./output/etc/test/compliance/loading/MissingEndTag.mlet" },
new String[] { String.class.getName() }
);
assertTrue(!server.isRegistered(new ObjectName("test:name=Trivial")));
}
catch (AssertionFailedError e)
{
throw e;
}
catch (MBeanException e)
{
assertTrue(e.getTargetException() instanceof ServiceNotFoundException);
}
finally
{
try
{
server.unregisterMBean(name);
}
catch (Exception ignored) {}
}
}
}