wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r1003074 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/model/wadl/ test/java/org/apache/wink/common/model/wadl/
Date Thu, 30 Sep 2010 14:23:20 GMT
Author: bluk
Date: Thu Sep 30 14:23:20 2010
New Revision: 1003074

URL: http://svn.apache.org/viewvc?rev=1003074&view=rev
Log:
Build simple WADL responses

Added:
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildResponseTest.java
Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLGenerator.java
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildRepresentationTest.java
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/WADLGeneratorTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLGenerator.java?rev=1003074&r1=1003073&r2=1003074&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLGenerator.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLGenerator.java
Thu Sep 30 14:23:20 2010
@@ -21,7 +21,9 @@
 package org.apache.wink.common.model.wadl;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Modifier;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -30,11 +32,13 @@ import javax.ws.rs.DefaultValue;
 import javax.ws.rs.FormParam;
 import javax.ws.rs.HeaderParam;
 import javax.ws.rs.MatrixParam;
+import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.xml.namespace.QName;
 
+import org.apache.wink.common.internal.i18n.Messages;
 import org.apache.wink.common.internal.registry.Injectable;
 import org.apache.wink.common.internal.registry.Injectable.ParamType;
 import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
@@ -65,7 +69,7 @@ public class WADLGenerator {
     /* package */Set<ClassMetadata> buildClassMetdata(Set<Class<?>> classes)
{
         Set<ClassMetadata> metadataSet = new HashSet<ClassMetadata>(classes.size());
         for (Class<?> c : classes) {
-            if (!ResourceMetadataCollector.isResource(c)) {
+            if (!isResource(c)) {
                 /* not a resource, so skip it */
                 continue;
             }
@@ -112,6 +116,11 @@ public class WADLGenerator {
         if (r != null) {
             m.setRequest(r);
         }
+
+        List<Response> resp = buildResponse(metadata);
+        if (resp != null) {
+            m.getResponse().addAll(resp);
+        }
         return m;
     }
 
@@ -134,7 +143,7 @@ public class WADLGenerator {
                     case ENTITY:
                         /* need to build the representation */
                         Set<Representation> representations =
-                            buildRepresentation(classMetadata, metadata, p);
+                            buildIncomingRepresentation(classMetadata, metadata, p);
                         r.getRepresentation().addAll(representations);
                         break;
                     case COOKIE:
@@ -260,9 +269,9 @@ public class WADLGenerator {
         return p;
     }
 
-    /* package */Set<Representation> buildRepresentation(ClassMetadata classMetadata,
-                                                         MethodMetadata methodMetadata,
-                                                         Injectable metadata) {
+    /* package */Set<Representation> buildIncomingRepresentation(ClassMetadata classMetadata,
+                                                                 MethodMetadata methodMetadata,
+                                                                 Injectable metadata) {
         if (methodMetadata == null) {
             return null;
         }
@@ -287,7 +296,8 @@ public class WADLGenerator {
                  * it
                  */
                 /*
-                 * special cases include application/xml, text/xml;  should set the element
attribute
+                 * special cases include application/xml, text/xml; should set
+                 * the element attribute
                  */
                 List<Param> params = new ArrayList<Param>();
                 if (mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE) || mt
@@ -306,7 +316,7 @@ public class WADLGenerator {
                      * required to be supported in other places than the
                      * resource method parameters
                      */
-                    if(!params.isEmpty()) {
+                    if (!params.isEmpty()) {
                         r.getParam().addAll(params);
                     }
                 }
@@ -326,4 +336,104 @@ public class WADLGenerator {
 
         return reps;
     }
+
+    /* package */List<Response> buildResponse(MethodMetadata metadata) {
+        if (metadata == null) {
+            return null;
+        }
+        Response r = null;
+        // Set<MediaType> produces = metadata.getProduces();
+        Set<Representation> representations = buildOutgoingRepresentation(metadata,
null);
+        if (representations != null && !representations.isEmpty()) {
+            r = new Response();
+            r.getRepresentation().addAll(representations);
+        }
+
+        java.lang.reflect.Method m = metadata.getReflectionMethod();
+        if (Void.TYPE.equals(m.getReturnType())) {
+            if (r == null) {
+                r = new Response();
+            }
+            r.getStatus().add(Long.valueOf(204));
+        } else if (!javax.ws.rs.core.Response.class.isAssignableFrom(m.getReturnType()))
{
+            if (r == null) {
+                r = new Response();
+            }
+            r.getStatus().add(Long.valueOf(200));
+        }
+
+        return Collections.singletonList(r);
+    }
+
+    /* package */Set<Representation> buildOutgoingRepresentation(MethodMetadata methodMetadata,
+                                                                 Class<?> returnType)
{
+        if (methodMetadata == null) {
+            return null;
+        }
+
+        /*
+         * first check all the consumes
+         */
+        Set<MediaType> producesMT = methodMetadata.getProduces();
+        Set<Representation> reps = null;
+        if (producesMT != null && !producesMT.isEmpty()) {
+            reps = new HashSet<Representation>();
+            for (MediaType mt : producesMT) {
+                Representation r = new Representation();
+                r.setMediaType(mt.toString());
+
+                /*
+                 * if the representation is a special case, we need to build for
+                 * it
+                 */
+                /*
+                 * TODO: special cases include application/xml, text/xml; should
+                 * set the element attribute
+                 */
+                reps.add(r);
+            }
+        } else {
+            /*
+             * there aren't any produces so we can't look for that but maybe we
+             * can look at the Providers registry and find all the relevant
+             * media types there
+             */
+        }
+
+        return reps;
+    }
+
+    /*
+     * Customized isResource method so it accepts interfaces.
+     */
+    private static boolean isResource(Class<?> cls) {
+        if (ResourceMetadataCollector.isDynamicResource(cls)) {
+            return true;
+        }
+
+        if (cls.getAnnotation(Path.class) != null) {
+            return true;
+        }
+
+        Class<?> declaringClass = cls;
+
+        while (!declaringClass.equals(Object.class)) {
+            // try a superclass
+            Class<?> superclass = declaringClass.getSuperclass();
+            if (superclass.getAnnotation(Path.class) != null) {
+                return true;
+            }
+
+            // try interfaces
+            Class<?>[] interfaces = declaringClass.getInterfaces();
+            for (Class<?> interfaceClass : interfaces) {
+                if (interfaceClass.getAnnotation(Path.class) != null) {
+                    return true;
+                }
+            }
+            declaringClass = declaringClass.getSuperclass();
+        }
+        return false;
+    }
+
 }

Modified: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildRepresentationTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildRepresentationTest.java?rev=1003074&r1=1003073&r2=1003074&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildRepresentationTest.java
(original)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildRepresentationTest.java
Thu Sep 30 14:23:20 2010
@@ -91,7 +91,7 @@ public class BuildRepresentationTest {
         });
 
         try {
-            generator.buildRepresentation(classMeta, methodMeta, metadata);
+            generator.buildIncomingRepresentation(classMeta, methodMeta, metadata);
             fail();
         } catch (IllegalArgumentException e) {
             /* expected */
@@ -101,7 +101,7 @@ public class BuildRepresentationTest {
 
     @Test
     public void testNullMethodMetadata() throws Exception {
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, null,
metadata);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
null, metadata);
         assertNull(reprSet);
 
         mockContext.assertIsSatisfied();
@@ -116,7 +116,7 @@ public class BuildRepresentationTest {
             }
         });
 
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, methodMeta,
null);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
methodMeta, null);
 
         assertNull(reprSet);
 
@@ -132,7 +132,7 @@ public class BuildRepresentationTest {
             }
         });
 
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, methodMeta,
null);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
methodMeta, null);
         assertNull(reprSet);
 
         mockContext.assertIsSatisfied();
