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

File MgnlGroovyJCRNodeTest.java

 

Code metrics

0
44
8
1
183
96
8
0.18
5.5
8
1
14.8% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
MgnlGroovyJCRNodeTest 55 44 14.8% 8 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-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.nodes;
35   
36    import static org.junit.Assert.*;
37   
38    import info.magnolia.jcr.util.NodeTypes;
39    import info.magnolia.test.mock.jcr.MockNode;
40    import info.magnolia.test.mock.jcr.MockProperty;
41   
42    import java.math.BigDecimal;
43    import java.util.Iterator;
44   
45    import javax.jcr.Node;
46    import javax.jcr.Property;
47   
48    import org.apache.commons.collections.IteratorUtils;
49    import org.junit.Before;
50    import org.junit.Test;
51   
52    import com.google.common.collect.Lists;
53   
54   
 
55    public class MgnlGroovyJCRNodeTest {
56    protected static final BigDecimal ONE = new BigDecimal(1);
57    protected MockNode node;
58   
 
59  8 toggle @Before
60    public void setUp() throws Exception {
61  8 node = new MockNode("qux");
62  8 node.setProperty("foo", 1);
63  8 node.setProperty("bar", "IAmBar");
64  8 node.setProperty("name", "IAmCalledNameButAmDifferentFromNodeName");
65    }
66   
 
67  1 toggle @Test
68    public void lookUpName() throws Exception {
69    // GIVEN see setUp()
70   
71    // WHEN
72  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
73   
74    // THEN
75  1 assertEquals("qux", wrappedNode.get("name"));
76  1 assertEquals("IAmCalledNameButAmDifferentFromNodeName", wrappedNode.get("@name"));
77    }
78   
 
79  1 toggle @Test
80    public void lookUpPropertyWithAndWithoutAtNotationReturnsSameValue() throws Exception {
81    // GIVEN see setUp()
82   
83    // WHEN
84  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
85   
86    // THEN
87  1 assertEquals(ONE, wrappedNode.get("foo"));
88  1 assertEquals(ONE, wrappedNode.get("@foo"));
89   
90  1 assertEquals("IAmBar", wrappedNode.get("bar"));
91  1 assertEquals("IAmBar", wrappedNode.get("@bar"));
92    }
93   
 
94  1 toggle @Test
95    public void fetChildren() throws Exception {
96    // GIVEN
97  1 node.addNode(new MockNode("baz"));
98  1 node.addNode(new MockNode("bar"));
99  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
100   
101    // WHEN
102  1 Iterator<Node> children = (Iterator<Node>) wrappedNode.get("children");
103   
104    // THEN
105  1 assertEquals(2, Lists.newArrayList(children).size());
106    }
107   
 
108    toggle @Test
109    public void getChildrenOrNodesWithContentNode() throws Exception {
110    // GIVEN
111    node.addNode(new MockNode("bar"));
112    node.addNode(new MockNode("baz", NodeTypes.ContentNode.NAME));
113    node.addNode(new MockNode("qux", NodeTypes.ContentNode.NAME));
114    MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
115   
116    // WHEN
117    Iterator<Node> children = (Iterator<Node>) wrappedNode.get("children");
118    Iterator<Node> nodes = (Iterator<Node>) wrappedNode.get("nodes");
119   
120    // THEN
121    assertEquals(3, Lists.newArrayList(children).size());
122    assertEquals(3, Lists.newArrayList(nodes).size());
123    }
124   
 
125  1 toggle @Test
126    public void iterator() throws Exception {
127    // GIVEN
128  1 node.addNode(new MockNode("baz"));
129  1 node.addNode(new MockNode("bar"));
130  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
131   
132    // WHEN
133  1 Iterator<Node> iter = wrappedNode.getNodes();
134   
135    // THEN
136  1 Object[] children = IteratorUtils.toArray(iter);
137  1 assertEquals(2, children.length);
138    }
139   
 
140  1 toggle @Test
141    public void iteratorWithContentNode() throws Exception {
142    // GIVEN
143  1 node.addNode(new MockNode("bar"));
144  1 node.addNode(new MockNode("baz", NodeTypes.ContentNode.NAME));
145  1 node.addNode(new MockNode("qux", NodeTypes.ContentNode.NAME));
146  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
147   
148    // WHEN
149  1 Iterator<Node> iter = wrappedNode.getNodes();
150   
151    // THEN
152  1 Object[] children = IteratorUtils.toArray(iter);
153  1 assertEquals(3, children.length);
154    }
155   
 
156  1 toggle @Test
157    public void showNode() throws Exception {
158    // GIVEN
159  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
160  1 Node n1 = new MockNode("node1", "mgnl:metaData");
161  1 Node n2 = new MockNode("jcr:author");
162  1 Node n3 = new MockNode("rep:access");
163  1 Node n4 = new MockNode("bar", "mgnl:page");
164   
165    // THEN
166  1 assertFalse(wrappedNode.showNode(n1));
167  1 assertFalse(wrappedNode.showNode(n2));
168  1 assertFalse(wrappedNode.showNode(n3));
169  1 assertTrue(wrappedNode.showNode(n4));
170    }
171   
 
172  1 toggle @Test
173    public void showProperty() throws Exception {
174    // GIVEN
175  1 MgnlGroovyJCRNode wrappedNode = new MgnlGroovyJCRNode(node);
176  1 Property p1 = new MockProperty("jcr:prop", "value", node);
177  1 Property p2 = new MockProperty("p2", "value", node);
178   
179    // THEN
180  1 assertFalse(wrappedNode.showProperty(p1));
181  1 assertTrue(wrappedNode.showProperty(p2));
182    }
183    }