View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 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.templating.imaging;
35  
36  import static org.junit.Assert.*;
37  import static org.mockito.Mockito.*;
38  
39  import info.magnolia.context.Context;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.imaging.ImagingSupport;
42  import info.magnolia.module.site.Site;
43  import info.magnolia.module.site.theme.Theme;
44  import info.magnolia.objectfactory.ComponentProvider;
45  import info.magnolia.templating.imaging.parameters.ThemeAwareParameter;
46  import info.magnolia.templating.imaging.parameters.ThemeAwareParameterProvider;
47  import info.magnolia.test.ComponentsTestUtil;
48  import info.magnolia.test.mock.MockComponentProvider;
49  
50  import java.awt.image.BufferedImage;
51  import java.io.ByteArrayInputStream;
52  import java.io.File;
53  import java.util.HashMap;
54  import java.util.Map;
55  
56  import javax.jcr.Binary;
57  
58  import org.apache.commons.io.FileUtils;
59  import org.junit.After;
60  import org.junit.Before;
61  import org.junit.Test;
62  
63  /**
64   * Tests for {@link info.magnolia.templating.imaging.ThemeAwareImageGenerator}.
65   */
66  public class ThemeAwareImageGeneratorTest {
67  
68      private ImagingSupport imagingSupport;
69      private ThemeAwareParameterProvider provider;
70      private ThemeAwareParameter parameter;
71  
72      @Before
73      public void setUp() throws Exception {
74  
75          Context ctx = mock(Context.class);
76          MgnlContext.setInstance(ctx);
77  
78          Theme theme = mock(Theme.class);
79          imagingSupport = mock(ImagingSupport.class);
80  
81          DummyModule module = new DummyModule();
82          module.addTheme("pop", theme);
83  
84          when(theme.getImaging()).thenReturn(imagingSupport);
85  
86          provider = mock(ThemeAwareParameterProvider.class);
87          parameter = mock(ThemeAwareParameter.class);
88  
89          Binary binary = mock(Binary.class);
90          File file = new File(getClass().getResource("/info/magnolia/templating/imaging/parameters/testImage.gif").getFile());
91          when(binary.getStream()).thenReturn(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)));
92          when(parameter.getBinary()).thenReturn(binary);
93          when(provider.getParameter()).thenReturn(parameter);
94  
95          ComponentsTestUtil.setInstance(DummyModule.class, module);
96          ComponentsTestUtil.setImplementation(ComponentProvider.class, MockComponentProvider.class);
97      }
98  
99      @After
100     public void tearDown() {
101         MgnlContext.setInstance(null);
102         ComponentsTestUtil.clear();
103     }
104 
105     @Test
106     public void missingVariationReturnsOriginalImage() throws Exception {
107         // GIVEN
108         assertNull(parameter.getImageVariation());
109 
110         // WHEN
111         BufferedImage image = new ThemeAwareImageGenerator().generate(provider);
112 
113         // THEN
114         assertNotNull(image);
115     }
116 
117     /**
118      * DummyModule.
119      */
120     protected static class DummyModule {
121         private Site site;
122 
123         private Map<String, Theme> themes = new HashMap<String, Theme>();
124 
125         public Site getSite() {
126             return this.site;
127         }
128 
129         public void setSite(Site site) {
130             this.site = site;
131         }
132 
133         public Map<String, Theme> getThemes() {
134             return this.themes;
135         }
136 
137         public void setThemes(Map<String, Theme> themes) {
138             this.themes = themes;
139         }
140 
141         public void addTheme(String name, Theme theme) {
142             themes.put(name, theme);
143         }
144 
145         public Theme getTheme(String name) {
146             return getThemes().get(name);
147         }
148 
149     }
150 }