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