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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 }