Index: src/test/java/groovyx/net/ws/WSClientTest.java =================================================================== --- src/test/java/groovyx/net/ws/WSClientTest.java (revision 0) +++ src/test/java/groovyx/net/ws/WSClientTest.java (revision 0) @@ -0,0 +1,123 @@ +package groovyx.net.ws; + +import java.lang.reflect.InvocationTargetException; + +import org.junit.BeforeClass; +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; + +public class WSClientTest { + + private static WSClient wsClient; + + enum MyColor { + BLUE, RED, GREEN + } + + @BeforeClass + public static void createWSClient() { + wsClient = new WSClient(null, WSClientTest.class.getClassLoader()); + } + + /** + *

+ * Create method calls newIntance(). Calling this on an Enum class throws InstantiationException. + * + *

+ * Method catches the exception but just prints stack trace and does nothing, thus returning null. + */ + @Test() + public void createMethodReturnsNullForEnum() { + Object result = wsClient.create("groovyx.net.ws.WSClientTest$MyColor"); + assertNull(result); + } + + /** + *

+ * Specifying Enum type with dot notation throws {@link ClassNotFoundException}. + * + *

+ * Method catches the exception but just prints stack trace and does nothing, thus returning null. + */ + @Test() + public void createMethodReturnsNullForEnumWithDotNotation() { + Object result = wsClient.create("groovyx.net.ws.WSClientTest$MyColor.BLUE"); + assertNull(result); + } + + /** + *

+ * Method correctly creates the given Enum type with given value. + * + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws NoSuchMethodException + * @throws ClassNotFoundException + * @throws SecurityException + */ + @Test() + public void createEnumMethodSuccess() throws SecurityException, ClassNotFoundException, NoSuchMethodException, + IllegalAccessException, InvocationTargetException { + Object result = wsClient.createEnum("groovyx.net.ws.WSClientTest$MyColor", "GREEN"); + assertNotNull(result); + assertEquals(MyColor.GREEN, result); + } + + /** + *

+ * Method throws {@link IllegalArgumentException} when called with null args. + * + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws NoSuchMethodException + * @throws ClassNotFoundException + * @throws SecurityException + */ + @Test(expected = IllegalArgumentException.class) + public void createEnumMethodWithNullArgs() throws SecurityException, ClassNotFoundException, NoSuchMethodException, + IllegalAccessException, InvocationTargetException { + + wsClient.createEnum(null, "GREEN"); + + wsClient.createEnum("groovyx.net.ws.WSClientTest$MyColor", null); + + wsClient.createEnum(null, null); + } + + /** + *

+ * Method throws {@link ClassNotFoundException} when called with an invalid Enum class name. + * + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws NoSuchMethodException + * @throws ClassNotFoundException + * @throws SecurityException + */ + @Test(expected = ClassNotFoundException.class) + public void createEnumMethodWithInvalidEnumClassName() throws SecurityException, ClassNotFoundException, + NoSuchMethodException, IllegalAccessException, InvocationTargetException { + + wsClient.createEnum("groovyx.net.ws.WSClientTest$Foo", "RED"); + } + + /** + *

+ * Method throws {@link IllegalArgumentException} when called with a non Enum class name. + * + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws NoSuchMethodException + * @throws ClassNotFoundException + * @throws SecurityException + */ + @Test(expected = IllegalArgumentException.class) + public void createEnumMethodWithNonEnumClassName() throws SecurityException, ClassNotFoundException, + NoSuchMethodException, IllegalAccessException, InvocationTargetException { + + wsClient.createEnum("java.lang.String", "RED"); + } +} Index: src/main/java/groovyx/net/ws/WSClient.java =================================================================== --- src/main/java/groovyx/net/ws/WSClient.java (revision 573) +++ src/main/java/groovyx/net/ws/WSClient.java (working copy) @@ -12,6 +12,8 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; @@ -41,24 +43,35 @@ import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.codehaus.groovy.runtime.InvokerHelper; - public class WSClient extends GroovyObjectSupport { private Client client = null; + private String loc = null; + private Map mproxy = null; + private Map mssl = null; + private Map mba = null; + private Boolean bssl = false; + private Boolean bmtom = false; + private Boolean bproxy = false; + private Boolean bba = false; + private ClassLoader cl = null; + private String wsdl = null; + private TrustManagerFactory tmf = null; + private KeyManagerFactory kmf = null; - + @Override public Object invokeMethod(String name, Object args) { Object[] args_a = InvokerHelper.asArray(args); @@ -106,6 +119,47 @@ return o; } + /** + * @param enumClazzName + * The Enum class name. + * @param enumValue + * The enum value. + * @return The specified enum of the specified value. + * @throws ClassNotFoundException + * If the specified enum class can not be found. + * @throws NoSuchMethodException + * In the unlikely scenario where the enum class does not have a method called "valueOf". + * @throws SecurityException + * If this exception is thrown while getting "valueOf" method. + * @throws InvocationTargetException + * If this exception is thrown while invoking "valueOf" method. + * @throws IllegalAccessException + * If this exception is thrown while invoking "valueOf" method. + */ + @SuppressWarnings("unchecked") + public Object createEnum(final String enumClazzName, final String enumValue) throws ClassNotFoundException, + SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + if (enumClazzName == null) { + throw new IllegalArgumentException("Must provide the enum class name"); + } + + if (enumValue == null) { + throw new IllegalArgumentException("Must provide the enum value string"); + } + + Object o = null; + + Class clazz = Thread.currentThread().getContextClassLoader().loadClass(enumClazzName); + if (clazz.isEnum()) { + Method method = clazz.getMethod("valueOf", String.class); + o = method.invoke(clazz, enumValue); + } else { + throw new IllegalArgumentException("Class '" + enumClazzName + "' is not an Enum. Call create() instead."); + } + + return o; + } + private void initializeBa() { mba.put("http.user", System.getProperty("http.user")); mba.put("http.password", System.getProperty("http.password")); @@ -118,7 +172,6 @@ mproxy.put("http.proxy.password", System.getProperty("http.proxy.password")); } - public WSClient(String loc, ClassLoader cl) { this.cl = cl; this.loc = loc; @@ -129,7 +182,7 @@ try { url = new URL(loc); - if (url.getProtocol().equals("https")&&!bssl) + if (url.getProtocol().equals("https") && !bssl) setSSL(); } catch (MalformedURLException e) { e.printStackTrace(); @@ -144,7 +197,7 @@ configureSSL(); getWsdl(); - System.out.println("wsdl >"+wsdl); + System.out.println("wsdl >" + wsdl); try { client = DynamicClientFactory.newInstance().createClient(wsdl, cl); @@ -170,17 +223,14 @@ } - public void getWsdl() throws IllegalArgumentException { URL url; try { url = new URL(loc); - if (url.getQuery().compareTo("wsdl") > 0) - throw new IllegalArgumentException( - "Bad query. Expected 'wsdl', not '" + url.getQuery() - + "'"); + if (url.getQuery() != null && url.getQuery().compareTo("wsdl") > 0) + throw new IllegalArgumentException("Bad query. Expected 'wsdl', not '" + url.getQuery() + "'"); if (url.getProtocol().equals("https")) { try { @@ -197,8 +247,7 @@ else if (tmf == null) ctx.init(kmf.getKeyManagers(), null, null); - SSLSocket socket = (SSLSocket) ctx.getSocketFactory() - .createSocket(url.getHost(), url.getPort()); + SSLSocket socket = (SSLSocket) ctx.getSocketFactory().createSocket(url.getHost(), url.getPort()); try { socket.startHandshake(); } catch (SSLHandshakeException e) { @@ -206,7 +255,8 @@ e.printStackTrace(); } - PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); + PrintWriter out = + new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); out.println("GET " + url.getFile() + " HTTP/1.0"); out.println(); out.flush(); @@ -218,8 +268,7 @@ File myWsdl = File.createTempFile("wsdl", null); wsdl = myWsdl.getAbsolutePath(); - BufferedWriter wout = new BufferedWriter(new FileWriter( - myWsdl)); + BufferedWriter wout = new BufferedWriter(new FileWriter(myWsdl)); String inputLine; int i = 0; while ((inputLine = in.readLine()) != null) @@ -318,17 +367,13 @@ this.mssl = new HashMap(); this.bssl = true; - String def_truststore = System.getProperty("java.home") - + "/lib/security/cacerts"; + String def_truststore = System.getProperty("java.home") + "/lib/security/cacerts"; String def_truststore_pass = "changeit"; mssl.put("https.keystore", System.getProperty("https.keystore", "")); - mssl.put("https.keystore.pass", System.getProperty( - "https.keystore.pass", "")); - mssl.put("https.truststore", System.getProperty("https.truststore", - def_truststore)); - mssl.put("https.truststore.pass", System.getProperty( - "https.truststore.pass", def_truststore_pass)); + mssl.put("https.keystore.pass", System.getProperty("https.keystore.pass", "")); + mssl.put("https.truststore", System.getProperty("https.truststore", def_truststore)); + mssl.put("https.truststore.pass", System.getProperty("https.truststore.pass", def_truststore_pass)); } private void configureSSL() {