Advertisement
tpeierls

Extra call in ClientResource ctor/wrap?

Sep 20th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.40 KB | None | 0 0
  1. package com.example.restlet.wraptest;
  2.  
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5.  
  6. import org.restlet.*;
  7. import org.restlet.data.*;
  8. import org.restlet.resource.*;
  9. import org.restlet.routing.*;
  10.  
  11. import org.junit.*;
  12. import static org.junit.Assert.*;
  13.  
  14.  
  15. /**
  16.  * Testing whether ClientResource makes extra GET call at construction
  17.  * or during wrap().
  18.  */
  19. public class WrapTest extends Application {
  20.  
  21.     /** Just runs the component. */
  22.     public static void main(String... args) throws Exception {
  23.         new WrapTest().startComponent();
  24.     }
  25.  
  26.     Component component;
  27.     AtomicInteger requestCount = new AtomicInteger();
  28.  
  29.     @Before public void startComponent() throws Exception {
  30.         component = new Component();
  31.         component.getServers().add(Protocol.HTTP, 8111);
  32.         component.getDefaultHost().attachDefault(this);
  33.         component.start();
  34.  
  35.         // Reset request counter.
  36.         requestCount.set(0);
  37.  
  38.         // Give the component a chance to start up completely:
  39.         TimeUnit.MILLISECONDS.sleep(500);
  40.     }
  41.  
  42.     @After public void stopComponent() throws Exception {
  43.         component.stop();
  44.     }
  45.  
  46.     @Test public void callFromClient() {
  47.         ClientResource clientResource = new ClientResource("http://localhost:8111/xxx");
  48.         String text = clientResource.wrap(MyResource.class).getText();
  49.         assertEquals("content", text);
  50.         assertEquals(1, requestCount.get());
  51.     }
  52.  
  53.  
  54.     @Override public Restlet createInboundRoot() {
  55.         Filter filter = new RequestCounter(getContext(), requestCount);
  56.         filter.setNext(MyServerResource.class);
  57.         return filter;
  58.     }
  59.  
  60.     public static class RequestCounter extends Filter {
  61.  
  62.         private final AtomicInteger requestCount;
  63.  
  64.         RequestCounter(Context context, AtomicInteger requestCount) {
  65.             super(context);
  66.             this.requestCount = requestCount;
  67.         }
  68.  
  69.         @Override protected void afterHandle(Request request, Response response) {
  70.             int c = requestCount.incrementAndGet();
  71.             System.err.printf("Handled request %d%n", c);
  72.         }
  73.     }
  74.  
  75.     public interface MyResource {
  76.         @Get String getText();
  77.     }
  78.  
  79.     public static class MyServerResource extends ServerResource implements MyResource {
  80.         @Override public String getText() { return "content"; }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement