KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > junit > AbstractTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.junit;
12
13 import junit.framework.TestCase;
14
15 import javax.naming.InitialContext JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.management.InstanceNotFoundException JavaDoc;
18 import javax.management.MBeanException JavaDoc;
19 import javax.management.ReflectionException JavaDoc;
20 import javax.management.MalformedObjectNameException JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
27 import org.jboss.portal.common.util.Tools;
28 import org.jboss.portal.common.junit.ExtendedAssert;
29 import org.jboss.portal.junit.response.ResultSetResponse;
30 import org.jboss.portal.junit.result.InvokeURLResult;
31 import org.jboss.portal.junit.result.AssertResult;
32 import org.apache.commons.httpclient.HostConfiguration;
33 import org.apache.commons.httpclient.HttpState;
34 import org.apache.commons.httpclient.SimpleHttpConnectionManager;
35 import org.apache.commons.httpclient.HttpClient;
36 import org.apache.commons.httpclient.methods.GetMethod;
37
38 /**
39  * Base case for client side test case.
40  *
41  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
42  * @version $Revision: 1.4 $
43  */

44 public class AbstractTestCase extends TestCase
45 {
46
47    private File JavaDoc testRoot;
48    private Properties JavaDoc props;
49    private HostConfiguration host;
50    private HttpClient client;
51    private RMIAdaptor adaptor;
52    private ClientSession session;
53
54    public AbstractTestCase(String JavaDoc s)
55    {
56       super(s);
57    }
58
59    protected void setUp() throws Exception JavaDoc
60    {
61       testRoot = new File JavaDoc(System.getProperty("test.root"));
62
63       //
64
props = new Properties JavaDoc();
65       props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
66       props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
67       props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
68
69       //
70
host = new HostConfiguration();
71       host.setHost("localhost", 8080);
72       client = new HttpClient(new SimpleHttpConnectionManager());
73
74       InitialContext JavaDoc ctx = null;
75       try
76       {
77          ctx = new InitialContext JavaDoc(props);
78          adaptor = (RMIAdaptor)ctx.lookup("jmx/invoker/RMIAdaptor");
79       }
80       finally
81       {
82          Tools.safeClose(ctx);
83       }
84
85       HttpState state = new HttpState();
86       client.setState(state);
87    }
88
89    protected void tearDown() throws Exception JavaDoc
90    {
91       if (session != null)
92       {
93          if (!session.isClosed())
94          {
95             session.close();
96             System.out.println("Warning session not closed");
97          }
98       }
99       session = null;
100       adaptor = null;
101       client.setState(null);
102    }
103
104    protected final HttpClient getClient()
105    {
106       return client;
107    }
108
109    protected final HostConfiguration getHost()
110    {
111       return host;
112    }
113
114    protected final RMIAdaptor getRMIAdaptor() throws Exception JavaDoc
115    {
116       return adaptor;
117    }
118
119    public ClientSession createSession()
120    {
121       if (session != null && session.isClosed())
122       {
123          throw new IllegalStateException JavaDoc("Previous session not closed");
124       }
125       session = new ClientSession(testRoot, host, client, getName(), adaptor);
126       return session;
127    }
128
129    public static Result start(ClientSession session, String JavaDoc componentName) throws Throwable JavaDoc
130    {
131       ServerResponse resp = session.doGet("/test/", new String JavaDoc[]{componentName});
132       assertTrue(resp instanceof ResultSetResponse);
133       ResultSetResponse rs = (ResultSetResponse)resp;
134       Result result = rs.getResult(componentName);
135       assertNotNull(result);
136       return result;
137    }
138
139    public static Result doGet(ClientSession session, Result result, String JavaDoc componentName) throws Throwable JavaDoc
140    {
141       assertTrue(result instanceof InvokeURLResult);
142       String JavaDoc url = ((InvokeURLResult)result).getURL();
143       ServerResponse resp = session.doGet(url);
144       assertTrue(resp instanceof ResultSetResponse);
145       ResultSetResponse rs = (ResultSetResponse)resp;
146       result = rs.getResult(componentName);
147       assertNotNull(result);
148       return result;
149    }
150
151    public static void check(Result result, int expected) throws Throwable JavaDoc
152    {
153       assertTrue(result instanceof AssertResult);
154       AssertResult aresult = (AssertResult)result;
155       aresult.check(expected);
156    }
157 }
158
Popular Tags