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

File GroovyBaseTestCase.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
34% of files have more coverage

Code metrics

0
16
17
4
261
163
17
1.06
0.94
4.25
1
42.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
GroovyBaseTestCase 58 8 0% 5 0
1.0100%
GroovyBaseTestCase.NameIgnoringHierarchyManagerProvider 122 2 0% 2 4
0.00%
GroovyBaseTestCase.ExtendedMockContent 136 1 50% 1 2
0.00%
GroovyBaseTestCase.GroovyUser 152 5 61.1% 9 11
0.2142857221.4%
 

Contributing tests

This file is covered by 10 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;
35   
36    import java.util.ArrayList;
37    import java.util.Collection;
38   
39    import javax.security.auth.Subject;
40   
41    import org.junit.After;
42    import org.junit.Before;
43    import static org.junit.Assert.*;
44   
45    import info.magnolia.cms.core.HierarchyManager;
46    import info.magnolia.cms.core.NodeData;
47    import info.magnolia.cms.core.SystemProperty;
48    import info.magnolia.cms.security.User;
49    import info.magnolia.module.groovy.support.HierarchyManagerProvider;
50    import info.magnolia.objectfactory.ClassFactory;
51    import info.magnolia.test.mock.MockContent;
52   
53   
54   
55    /**
56    * Convenience superclass for testing some groovy module specific features.
57    */
 
58    public abstract class GroovyBaseTestCase {
59   
 
60  23 toggle public GroovyBaseTestCase() {
61  23 super();
62    }
63   
 
64  23 toggle @After
65    public void tearDown() throws Exception {
66  23 SystemProperty.getProperties().clear();
67    }
68   
 
69  23 toggle @Before
70    public void setUp() throws Exception {
71  23 SystemProperty.getProperties().clear();
72    }
73   
 
74  7 toggle protected final void assertMethodCallOnNewInstance(Object expected, ClassFactory cf, String className, String methodName) throws Exception {
75  7 final Class< ? > cl = cf.forName(className);
76  7 final Object o = cf.newInstance(cl);
77  7 final Object value = o.getClass().getMethod(methodName).invoke(o);
78  7 assertEquals(expected, value);
79    }
80   
81    /**
82    * Converts new-lines into a format that will be valid for multi-line
83    * property values.
84    */
 
85  8 toggle protected static final String forProps(String s) {
86  8 return s.replace("\n", "\\n\\\n");
87    }
88   
89    protected static final String DUMMY_CLASS_STR_1 = ""
90    + "package foo.bar\n"
91    + "class Baz {\n"
92    + " String something() {\n"
93    + " return \"first value\"\n"
94    + " }\n"
95    + "}\n";
96   
97    protected static final String DUMMY_CLASS_STR_2 = ""
98    + "package foo.bar\n"
99    + "class Baz {\n"
100    + " String something() {\n"
101    + " return \"new value\"\n"
102    + " }\n"
103    + "}\n";
104   
105    protected static final String DUPLICATE_CLASS_IN_REPO = ""
106    + "package info.magnolia.module.groovy.test\n"
107    + "class Duplicate {\n"
108    + " Duplicate() {\n"
109    + " }\n"
110    + "\n"
111    + " java.lang.String value() {\n"
112    + " return \"from repo\"\n"
113    + " }\n"
114    + "}\n";
115   
116    protected static final String DEMO_PROJECT_HM = ""
117    + "/demo-project/about/abstract=string:Lorem ipsum about\n"
118    + "/demo-project/about/MetaData/mgnl\\:lastmodified=date:2008-08-20T21:12:34.455+02:00\n"
119    + "/demo-project/multimedia/abstract=string:Lorem ipsum multimedia\n"
120    + "/demo-project/multimedia/MetaData/mgnl\\:lastmodified=date:2008-08-21T21:12:34.455+02:00\n";
121   
 
122    public static final class NameIgnoringHierarchyManagerProvider implements HierarchyManagerProvider {
123   
124    private final HierarchyManager hm;
125   
 
126  0 toggle public NameIgnoringHierarchyManagerProvider(HierarchyManager hm) {
127  0 this.hm = hm;
128    }
129   
 
130  0 toggle @Override
131    public HierarchyManager getHierarchyManager(String name) {
132  0 return hm;
133    }
134    }
135   
 
136    public static final class ExtendedMockContent extends MockContent {
137   
 
138  0 toggle public ExtendedMockContent(String name) {
139  0 super(name);
140    }
141   
 
142    toggle @Override
143    public Collection<NodeData> getNodeDataCollection() {
144    return super.getNodeDataCollection(null);
145    }
146    }
147   
148    /**
149    * A fake user who has roles 'superuser' and scripter. Only methods implemented are getRoles() and hasRole().
150    *
151    */
 
152    public static final class GroovyUser implements User {
153   
154    private static final long serialVersionUID = 1L;
155   
156    private Collection<String> roles = new ArrayList<String>();
157   
 
158  1 toggle public GroovyUser() {
159  1 roles.add("superuser");
160  1 roles.add("scripter");
161    }
162   
 
163    toggle public void setSubject(Subject subject) {
164   
165    }
166   
 
167  0 toggle @Override
168    public void setProperty(String propertyName, String value) {
169   
170    }
171   
 
172    toggle @Override
173    public void setEnabled(boolean enabled) {
174   
175    }
176   
 
177  0 toggle @Override
178    public void removeRole(String roleName) throws UnsupportedOperationException {
179   
180    }
181   
 
182  0 toggle @Override
183    public void removeGroup(String groupName) throws UnsupportedOperationException {
184   
185    }
186   
 
187    toggle @Override
188    public boolean isEnabled() {
189    return true;
190    }
191   
 
192  0 toggle @Override
193    public boolean inGroup(String groupName) {
194  0 return false;
195    }
196   
 
197  0 toggle @Override
198    public boolean hasRole(String roleName) {
199  0 return roles.contains(roleName);
200    }
201   
 
202    toggle public Subject getSubject() {
203    return null;
204    }
205   
 
206    toggle @Override
207    public Collection<String> getRoles() {
208    return roles;
209    }
210   
 
211  0 toggle @Override
212    public String getProperty(String propertyName) {
213  0 return null;
214    }
215   
 
216    toggle @Override
217    public String getPassword() {
218    return null;
219    }
220   
 
221    toggle @Override
222    public String getName() {
223    return null;
224    }
225   
 
226    toggle @Override
227    public String getLanguage() {
228    return null;
229    }
230   
 
231    toggle @Override
232    public String getIdentifier() {
233    return null;
234    }
235   
 
236    toggle @Override
237    public Collection<String> getGroups() {
238    return null;
239    }
240   
 
241    toggle @Override
242    public Collection<String> getAllRoles() {
243    return roles;
244    }
245   
 
246    toggle @Override
247    public Collection<String> getAllGroups() {
248    return null;
249    }
250   
 
251  0 toggle @Override
252    public void addRole(String roleName) throws UnsupportedOperationException {
253   
254    }
255   
 
256  0 toggle @Override
257    public void addGroup(String groupName) throws UnsupportedOperationException {
258   
259    }
260    }
261    }