@@ -151,7 +151,7 @@ public class BuildRepresentationTest {
             }
         });
 
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, methodMeta,
null);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
methodMeta, null);
         assertEquals(3, reprSet.size());
         List<Representation> orderedRepr = new ArrayList<Representation>(reprSet);
         Collections.sort(orderedRepr, new Comparator<Representation>() {
@@ -182,7 +182,7 @@ public class BuildRepresentationTest {
             }
         });
 
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, methodMeta,
null);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
methodMeta, null);
         assertEquals(1, reprSet.size());
         List<Representation> orderedRepr = new ArrayList<Representation>(reprSet);
         Collections.sort(orderedRepr, new Comparator<Representation>() {
@@ -211,7 +211,7 @@ public class BuildRepresentationTest {
             }
         });
 
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, methodMeta,
null);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
methodMeta, null);
         assertEquals(1, reprSet.size());
         List<Representation> orderedRepr = new ArrayList<Representation>(reprSet);
         Collections.sort(orderedRepr, new Comparator<Representation>() {
@@ -277,7 +277,7 @@ public class BuildRepresentationTest {
             }
         });
 
-        Set<Representation> reprSet = generator.buildRepresentation(classMeta, methodMeta,
null);
+        Set<Representation> reprSet = generator.buildIncomingRepresentation(classMeta,
methodMeta, null);
         assertEquals(1, reprSet.size());
         List<Representation> orderedRepr = new ArrayList<Representation>(reprSet);
         Collections.sort(orderedRepr, new Comparator<Representation>() {

Added: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildResponseTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildResponseTest.java?rev=1003074&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildResponseTest.java
(added)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/BuildResponseTest.java
Thu Sep 30 14:23:20 2010
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *  
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *  
+ */
+
+package org.apache.wink.common.model.wadl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+import org.apache.wink.common.internal.registry.metadata.MethodMetadata;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.lib.legacy.ClassImposteriser;
+import org.junit.Test;
+
+public class BuildResponseTest {
+
+    final private WADLGenerator generator   = new WADLGenerator();
+
+    final private Mockery       mockContext = new Mockery() {
+                                                {
+                                                    setImposteriser(ClassImposteriser.INSTANCE);
+                                                }
+                                            };
+
+    final MethodMetadata        methodMeta  = mockContext.mock(MethodMetadata.class);
+
+    @Path("somepath")
+    static class Temp {
+        @GET
+        public void returnVoid() {
+
+        }
+    }
+
+    @Test
+    public void testNullMetadata() throws Exception {
+        mockContext.checking(new Expectations() {
+            {
+            }
+        });
+
+        List<Response> r = generator.buildResponse(null);
+        assertNull(r);
+        mockContext.assertIsSatisfied();
+    }
+
+    @Test
+    public void testVoidMethodReturn() throws Exception {
+        final java.lang.reflect.Method m = Temp.class.getMethod("returnVoid", (Class[])null);
+
+        mockContext.checking(new Expectations() {
+            {
+                oneOf(methodMeta).getProduces();
+                will(returnValue(null));
+                
+                oneOf(methodMeta).getReflectionMethod();
+                will(returnValue(m));
+            }
+        });
+
+        List<Response> resps = generator.buildResponse(methodMeta);
+        Response r = resps.get(0);
+        assertNotNull(r);
+        assertEquals(0, r.getAny().size());
+        assertEquals(0, r.getDoc().size());
+        assertEquals(0, r.getOtherAttributes().size());
+        assertEquals(0, r.getParam().size());
+        assertEquals(0, r.getRepresentation().size());
+        assertEquals(1, r.getStatus().size());
+        assertEquals(Long.valueOf(204), r.getStatus().get(0));
+
+        mockContext.assertIsSatisfied();
+    }
+
+}

Modified: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/WADLGeneratorTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/WADLGeneratorTest.java?rev=1003074&r1=1003073&r2=1003074&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/WADLGeneratorTest.java
(original)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/model/wadl/WADLGeneratorTest.java
Thu Sep 30 14:23:20 2010
@@ -34,6 +34,7 @@ import javax.ws.rs.HeaderParam;
 import javax.ws.rs.HttpMethod;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.xml.bind.JAXBContext;
@@ -42,12 +43,6 @@ import javax.xml.bind.Marshaller;
 
 import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
 import org.apache.wink.common.internal.registry.metadata.MethodMetadata;
-import org.apache.wink.common.model.wadl.Application;
-import org.apache.wink.common.model.wadl.Method;
-import org.apache.wink.common.model.wadl.ObjectFactory;
-import org.apache.wink.common.model.wadl.Request;
-import org.apache.wink.common.model.wadl.Resource;
-import org.apache.wink.common.model.wadl.WADLGenerator;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
 import org.jmock.lib.legacy.ClassImposteriser;
@@ -80,12 +75,15 @@ public class WADLGeneratorTest {
         Assert.assertNull(app.getGrammars());
     }
 
-    @Path("resource1")
-    @Consumes(value={MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
+    @Path("resource1/{pp}")
+    @Consumes(value = {MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
     static class Resource1 {
 
         @GET
-        public String hello(String abcd, @QueryParam("q2") String q, @QueryParam("q3") int
q2, @HeaderParam("h1234") String h1) {
+        public String hello(String abcd,
+                            @QueryParam("q2") String q,
+                            @QueryParam("q3") int q2,
+                            @HeaderParam("h1234") String h1, @PathParam("pp") String somePath)
{
             return null;
         }
     }
@@ -114,6 +112,21 @@ public class WADLGeneratorTest {
         }
     }
 
+    static class BasicResourceWithVoidReturn {
+
+        @GET
+        public void basicReturn() {
+
+        }
+    }
+
+    @Path("some/path/{id}")
+    static interface MyInterface {
+
+        @GET
+        public void somePath(@PathParam("id") String someId);
+    }
+
     @Test
     public void testBuildClassMetadataResource1() {
         WADLGenerator generator = new WADLGenerator();
@@ -213,6 +226,8 @@ public class WADLGeneratorTest {
 
         final ClassMetadata metadata = mockContext.mock(ClassMetadata.class);
         final MethodMetadata methodMeta = mockContext.mock(MethodMetadata.class);
+        final java.lang.reflect.Method method =
+            BasicResourceWithVoidReturn.class.getMethod("basicReturn");
 
         mockContext.checking(new Expectations() {
             {
@@ -227,6 +242,12 @@ public class WADLGeneratorTest {
 
                 oneOf(methodMeta).getFormalParameters();
                 will(returnValue(Collections.emptyList()));
+
+                oneOf(methodMeta).getProduces();
+                will(returnValue(Collections.emptySet()));
+
+                oneOf(methodMeta).getReflectionMethod();
+                will(returnValue(method));
             }
         });
 
@@ -246,13 +267,19 @@ public class WADLGeneratorTest {
         assertEquals(0, m.getDoc().size());
         assertNull(m.getHref());
         assertNull(m.getRequest());
-        assertEquals(0, m.getResponse().size());
+        assertEquals(1, m.getResponse().size());
+        List<Response> resps = m.getResponse();
+        assertEquals(Collections.singletonList(Long.valueOf(204)), resps.get(0).getStatus());
+        assertEquals(0, resps.get(0).getAny().size());
+        assertEquals(0, resps.get(0).getDoc().size());
+        assertEquals(0, resps.get(0).getOtherAttributes().size());
+        assertEquals(0, resps.get(0).getParam().size());
 
         mockContext.assertIsSatisfied();
     }
 
     @Test
-    public void testBuildBasicMethodMetadataWithMock() {
+    public void testBuildBasicMethodMetadataWithMock() throws Exception {
         WADLGenerator generator = new WADLGenerator();
         Mockery mockContext = new Mockery() {
             {
@@ -261,6 +288,8 @@ public class WADLGeneratorTest {
         };
         final MethodMetadata metadata = mockContext.mock(MethodMetadata.class);
         final ClassMetadata classMeta = mockContext.mock(ClassMetadata.class);
+        final java.lang.reflect.Method method =
+            BasicResourceWithVoidReturn.class.getMethod("basicReturn");
 
         mockContext.checking(new Expectations() {
             {
@@ -269,6 +298,12 @@ public class WADLGeneratorTest {
 
                 oneOf(metadata).getFormalParameters();
                 will(returnValue(null));
+
+                oneOf(metadata).getProduces();
+                will(returnValue(Collections.emptySet()));
+
+                oneOf(metadata).getReflectionMethod();
+                will(returnValue(method));
             }
         });
 
@@ -276,10 +311,16 @@ public class WADLGeneratorTest {
         assertEquals("myHttpMethod", m.getName());
         assertEquals(0, m.getDoc().size());
         assertEquals(0, m.getAny().size());
-        assertEquals(0, m.getResponse().size());
         assertNull(m.getHref());
         assertNull(m.getId());
         assertNull(m.getRequest());
+        assertEquals(1, m.getResponse().size());
+        List<Response> resps = m.getResponse();
+        assertEquals(Collections.singletonList(Long.valueOf(204)), resps.get(0).getStatus());
+        assertEquals(0, resps.get(0).getAny().size());
+        assertEquals(0, resps.get(0).getDoc().size());
+        assertEquals(0, resps.get(0).getOtherAttributes().size());
+        assertEquals(0, resps.get(0).getParam().size());
 
         mockContext.assertIsSatisfied();
     }
@@ -294,7 +335,7 @@ public class WADLGeneratorTest {
         };
         final MethodMetadata metadata = mockContext.mock(MethodMetadata.class);
         final ClassMetadata classMeta = mockContext.mock(ClassMetadata.class);
-        
+
         mockContext.checking(new Expectations() {
             {
                 oneOf(metadata).getFormalParameters();
@@ -315,6 +356,8 @@ public class WADLGeneratorTest {
         WADLGenerator generator = new WADLGenerator();
         Set<Class<?>> classes = new HashSet<Class<?>>();
         classes.add(Resource1.class);
+        classes.add(Resource2.class);
+        classes.add(MyInterface.class);
         Application app = generator.generate(classes);
         Assert.assertNotNull(app);
         marshalIt(app);



Mime
View raw message