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