Clover icon

Magnolia Module EhCache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:52:57 CET
  2. Package info.magnolia.module.cache.ehcache

File EhCacheNode2BeanTest.java

 

Code metrics

0
26
3
1
127
73
3
0.12
8.67
3
1

Classes

Class Line # Actions
EhCacheNode2BeanTest 65 26 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2014-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.module.cache.ehcache;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.Mockito.mock;
38   
39    import info.magnolia.context.MgnlContext;
40    import info.magnolia.init.DefaultMagnoliaInitPaths;
41    import info.magnolia.init.MagnoliaConfigurationProperties;
42    import info.magnolia.init.MagnoliaInitPaths;
43    import info.magnolia.jcr.node2bean.Node2BeanTransformer;
44    import info.magnolia.jcr.node2bean.TypeMapping;
45    import info.magnolia.jcr.node2bean.impl.Node2BeanProcessorImpl;
46    import info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl;
47    import info.magnolia.jcr.node2bean.impl.TypeMappingImpl;
48    import info.magnolia.module.ModuleManager;
49    import info.magnolia.module.cache.mbean.CacheMonitor;
50    import info.magnolia.test.ComponentsTestUtil;
51    import info.magnolia.test.mock.jcr.SessionTestUtil;
52   
53    import javax.jcr.Session;
54   
55    import org.junit.After;
56    import org.junit.Before;
57    import org.junit.Test;
58   
59    import net.sf.ehcache.config.PersistenceConfiguration;
60    import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
61   
62    /**
63    * Ensures the n2b transformers work as expected.
64    */
 
65    public class EhCacheNode2BeanTest extends AbstractEhCacheTest {
 
66  1 toggle @Test
67    public void testSampleConfig() throws Exception {
68    // GIVEN
69  1 final Node2BeanTransformer transformer = new Node2BeanTransformerImpl();
70  1 final Node2BeanProcessorImpl node2bean = new Node2BeanProcessorImpl(typeMapping, transformer);
71   
72  1 final Session session = SessionTestUtil.createSession("test",
73    "/cfg/factory.class=" + EhCacheFactory.class.getName(),
74    "/cfg/factory/caches/myTestCfg.diskExpiryThreadIntervalSeconds=1",
75    "/cfg/factory/caches/myTestCfg.diskSpoolBufferSizeMB=2",
76    "/cfg/factory/caches/myTestCfg.eternal=false",
77    "/cfg/factory/caches/myTestCfg.maxElementsInMemory=3",
78    "/cfg/factory/caches/myTestCfg.maxElementsOnDisk=4",
79    "/cfg/factory/caches/myTestCfg.memoryStoreEvictionPolicy=FiFo",
80    "/cfg/factory/caches/myTestCfg.timeToIdleSeconds=5",
81    "/cfg/factory/caches/myTestCfg.timeToLiveSeconds=6",
82    // "/cfg/factory/caches/myTestCfg.diskPersistent=true", Cannot use both <persistence ...> and diskPersistent in a single cache configuration.
83    // "/cfg/factory/caches/myTestCfg.overflowToDisk=true", Cannot use both <persistence ...> and overflowToDisk in a single cache configuration.
84    "/cfg/factory/caches/myTestCfg/persistence.strategy=localRestartable",
85    "/cfg/factory/caches/myTestCfg/persistence.synchronousWrites=true",
86    ""
87    );
88   
89    // WHEN
90  1 final Object bean = node2bean.toBean(session.getNode("/cfg/factory"));
91   
92    // THEN
93  1 assertTrue(bean instanceof EhCacheFactory);
94  1 EhCacheFactory f = (EhCacheFactory) bean;
95  1 final EhCacheConfiguration cfg = f.getCaches().get("myTestCfg");
96  1 assertEquals(1, cfg.getDiskExpiryThreadIntervalSeconds());
97  1 assertEquals(2, cfg.getDiskSpoolBufferSizeMB());
98  1 assertEquals(3, cfg.getMaxEntriesLocalHeap());
99  1 assertEquals(4, cfg.getMaxEntriesLocalDisk());
100  1 assertEquals(5, cfg.getTimeToIdleSeconds());
101  1 assertEquals(6, cfg.getTimeToLiveSeconds());
102  1 assertEquals(false, cfg.isEternal());
103  1 assertEquals(MemoryStoreEvictionPolicy.FIFO, cfg.getMemoryStoreEvictionPolicy());
104    //assertTrue(cfg.getPersistenceConfiguration() instanceof EhCachePersistenceConfiguration); // We don't really care that it IS an instance of our subclass, but it's a good indicator things worked as expected ;)
105  1 assertEquals(true, cfg.getPersistenceConfiguration().getSynchronousWrites());
106  1 assertEquals(PersistenceConfiguration.Strategy.LOCALRESTARTABLE, cfg.getPersistenceConfiguration().getStrategy());
107    }
108   
109    private TypeMapping typeMapping;
110   
 
111  1 toggle @Before
112    public void setUp() throws Exception {
113  1 super.setUp();
114  1 typeMapping = new TypeMappingImpl();
115  1 ComponentsTestUtil.setInstance(TypeMapping.class, typeMapping);
116  1 ComponentsTestUtil.setInstance(CacheMonitor.class, cacheMonitor);
117  1 ComponentsTestUtil.setInstance(ModuleManager.class, mock(ModuleManager.class));
118  1 ComponentsTestUtil.setInstance(MagnoliaInitPaths.class, mock(DefaultMagnoliaInitPaths.class));
119  1 ComponentsTestUtil.setInstance(MagnoliaConfigurationProperties.class, magnoliaConfigurationProperties);
120    }
121   
 
122  1 toggle @After
123    public void tearDown() throws Exception {
124  1 super.tearDown();
125  1 MgnlContext.setInstance(null);
126    }
127    }