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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 }