1 22 package org.jboss.test.naming.test; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import javax.management.ObjectInstance ; 28 import javax.management.ObjectName ; 29 import javax.management.RuntimeMBeanException ; 30 import javax.naming.Context ; 31 import javax.naming.NamingEnumeration ; 32 import javax.naming.NamingException ; 33 34 import org.jboss.test.JBossTestCase; 35 36 55 public class ExternalContextUnitTestCase extends JBossTestCase 56 { 57 private ObjectName [] contextNames; 58 59 61 66 public ExternalContextUnitTestCase(String name) 67 { 68 super(name); 69 } 70 71 76 public void testExternalContexts() throws Exception 77 { 78 if (contextNames == null) 79 { 80 getLog().debug("No ExternalContext names exist"); 81 return; 82 } 83 84 for (int n = 0; n < contextNames.length; n++) 85 { 86 ObjectName name = contextNames[n]; 87 String jndiName = name.getKeyProperty("jndiName"); 88 if (jndiName == null) 89 { 90 getLog().debug("Skipping " + name + " as it has no jndiName property"); 91 continue; 92 } 93 Context ctx = (Context )getInitialContext().lookup(jndiName); 94 getLog().debug("+++ Listing for: " + ctx); 95 list(ctx); 96 } 97 } 98 99 104 protected void setUp() throws Exception 105 { 106 try 107 { 108 super.setUp(); 109 contextNames = null; 110 ObjectName pattern = new ObjectName ("*:service=ExternalContext,*"); 111 Set names = getServer().queryMBeans(pattern, null); 112 Iterator iter = names.iterator(); 113 ArrayList tmp = new ArrayList (); 114 while (iter.hasNext()) 115 { 116 ObjectInstance oi = (ObjectInstance )iter.next(); 117 ObjectName name = oi.getObjectName(); 118 getLog().debug(name); 119 tmp.add(name); 120 } 121 if (tmp.size() > 0) 122 { 123 contextNames = new ObjectName [tmp.size()]; 124 tmp.toArray(contextNames); 125 } 126 } 127 catch (Exception x) 128 { 129 if (x instanceof RuntimeMBeanException ) 130 { 131 getLog().error("setUp RuntimeMBeanException:",((RuntimeMBeanException )x).getTargetException()); 132 } 133 else 134 { 135 getLog().error("setUp Error:" , x); 136 } 137 } 138 } 139 140 private void list(Context ctx) throws NamingException 141 { 142 NamingEnumeration enumeration = ctx.list(""); 143 while (enumeration.hasMore()) 144 { 145 getLog().debug(enumeration.next()); 146 } 147 enumeration.close(); 148 } 149 150 } 151 | Popular Tags |