1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.groovy.validator; |
35 |
|
|
36 |
|
import static org.junit.Assert.*; |
37 |
|
import static org.mockito.Matchers.anyString; |
38 |
|
import static org.mockito.Matchers.eq; |
39 |
|
import static org.mockito.Mockito.*; |
40 |
|
|
41 |
|
import info.magnolia.context.MgnlContext; |
42 |
|
import info.magnolia.context.SystemContext; |
43 |
|
import info.magnolia.test.ComponentsTestUtil; |
44 |
|
import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter; |
45 |
|
|
46 |
|
import javax.jcr.Node; |
47 |
|
import javax.jcr.Session; |
48 |
|
|
49 |
|
import org.junit.After; |
50 |
|
import org.junit.Before; |
51 |
|
import org.junit.Test; |
52 |
|
|
53 |
|
import com.vaadin.data.Property; |
54 |
|
|
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (60) |
Complexity: 8 |
Complexity Density: 0.15 |
|
56 |
|
public class GroovyValidatorTest { |
57 |
|
private Session session; |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
59 |
6 |
@Before... |
60 |
|
public void setUp() throws Exception { |
61 |
6 |
SystemContext ctx = mock(SystemContext.class); |
62 |
6 |
session = mock(Session.class); |
63 |
6 |
when(ctx.getJCRSession(anyString())).thenReturn(session); |
64 |
6 |
ComponentsTestUtil.setInstance(SystemContext.class, ctx); |
65 |
6 |
MgnlContext.setInstance(ctx); |
66 |
|
} |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
68 |
6 |
@After... |
69 |
|
public void tearDown() { |
70 |
6 |
MgnlContext.setInstance(null); |
71 |
6 |
ComponentsTestUtil.setInstance(SystemContext.class, null); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void validateGroovyClassWithPackage() throws Exception { |
76 |
|
|
77 |
1 |
JcrNodeAdapter nodeAdapter = mock(JcrNodeAdapter.class); |
78 |
1 |
Node node = mock(Node.class); |
79 |
1 |
when(node.getPath()).thenReturn("/foo/bar/Baz"); |
80 |
1 |
when(nodeAdapter.getJcrItem()).thenReturn(node); |
81 |
|
|
82 |
1 |
when(session.nodeExists(eq("/foo/bar/"))).thenReturn(true); |
83 |
|
|
84 |
|
|
85 |
1 |
GroovyValidator validator = new GroovyValidator(nodeAdapter, "error"); |
86 |
|
|
87 |
|
|
88 |
1 |
assertTrue(validator.isValidValue("package foo.bar\n class Baz{}")); |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
91 |
1 |
@Test... |
92 |
|
public void validateGroovyClassWithInexistentPackage() throws Exception { |
93 |
|
|
94 |
1 |
JcrNodeAdapter nodeAdapter = mock(JcrNodeAdapter.class); |
95 |
1 |
Node node = mock(Node.class); |
96 |
1 |
when(node.getPath()).thenReturn("/foo/bar/Baz"); |
97 |
1 |
when(nodeAdapter.getJcrItem()).thenReturn(node); |
98 |
|
|
99 |
1 |
when(session.nodeExists(eq("/foo/bar/"))).thenReturn(false); |
100 |
|
|
101 |
|
|
102 |
1 |
GroovyValidator validator = new GroovyValidator(nodeAdapter, "error"); |
103 |
|
|
104 |
|
|
105 |
1 |
assertFalse(validator.isValidValue("package foo.bar\n class Baz{}")); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
108 |
1 |
@Test... |
109 |
|
public void validateGroovyClassWithWrongPackage() throws Exception { |
110 |
|
|
111 |
1 |
JcrNodeAdapter nodeAdapter = mock(JcrNodeAdapter.class); |
112 |
1 |
Node node = mock(Node.class); |
113 |
1 |
when(node.getPath()).thenReturn("/qux/Baz"); |
114 |
1 |
when(nodeAdapter.getJcrItem()).thenReturn(node); |
115 |
|
|
116 |
1 |
when(session.nodeExists(eq("/qux/"))).thenReturn(true); |
117 |
|
|
118 |
|
|
119 |
1 |
GroovyValidator validator = new GroovyValidator(nodeAdapter, "error"); |
120 |
|
|
121 |
|
|
122 |
1 |
assertFalse(validator.isValidValue("package foo.bar\n class Baz{}")); |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
public void validateGroovyClassNoPackage() throws Exception { |
127 |
|
|
128 |
1 |
JcrNodeAdapter nodeAdapter = mock(JcrNodeAdapter.class); |
129 |
1 |
Node node = mock(Node.class); |
130 |
1 |
when(node.getPath()).thenReturn("/Baz"); |
131 |
1 |
when(nodeAdapter.getJcrItem()).thenReturn(node); |
132 |
|
|
133 |
1 |
when(session.nodeExists(eq("/"))).thenReturn(true); |
134 |
|
|
135 |
|
|
136 |
1 |
GroovyValidator validator = new GroovyValidator(nodeAdapter, "error"); |
137 |
|
|
138 |
|
|
139 |
1 |
assertTrue(validator.isValidValue("class Baz{}")); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
142 |
1 |
@Test... |
143 |
|
public void validateGroovyClassDeclaringPackageButUnderRoot() throws Exception { |
144 |
|
|
145 |
1 |
JcrNodeAdapter nodeAdapter = mock(JcrNodeAdapter.class); |
146 |
1 |
Node node = mock(Node.class); |
147 |
1 |
when(node.getPath()).thenReturn("/Baz"); |
148 |
1 |
when(nodeAdapter.getJcrItem()).thenReturn(node); |
149 |
|
|
150 |
1 |
when(session.nodeExists(eq("/"))).thenReturn(true); |
151 |
|
|
152 |
|
|
153 |
1 |
GroovyValidator validator = new GroovyValidator(nodeAdapter, "error"); |
154 |
|
|
155 |
|
|
156 |
1 |
assertFalse(validator.isValidValue("package foo\nclass Baz{}")); |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
159 |
1 |
@Test... |
160 |
|
public void validateGroovyScript() throws Exception { |
161 |
|
|
162 |
1 |
JcrNodeAdapter nodeAdapter = mock(JcrNodeAdapter.class); |
163 |
1 |
Node node = mock(Node.class); |
164 |
1 |
when(node.getPath()).thenReturn("/Baz"); |
165 |
|
|
166 |
1 |
when(nodeAdapter.getJcrItem()).thenReturn(node); |
167 |
1 |
Property<Boolean> property = mock(Property.class); |
168 |
1 |
when(property.getValue()).thenReturn(true); |
169 |
1 |
when(nodeAdapter.getItemProperty(eq("script"))).thenReturn(property); |
170 |
|
|
171 |
1 |
when(session.nodeExists(eq("/"))).thenReturn(true); |
172 |
|
|
173 |
|
|
174 |
1 |
GroovyValidator validator = new GroovyValidator(nodeAdapter, "error"); |
175 |
|
|
176 |
|
|
177 |
1 |
assertTrue(validator.isValidValue("def x = 10\n println x")); |
178 |
|
} |
179 |
|
|
180 |
|
} |