KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > ws > JBossWSTest


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.ws;
23
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.management.MBeanServerConnection JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import junit.framework.TestCase;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.ws.utils.DOMWriter;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42
43 /**
44  * Base class for JBossWS test cases
45  *
46  * @author Thomas.Diesler@jboss.org
47  * @since 14-Oct-2004
48  */

49 public abstract class JBossWSTest extends TestCase
50 {
51    // provide logging
52
protected Logger log = Logger.getLogger(getClass().getName());
53
54    private JBossWSTestHelper delegate = new JBossWSTestHelper();
55
56    /** Get the MBeanServerConnection from JNDI */
57    public MBeanServerConnection JavaDoc getServer() throws NamingException JavaDoc
58    {
59       return delegate.getServer();
60    }
61
62    /** True, if -Djbossws.target.server=jboss */
63    public boolean isTargetServerJBoss()
64    {
65       return delegate.isTargetServerJBoss();
66    }
67
68    /** True, if -Djbossws.target.server=tomcat */
69    public boolean isTargetServerTomcat()
70    {
71       return delegate.isTargetServerTomcat();
72    }
73
74    /**
75     * Locate a classpath resource.
76     *
77     * @param name
78     * @return
79     */

80    public URL JavaDoc getResource(String JavaDoc name)
81    {
82       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
83       URL JavaDoc resURL = loader.getResource(name);
84       return resURL;
85    }
86
87    /** Deploy the given archive
88     */

89    public void deploy(String JavaDoc archive) throws Exception JavaDoc
90    {
91       delegate.deploy(archive);
92    }
93
94    /** Undeploy the given archive
95     */

96    public void undeploy(String JavaDoc archive) throws Exception JavaDoc
97    {
98       delegate.undeploy(archive);
99    }
100
101    /** Get the client's env context for a given name.
102     */

103    protected InitialContext JavaDoc getInitialContext(String JavaDoc clientName) throws NamingException JavaDoc
104    {
105       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
106       Hashtable JavaDoc env = iniCtx.getEnvironment();
107       env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
108       env.put("j2ee.clientName", clientName);
109       return new InitialContext JavaDoc(env);
110    }
111
112    /** Get the client's env context
113     */

114    protected InitialContext JavaDoc getInitialContext() throws NamingException JavaDoc
115    {
116       return getInitialContext("jbossws-client");
117    }
118
119    public boolean hasJDK15()
120    {
121       try
122       {
123          Class.forName("java.lang.Enum");
124          return true;
125       }
126       catch (ClassNotFoundException JavaDoc ex)
127       {
128          return false;
129       }
130    }
131
132    /**
133     * Get the JBoss server host from system property "jbosstest.server.host"
134     * This defaults to "" + getServerHost() + ""
135     */

136    public String JavaDoc getServerHost()
137    {
138       String JavaDoc hostName = System.getProperty("jbosstest.server.host", "localhost");
139       return hostName;
140    }
141
142    public static void assertEquals(Element JavaDoc expElement, Element JavaDoc wasElement, boolean ignoreWhitespace)
143    {
144       normalizeWhitspace(expElement, ignoreWhitespace);
145       normalizeWhitspace(wasElement, ignoreWhitespace);
146       String JavaDoc expStr = DOMWriter.printNode(expElement, false);
147       String JavaDoc wasStr = DOMWriter.printNode(wasElement, false);
148       if (expStr.equals(wasStr) == false)
149       {
150          System.out.println("\nExp: " + expStr + "\nWas: " + wasStr);
151          Logger.getLogger(JBossWSTest.class).error("\nExp: " + expStr + "\nWas: " + wasStr);
152       }
153       assertEquals(expStr, wasStr);
154    }
155
156    public static void assertEquals(Element JavaDoc expElement, Element JavaDoc wasElement)
157    {
158       assertEquals(expElement, wasElement, false);
159    }
160
161    public static void assertEquals(Object JavaDoc exp, Object JavaDoc was)
162    {
163       if (exp instanceof Object JavaDoc[] && was instanceof Object JavaDoc[])
164          assertEqualsArray((Object JavaDoc[])exp, (Object JavaDoc[])was);
165       else if (exp instanceof byte[] && was instanceof byte[])
166          assertEqualsArray((byte[])exp, (byte[])was);
167       else if (exp instanceof boolean[] && was instanceof boolean[])
168          assertEqualsArray((boolean[])exp, (boolean[])was);
169       else if (exp instanceof short[] && was instanceof short[])
170          assertEqualsArray((short[])exp, (short[])was);
171       else if (exp instanceof int[] && was instanceof int[])
172          assertEqualsArray((int[])exp, (int[])was);
173       else if (exp instanceof long[] && was instanceof long[])
174          assertEqualsArray((long[])exp, (long[])was);
175       else if (exp instanceof float[] && was instanceof float[])
176          assertEqualsArray((float[])exp, (float[])was);
177       else if (exp instanceof double[] && was instanceof double[])
178          assertEqualsArray((double[])exp, (double[])was);
179       else TestCase.assertEquals(exp, was);
180    }
181
182    private static void assertEqualsArray(Object JavaDoc[] exp, Object JavaDoc[] was)
183    {
184       if (exp == null && was == null)
185          return;
186
187       if (exp != null && was != null)
188       {
189          if (exp.length != was.length)
190          {
191             fail("Expected <" + exp.length + "> array items, but was <" + was.length + ">");
192          }
193          else
194          {
195             for (int i = 0; i < exp.length; i++)
196             {
197
198                Object JavaDoc compExp = exp[i];
199                Object JavaDoc compWas = was[i];
200                assertEquals(compExp, compWas);
201             }
202          }
203       }
204       else if (exp == null)
205       {
206          fail("Expected a null array, but was: " + Arrays.asList(was));
207       }
208       else if (was == null)
209       {
210          fail("Expected " + Arrays.asList(exp) + ", but was: null");
211       }
212    }
213
214    private static void assertEqualsArray(byte[] exp, byte[] was)
215    {
216       assertTrue("Arrays don't match", Arrays.equals(exp, was));
217    }
218
219    private static void assertEqualsArray(boolean[] exp, boolean[] was)
220    {
221       assertTrue("Arrays don't match", Arrays.equals(exp, was));
222    }
223
224    private static void assertEqualsArray(short[] exp, short[] was)
225    {
226       assertTrue("Arrays don't match", Arrays.equals(exp, was));
227    }
228
229    private static void assertEqualsArray(int[] exp, int[] was)
230    {
231       assertTrue("Arrays don't match", Arrays.equals(exp, was));
232    }
233
234    private static void assertEqualsArray(long[] exp, long[] was)
235    {
236       assertTrue("Arrays don't match", Arrays.equals(exp, was));
237    }
238
239    private static void assertEqualsArray(float[] exp, float[] was)
240    {
241       assertTrue("Arrays don't match", Arrays.equals(exp, was));
242    }
243
244    private static void assertEqualsArray(double[] exp, double[] was)
245    {
246       assertTrue("Arrays don't match", Arrays.equals(exp, was));
247    }
248
249    /** Removes whitespace text nodes if they have an element sibling.
250     */

251    private static void normalizeWhitspace(Element JavaDoc element, boolean ignoreWhitespace)
252    {
253       boolean hasChildElement = false;
254       ArrayList JavaDoc toDetach = new ArrayList JavaDoc();
255
256       String JavaDoc nodeName = element.getNodeName();
257       NodeList JavaDoc childNodes = element.getChildNodes();
258       for (int i = 0; i < childNodes.getLength(); i++)
259       {
260          Node JavaDoc node = childNodes.item(i);
261          if (node.getNodeType() == Node.TEXT_NODE)
262          {
263             String JavaDoc nodeValue = node.getNodeValue();
264             if (nodeValue.trim().length() == 0)
265                toDetach.add(node);
266          }
267          if (node.getNodeType() == Node.ELEMENT_NODE)
268          {
269             normalizeWhitspace((Element JavaDoc)node, ignoreWhitespace);
270             hasChildElement = true;
271          }
272       }
273
274       // remove whitespace nodes
275
if (hasChildElement || ignoreWhitespace)
276       {
277          Iterator JavaDoc it = toDetach.iterator();
278          while (it.hasNext())
279          {
280             Node JavaDoc node = (Node JavaDoc)it.next();
281             element.removeChild(node);
282          }
283       }
284    }
285 }
286
Popular Tags