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.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
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
108 assertNull(parameter.getImageVariation());
109
110
111 BufferedImage image = new ThemeAwareImageGenerator().generate(provider);
112
113
114 assertNotNull(image);
115 }
116
117
118
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 }