Clover icon

magnolia-module-groovy 2.4.7

  1. Project Clover database Thu Dec 1 2016 10:48:40 CET
  2. Package info.magnolia.module.groovy.support.classes

File MgnlGroovyClassLoaderTest.java

 

Code metrics

0
38
11
1
205
119
16
0.42
3.45
11
1.45

Classes

Class Line # Actions
MgnlGroovyClassLoaderTest 54 38 0% 16 6
0.87755187.8%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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   
 
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   
 
86  9 toggle @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   
 
97  9 toggle @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   
 
104  1 toggle @Test(expected = CompilationFailedException.class)
105    public void throwCFEWhenCompileChecksAreOnAndPackageDeclarationIsMissing() throws Exception {
106    // WHEN
107  1 gcl.verify(NO_PACKAGE_SOURCE, true, "/foo/bar/NoPackage");
108    // THEN should have failed with CompilationFailedException as source did not declared a package"
109    }
110   
 
111  1 toggle @Test(expected = CompilationFailedException.class)
112    public void throwCFEWhenCompileChecksAreOnAndPackageDeclarationIsWrong() throws Exception {
113    // WHEN
114  1 gcl.verify(NON_EXISTENT_PACKAGE_SOURCE, true, "/non/existent/NonExistent");
115    // THEN should have failed with CompilationFailedException as source declares a package which does not exist in repository
116    }
117   
 
118  1 toggle @Test
119    public void doNotThrowCFEWhenCompileChecksAreOnAndPackageDeclarationIsCorrect() throws Exception {
120    // WHEN
121  1 try {
122  1 gcl.verify(WITH_PACKAGE_SOURCE, true, "/foo/bar/WithPackage");
123    } catch (CompilationFailedException e) {
124    // THEN
125  0 fail("should not have failed but instead failed with " + e);
126    }
127    }
128   
 
129  1 toggle @Test
130    public void doNotThrowCFEWhenCompileChecksAreOnAndPackageDeclarationIsEmptyAndMgnlPathIsRoot() throws Exception {
131    // WHEN
132  1 try {
133  1 gcl.verify(NO_PACKAGE_SOURCE, true, "/NoPackage");
134    } catch (CompilationFailedException e) {
135    // THEN
136  0 fail("should not have failed but instead failed with " + e);
137    }
138    }
139   
 
140  1 toggle @Test
141    public void doNotThrowCFEWhenCompileChecksAreOffAndPackageDeclarationIsWrong() throws Exception {
142    // WHEN
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    // THEN
148  0 fail("should not have failed but instead failed with " + e);
149    }
150    }
151   
 
152  1 toggle @Test
153    public void doNotThrowCFEWhenCompileChecksAreOnAndClassDeclarationIsFound() throws Exception {
154    // WHEN
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    // THEN
160  0 fail("should not have failed but instead failed with " + e);
161    }
162    }
163   
 
164  1 toggle @Test(expected = CompilationFailedException.class)
165    public void throwCFEWhenCompileChecksAreOnAndClassDeclarationIsNotFound() throws Exception {
166    // WHEN
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    // THEN should have failed
170    }
171   
 
172  1 toggle @Test
173    public void mgnlGroovyClassLoaderCallMethod() throws Exception {
174  1 try {
175    // WHEN
176  1 gcl.verify(CLASS_A, true, "/foo/ClassA");
177  1 gcl.verify(CLASS_B_DEPENDS_ON_A, true, "/foo/ClassB");
178    // THEN
179    } catch (MultipleCompilationErrorsException e) {
180  0 fail("MultipleCompilationErrorsException should not occur");
181    }
182    }
183   
 
184  1 toggle @Test
185    public void resolveScriptNameFromSource() throws Exception {
186   
187    // WHEN
188  1 String name = MgnlGroovyClassLoader.resolveScriptNameFromSource("package meh\nclass Foo {}");
189   
190    // THEN
191  1 assertEquals("Foo", name);
192   
193    // WHEN
194  1 name = MgnlGroovyClassLoader.resolveScriptNameFromSource("class Baz{}\nclass Foo {} ");
195   
196    // THEN
197  1 assertEquals("Baz", name);
198   
199    // WHEN
200  1 name = MgnlGroovyClassLoader.resolveScriptNameFromSource("x = 10");
201   
202    // THEN
203  1 assertEquals("untitledGroovyScript", name);
204    }
205    }