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

 

Code metrics

0
38
6
1
165
81
7
0.18
6.33
6
1.17

Classes

Class Line # Actions
InstallBinaryResourceTaskTest 61 38 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 5 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.cms.beans.runtime.FileProperties;
40    import info.magnolia.jcr.util.NodeTypes;
41    import info.magnolia.jcr.util.NodeUtil;
42    import info.magnolia.module.InstallContext;
43    import info.magnolia.module.delta.TaskExecutionException;
44    import info.magnolia.module.resources.ResourceTypes;
45    import info.magnolia.module.resources.ResourcesModule;
46    import info.magnolia.test.mock.jcr.MockSession;
47   
48    import java.io.InputStream;
49   
50    import javax.jcr.Node;
51    import javax.jcr.RepositoryException;
52   
53    import org.apache.commons.io.IOUtils;
54    import org.apache.jackrabbit.JcrConstants;
55    import org.junit.Before;
56    import org.junit.Test;
57   
58    /**
59    * Test for {@link InstallBinaryResourceTask}.
60    */
 
61    public class InstallBinaryResourceTaskTest {
62   
63    private InstallBinaryResourceTask task;
64    private InstallContext installContext;
65    private MockSession session;
66   
 
67  5 toggle @Before
68    public void setUp() throws RepositoryException {
69  5 installContext = mock(InstallContext.class);
70  5 session = new MockSession("resources");
71  5 when(installContext.getJCRSession(ResourcesModule.DEFAULT_WORKSPACE)).thenReturn(session);
72    }
73   
 
74  1 toggle @Test
75    public void testExecute() throws Exception {
76    // GIVEN
77  1 task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png");
78   
79    // WHEN
80  1 task.execute(installContext);
81   
82    // THEN
83  1 Node script = session.getNode("/templating-kit/img/magnolia-logoTest");
84  1 assertTrue(script.hasNode(ResourceTypes.BINARY_SUFFIX));
85   
86  1 Node binary = script.getNode(ResourceTypes.BINARY_SUFFIX);
87   
88  1 assertEquals(NodeTypes.Resource.NAME, binary.getPrimaryNodeType().getName());
89  1 assertEquals("magnolia-logoTest", binary.getProperty(FileProperties.PROPERTY_FILENAME).getString());
90  1 assertEquals("png", binary.getProperty(FileProperties.PROPERTY_EXTENSION).getString());
91  1 assertEquals("4708", binary.getProperty(FileProperties.PROPERTY_SIZE).getString());
92  1 assertEquals("51", binary.getProperty(FileProperties.PROPERTY_HEIGHT).getString());
93  1 assertEquals("202", binary.getProperty(FileProperties.PROPERTY_WIDTH).getString());
94  1 assertEquals("image/png", binary.getProperty(JcrConstants.JCR_MIMETYPE).getString());
95  1 assertTrue(binary.hasProperty(JcrConstants.JCR_DATA));
96   
97  1 InputStream stream2 = binary.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
98  1 long jcrDataLength = IOUtils.toByteArray(stream2).length;
99  1 assertEquals(4708, jcrDataLength);
100   
101    // for manual check on filesystem:
102    // InputStream stream1 = binary.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
103    // FileOutputStream out = new FileOutputStream("test.png");
104    // IOUtils.copy(stream1, out);
105    // out.flush();
106    // out.close();
107    }
108   
 
109  1 toggle @Test
110    public void worksWhenResourceNodeIsAlreadyExisting() throws Exception {
111    // GIVEN
112  1 NodeUtil.createPath(session.getRootNode(), "templating-kit/img/magnolia-logoTest", NodeTypes.ContentNode.NAME).addNode(ResourceTypes.BINARY_SUFFIX, NodeTypes.Resource.NAME).setProperty(FileProperties.PROPERTY_FILENAME, "someName");
113  1 task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png");
114   
115    // WHEN
116  1 task.execute(installContext);
117   
118    // THEN
119  1 Node script = session.getNode("/templating-kit/img/magnolia-logoTest");
120  1 assertTrue(script.hasNode(ResourceTypes.BINARY_SUFFIX));
121  1 Node binary = script.getNode(ResourceTypes.BINARY_SUFFIX);
122  1 assertEquals("magnolia-logoTest", binary.getProperty(FileProperties.PROPERTY_FILENAME).getString());
123    }
124   
 
125  1 toggle @Test
126    public void testResourceDoesntExist() throws Exception {
127    // GIVEN
128  1 task = new InstallBinaryResourceTask("/nonexisting");
129   
130    // WHEN
131  1 try {
132  1 task.execute(installContext);
133    // THEN
134    } catch (TaskExecutionException e) {
135  1 assertEquals("Can't find resource [/nonexisting] to install.", e.getMessage());
136    }
137    }
138   
 
139  1 toggle @Test
140    public void testExtensionIsStrippedFromResourceFile() throws Exception {
141    // GIVEN
142  1 task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png", "", "", "", false, true);
143   
144    // WHEN
145  1 task.execute(installContext);
146   
147    // THEN
148  1 assertTrue(session.itemExists("/templating-kit/img/magnolia-logoTest"));
149  1 assertTrue(session.itemExists("/templating-kit/img/magnolia-logoTest/binary/extension"));
150    }
151   
 
152  1 toggle @Test
153    public void testExtensionIsNotStrippedFromResourceFile() throws Exception {
154    // GIVEN
155  1 task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png", "", "", "", false, false);
156   
157    // WHEN
158  1 task.execute(installContext);
159   
160    // THEN
161  1 assertTrue(session.itemExists("/templating-kit/img/magnolia-logoTest.png"));
162  1 assertTrue(session.itemExists("/templating-kit/img/magnolia-logoTest.png/binary/extension"));
163    }
164   
165    }