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.support.classes; |
35 |
|
|
36 |
|
import static info.magnolia.module.groovy.GroovyModule.SCRIPTS_REPOSITORY_NAME; |
37 |
|
import static org.junit.Assert.*; |
38 |
|
import static org.mockito.Matchers.anyString; |
39 |
|
import static org.mockito.Mockito.*; |
40 |
|
|
41 |
|
import info.magnolia.context.MgnlContext; |
42 |
|
import info.magnolia.context.SystemContext; |
43 |
|
import info.magnolia.module.groovy.GroovyBaseTestCase; |
44 |
|
import info.magnolia.test.ComponentsTestUtil; |
45 |
|
import info.magnolia.test.mock.jcr.SessionTestUtil; |
46 |
|
|
47 |
|
import javax.jcr.Session; |
48 |
|
|
49 |
|
import org.codehaus.groovy.control.CompilationFailedException; |
50 |
|
import org.codehaus.groovy.control.MultipleCompilationErrorsException; |
51 |
|
import org.junit.Test; |
52 |
|
|
53 |
|
|
|
|
| 87.8% |
Uncovered Elements: 6 (49) |
Complexity: 16 |
Complexity Density: 0.42 |
|
54 |
|
public class MgnlGroovyClassLoaderTest extends GroovyBaseTestCase { |
55 |
|
|
56 |
|
public static final String NO_PACKAGE_SOURCE = "class NoPackage{}"; |
57 |
|
|
58 |
|
public static final String WITH_PACKAGE_SOURCE = "package foo.bar\nclass WithPackage{}"; |
59 |
|
|
60 |
|
public static final String NON_EXISTENT_PACKAGE_SOURCE = "package non.existent\nclass NonExistent{}"; |
61 |
|
|
62 |
|
public static final String WRONG_CLASS_NAME_SOURCE = "package foo.bar\nclass Wrong{}"; |
63 |
|
|
64 |
|
public static final String MULTIPLE_CLASSES_IN_ONE_SOURCE = "package foo.bar\nclass Baz{}\nclass AnotherBaz{}"; |
65 |
|
|
66 |
|
private MgnlGroovyClassLoader gcl = null; |
67 |
|
|
68 |
|
public static final String CLASS_A = |
69 |
|
"package foo;\n" + |
70 |
|
"public class ClassA{\n" + |
71 |
|
" public ClassA(){}\n" + |
72 |
|
" public String getString(){\n" + |
73 |
|
" return \"my string\";" + |
74 |
|
" }" + |
75 |
|
"}"; |
76 |
|
|
77 |
|
public static final String CLASS_B_DEPENDS_ON_A = |
78 |
|
"package foo;\n" + |
79 |
|
"public class ClassB{\n" + |
80 |
|
" public ClassB(){}\n" + |
81 |
|
" public String getString(){\n" + |
82 |
|
" return new ClassA().getString();" + |
83 |
|
" }" + |
84 |
|
"}"; |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
86 |
9 |
@Override... |
87 |
|
public void setUp() throws Exception { |
88 |
9 |
super.setUp(); |
89 |
9 |
Session session = SessionTestUtil.createSession(SCRIPTS_REPOSITORY_NAME, "/foo/bar\n"); |
90 |
9 |
SystemContext context = mock(SystemContext.class); |
91 |
9 |
when(context.getJCRSession(anyString())).thenReturn(session); |
92 |
9 |
MgnlContext.setInstance(context); |
93 |
9 |
ComponentsTestUtil.setInstance(SystemContext.class, context); |
94 |
9 |
gcl = new MgnlGroovyClassLoader(); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
97 |
9 |
@Override... |
98 |
|
public void tearDown() throws Exception { |
99 |
9 |
super.tearDown(); |
100 |
9 |
MgnlContext.setInstance(null); |
101 |
9 |
ComponentsTestUtil.setInstance(SystemContext.class, null); |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
104 |
1 |
@Test(expected = CompilationFailedException.class)... |
105 |
|
public void throwCFEWhenCompileChecksAreOnAndPackageDeclarationIsMissing() throws Exception { |
106 |
|
|
107 |
1 |
gcl.verify(NO_PACKAGE_SOURCE, true, "/foo/bar/NoPackage"); |
108 |
|
|
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
111 |
1 |
@Test(expected = CompilationFailedException.class)... |
112 |
|
public void throwCFEWhenCompileChecksAreOnAndPackageDeclarationIsWrong() throws Exception { |
113 |
|
|
114 |
1 |
gcl.verify(NON_EXISTENT_PACKAGE_SOURCE, true, "/non/existent/NonExistent"); |
115 |
|
|
116 |
|
} |
117 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
public void doNotThrowCFEWhenCompileChecksAreOnAndPackageDeclarationIsCorrect() throws Exception { |
120 |
|
|
121 |
1 |
try { |
122 |
1 |
gcl.verify(WITH_PACKAGE_SOURCE, true, "/foo/bar/WithPackage"); |
123 |
|
} catch (CompilationFailedException e) { |
124 |
|
|
125 |
0 |
fail("should not have failed but instead failed with " + e); |
126 |
|
} |
127 |
|
} |
128 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
1PASS
|
|
129 |
1 |
@Test... |
130 |
|
public void doNotThrowCFEWhenCompileChecksAreOnAndPackageDeclarationIsEmptyAndMgnlPathIsRoot() throws Exception { |
131 |
|
|
132 |
1 |
try { |
133 |
1 |
gcl.verify(NO_PACKAGE_SOURCE, true, "/NoPackage"); |
134 |
|
} catch (CompilationFailedException e) { |
135 |
|
|
136 |
0 |
fail("should not have failed but instead failed with " + e); |
137 |
|
} |
138 |
|
} |
139 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
140 |
1 |
@Test... |
141 |
|
public void doNotThrowCFEWhenCompileChecksAreOffAndPackageDeclarationIsWrong() throws Exception { |
142 |
|
|
143 |
1 |
try { |
144 |
1 |
gcl.verify(NO_PACKAGE_SOURCE, false, null); |
145 |
1 |
gcl.verify(WITH_PACKAGE_SOURCE, false, "/i/will/be/ignored"); |
146 |
|
} catch (CompilationFailedException e) { |
147 |
|
|
148 |
0 |
fail("should not have failed but instead failed with " + e); |
149 |
|
} |
150 |
|
} |
151 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
152 |
1 |
@Test... |
153 |
|
public void doNotThrowCFEWhenCompileChecksAreOnAndClassDeclarationIsFound() throws Exception { |
154 |
|
|
155 |
1 |
try { |
156 |
1 |
gcl.verify(WITH_PACKAGE_SOURCE, true, "/foo/bar/WithPackage"); |
157 |
1 |
gcl.verify(MULTIPLE_CLASSES_IN_ONE_SOURCE, true, "/foo/bar/AnotherBaz"); |
158 |
|
} catch (CompilationFailedException e) { |
159 |
|
|
160 |
0 |
fail("should not have failed but instead failed with " + e); |
161 |
|
} |
162 |
|
} |
163 |
|
|
|
|
| 50% |
Uncovered Elements: 1 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
164 |
1 |
@Test(expected = CompilationFailedException.class)... |
165 |
|
public void throwCFEWhenCompileChecksAreOnAndClassDeclarationIsNotFound() throws Exception { |
166 |
|
|
167 |
1 |
gcl.verify(WRONG_CLASS_NAME_SOURCE, true, "/foo/bar/Something"); |
168 |
0 |
gcl.verify(MULTIPLE_CLASSES_IN_ONE_SOURCE, true, "/foo/bar/Something"); |
169 |
|
|
170 |
|
} |
171 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
172 |
1 |
@Test... |
173 |
|
public void mgnlGroovyClassLoaderCallMethod() throws Exception { |
174 |
1 |
try { |
175 |
|
|
176 |
1 |
gcl.verify(CLASS_A, true, "/foo/ClassA"); |
177 |
1 |
gcl.verify(CLASS_B_DEPENDS_ON_A, true, "/foo/ClassB"); |
178 |
|
|
179 |
|
} catch (MultipleCompilationErrorsException e) { |
180 |
0 |
fail("MultipleCompilationErrorsException should not occur"); |
181 |
|
} |
182 |
|
} |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
184 |
1 |
@Test... |
185 |
|
public void resolveScriptNameFromSource() throws Exception { |
186 |
|
|
187 |
|
|
188 |
1 |
String name = MgnlGroovyClassLoader.resolveScriptNameFromSource("package meh\nclass Foo {}"); |
189 |
|
|
190 |
|
|
191 |
1 |
assertEquals("Foo", name); |
192 |
|
|
193 |
|
|
194 |
1 |
name = MgnlGroovyClassLoader.resolveScriptNameFromSource("class Baz{}\nclass Foo {} "); |
195 |
|
|
196 |
|
|
197 |
1 |
assertEquals("Baz", name); |
198 |
|
|
199 |
|
|
200 |
1 |
name = MgnlGroovyClassLoader.resolveScriptNameFromSource("x = 10"); |
201 |
|
|
202 |
|
|
203 |
1 |
assertEquals("untitledGroovyScript", name); |
204 |
|
} |
205 |
|
} |