Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.restlet.genparm;
- import java.io.IOException;
- import org.restlet.*;
- import org.restlet.data.*;
- import org.restlet.representation.*;
- import org.restlet.resource.*;
- import org.restlet.routing.*;
- import org.junit.*;
- import static org.junit.Assert.*;
- public class GenericParameterTest extends Application {
- public static void main(String... args) throws Exception {
- GenericParameterTest re = new GenericParameterTest();
- re.startComponent();
- }
- @Before public void startComponent() throws Exception {
- component = new Component();
- component.getServers().add(Protocol.HTTP, 8111);
- component.getDefaultHost().attachDefault(this);
- component.start();
- clientResource = new ClientResource("http://localhost:8111");
- }
- @After public void stopComponent() throws Exception {
- component.stop();
- }
- @Test public void postAnnotationIsNotOnGenericVersionOfMethod() throws IOException {
- Representation rep = new StringRepresentation("abc");
- assertEquals("added string abc", resourceFor("/postNotOnGeneric").post(rep).getText());
- }
- @Test public void postAnnotationIsOnGenericVersionOfMethod() throws IOException {
- Representation rep = new StringRepresentation("abc");
- try {
- resourceFor("/postOnGeneric").post(rep);
- fail("Expecting resource exception");
- } catch (ResourceException ex) {
- Status status = ex.getStatus();
- assertEquals(500, status.getCode());
- assertEquals("Internal Server Error", status.getReasonPhrase());
- }
- }
- ClientResource resourceFor(String uri) {
- return clientResource.getChild(uri);
- }
- @Override public Restlet createInboundRoot() {
- Router router = new Router(getContext());
- router.attach("/postNotOnGeneric", StringServerResource.class);
- router.attach("/postOnGeneric", StringServerResource2.class);
- return router;
- }
- public interface GenericResource<T> {
- String addValue(T parameter);
- }
- public interface StringResource extends GenericResource<String> {
- @Post String addValue(String parameter);
- }
- public static class StringServerResource extends ServerResource implements StringResource {
- public String addValue(String parameter) {
- return "added string " + parameter;
- }
- }
- public interface GenericResource2<T> {
- @Post String addValue(T parameter);
- }
- public interface StringResource2 extends GenericResource2<String> {
- @Post String addValue(String parameter);
- }
- public static class StringServerResource2 extends ServerResource implements StringResource2 {
- public String addValue(String parameter) {
- return "added string " + parameter;
- }
- }
- Component component;
- ClientResource clientResource;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement