Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.setup

File InstallTextResourceTaskTest.java

 

Code metrics

0
39
7
1
161
81
8
0.21
5.57
7
1.14

Classes

Class Line # Actions
InstallTextResourceTaskTest 56 39 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2015 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.resources.setup;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.Mockito.*;
38   
39    import info.magnolia.jcr.util.NodeTypes;
40    import info.magnolia.module.InstallContext;
41    import info.magnolia.module.delta.TaskExecutionException;
42    import info.magnolia.module.resources.ResourceTypes;
43    import info.magnolia.module.resources.ResourcesModule;
44    import info.magnolia.test.mock.jcr.MockSession;
45   
46    import javax.jcr.Node;
47    import javax.jcr.RepositoryException;
48   
49    import org.apache.commons.lang3.StringUtils;
50    import org.junit.Before;
51    import org.junit.Test;
52   
53    /**
54    * Test for {@link InstallResourceTask}.
55    */
 
56    public class InstallTextResourceTaskTest {
57   
58    private InstallTextResourceTask task;
59    private InstallContext installContext;
60    private MockSession session;
61   
 
62  6 toggle @Before
63    public void setUp() throws RepositoryException {
64  6 installContext = mock(InstallContext.class);
65  6 session = new MockSession("resources");
66  6 when(installContext.getJCRSession(ResourcesModule.DEFAULT_WORKSPACE)).thenReturn(session);
67  6 Node jqueryNode = session.getRootNode().addNode("templating-kit").addNode("js").addNode("libs").addNode("jquery");
68  6 jqueryNode.setProperty("extension", "js");
69  6 jqueryNode.setProperty(NodeTypes.Renderable.TEMPLATE, ResourceTypes.PROCESSED_JS);
70   
71  6 Node scriptloaderNode = session.getNode("/templating-kit/js").addNode("scriptloader-libs");
72  6 scriptloaderNode.setProperty("extension", "js");
73  6 scriptloaderNode.setProperty("modelClass", "some.Class");
74  6 scriptloaderNode.setProperty(NodeTypes.Renderable.TEMPLATE, ResourceTypes.JS);
75  6 scriptloaderNode.setProperty("text", "old value");
76    }
77   
 
78  1 toggle @Test
79    public void testResourceTemplatesAreNotRewritten() throws Exception {
80    // GIVEN
81  1 task = new InstallTextResourceTask("/templating-kit/js/scriptloader-libs.js", ResourceTypes.JS, InstallTextResourceTask.DEFAULT_ENCODING, "", "", "", "some.Class", true);
82   
83    // WHEN
84  1 task.execute(installContext);
85   
86    // THEN
87  1 Node script = session.getNode("/templating-kit/js/scriptloader-libs");
88  1 assertEquals(ResourceTypes.JS, NodeTypes.Renderable.getTemplate(script));
89  1 assertEquals("some.Class", script.getProperty("modelClass").getString());
90  1 assertTrue(StringUtils.contains(script.getProperty("text").getString(), "var test = \"new value\";"));
91    }
92   
 
93  1 toggle @Test
94    public void testInstallNewResource() throws Exception {
95    // GIVEN
96  1 task = new InstallTextResourceTask("/templating-kit/js/libs/jquery.datepicker.js", true);
97   
98    // WHEN
99  1 task.execute(installContext);
100   
101    // THEN
102  1 Node resource = session.getNode("/templating-kit/js/libs/jquery.datepicker");
103  1 assertEquals(ResourceTypes.PROCESSED_JS, NodeTypes.Renderable.getTemplate(resource));
104  1 assertEquals("js", resource.getProperty("extension").getString());
105  1 assertFalse(resource.hasProperty(ResourceTypes.BINARY_SUFFIX));
106    }
107   
 
108  1 toggle @Test
109    public void testResourceDoesntExist() throws Exception {
110    // GIVEN
111  1 task = new InstallTextResourceTask("/nonexisting", ResourceTypes.JS);
112   
113    // WHEN
114  1 try {
115  1 task.execute(installContext);
116    // THEN
117    } catch (TaskExecutionException e) {
118  1 assertEquals("Can't find resource [/nonexisting] to install.", e.getMessage());
119    }
120    }
121   
 
122  1 toggle @Test
123    public void testDifferentEncoding() throws Exception {
124    // GIVEN
125  1 task = new InstallTextResourceTask("/templating-kit/css/ISO-8859-1.css", false, "ISO-8859-1");
126   
127    // WHEN
128  1 task.execute(installContext);
129   
130    // THEN
131  1 Node resource = session.getNode("/templating-kit/css/ISO-8859-1");
132  1 assertEquals("French / Fran\u00E7ais (ISO Latin-1 / ISO 8859-1)\n", resource.getProperty("text").getString());
133    }
134   
 
135  1 toggle @Test
136    public void testExtensionIsStrippedFromResourceFile() throws Exception {
137    // GIVEN
138  1 task = new InstallTextResourceTask("/templating-kit/js/libs/jquery.datepicker.js", ResourceTypes.PROCESSED_JS, "UTF-8", "", "", "", "", false, true);
139   
140    // WHEN
141  1 task.execute(installContext);
142   
143    // THEN
144  1 assertTrue(session.itemExists("/templating-kit/js/libs/jquery.datepicker"));
145  1 assertTrue(session.itemExists("/templating-kit/js/libs/jquery.datepicker/extension"));
146    }
147   
 
148  1 toggle @Test
149    public void testExtensionIsNotStrippedFromResourceFile() throws Exception {
150    // GIVEN
151  1 task = new InstallTextResourceTask("/templating-kit/js/libs/jquery.datepicker.js", ResourceTypes.PROCESSED_JS, "UTF-8", "", "", "", "", false, false);
152   
153    // WHEN
154  1 task.execute(installContext);
155   
156    // THEN
157  1 assertTrue(session.itemExists("/templating-kit/js/libs/jquery.datepicker.js"));
158  1 assertTrue(!session.itemExists("/templating-kit/js/libs/jquery.datepicker/extension"));
159    }
160   
161    }