001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.servlet;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Proxy;
023: import java.net.MalformedURLException;
024: import java.net.URL;
025: import java.net.URLConnection;
026: import java.util.Properties;
027: import javax.naming.InitialContext;
028:
029: import org.apache.openejb.test.TestClient;
030: import org.apache.openejb.test.TestManager;
031:
032: public abstract class ServletTestClient extends TestClient {
033: protected URL serverUrl;
034: private final String servletName;
035:
036: public ServletTestClient(String servletName) {
037: super ("Servlet." + servletName + ".");
038: this .servletName = servletName;
039: String serverUri = System.getProperty("openejb.server.uri",
040: "http://127.0.0.1:8080/openejb/ejb");
041: try {
042: serverUrl = new URL(serverUri);
043: } catch (MalformedURLException e) {
044: throw new RuntimeException(e);
045: }
046: }
047:
048: /**
049: * Sets up the fixture, for example, open a network connection.
050: * This method is called before a test is executed.
051: */
052: protected void setUp() throws Exception {
053:
054: Properties properties = TestManager.getServer()
055: .getContextEnvironment();
056: //properties.put(Context.SECURITY_PRINCIPAL, "STATEFUL_test00_CLIENT");
057: //properties.put(Context.SECURITY_CREDENTIALS, "STATEFUL_test00_CLIENT");
058:
059: initialContext = new InitialContext(properties);
060: }
061:
062: protected Object invoke(String methodName) {
063: InputStream in = null;
064: try {
065: URL url = new URL(serverUrl, "/itests/" + servletName
066: + "?method=" + methodName);
067: URLConnection connection = url.openConnection();
068: connection.connect();
069: in = connection.getInputStream();
070: String response = readAll(in);
071: if (response.startsWith("FAILED")) {
072: response = response.substring("FAILED".length()).trim();
073: fail(response);
074: }
075: } catch (Exception e) {
076: fail("Received Exception " + e.getClass() + " : "
077: + e.getMessage());
078: } finally {
079: if (in != null) {
080: try {
081: in.close();
082: } catch (Exception ignored) {
083: }
084: }
085: }
086: return null;
087: }
088:
089: private static String readAll(InputStream in) throws IOException {
090: // SwizzleStream block read methods are broken so read byte at a time
091: StringBuilder sb = new StringBuilder();
092: int i = in.read();
093: while (i != -1) {
094: sb.append((char) i);
095: i = in.read();
096: }
097: return sb.toString();
098: }
099:
100: @SuppressWarnings({"unchecked"})
101: protected <T> T newServletProxy(Class<T> clazz) {
102: Object proxy = Proxy.newProxyInstance(getClass()
103: .getClassLoader(), new Class[] { clazz },
104: new ServletInvocationHandler());
105: return (T) proxy;
106: }
107:
108: private class ServletInvocationHandler implements InvocationHandler {
109: public Object invoke(Object proxy, Method method, Object[] args)
110: throws Throwable {
111: if (method.getParameterTypes().length != 0)
112: throw new IllegalArgumentException(
113: "ServletProxy only supports no-argument methods: "
114: + method);
115:
116: String methodName = method.getName();
117:
118: return ServletTestClient.this.invoke(methodName);
119: }
120: }
121: }
|