Exception targetEx = new Exception();
MBeanException mbe = new MBeanException(targetEx);
Assert.assertEquals(targetEx, mbe.getTargetException());
Assert.assertEquals(null, mbe.getCause());
Assert.assertEquals(null, mbe.getMessage());
Assert.assertEquals(null, mbe.getTargetException().getMessage());
targetEx = new Exception("actual message");
mbe = new MBeanException(targetEx);
mbe.initCause(targetEx);
Assert.assertEquals(targetEx, mbe.getCause());
Assert.assertEquals(null, mbe.getMessage());
mbe = toMBeanException(null, null);
Assert.assertEquals(null, mbe.getMessage());
Assert.assertTrue(null != mbe.getCause());
Assert.assertTrue(null != mbe.getTargetException());
mbe = toMBeanException(null, "a");
Assert.assertEquals("a", mbe.getMessage());
Exception e = new Exception("b", new Exception("c", new Exception("d")));
mbe = toMBeanException(e, "a");
Assert.assertEquals("a(b(c(d)))", mbe.getMessage());
Assert.assertEquals(e, mbe.getCause());
Assert.assertEquals(e, mbe.getTargetException());
mbe = toMBeanException(e, null);
Assert.assertEquals("b(c(d))", mbe.getMessage());
e = new Exception();
mbe = toMBeanException(e, "a");
Assert.assertEquals("a", mbe.getMessage());
e = new Exception("b", null);
mbe = toMBeanException(e, "a");
Assert.assertEquals("a(b)", mbe.getMessage());
e = new Exception("b", new Exception(null, new Exception("c")));
mbe = toMBeanException(e, "a");
Assert.assertEquals("a(b(c))", mbe.getMessage());