Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.variation

File DefaultVariationTest.java

 

Code metrics

0
37
6
1
149
75
6
0.16
6.17
6
1

Classes

Class Line # Actions
DefaultVariationTest 57 37 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 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.imaging.variation;
35   
36    import static org.hamcrest.CoreMatchers.is;
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.*;
39   
40    import info.magnolia.cms.beans.runtime.FileProperties;
41    import info.magnolia.context.MgnlContext;
42    import info.magnolia.test.mock.MockWebContext;
43    import info.magnolia.test.mock.jcr.MockNode;
44   
45    import javax.jcr.Property;
46    import javax.jcr.RepositoryException;
47    import javax.jcr.Session;
48    import javax.jcr.Workspace;
49   
50    import org.junit.After;
51    import org.junit.Before;
52    import org.junit.Test;
53   
54    /**
55    * Tests for {@link info.magnolia.imaging.variation.DefaultVariation}.
56    */
 
57    public class DefaultVariationTest {
58   
59    private static final String CONTEXT_PATH = "/test";
60    private static final String WORKSPACE_NAME = "some-workspace";
61   
62    private DefaultVariation variation;
63   
 
64  4 toggle @Before
65    public void setUp() {
66  4 MockWebContext ctx = new MockWebContext();
67  4 ctx.setContextPath(CONTEXT_PATH);
68  4 MgnlContext.setInstance(ctx);
69   
70  4 variation = new DefaultVariation();
71    }
72   
 
73  4 toggle @After
74    public void tearDown() {
75  4 MgnlContext.setInstance(null);
76    }
77   
 
78  1 toggle @Test
79    public void variationReturnsNullWhenBinaryIsNull() throws Exception {
80    // GIVEN
81   
82    // WHEN
83  1 String link = variation.createLink(null);
84   
85    // THEN
86  1 assertNull(link);
87    }
88   
 
89  1 toggle @Test
90    public void defaultVariationReturnsLinkToParent() throws Exception {
91    // GIVEN
92  1 final String parentNodeName = "parent";
93  1 MockNode parent = new MockNode(parentNodeName);
94  1 Property binary = mock(Property.class);
95  1 Session session = mock(Session.class);
96  1 Workspace workspace = mock(Workspace.class);
97   
98  1 when(workspace.getName()).thenReturn(WORKSPACE_NAME);
99  1 when(session.getWorkspace()).thenReturn(workspace);
100  1 when(binary.getSession()).thenReturn(session);
101  1 when(binary.getParent()).thenReturn(parent);
102   
103    // WHEN
104  1 String link = variation.createLink(binary);
105   
106    // THEN
107  1 assertThat(link, is(String.format("%s/.imaging/default/%s/%s.jpg", CONTEXT_PATH, WORKSPACE_NAME, parentNodeName)));
108    }
109   
 
110  1 toggle @Test
111    public void defaultVariationReturnsLinkToParentWithExtension() throws Exception {
112    // GIVEN
113  1 final String pngExtension = "png";
114  1 final String parentNodeName = "anotherParent";
115  1 MockNode parent = new MockNode(parentNodeName);
116  1 parent.setProperty(FileProperties.EXTENSION, pngExtension);
117  1 Property binary = mock(Property.class);
118  1 Session session = mock(Session.class);
119  1 Workspace workspace = mock(Workspace.class);
120   
121  1 when(workspace.getName()).thenReturn(WORKSPACE_NAME);
122  1 when(session.getWorkspace()).thenReturn(workspace);
123  1 when(binary.getSession()).thenReturn(session);
124  1 when(binary.getParent()).thenReturn(parent);
125   
126    // WHEN
127  1 String link = variation.createLink(binary);
128   
129    // THEN
130  1 assertThat(link, is(String.format("%s/.imaging/default/%s/%s.%s", CONTEXT_PATH, WORKSPACE_NAME, parentNodeName, pngExtension)));
131    }
132   
 
133  1 toggle @Test
134    public void defaultVariationReturnsNullWhenException() throws Exception {
135    // GIVEN
136  1 MockNode parent = new MockNode("parent");
137  1 Property binary = mock(Property.class);
138   
139  1 when(binary.getSession()).thenThrow(new RepositoryException("This exception is thrown on purpose."));
140  1 when(binary.getParent()).thenReturn(parent);
141   
142    // WHEN
143  1 String link = variation.createLink(binary);
144   
145    // THEN
146  1 assertNull(link);
147    }
148   
149    }