KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > naming > test > ExternalContextUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.naming.test;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.management.ObjectInstance JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.RuntimeMBeanException JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.NamingEnumeration JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33
34 import org.jboss.test.JBossTestCase;
35
36 /**
37  * A test of the ExternalContext naming mbean. To test there needs to be one or
38  * more ExternalContex mbeans setup. An example filesystem context setup would
39  * be:
40   <mbean code="org.jboss.naming.ExternalContext" name="jboss:service=ExternalContext,jndiName=external/fs/tmp">
41     <attribute name="JndiName">external/fs/Scott</attribute>
42     <attribute name="Properties">tmp.fs</attribute>
43     <attribute name="RemoteAccess">true</attribute>
44   </mbean>
45
46 where tmp.fs is a Properties file containing:
47 # JNDI properties for /Scott filesystem directory
48 java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
49 java.naming.provider.url=file:/tmp
50
51  *
52  * @author Scott_Stark@displayscape.com
53  * @version $Revision: 37406 $
54  */

55 public class ExternalContextUnitTestCase extends JBossTestCase
56 {
57    private ObjectName JavaDoc[] contextNames;
58
59 // private RemoteMBeanServer server;
60

61    /**
62     * Constructor for the ExternalContextUnitTestCase object
63     *
64     * @param name Testcase name
65     */

66    public ExternalContextUnitTestCase(String JavaDoc name)
67    {
68       super(name);
69    }
70
71    /**
72     * A unit test for JUnit
73     *
74     * @exception Exception Description of Exception
75     */

76    public void testExternalContexts() throws Exception JavaDoc
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 JavaDoc name = contextNames[n];
87          String JavaDoc 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 JavaDoc ctx = (Context JavaDoc)getInitialContext().lookup(jndiName);
94          getLog().debug("+++ Listing for: " + ctx);
95          list(ctx);
96       }
97    }
98
99    /**
100     * The JUnit setup method
101     *
102     * @exception Exception Description of Exception
103     */

104    protected void setUp() throws Exception JavaDoc
105    {
106       try
107       {
108          super.setUp();
109          contextNames = null;
110          ObjectName JavaDoc pattern = new ObjectName JavaDoc("*:service=ExternalContext,*");
111          Set JavaDoc names = getServer().queryMBeans(pattern, null);
112          Iterator JavaDoc iter = names.iterator();
113          ArrayList JavaDoc tmp = new ArrayList JavaDoc();
114          while (iter.hasNext())
115          {
116             ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc)iter.next();
117             ObjectName JavaDoc name = oi.getObjectName();
118             getLog().debug(name);
119             tmp.add(name);
120          }
121          if (tmp.size() > 0)
122          {
123             contextNames = new ObjectName JavaDoc[tmp.size()];
124             tmp.toArray(contextNames);
125          }
126       }
127       catch (Exception JavaDoc x)
128       {
129          if (x instanceof RuntimeMBeanException JavaDoc)
130          {
131             getLog().error("setUp RuntimeMBeanException:",((RuntimeMBeanException JavaDoc)x).getTargetException());
132          }
133          else
134          {
135             getLog().error("setUp Error:" , x);
136          }
137       }
138    }
139
140    private void list(Context JavaDoc ctx) throws NamingException JavaDoc
141    {
142       NamingEnumeration JavaDoc enumeration = ctx.list("");
143       while (enumeration.hasMore())
144       {
145          getLog().debug(enumeration.next());
146       }
147       enumeration.close();
148    }
149
150 }
151
Popular Tags