SHARE
TWEET

SelfInjectingServerResourceModuleTest

tpeierls Mar 15th, 2012 211 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.restlet.ext.guice.example;
  2.  
  3. import org.restlet.*;
  4. import org.restlet.data.*;
  5. import org.restlet.resource.*;
  6. import org.restlet.routing.*;
  7. import org.restlet.ext.guice.SelfInjectingServerResource;
  8. import org.restlet.ext.guice.SelfInjectingServerResourceModule;
  9.  
  10. import javax.inject.Inject;
  11. import javax.inject.Named;
  12.  
  13. import com.google.inject.*;
  14.  
  15. import org.junit.*;
  16. import org.junit.runner.*;
  17. import static org.junit.Assert.*;
  18.  
  19.  
  20. public class SelfInjectingServerResourceModuleTest extends Application {
  21.  
  22.     @Test public void testReturnsMessage() {
  23.         ClientResource client = new ClientResource("http://localhost:8118");
  24.         String msg = client.getChild("/hello", HelloResource.class).getMessage();
  25.         assertEquals(HELLO_MSG, msg);
  26.     }
  27.  
  28.     @Before public void createInjector() {
  29.         Guice.createInjector(
  30.             new TestModule(),
  31.             new SelfInjectingServerResourceModule()
  32.         );
  33.     }
  34.  
  35.     @Before public void startComponent() throws Exception {
  36.         component = new Component();
  37.         component.getServers().add(Protocol.HTTP, 8118);
  38.         component.getDefaultHost().attachDefault(this);
  39.         component.start();
  40.     }
  41.  
  42.     @After public void stopComponent() throws Exception {
  43.         component.stop();
  44.     }
  45.  
  46.     private volatile Component component;
  47.  
  48.  
  49.     @Override public Restlet createInboundRoot() {
  50.         Router router = new Router(getContext());
  51.         router.attach("/hello", HelloServerResource.class);
  52.         return router;
  53.     }
  54.  
  55.     public interface HelloResource {
  56.         @Get String getMessage();
  57.     }
  58.  
  59.     public static class HelloServerResource
  60.             extends SelfInjectingServerResource implements HelloResource {
  61.  
  62.         @Override public String getMessage() {
  63.             return msg;
  64.         }
  65.  
  66.         @Inject @Named(HELLO_KEY) private String msg;
  67.     }
  68.  
  69.     static class TestModule extends AbstractModule {
  70.         @Provides @Named(HELLO_KEY) String helloMessage() {
  71.             return HELLO_MSG;
  72.         }
  73.         protected void configure() {}
  74.     }
  75.  
  76.     static final String HELLO_KEY = "hello.message";
  77.     static final String HELLO_MSG = "This resource was injected by Guice!";
  78. }
RAW Paste Data
Top