KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > naming > rmi > test > RMIContextTestCase


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.naming.rmi.test;
9
10 import java.util.Hashtable JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import org.apache.avalon.excalibur.naming.test.AbstractContextTestCase;
14 import org.apache.avalon.excalibur.naming.rmi.server.Main;
15 import org.apache.avalon.excalibur.naming.rmi.RMIInitialContextFactory;
16
17 /**
18  * Unit testing for JNDI system
19  *
20  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
21  * @version $Revision: 1.5 $
22  */

23 public class RMIContextTestCase
24     extends AbstractContextTestCase
25 {
26     private static int m_numTests = 0;
27     private static int m_id = 0;
28     private static Main m_server = new Main();
29     private static Thread JavaDoc m_serverThread;
30     private Context JavaDoc m_rootContext;
31     private static boolean m_setUp = false;
32
33     static
34     {
35         Class JavaDoc testCase = AbstractContextTestCase.class;
36
37         Method JavaDoc[] methods = testCase.getMethods();
38
39         for (int i = 0; i < methods.length; i++)
40         {
41             if (methods[i].getName().startsWith("test"))
42             {
43                 RMIContextTestCase.m_numTests++;
44             }
45         }
46     }
47
48     public RMIContextTestCase( String JavaDoc name )
49     {
50         super( name );
51     }
52
53     public void setUp()
54     {
55         try
56         {
57             if ( ! RMIContextTestCase.m_setUp )
58             {
59                 RMIContextTestCase.m_server.start();
60
61                 RMIContextTestCase.m_serverThread = new Thread JavaDoc( m_server );
62                 RMIContextTestCase.m_serverThread.start();
63                 RMIContextTestCase.m_setUp = true;
64             }
65
66             final RMIInitialContextFactory factory = new RMIInitialContextFactory();
67             m_rootContext = factory.getInitialContext( new Hashtable JavaDoc() );
68
69             m_context = m_rootContext.createSubcontext( "test" + RMIContextTestCase.m_id++ );
70         }
71         catch( final Exception JavaDoc e )
72         {
73             System.out.println( "Failed test initialisation " + e );
74             e.printStackTrace();
75         }
76     }
77
78     public void tearDown()
79     {
80         try
81         {
82             m_context.close();
83             m_context = null;
84             m_rootContext.close();
85
86             if ( RMIContextTestCase.m_id >= RMIContextTestCase.m_numTests )
87             {
88                 RMIContextTestCase.m_server.stop();
89                 RMIContextTestCase.m_serverThread.interrupt();
90            }
91         }
92         catch( final Exception JavaDoc e )
93         {
94             System.out.println( "Failed test destruction" + e );
95             e.printStackTrace();
96         }
97     }
98 }
99
Popular Tags