You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2010/11/04 21:24:15 UTC

svn commit: r1031216 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/model/wadl/WADLDoc.java main/java/org/apache/wink/common/model/wadl/WADLGenerator.java test/java/org/apache/wink/common/model/wadl/WADLGeneratorTest.java

Author: bluk
Date: Thu Nov  4 20:24:15 2010
New Revision: 1031216

URL: http://svn.apache.org/viewvc?rev=1031216&view=rev
Log:
Add @WADLDoc so you can document elements easier

Added:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLDoc.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/WADLGeneratorTest.java

Added: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLDoc.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLDoc.java?rev=1031216&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLDoc.java (added)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/wadl/WADLDoc.java Thu Nov  4 20:24:15 2010
@@ -0,0 +1,31 @@
+/*
+ * 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 java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
+@Retention(value = RetentionPolicy.RUNTIME)
+public @interface WADLDoc {
+
+    String value();
+}

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=1031216&r1=1031215&r2=1031216&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 Nov  4 20:24:15 2010
@@ -101,6 +101,16 @@ public class WADLGenerator {
     /* package */Resource buildResource(ClassMetadata metadata) {
         Resource r = new Resource();
 
+        if (metadata != null) {
+            Class<?> resClass = metadata.getResourceClass();
+            if (resClass != null) {
+                WADLDoc d = resClass.getAnnotation(WADLDoc.class);
+                if (d != null) {
+                    r.getDoc().add(getDocument(d));
+                }
+            }
+        }
+
         /* set the path */
         String path = metadata.getPath();
         UriTemplateProcessor processor = JaxRsUriTemplateProcessor.newNormalizedInstance(path);
@@ -311,6 +321,17 @@ public class WADLGenerator {
             for (MethodMetadata methodMeta : methodMetadata) {
                 Resource subRes = new Resource();
                 subRes.setPath(methodMeta.getPath());
+
+                if (methodMeta != null) {
+                    java.lang.reflect.Method reflMethod = methodMeta.getReflectionMethod();
+                    if (reflMethod != null) {
+                        WADLDoc d = reflMethod.getAnnotation(WADLDoc.class);
+                        if (d != null) {
+                            subRes.getDoc().add(getDocument(d));
+                        }
+                    }
+                }
+
                 methodOrSubresource.add(subRes);
 
                 /* also scan for all the path and matrix parameters */
@@ -398,6 +419,15 @@ public class WADLGenerator {
     /* package */Method buildMethod(ClassMetadata classMetadata, MethodMetadata metadata) {
         Method m = new Method();
         m.setName(metadata.getHttpMethod());
+
+        java.lang.reflect.Method reflMethod = metadata.getReflectionMethod();
+        if (reflMethod != null) {
+            WADLDoc d = reflMethod.getAnnotation(WADLDoc.class);
+            if (d != null) {
+                m.getDoc().add(getDocument(d));
+            }
+        }
+
         Request r = buildRequest(classMetadata, metadata);
         if (r != null) {
             m.setRequest(r);
@@ -412,6 +442,7 @@ public class WADLGenerator {
 
     /* package */Request buildRequest(ClassMetadata classMetadata, MethodMetadata metadata) {
         Request r = null;
+
         List<Injectable> params = metadata.getFormalParameters();
         if (params != null && params.size() > 0) {
             if (r == null) {
@@ -452,6 +483,17 @@ public class WADLGenerator {
                         break;
                     case ENTITY:
                         hasValidParams = true;
+
+                        Annotation[] anns = p.getAnnotations();
+                        if (anns != null && anns.length > 0) {
+                            for (Annotation a : anns) {
+                                if (WADLDoc.class.equals(a.annotationType())) {
+                                    WADLDoc d = (WADLDoc)a;
+                                    r.getDoc().add(getDocument(d));
+                                }
+                            }
+                        }
+
                         /* need to build the representation */
                         Set<Representation> representations =
                             buildIncomingRepresentation(classMetadata, metadata, p);
@@ -579,6 +621,11 @@ public class WADLGenerator {
                 DefaultValue paramAnn = (DefaultValue)a;
                 p.setDefault(paramAnn.value());
             }
+
+            if (WADLDoc.class.equals(a.annotationType())) {
+                WADLDoc d = (WADLDoc)a;
+                p.getDoc().add(getDocument(d));
+            }
         }
 
         return p;
@@ -718,6 +765,12 @@ public class WADLGenerator {
         return reps;
     }
 
+    /* package */Doc getDocument(WADLDoc desc) {
+        Doc d = new Doc();
+        d.setTitle(desc.value());
+        return d;
+    }
+
     /*
      * Customized isResource method so it accepts interfaces.
      */

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=1031216&r1=1031215&r2=1031216&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 Nov  4 20:24:15 2010
@@ -22,6 +22,7 @@ package org.apache.wink.common.model.wad
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.Collections;
 import java.util.HashSet;
@@ -77,15 +78,19 @@ public class WADLGeneratorTest {
 
     @Path("resource1/{pp}")
     @Consumes(value = {MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
+    @WADLDoc("this is resource1")
     static class Resource1 {
 
         @GET
-        public String hello(String abcd,
-                            @QueryParam("q2") String q,
+        @WADLDoc("this is the hello method")
+        public String hello(@WADLDoc("request doc") String abcd,
+                            @WADLDoc("q2 parameter doc") @QueryParam("q2") String q,
                             @QueryParam("q3") int q2,
-                            @HeaderParam("h1234") String h1, @PathParam("pp") String somePath) {
+                            @HeaderParam("h1234") String h1,
+                            @PathParam("pp") String somePath) {
             return null;
         }
+
     }
 
     @Path("resource2")
@@ -195,15 +200,18 @@ public class WADLGeneratorTest {
 
         mockContext.checking(new Expectations() {
             {
+                oneOf(metadata).getResourceClass();
+                will(returnValue(null));
+
                 oneOf(metadata).getPath();
                 will(returnValue("myPath"));
 
                 oneOf(metadata).getResourceMethods();
                 will(returnValue(null));
-                
+
                 oneOf(metadata).getInjectableFields();
                 will(returnValue(null));
-                
+
                 oneOf(metadata).getSubResourceMethods();
                 will(returnValue(null));
 
@@ -240,6 +248,9 @@ public class WADLGeneratorTest {
 
         mockContext.checking(new Expectations() {
             {
+                oneOf(metadata).getResourceClass();
+                will(returnValue(BasicResourceWithVoidReturn.class));
+
                 oneOf(metadata).getPath();
                 will(returnValue("myResourcePath"));
 
@@ -255,12 +266,12 @@ public class WADLGeneratorTest {
                 oneOf(methodMeta).getProduces();
                 will(returnValue(Collections.emptySet()));
 
-                oneOf(methodMeta).getReflectionMethod();
+                exactly(2).of(methodMeta).getReflectionMethod();
                 will(returnValue(method));
-                
+
                 oneOf(methodMeta).getFormalParameters();
                 will(returnValue(null));
-                
+
                 oneOf(metadata).getInjectableFields();
                 will(returnValue(null));
 
@@ -323,7 +334,7 @@ public class WADLGeneratorTest {
                 oneOf(metadata).getProduces();
                 will(returnValue(Collections.emptySet()));
 
-                oneOf(metadata).getReflectionMethod();
+                exactly(2).of(metadata).getReflectionMethod();
                 will(returnValue(method));
             }
         });
@@ -383,4 +394,34 @@ public class WADLGeneratorTest {
         Assert.assertNotNull(app);
         marshalIt(app);
     }
+
+    @Test
+    public void testWADLDocResource1() throws Exception {
+        WADLGenerator generator = new WADLGenerator();
+        Set<Class<?>> classes = new HashSet<Class<?>>();
+        classes.add(Resource1.class);
+        Application app = generator.generate("", classes);
+        Resource res = app.getResources().get(0).getResource().get(0);
+        assertEquals(1, res.getDoc().size());
+        assertEquals("this is resource1", res.getDoc().get(0).getTitle());
+        Method m = (Method)res.getMethodOrResource().get(0);
+        assertEquals(1, m.getDoc().size());
+        assertEquals("this is the hello method", m.getDoc().get(0).getTitle());
+
+        assertEquals(1, m.getRequest().getDoc().size());
+        assertEquals("request doc", m.getRequest().getDoc().get(0).getTitle());
+
+        boolean isFound = false;
+        List<Param> params = m.getRequest().getParam();
+        for (Param p : params) {
+            if (p.getName().equals("q2")) {
+                isFound = true;
+                assertEquals(1, p.getDoc().size());
+                assertEquals("q2 parameter doc", p.getDoc().get(0).getTitle());
+            } else {
+                assertEquals(0, p.getDoc().size());
+            }
+        }
+        assertTrue(isFound);
+    }
 }