Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
33   96   9   4,12
0   64   0,27   2,67
8     1,12  
3    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  UrlRewriterTest       Line # 14 24 0% 5 0 100% 1.0
  UrlRewriterTest.UrlRewriteTestRunnable       Line # 60 4 0% 2 0 100% 1.0
  UrlRewriterTest.MockRewriter       Line # 79 5 0% 2 0 100% 1.0
 
  (6)
 
1    package net.sourceforge.pebble.util;
2   
3    import junit.framework.TestCase;
4   
5    /**
6    * The HttpsUrlRewriter implementation is actually not tested, as it
7    * delegates its functionality to another utility class, depends upon
8    * configuration and is badly testable. This test makes sure that the
9    * mechanics of using the currently configured threadlocal
10    * implementation - be it whatever it might be - is used.
11    * @author Olaf Kock
12    */
13   
 
14    public class UrlRewriterTest extends TestCase {
15    private static final String HTTP_URL = "http://pebble.sf.net";
16    private static final String HTTPS_URL = "https://sf.net/projects/pebble";
17   
 
18  2 toggle public void testDefaultDoesntRewrite() throws Exception {
19  2 assertEquals(HTTP_URL, UrlRewriter.doRewrite(HTTP_URL));
20    }
21   
 
22  2 toggle public void testMockRewriterIsUsed() {
23  2 MockRewriter mockRewriter = new MockRewriter(HTTP_URL, HTTPS_URL);
24  2 UrlRewriter.useThisRewriter(mockRewriter);
25  2 assertEquals(HTTPS_URL, UrlRewriter.doRewrite(HTTP_URL));
26  2 UrlRewriter.clear();
27  2 assertEquals(HTTP_URL, UrlRewriter.doRewrite(HTTP_URL));
28  2 assertEquals(1, mockRewriter.count);
29    }
30   
 
31  2 toggle public void testThreadedUsage() throws InterruptedException {
32  2 MockRewriter m1 = new MockRewriter("s1", "u1");
33  2 MockRewriter m2 = new MockRewriter("s2", "u2");
34  2 MockRewriter m3 = new MockRewriter("s3", "u3");
35  2 Thread t1 = new Thread(new UrlRewriteTestRunnable(m1));
36  2 Thread t2 = new Thread(new UrlRewriteTestRunnable(m2));
37  2 Thread t3 = new Thread(new UrlRewriteTestRunnable(m3));
38  2 t1.start();
39  2 t2.start();
40  2 t3.start();
41  2 t1.join(1000);
42  2 t2.join(1000);
43  2 t3.join(1000);
44  2 assertEquals(1, m1.count);
45  2 assertEquals(1, m2.count);
46  2 assertEquals(1, m3.count);
47    }
48   
 
49  6 toggle static void sleep() {
50  6 try { Thread.sleep(500); } catch (InterruptedException ignore) {}
51    }
52   
53    /**
54    * Test the usage of the correct thread local rewriter. This is hacked by
55    * all Threads waiting half a second before the actual rewrite - hopefully
56    * this will be enough to make sure that the most obvious race conditions
57    * are met.
58    */
59   
 
60    static class UrlRewriteTestRunnable implements Runnable {
61    public boolean done = false;
62    private final MockRewriter m;
63   
 
64  6 toggle public UrlRewriteTestRunnable(MockRewriter m) {
65  6 this.m = m;
66    }
67   
 
68  6 toggle public void run() {
69  6 UrlRewriter.useThisRewriter(m);
70  6 sleep();
71  6 assertEquals(m.result, UrlRewriter.doRewrite(m.expectation));
72    }
73    }
74   
75    /**
76    * Very simple Mock implementation without external dependencies
77    * to a mocking library...
78    */
 
79    static class MockRewriter extends UrlRewriter {
80    private final String expectation;
81    private final String result;
82    public int count = 0;
83   
 
84  8 toggle public MockRewriter(String expectation, String result) {
85  8 this.expectation = expectation;
86  8 this.result = result;
87    }
88   
 
89  8 toggle @Override
90    public String rewrite(String url) {
91  8 assertEquals(expectation, url);
92  8 count++;
93  8 return result;
94    }
95    }
96    }