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 InstallResourceTaskTest.java

 

Code metrics

0
44
5
1
157
83
6
0.14
8.8
5
1.2

Classes

Class Line # Actions
InstallResourceTaskTest 59 44 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2012-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.test.mock.MockHierarchyManager;
44    import info.magnolia.test.mock.jcr.MockSession;
45   
46    import java.lang.reflect.Field;
47    import java.nio.charset.Charset;
48   
49    import javax.jcr.Node;
50    import javax.jcr.RepositoryException;
51   
52    import org.apache.commons.lang3.StringUtils;
53    import org.junit.Before;
54    import org.junit.Test;
55   
56    /**
57    * Test for InstallResourceTask.
58    */
 
59    public class InstallResourceTaskTest {
60   
61    private InstallResourceTask task;
62    private InstallContext installContext;
63    private MockHierarchyManager hm;
64    private MockSession session;
65   
 
66  4 toggle @Before
67    public void setUp() throws RepositoryException {
68  4 installContext = mock(InstallContext.class);
69  4 session = new MockSession("resources");
70  4 hm = new MockHierarchyManager(session);
71  4 when(installContext.getHierarchyManager("resources")).thenReturn(hm);
72   
73  4 Node jqueryNode = session.getRootNode().addNode("templating-kit").addNode("js").addNode("libs").addNode("jquery");
74  4 jqueryNode.setProperty("extension", "js");
75  4 jqueryNode.setProperty(NodeTypes.Renderable.TEMPLATE, "resources:js");
76   
77  4 Node scriptloaderNode = session.getNode("/templating-kit/js").addNode("scriptloader-libs");
78  4 scriptloaderNode.setProperty("extension", "js");
79  4 scriptloaderNode.setProperty("modelClass", "some.Class");
80  4 scriptloaderNode.setProperty(NodeTypes.Renderable.TEMPLATE, "resources:processedJs");
81  4 scriptloaderNode.setProperty("text", "old value");
82    }
83   
 
84  1 toggle @Test
85    public void testResourceTemplatesAreNotRewritten() throws Exception {
86    // GIVEN
87  1 task = new InstallResourceTask("/templating-kit/js/scriptloader-libs.js", "resources:js", false, null, true);
88   
89    // WHEN
90  1 task.execute(installContext);
91   
92    // THEN
93  1 Node script = session.getNode("/templating-kit/js/scriptloader-libs");
94  1 assertEquals("resources:processedJs", NodeTypes.Renderable.getTemplate(script));
95  1 assertEquals("some.Class", script.getProperty("modelClass").getString());
96  1 assertTrue(StringUtils.contains(script.getProperty("text").getString(), "var test = \"new value\";"));
97    }
98   
 
99  1 toggle @Test
100    public void testInstallNewResource() throws Exception {
101    // GIVEN
102  1 task = new InstallResourceTask("/templating-kit/js/libs/jquery.datepicker.js", "resources:js", false);
103   
104    // WHEN
105  1 task.execute(installContext);
106   
107    // THEN
108  1 Node resource = session.getNode("/templating-kit/js/libs/jquery.datepicker");
109  1 assertEquals("resources:js", NodeTypes.Renderable.getTemplate(resource));
110  1 assertEquals("js", resource.getProperty("extension").getString());
111  1 assertFalse(resource.hasProperty(ResourceTypes.BINARY_SUFFIX));
112    }
113   
 
114  1 toggle @Test
115    public void testResourceDoesntExist() throws Exception {
116    // GIVEN
117  1 task = new InstallResourceTask("/nonexisting", "resources:js", false);
118   
119    // WHEN
120  1 try {
121  1 task.execute(installContext);
122    // THEN
123    } catch (TaskExecutionException e) {
124  1 assertEquals("Can't find resource [/nonexisting] to install.", e.getMessage());
125    }
126    }
127   
 
128  1 toggle @Test
129    public void testDifferentPlatformEncoding() throws Exception {
130    // GIVEN
131  1 task = new InstallResourceTask("/templating-kit/js/utf-8-javascript.js", "resources:js", false);
132   
133    // hack to set different platform encoding
134  1 String originalPlatformEncoding = System.getProperty("file.encoding");
135  1 System.setProperty("file.encoding", "ISO-8859-1");
136  1 Field charset = Charset.class.getDeclaredField("defaultCharset");
137  1 charset.setAccessible(true);
138  1 charset.set(null, null);
139  1 assertEquals("ISO-8859-1", System.getProperty("file.encoding"));
140   
141    // WHEN
142  1 task.execute(installContext);
143   
144    // THEN
145  1 Node resource = session.getNode("/templating-kit/js/utf-8-javascript");
146  1 assertEquals("resources:js", NodeTypes.Renderable.getTemplate(resource));
147  1 assertEquals("js", resource.getProperty("extension").getString());
148  1 assertTrue(StringUtils.contains(resource.getProperty("text").getString(), "alert(\"Text ohne Umbruch – mit vielen Gr\u00fc\u00dfen\");"));
149   
150    // AFTER back to original platform encoding
151  1 System.setProperty("file.encoding", originalPlatformEncoding);
152  1 charset = Charset.class.getDeclaredField("defaultCharset");
153  1 charset.setAccessible(true);
154  1 charset.set(null, null);
155    }
156   
157    }