View Javadoc

1   package com.atlassian.plugin.util.validation;
2   
3   import junit.framework.TestCase;
4   import org.dom4j.Element;
5   import org.dom4j.DocumentFactory;
6   import static com.atlassian.plugin.util.validation.ValidationPattern.createPattern;
7   import static com.atlassian.plugin.util.validation.ValidationPattern.test;
8   import com.atlassian.plugin.PluginParseException;
9   
10  public class TestValidationPattern extends TestCase
11  {
12      private Element root;
13  
14      @Override
15      public void setUp()
16      {
17          DocumentFactory factory = DocumentFactory.getInstance();
18          root = factory.createElement("root");
19          root.addAttribute("foo", "bar");
20          Element child = factory.createElement("child");
21          child.addAttribute("some", "thing");
22          child.setText("mybody");
23          Element emptyChild = factory.createElement("child");
24          root.add(child);
25          root.add(emptyChild);
26      }
27  
28      public void testSuccess()
29      {
30          createPattern().
31                  rule(".",
32                          test("child").withError("Child is required"),
33                          test("not(baz)").withError("Baz should not exist")).
34                  rule("child[1]",
35                          test(".[@some = 'thing']").withError("Need some attribute")).
36                  evaluate(root);
37  
38      }
39  
40      /*public void testSuccessPerfTest()
41      {
42          ValidationPattern ptn = createPattern().
43                  rule(".",
44                          test("child").withError("Child is required"),
45                          test("not(baz)").withError("Baz should not exist")).
46                  rule("child[1]",
47                          test(".[@some = 'thing']").withError("Need some attribute"));
48  
49          for (int x=0; x<1000; x++)
50          {
51              ptn.evaluate(root);
52          }
53  
54          long start = System.currentTimeMillis();
55          for (int x=0; x<5000; x++)
56          {
57              ptn.evaluate(root);
58          }
59          long end = System.currentTimeMillis();
60          System.out.println("Time elapsed: "+(end-start)+" ms");
61  
62      }*/
63  
64      public void testErrorMessageWithEmptyList()
65      {
66          try
67          {
68              createPattern().
69                      rule(".",
70                              test("baz").withError("Baz should exist")).
71                      evaluate(root);
72              fail();
73          }
74          catch (PluginParseException ex)
75          {
76              assertTrue(ex.getMessage().startsWith("Baz should exist"));
77          }
78      }
79  
80      public void testErrorMessageWithNull()
81      {
82          try
83          {
84              createPattern().
85                      rule(".",
86                              test("baz[1]").withError("Baz should exist")).
87                      evaluate(root);
88              fail();
89          }
90          catch (PluginParseException ex)
91          {
92              assertTrue(ex.getMessage().startsWith("Baz should exist"));
93          }
94      }
95  
96      public void testErrorMessageWithBoolean()
97      {
98          try
99          {
100             createPattern().
101                     rule(".",
102                             test("not(not(baz))").withError("Baz should exist")).
103                     evaluate(root);
104             fail();
105         }
106         catch (PluginParseException ex)
107         {
108             assertTrue(ex.getMessage().startsWith("Baz should exist"));
109         }
110     }
111 
112 
113 }