Advertisement
tpeierls

Generic parameter test

Jan 25th, 2012
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.96 KB | None | 0 0
  1.  
  2. package com.example.restlet.genparm;
  3.  
  4. import java.io.IOException;
  5.  
  6. import org.restlet.*;
  7. import org.restlet.data.*;
  8. import org.restlet.representation.*;
  9. import org.restlet.resource.*;
  10. import org.restlet.routing.*;
  11.  
  12. import org.junit.*;
  13. import static org.junit.Assert.*;
  14.  
  15.  
  16. public class GenericParameterTest extends Application {
  17.  
  18.     public static void main(String... args) throws Exception {
  19.         GenericParameterTest re = new GenericParameterTest();
  20.         re.startComponent();
  21.     }
  22.  
  23.     @Before public void startComponent() throws Exception {
  24.         component = new Component();
  25.         component.getServers().add(Protocol.HTTP, 8111);
  26.         component.getDefaultHost().attachDefault(this);
  27.         component.start();
  28.         clientResource = new ClientResource("http://localhost:8111");
  29.     }
  30.  
  31.     @After public void stopComponent() throws Exception {
  32.         component.stop();
  33.     }
  34.  
  35.     @Test public void postAnnotationIsNotOnGenericVersionOfMethod() throws IOException {
  36.         Representation rep = new StringRepresentation("abc");
  37.         assertEquals("added string abc", resourceFor("/postNotOnGeneric").post(rep).getText());
  38.     }
  39.  
  40.     @Test public void postAnnotationIsOnGenericVersionOfMethod() throws IOException {
  41.         Representation rep = new StringRepresentation("abc");
  42.         try {
  43.             resourceFor("/postOnGeneric").post(rep);
  44.             fail("Expecting resource exception");
  45.         } catch (ResourceException ex) {
  46.             Status status = ex.getStatus();
  47.             assertEquals(500, status.getCode());
  48.             assertEquals("Internal Server Error", status.getReasonPhrase());
  49.         }
  50.     }
  51.  
  52.  
  53.     ClientResource resourceFor(String uri) {
  54.         return clientResource.getChild(uri);
  55.     }
  56.  
  57.     @Override public Restlet createInboundRoot() {
  58.         Router router = new Router(getContext());
  59.         router.attach("/postNotOnGeneric", StringServerResource.class);
  60.         router.attach("/postOnGeneric", StringServerResource2.class);
  61.         return router;
  62.     }
  63.  
  64.     public interface GenericResource<T> {
  65.         String addValue(T parameter);
  66.     }
  67.  
  68.     public interface StringResource extends GenericResource<String> {
  69.         @Post String addValue(String parameter);
  70.     }
  71.  
  72.     public static class StringServerResource extends ServerResource implements StringResource {
  73.         public String addValue(String parameter) {
  74.             return "added string " + parameter;
  75.         }
  76.     }
  77.  
  78.     public interface GenericResource2<T> {
  79.         @Post String addValue(T parameter);
  80.     }
  81.  
  82.     public interface StringResource2 extends GenericResource2<String> {
  83.         @Post String addValue(String parameter);
  84.     }
  85.  
  86.     public static class StringServerResource2 extends ServerResource implements StringResource2 {
  87.         public String addValue(String parameter) {
  88.             return "added string " + parameter;
  89.         }
  90.     }
  91.  
  92.     Component component;
  93.     ClientResource clientResource;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement