1 11 package org.jboss.portal.junit; 12 13 import junit.framework.TestCase; 14 15 import javax.naming.InitialContext ; 16 import javax.management.ObjectName ; 17 import javax.management.InstanceNotFoundException ; 18 import javax.management.MBeanException ; 19 import javax.management.ReflectionException ; 20 import javax.management.MalformedObjectNameException ; 21 import java.util.Properties ; 22 import java.net.URL ; 23 import java.io.File ; 24 import java.io.IOException ; 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 44 public class AbstractTestCase extends TestCase 45 { 46 47 private File testRoot; 48 private Properties props; 49 private HostConfiguration host; 50 private HttpClient client; 51 private RMIAdaptor adaptor; 52 private ClientSession session; 53 54 public AbstractTestCase(String s) 55 { 56 super(s); 57 } 58 59 protected void setUp() throws Exception 60 { 61 testRoot = new File (System.getProperty("test.root")); 62 63 props = new Properties (); 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 host = new HostConfiguration(); 71 host.setHost("localhost", 8080); 72 client = new HttpClient(new SimpleHttpConnectionManager()); 73 74 InitialContext ctx = null; 75 try 76 { 77 ctx = new InitialContext (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 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 115 { 116 return adaptor; 117 } 118 119 public ClientSession createSession() 120 { 121 if (session != null && session.isClosed()) 122 { 123 throw new IllegalStateException ("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 componentName) throws Throwable 130 { 131 ServerResponse resp = session.doGet("/test/", new String []{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 componentName) throws Throwable 140 { 141 assertTrue(result instanceof InvokeURLResult); 142 String 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 152 { 153 assertTrue(result instanceof AssertResult); 154 AssertResult aresult = (AssertResult)result; 155 aresult.check(expected); 156 } 157 } 158 | Popular Tags |