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.validator

File GroovyValidatorTest.java

 

Code metrics

0
52
8
1
180
94
8
0.15
6.5
8
1

Classes

Class Line # Actions
GroovyValidatorTest 56 52 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-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.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   
 
56    public class GroovyValidatorTest {
57    private Session session;
58   
 
59  6 toggle @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   
 
68  6 toggle @After
69    public void tearDown() {
70  6 MgnlContext.setInstance(null);
71  6 ComponentsTestUtil.setInstance(SystemContext.class, null);
72    }
73   
 
74  1 toggle @Test
75    public void validateGroovyClassWithPackage() throws Exception {
76    // GIVEN
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    // WHEN
85  1 GroovyValidator validator = new GroovyValidator(nodeAdapter, "error");
86   
87    // THEN
88  1 assertTrue(validator.isValidValue("package foo.bar\n class Baz{}"));
89    }
90   
 
91  1 toggle @Test
92    public void validateGroovyClassWithInexistentPackage() throws Exception {
93    // GIVEN
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    // WHEN
102  1 GroovyValidator validator = new GroovyValidator(nodeAdapter, "error");
103   
104    // THEN
105  1 assertFalse(validator.isValidValue("package foo.bar\n class Baz{}"));
106    }
107   
 
108  1 toggle @Test
109    public void validateGroovyClassWithWrongPackage() throws Exception {
110    // GIVEN
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    // WHEN
119  1 GroovyValidator validator = new GroovyValidator(nodeAdapter, "error");
120   
121    // THEN
122  1 assertFalse(validator.isValidValue("package foo.bar\n class Baz{}"));
123    }
124   
 
125  1 toggle @Test
126    public void validateGroovyClassNoPackage() throws Exception {
127    // GIVEN
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    // WHEN
136  1 GroovyValidator validator = new GroovyValidator(nodeAdapter, "error");
137   
138    // THEN
139  1 assertTrue(validator.isValidValue("class Baz{}"));
140    }
141   
 
142  1 toggle @Test
143    public void validateGroovyClassDeclaringPackageButUnderRoot() throws Exception {
144    // GIVEN
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    // WHEN
153  1 GroovyValidator validator = new GroovyValidator(nodeAdapter, "error");
154   
155    // THEN
156  1 assertFalse(validator.isValidValue("package foo\nclass Baz{}"));
157    }
158   
 
159  1 toggle @Test
160    public void validateGroovyScript() throws Exception {
161    // GIVEN
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    // WHEN
174  1 GroovyValidator validator = new GroovyValidator(nodeAdapter, "error");
175   
176    // THEN
177  1 assertTrue(validator.isValidValue("def x = 10\n println x"));
178    }
179   
180    }