Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging

File ImagingModuleTest.java

 

Code metrics

0
34
5
1
146
75
5
0.15
6.8
5
1

Classes

Class Line # Actions
ImagingModuleTest 61 34 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-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.imaging;
35   
36    import static info.magnolia.test.hamcrest.NodeMatchers.hasNode;
37    import static org.hamcrest.CoreMatchers.not;
38    import static org.hamcrest.MatcherAssert.assertThat;
39    import static org.hamcrest.core.Is.is;
40    import static org.mockito.Mockito.*;
41   
42    import info.magnolia.context.MgnlContext;
43    import info.magnolia.context.SystemContext;
44    import info.magnolia.jcr.util.NodeTypes;
45    import info.magnolia.jcr.util.NodeUtil;
46    import info.magnolia.module.ModuleLifecycleContext;
47    import info.magnolia.objectfactory.guice.GuiceUtils;
48    import info.magnolia.repository.RepositoryConstants;
49    import info.magnolia.test.RepositoryTestCase;
50   
51    import java.lang.reflect.Field;
52    import java.lang.reflect.Modifier;
53   
54    import javax.jcr.Node;
55    import javax.jcr.Session;
56   
57    import org.apache.commons.lang3.StringUtils;
58    import org.junit.Before;
59    import org.junit.Test;
60   
 
61    public class ImagingModuleTest extends RepositoryTestCase {
62   
63    private Node generatorsNode;
64    private Session imagingSession;
65    private ImagingModule module;
66    private ModuleLifecycleContext moduleLifecycleContext;
67   
 
68  3 toggle @Override
69    @Before
70    public void setUp() throws Exception {
71  3 super.setUp();
72  3 module = new ImagingModule(GuiceUtils.providerForInstance((SystemContext) MgnlContext.getInstance()));
73   
74  3 Field field = ImagingModule.class.getDeclaredField("GENERATOR_CACHE_FLUSH_DELAY");
75  3 setFieldValue(field, 1L);
76  3 field = ImagingModule.class.getDeclaredField("GENERATOR_CACHE_FLUSH_MAX_DELAY");
77  3 setFieldValue(field, 10L);
78   
79  3 moduleLifecycleContext = mock(ModuleLifecycleContext.class);
80  3 when(moduleLifecycleContext.getPhase()).thenReturn(ModuleLifecycleContext.PHASE_SYSTEM_STARTUP);
81   
82  3 Session configSession = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
83  3 generatorsNode = NodeUtil.createPath(configSession.getRootNode(), StringUtils.substring(ImagingModule.GENERATORS_PATH, 1), NodeTypes.Content.NAME);
84  3 generatorsNode.addNode("fooGenerator", NodeTypes.ContentNode.NAME);
85   
86  3 imagingSession = MgnlContext.getJCRSession(ImagingModule.IMAGING);
87  3 NodeUtil.createPath(imagingSession.getRootNode(), "fooGenerator/foo/bar/baz", NodeTypes.ContentNode.NAME);
88  3 NodeUtil.createPath(imagingSession.getRootNode(), "barGenerator/foo/bar/baz", NodeTypes.ContentNode.NAME);
89    }
90   
 
91  1 toggle @Test
92    public void imagingCacheIsFlushedAfterConfigChange() throws Exception {
93    // GIVEN
94  1 module.start(moduleLifecycleContext);
95  1 generatorsNode.getNode("fooGenerator").setProperty("test", "test");
96   
97    // WHEN
98  1 generatorsNode.getSession().save();
99   
100    // THEN
101    // give it some time to trigger the event
102  1 Thread.sleep(100);
103  1 assertThat(imagingSession.getRootNode(), not(hasNode("fooGenerator")));
104  1 assertThat(imagingSession.getRootNode(), hasNode("barGenerator"));
105    }
106   
 
107  1 toggle @Test
108    public void moduleStartRegistersSpiProviders() throws Exception {
109    // GIVEN
110   
111    // WHEN
112  1 module.start(mock(ModuleLifecycleContext.class));
113   
114    // THEN
115  1 int registered = TestImageWriterSpiRegistryCounter.getInstance().registered;
116  1 int unregistered = TestImageWriterSpiRegistryCounter.getInstance().deregistered;
117   
118    // somehow it un-registers the provider and re-registers it.
119  1 assertThat("Spi service has been registered.", registered - unregistered, is(1));
120    }
121   
 
122  1 toggle @Test
123    public void moduleStopUnRegistersSpiProviders() throws Exception {
124    // GIVEN
125   
126    // WHEN
127  1 module.start(mock(ModuleLifecycleContext.class));
128  1 module.stop(mock(ModuleLifecycleContext.class));
129   
130    // THEN
131  1 int registered = TestImageWriterSpiRegistryCounter.getInstance().registered;
132  1 int unregistered = TestImageWriterSpiRegistryCounter.getInstance().deregistered;
133   
134  1 assertThat("Spi service has been un-registered.", registered - unregistered, is(0));
135    }
136   
 
137  6 toggle private void setFieldValue(Field field, Object newValue) throws Exception {
138  6 field.setAccessible(true);
139   
140  6 Field modifiersField = Field.class.getDeclaredField("modifiers");
141  6 modifiersField.setAccessible(true);
142  6 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
143   
144  6 field.set(null, newValue);
145    }
146    }