1 22 package org.jboss.test.ws; 23 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 30 import javax.management.MBeanServerConnection ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 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 ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 43 49 public abstract class JBossWSTest extends TestCase 50 { 51 protected Logger log = Logger.getLogger(getClass().getName()); 53 54 private JBossWSTestHelper delegate = new JBossWSTestHelper(); 55 56 57 public MBeanServerConnection getServer() throws NamingException 58 { 59 return delegate.getServer(); 60 } 61 62 63 public boolean isTargetServerJBoss() 64 { 65 return delegate.isTargetServerJBoss(); 66 } 67 68 69 public boolean isTargetServerTomcat() 70 { 71 return delegate.isTargetServerTomcat(); 72 } 73 74 80 public URL getResource(String name) 81 { 82 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 83 URL resURL = loader.getResource(name); 84 return resURL; 85 } 86 87 89 public void deploy(String archive) throws Exception 90 { 91 delegate.deploy(archive); 92 } 93 94 96 public void undeploy(String archive) throws Exception 97 { 98 delegate.undeploy(archive); 99 } 100 101 103 protected InitialContext getInitialContext(String clientName) throws NamingException 104 { 105 InitialContext iniCtx = new InitialContext (); 106 Hashtable env = iniCtx.getEnvironment(); 107 env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming.client"); 108 env.put("j2ee.clientName", clientName); 109 return new InitialContext (env); 110 } 111 112 114 protected InitialContext getInitialContext() throws NamingException 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 ex) 127 { 128 return false; 129 } 130 } 131 132 136 public String getServerHost() 137 { 138 String hostName = System.getProperty("jbosstest.server.host", "localhost"); 139 return hostName; 140 } 141 142 public static void assertEquals(Element expElement, Element wasElement, boolean ignoreWhitespace) 143 { 144 normalizeWhitspace(expElement, ignoreWhitespace); 145 normalizeWhitspace(wasElement, ignoreWhitespace); 146 String expStr = DOMWriter.printNode(expElement, false); 147 String 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 expElement, Element wasElement) 157 { 158 assertEquals(expElement, wasElement, false); 159 } 160 161 public static void assertEquals(Object exp, Object was) 162 { 163 if (exp instanceof Object [] && was instanceof Object []) 164 assertEqualsArray((Object [])exp, (Object [])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 [] exp, Object [] 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 compExp = exp[i]; 199 Object 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 251 private static void normalizeWhitspace(Element element, boolean ignoreWhitespace) 252 { 253 boolean hasChildElement = false; 254 ArrayList toDetach = new ArrayList (); 255 256 String nodeName = element.getNodeName(); 257 NodeList childNodes = element.getChildNodes(); 258 for (int i = 0; i < childNodes.getLength(); i++) 259 { 260 Node node = childNodes.item(i); 261 if (node.getNodeType() == Node.TEXT_NODE) 262 { 263 String 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 )node, ignoreWhitespace); 270 hasChildElement = true; 271 } 272 } 273 274 if (hasChildElement || ignoreWhitespace) 276 { 277 Iterator it = toDetach.iterator(); 278 while (it.hasNext()) 279 { 280 Node node = (Node )it.next(); 281 element.removeChild(node); 282 } 283 } 284 } 285 } 286 | Popular Tags |