View Javadoc

1   package com.atlassian.gzipfilter.selector;
2   
3   import junit.framework.TestCase;
4   
5   public class TestPatternMatcher extends TestCase
6   {
7       PatternMatcher p = new PatternMatcher();
8   
9       public void testSimple()
10      {
11          assertTrue(p.matches("text/html", "text/html"));
12          assertTrue(p.matches("text/plain", "text/plain"));
13  
14          assertFalse(p.matches("text/plain", "text/html"));
15          assertFalse(p.matches("text/plain", ""));
16          assertFalse(p.matches("text/plain", ",,,,"));
17      }
18  
19      public void testMultipleInputs()
20      {
21          assertTrue(p.matches("text/html", "text/plain,text/html"));
22          assertTrue(p.matches("text/plain", "text/plain,text/html"));
23          assertTrue(p.matches("text/plain", "text/plain,text/html,application/x-javascript"));
24  
25          assertFalse(p.matches("application/msword", "text/plain,text/html,application/x-javascript"));
26          assertFalse(p.matches("text/javascript", "text/plain,text/html,application/x-javascript"));
27      }
28  
29      public void testRegularExpressions()
30      {
31          assertTrue(p.matches("text/html", "text/.*"));
32          assertTrue(p.matches("text/html", "application/x-javascript,text/.*"));
33          assertTrue(p.matches("text/plain", "application/x-javascript,text/.*"));
34          assertTrue(p.matches("application/javascript", "application/.?.?javascript,text/.*"));
35          assertTrue(p.matches("application/x-javascript", "application/.?.?javascript,text/.*"));
36  
37          assertFalse(p.matches("application/msword", "application/??javascript,text/.*"));
38          assertFalse(p.matches("application/msword", "application/??javascript,text/.*"));
39      }
40  
41      static final String TYPES_TO_GZIP = "text/.*,application/.?.?javascript,application/xml";
42  
43      public void testForRealWorldUsage()
44      {
45          // text/html,text/javascript,text/css,text/plain,application/x-javascript,application/javascript
46  
47          assertTrue(p.matches("text/html", TYPES_TO_GZIP));
48          assertTrue(p.matches("text/plain", TYPES_TO_GZIP));
49          assertTrue(p.matches("text/css", TYPES_TO_GZIP));
50          assertTrue(p.matches("text/javascript", TYPES_TO_GZIP));
51          assertTrue(p.matches("application/javascript", TYPES_TO_GZIP));
52          assertTrue(p.matches("application/x-javascript", TYPES_TO_GZIP));
53          assertTrue(p.matches("application/xml", TYPES_TO_GZIP));
54  
55          assertFalse(p.matches("application/msword", TYPES_TO_GZIP));
56          assertFalse(p.matches("application/octet-stream", TYPES_TO_GZIP));
57      }
58  
59      // Perfomance test to detemine whether caching works or not.
60      // Commented out because it requires java 1.5 methods, but we want
61      // this library to work with JDK 1.4.
62  
63  //    public static void main(String[] args) throws InterruptedException
64  //    {
65  //        long start = System.currentTimeMillis();
66  //        java.util.concurrent.ExecutorService ex = java.util.concurrent.Executors.newFixedThreadPool(20);
67  //        for (int i = 0; i < 20; i++)
68  //        {
69  //            ex.execute(new PerfTestThread());
70  //        }
71  //        ex.shutdown();
72  //        ex.awaitTermination(1000, java.util.concurrent.TimeUnit.SECONDS);
73  //
74  //        System.out.println("Time taken: " + (System.currentTimeMillis() - start));
75  //    }
76  //
77  //    private static class PerfTestThread implements Runnable
78  //    {
79  //        public void run()
80  //        {
81  //            TestPatternMatcher tpm = new TestPatternMatcher();
82  //            System.out.println("Starting Thread");
83  //            // Performance testing
84  //            for (int i = 0; i < 10000; i++)
85  //            {
86  //                tpm.testForRealWorldUsage();
87  //            }
88  //            System.out.println("Finishing Thread");
89  //
90  //        }
91  //    }
92  }