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