Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.restlet.wraptest;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- import org.restlet.*;
- import org.restlet.data.*;
- import org.restlet.resource.*;
- import org.restlet.routing.*;
- import org.junit.*;
- import static org.junit.Assert.*;
- /**
- * Testing whether ClientResource makes extra GET call at construction
- * or during wrap().
- */
- public class WrapTest extends Application {
- /** Just runs the component. */
- public static void main(String... args) throws Exception {
- new WrapTest().startComponent();
- }
- Component component;
- AtomicInteger requestCount = new AtomicInteger();
- @Before public void startComponent() throws Exception {
- component = new Component();
- component.getServers().add(Protocol.HTTP, 8111);
- component.getDefaultHost().attachDefault(this);
- component.start();
- // Reset request counter.
- requestCount.set(0);
- // Give the component a chance to start up completely:
- TimeUnit.MILLISECONDS.sleep(500);
- }
- @After public void stopComponent() throws Exception {
- component.stop();
- }
- @Test public void callFromClient() {
- ClientResource clientResource = new ClientResource("http://localhost:8111/xxx");
- String text = clientResource.wrap(MyResource.class).getText();
- assertEquals("content", text);
- assertEquals(1, requestCount.get());
- }
- @Override public Restlet createInboundRoot() {
- Filter filter = new RequestCounter(getContext(), requestCount);
- filter.setNext(MyServerResource.class);
- return filter;
- }
- public static class RequestCounter extends Filter {
- private final AtomicInteger requestCount;
- RequestCounter(Context context, AtomicInteger requestCount) {
- super(context);
- this.requestCount = requestCount;
- }
- @Override protected void afterHandle(Request request, Response response) {
- int c = requestCount.incrementAndGet();
- System.err.printf("Handled request %d%n", c);
- }
- }
- public interface MyResource {
- @Get String getText();
- }
- public static class MyServerResource extends ServerResource implements MyResource {
- @Override public String getText() { return "content"; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement