~linaro-automation/jenkins-remoting/linaro-trunk

« back to all changes in this revision

Viewing changes to src/test/java/hudson/remoting/ChannelFilterTest.java

  • Committer: Kohsuke Kawaguchi
  • Date: 2012-01-04 21:54:04 UTC
  • Revision ID: git-v1:b1c5a1b54aa9a578b80952e8bcbf9310d9ba543a
added a mechanism to intercept Callable execution

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package hudson.remoting;
 
2
 
 
3
import java.util.concurrent.Callable;
 
4
 
 
5
/**
 
6
 * @author Kohsuke Kawaguchi
 
7
 */
 
8
public class ChannelFilterTest extends RmiTestBase {
 
9
    public void testFilter() throws Exception {
 
10
        channel.addLocalExecutionInterceptor(new CallableFilter() {
 
11
            public <V> V call(Callable<V> callable) throws Exception {
 
12
                Object old = STORE.get();
 
13
                STORE.set("x");
 
14
                try {
 
15
                    return callable.call();
 
16
                } finally {
 
17
                    STORE.set(old);
 
18
                }
 
19
            }
 
20
        });
 
21
 
 
22
        Callable<Object> t = new Callable<Object>() {
 
23
            public Object call() throws Exception {
 
24
                return STORE.get();
 
25
            }
 
26
        };
 
27
        final Callable c = channel.export(Callable.class, t);
 
28
        
 
29
        assertEquals("x", channel.call(new CallableCallable(c)));
 
30
    }
 
31
    
 
32
    private final ThreadLocal<Object> STORE = new ThreadLocal<Object>();
 
33
 
 
34
    private static class CallableCallable implements hudson.remoting.Callable<Object, Exception> {
 
35
        private final Callable c;
 
36
 
 
37
        public CallableCallable(Callable c) {
 
38
            this.c = c;
 
39
        }
 
40
 
 
41
        public Object call() throws Exception {
 
42
           return c.call();
 
43
        }
 
44
    }
 
45
}