PageRenderTime 437ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/org.restlet.test/src/main/java/org/restlet/test/ext/guice/GuiceSelfInjectingServerResourceModuleTestCase.java

http://github.com/restlet/restlet-framework-java
Java | 145 lines | 97 code | 25 blank | 23 comment | 3 complexity | 43f88600f086c550f0853df6b922ca7f MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, CPL-1.0, LGPL-2.1
  1. /**
  2. * Copyright 2005-2020 Talend
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
  6. * select the license that you prefer but you may not use this file except in
  7. * compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the Apache 2.0 license at
  10. * http://www.opensource.org/licenses/apache-2.0
  11. *
  12. * You can obtain a copy of the EPL 1.0 license at
  13. * http://www.opensource.org/licenses/eclipse-1.0
  14. *
  15. * See the Licenses for the specific language governing permissions and
  16. * limitations under the Licenses.
  17. *
  18. * Alternatively, you can obtain a royalty free commercial license with less
  19. * limitations, transferable or non-transferable, directly at
  20. * https://restlet.talend.com/
  21. *
  22. * Restlet is a registered trademark of Talend S.A.
  23. */
  24. package org.restlet.test.ext.guice;
  25. import javax.inject.Inject;
  26. import javax.inject.Named;
  27. import org.junit.Test;
  28. import org.restlet.Application;
  29. import org.restlet.Client;
  30. import org.restlet.Component;
  31. import org.restlet.Restlet;
  32. import org.restlet.data.MediaType;
  33. import org.restlet.data.Protocol;
  34. import org.restlet.ext.guice.SelfInjectingServerResource;
  35. import org.restlet.ext.guice.SelfInjectingServerResourceModule;
  36. import org.restlet.resource.ClientResource;
  37. import org.restlet.resource.Get;
  38. import org.restlet.routing.Router;
  39. import org.restlet.test.RestletTestCase;
  40. import com.google.inject.AbstractModule;
  41. import com.google.inject.Guice;
  42. import com.google.inject.Provides;
  43. public class GuiceSelfInjectingServerResourceModuleTestCase extends
  44. RestletTestCase {
  45. public interface HelloResource {
  46. @Get
  47. String getMessage();
  48. }
  49. public static class HelloServerResource extends SelfInjectingServerResource
  50. implements HelloResource {
  51. @Inject
  52. @Named(HELLO_KEY)
  53. private String msg;
  54. @Override
  55. protected void doInit() {
  56. System.out.println("before doInit: msg=" + msg);
  57. try {
  58. super.doInit();
  59. } finally {
  60. System.out.println("after doInit: msg=" + msg);
  61. }
  62. }
  63. @Override
  64. public String getMessage() {
  65. return msg;
  66. }
  67. }
  68. private static class TestApplication extends Application {
  69. @Override
  70. public Restlet createInboundRoot() {
  71. Router router = new Router(getContext());
  72. router.attach("/hello", HelloServerResource.class);
  73. return router;
  74. }
  75. }
  76. static class TestModule extends AbstractModule {
  77. protected void configure() {
  78. }
  79. @Provides
  80. @Named(HELLO_KEY)
  81. String helloMessage() {
  82. return HELLO_MSG;
  83. }
  84. }
  85. static final String HELLO_KEY = "hello.message";
  86. static final String HELLO_MSG = "This resource was injected by Guice!";
  87. private volatile Client client;
  88. private volatile Component component;
  89. @Override
  90. protected void setUp() throws Exception {
  91. super.setUp();
  92. Guice.createInjector(new TestModule(),
  93. new SelfInjectingServerResourceModule());
  94. this.client = new Client(Protocol.HTTP);
  95. if (component == null) {
  96. component = new Component();
  97. component.getServers().add(Protocol.HTTP, TEST_PORT);
  98. component.getDefaultHost().attachDefault(new TestApplication());
  99. }
  100. if (!this.component.isStarted()) {
  101. this.component.start();
  102. }
  103. }
  104. @Override
  105. public void tearDown() throws Exception {
  106. this.client.stop();
  107. this.component.stop();
  108. this.component = null;
  109. super.tearDown();
  110. }
  111. @Test
  112. public void testReturnsMessage() {
  113. ClientResource client = new ClientResource("http://localhost:"
  114. + TEST_PORT);
  115. client.accept(MediaType.TEXT_PLAIN);
  116. String msg = client.getChild("/hello", HelloResource.class)
  117. .getMessage();
  118. assertEquals(HELLO_MSG, msg);
  119. }
  120. }