1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
@link |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (44) |
Complexity: 7 |
Complexity Density: 0.18 |
|
61 |
|
public class InstallBinaryResourceTaskTest { |
62 |
|
|
63 |
|
private InstallBinaryResourceTask task; |
64 |
|
private InstallContext installContext; |
65 |
|
private MockSession session; |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
67 |
5 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void testExecute() throws Exception { |
76 |
|
|
77 |
1 |
task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png"); |
78 |
|
|
79 |
|
|
80 |
1 |
task.execute(installContext); |
81 |
|
|
82 |
|
|
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 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void worksWhenResourceNodeIsAlreadyExisting() throws Exception { |
111 |
|
|
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 |
|
|
116 |
1 |
task.execute(installContext); |
117 |
|
|
118 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
public void testResourceDoesntExist() throws Exception { |
127 |
|
|
128 |
1 |
task = new InstallBinaryResourceTask("/nonexisting"); |
129 |
|
|
130 |
|
|
131 |
1 |
try { |
132 |
1 |
task.execute(installContext); |
133 |
|
|
134 |
|
} catch (TaskExecutionException e) { |
135 |
1 |
assertEquals("Can't find resource [/nonexisting] to install.", e.getMessage()); |
136 |
|
} |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
139 |
1 |
@Test... |
140 |
|
public void testExtensionIsStrippedFromResourceFile() throws Exception { |
141 |
|
|
142 |
1 |
task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png", "", "", "", false, true); |
143 |
|
|
144 |
|
|
145 |
1 |
task.execute(installContext); |
146 |
|
|
147 |
|
|
148 |
1 |
assertTrue(session.itemExists("/templating-kit/img/magnolia-logoTest")); |
149 |
1 |
assertTrue(session.itemExists("/templating-kit/img/magnolia-logoTest/binary/extension")); |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
152 |
1 |
@Test... |
153 |
|
public void testExtensionIsNotStrippedFromResourceFile() throws Exception { |
154 |
|
|
155 |
1 |
task = new InstallBinaryResourceTask("/templating-kit/img/magnolia-logoTest.png", "", "", "", false, false); |
156 |
|
|
157 |
|
|
158 |
1 |
task.execute(installContext); |
159 |
|
|
160 |
|
|
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 |
|
} |