From 55d5830ea8d4a90c6d4279bcc8509e3b02e99f0b Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 11 Apr 2013 15:28:47 +0200 Subject: [PATCH] added unit tests for GuavaCacheConfigurationReader --- .../scm/cache/CacheConfigurationLoader.java | 74 +++++ .../DefaultCacheConfigurationLoader.java | 119 ++++++++ .../scm/cache/EhCacheConfigurationReader.java | 71 ++--- .../java/sonia/scm/cache/EhCacheManager.java | 4 +- .../cache/GuavaCacheConfigurationReader.java | 109 +++----- .../cache/CacheConfigurationTestLoader.java | 239 ++++++++++++++++ .../scm/cache/EhConfigurationReaderTest.java | 259 +++++------------- .../cache/GuavaConfigurationReaderTest.java | 247 +++++++++++++++++ .../resources/sonia/scm/cache/gcache.001.xml | 49 ++++ .../resources/sonia/scm/cache/gcache.002.xml | 43 +++ .../resources/sonia/scm/cache/gcache.003.xml | 42 +++ .../resources/sonia/scm/cache/gcache.004.xml | 43 +++ 12 files changed, 981 insertions(+), 318 deletions(-) create mode 100644 scm-webapp/src/main/java/sonia/scm/cache/CacheConfigurationLoader.java create mode 100644 scm-webapp/src/main/java/sonia/scm/cache/DefaultCacheConfigurationLoader.java create mode 100644 scm-webapp/src/test/java/sonia/scm/cache/CacheConfigurationTestLoader.java create mode 100644 scm-webapp/src/test/java/sonia/scm/cache/GuavaConfigurationReaderTest.java create mode 100644 scm-webapp/src/test/resources/sonia/scm/cache/gcache.001.xml create mode 100644 scm-webapp/src/test/resources/sonia/scm/cache/gcache.002.xml create mode 100644 scm-webapp/src/test/resources/sonia/scm/cache/gcache.003.xml create mode 100644 scm-webapp/src/test/resources/sonia/scm/cache/gcache.004.xml diff --git a/scm-webapp/src/main/java/sonia/scm/cache/CacheConfigurationLoader.java b/scm-webapp/src/main/java/sonia/scm/cache/CacheConfigurationLoader.java new file mode 100644 index 0000000000..eaa3be383f --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/cache/CacheConfigurationLoader.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cache; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; + +import java.net.URL; + +import java.util.Iterator; + +/** + * + * @author Sebastian Sdorra + */ +public interface CacheConfigurationLoader +{ + + /** + * Method description + * + * + * @return + */ + public URL getDefaultResource(); + + /** + * Method description + * + * + * @return + */ + public File getManualFileResource(); + + /** + * Method description + * + * + * @param path + * + * @return + */ + public Iterator getModuleResources(); +} diff --git a/scm-webapp/src/main/java/sonia/scm/cache/DefaultCacheConfigurationLoader.java b/scm-webapp/src/main/java/sonia/scm/cache/DefaultCacheConfigurationLoader.java new file mode 100644 index 0000000000..aa2e57d4e6 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/cache/DefaultCacheConfigurationLoader.java @@ -0,0 +1,119 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cache; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.SCMContext; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; + +import java.net.URL; + +import java.util.Iterator; + +/** + * + * @author Sebastian Sdorra + */ +public class DefaultCacheConfigurationLoader implements CacheConfigurationLoader +{ + + /** + * Constructs ... + * + * + * @param defaultResource + * @param manualFileResource + * @param moduleResources + */ + public DefaultCacheConfigurationLoader(String defaultResource, + String manualFileResource, String moduleResources) + { + this.defaultResource = defaultResource; + this.manualFileResource = manualFileResource; + this.moduleResources = moduleResources; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public URL getDefaultResource() + { + return DefaultCacheConfigurationLoader.class.getResource(defaultResource); + } + + /** + * Method description + * + * + * @return + */ + @Override + public File getManualFileResource() + { + return new File(SCMContext.getContext().getBaseDirectory(), + manualFileResource); + } + + /** + * Method description + * + * + * @return + */ + @Override + public Iterator getModuleResources() + { + return CacheConfigurations.findModuleResources( + EhCacheConfigurationReader.class, moduleResources); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String defaultResource; + + /** Field description */ + private String manualFileResource; + + /** Field description */ + private String moduleResources; +} diff --git a/scm-webapp/src/main/java/sonia/scm/cache/EhCacheConfigurationReader.java b/scm-webapp/src/main/java/sonia/scm/cache/EhCacheConfigurationReader.java index bf3051cb3f..dbceb6c2e8 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/EhCacheConfigurationReader.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/EhCacheConfigurationReader.java @@ -48,7 +48,6 @@ import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import sonia.scm.SCMContext; //~--- JDK imports ------------------------------------------------------------ @@ -103,9 +102,14 @@ public class EhCacheConfigurationReader /** * Constructs ... * + * + * @param loader */ - public EhCacheConfigurationReader() + @VisibleForTesting + EhCacheConfigurationReader(CacheConfigurationLoader loader) { + this.loader = loader; + try { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); @@ -118,15 +122,21 @@ public class EhCacheConfigurationReader //~--- methods -------------------------------------------------------------- + public static InputStream read(){ + return new EhCacheConfigurationReader(new DefaultCacheConfigurationLoader( + DEFAULT, MANUAL_RESOURCE, MODULE_RESOURCES)).doRead(); + } + /** * Method description * * * @return */ - public InputStream read() + @VisibleForTesting + InputStream doRead() { - URL defaultConfig = getDefaultResource(); + URL defaultConfig = loader.getDefaultResource(); if (defaultConfig == null) { @@ -136,14 +146,14 @@ public class EhCacheConfigurationReader readConfiguration(defaultConfig, true); - Iterator it = getModuleResources(); + Iterator it = loader.getModuleResources(); while (it.hasNext()) { readConfiguration(it.next(), false); } - File manualFile = getManualFileResource(); + File manualFile = loader.getManualFileResource(); if (manualFile.exists()) { @@ -166,52 +176,6 @@ public class EhCacheConfigurationReader return createInputStream(doc); } - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param path - * - * @return - */ - @VisibleForTesting - protected URL getDefaultResource() - { - return EhCacheConfigurationReader.class.getResource(DEFAULT); - } - - /** - * Method description - * - * - * @return - */ - @VisibleForTesting - protected File getManualFileResource() - { - return new File(SCMContext.getContext().getBaseDirectory(), - MANUAL_RESOURCE); - } - - /** - * Method description - * - * - * @param path - * - * @return - */ - @VisibleForTesting - protected Iterator getModuleResources() - { - return CacheConfigurations.findModuleResources( - EhCacheConfigurationReader.class, MODULE_RESOURCES); - } - - //~--- methods -------------------------------------------------------------- - /** * Method description * @@ -467,6 +431,9 @@ public class EhCacheConfigurationReader /** Field description */ private DocumentBuilder builder; + /** Field description */ + private CacheConfigurationLoader loader; + /** Field description */ private Map nodeMap = Maps.newLinkedHashMap(); diff --git a/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java b/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java index 0342ecf32b..cc4b6e9b1f 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/EhCacheManager.java @@ -74,9 +74,7 @@ public class EhCacheManager implements CacheManager try { - EhCacheConfigurationReader reader = new EhCacheConfigurationReader(); - - stream = reader.read(); + stream = EhCacheConfigurationReader.read(); cacheManager = new net.sf.ehcache.CacheManager(stream); } finally diff --git a/scm-webapp/src/main/java/sonia/scm/cache/GuavaCacheConfigurationReader.java b/scm-webapp/src/main/java/sonia/scm/cache/GuavaCacheConfigurationReader.java index 4c28f5ce21..c77f4c3ef8 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/GuavaCacheConfigurationReader.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/GuavaCacheConfigurationReader.java @@ -40,8 +40,6 @@ import com.google.common.collect.Maps; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import sonia.scm.SCMContext; - //~--- JDK imports ------------------------------------------------------------ import java.io.File; @@ -84,9 +82,14 @@ public class GuavaCacheConfigurationReader /** * Constructs ... * + * + * @param loader */ - private GuavaCacheConfigurationReader() + @VisibleForTesting + GuavaCacheConfigurationReader(CacheConfigurationLoader loader) { + this.loader = loader; + try { this.context = @@ -109,21 +112,9 @@ public class GuavaCacheConfigurationReader */ public static GuavaCacheManagerConfiguration read() { - return new GuavaCacheConfigurationReader().doRead(); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - @VisibleForTesting - protected URL getDefaultResource() - { - return GuavaCacheConfigurationReader.class.getResource(DEFAULT); + return new GuavaCacheConfigurationReader( + new DefaultCacheConfigurationLoader( + DEFAULT, MANUAL_RESOURCE, MODULE_RESOURCES)).doRead(); } /** @@ -133,59 +124,9 @@ public class GuavaCacheConfigurationReader * @return */ @VisibleForTesting - protected File getManualFileResource() + GuavaCacheManagerConfiguration doRead() { - return new File(SCMContext.getContext().getBaseDirectory(), - MANUAL_RESOURCE); - } - - /** - * Method description - * - * - * @param path - * - * @return - */ - @VisibleForTesting - protected Iterator getModuleResources() - { - return CacheConfigurations.findModuleResources( - EhCacheConfigurationReader.class, MODULE_RESOURCES); - } - - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param caches - * - * @return - */ - private Map createNamedCacheMap( - List caches) - { - Map map = Maps.newLinkedHashMap(); - - for (GuavaNamedCacheConfiguration ncc : caches) - { - map.put(ncc.getName(), ncc); - } - - return map; - } - - /** - * Method description - * - * - * @return - */ - private GuavaCacheManagerConfiguration doRead() - { - URL defaultConfigUrl = getDefaultResource(); + URL defaultConfigUrl = loader.getDefaultResource(); if (defaultConfigUrl == null) { @@ -196,7 +137,7 @@ public class GuavaCacheConfigurationReader GuavaCacheManagerConfiguration config = readConfiguration(defaultConfigUrl, true); - Iterator it = getModuleResources(); + Iterator it = loader.getModuleResources(); while (it.hasNext()) { @@ -209,7 +150,7 @@ public class GuavaCacheConfigurationReader } } - File manualFile = getManualFileResource(); + File manualFile = loader.getManualFileResource(); if (manualFile.exists()) { @@ -233,6 +174,27 @@ public class GuavaCacheConfigurationReader return config; } + /** + * Method description + * + * + * @param caches + * + * @return + */ + private Map createNamedCacheMap( + List caches) + { + Map map = Maps.newLinkedHashMap(); + + for (GuavaNamedCacheConfiguration ncc : caches) + { + map.put(ncc.getName(), ncc); + } + + return map; + } + /** * Method description * @@ -305,4 +267,7 @@ public class GuavaCacheConfigurationReader /** Field description */ private JAXBContext context; + + /** Field description */ + private CacheConfigurationLoader loader; } diff --git a/scm-webapp/src/test/java/sonia/scm/cache/CacheConfigurationTestLoader.java b/scm-webapp/src/test/java/sonia/scm/cache/CacheConfigurationTestLoader.java new file mode 100644 index 0000000000..796730de61 --- /dev/null +++ b/scm-webapp/src/test/java/sonia/scm/cache/CacheConfigurationTestLoader.java @@ -0,0 +1,239 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cache; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.common.base.Function; +import com.google.common.collect.Iterators; +import com.google.common.io.Files; +import com.google.common.io.Resources; + +import org.junit.rules.TemporaryFolder; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.IOException; + +import java.net.URL; + +import java.util.Iterator; + +/** + * + * @author Sebastian Sdorra + */ +public class CacheConfigurationTestLoader implements CacheConfigurationLoader +{ + + /** + * Constructs ... + * + * + * @param tempFolder + */ + public CacheConfigurationTestLoader(TemporaryFolder tempFolder) + { + this.tempFolder = tempFolder; + } + + /** + * Constructs ... + * + * + * + * @param tempFolder + * @param defaultConfiguration + */ + public CacheConfigurationTestLoader(TemporaryFolder tempFolder, + String defaultConfiguration) + { + this.tempFolder = tempFolder; + this.defaultConfiguration = defaultConfiguration; + } + + /** + * Constructs ... + * + * + * + * @param tempFolder + * @param defaultConfiguration + * @param moduleConfigurations + */ + public CacheConfigurationTestLoader(TemporaryFolder tempFolder, + String defaultConfiguration, Iterator moduleConfigurations) + { + this.tempFolder = tempFolder; + this.defaultConfiguration = defaultConfiguration; + this.moduleConfigurations = moduleConfigurations; + } + + /** + * Constructs ... + * + * + * + * @param tempFolder + * @param defaultConfiguration + * @param moduleConfigurations + * @param manualConfiguration + */ + public CacheConfigurationTestLoader(TemporaryFolder tempFolder, + String defaultConfiguration, Iterator moduleConfigurations, + String manualConfiguration) + { + this.tempFolder = tempFolder; + this.defaultConfiguration = defaultConfiguration; + this.moduleConfigurations = moduleConfigurations; + this.manualConfiguration = manualConfiguration; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public URL getDefaultResource() + { + URL url = null; + + if (defaultConfiguration != null) + { + url = getResource(defaultConfiguration); + } + + return url; + } + + /** + * Method description + * + * + * @return + */ + @Override + public File getManualFileResource() + { + File file; + + if (manualConfiguration == null) + { + file = mock(File.class); + when(file.exists()).thenReturn(Boolean.FALSE); + } + else + { + try + { + file = tempFolder.newFile(); + + URL manual = getResource(manualConfiguration); + + Files.copy(Resources.newInputStreamSupplier(manual), file); + } + catch (IOException ex) + { + throw new RuntimeException("could not create manual config file", ex); + } + } + + return file; + } + + /** + * Method description + * + * + * @return + */ + @Override + public Iterator getModuleResources() + { + Iterator urlIterator; + + if (moduleConfigurations == null) + { + urlIterator = Iterators.emptyIterator(); + } + else + { + urlIterator = Iterators.transform(moduleConfigurations, + new Function() + { + + @Override + public URL apply(String resource) + { + return getResource(resource); + } + }); + } + + return urlIterator; + } + + /** + * Method description + * + * + * @param name + * + * @return + */ + private URL getResource(String name) + { + return Resources.getResource("sonia/scm/cache/".concat(name)); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String defaultConfiguration; + + /** Field description */ + private String manualConfiguration; + + /** Field description */ + private Iterator moduleConfigurations; + + /** Field description */ + private TemporaryFolder tempFolder; +} diff --git a/scm-webapp/src/test/java/sonia/scm/cache/EhConfigurationReaderTest.java b/scm-webapp/src/test/java/sonia/scm/cache/EhConfigurationReaderTest.java index d1f6b2c5f9..99ff81f856 100644 --- a/scm-webapp/src/test/java/sonia/scm/cache/EhConfigurationReaderTest.java +++ b/scm-webapp/src/test/java/sonia/scm/cache/EhConfigurationReaderTest.java @@ -33,11 +33,8 @@ package sonia.scm.cache; //~--- non-JDK imports -------------------------------------------------------- -import com.google.common.base.Function; import com.google.common.collect.Iterators; import com.google.common.io.Closeables; -import com.google.common.io.Files; -import com.google.common.io.Resources; import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.Configuration; @@ -49,16 +46,10 @@ import org.junit.rules.TemporaryFolder; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - //~--- JDK imports ------------------------------------------------------------ -import java.io.File; -import java.io.IOException; import java.io.InputStream; -import java.net.URL; - import java.util.Iterator; /** @@ -75,13 +66,11 @@ public class EhConfigurationReaderTest @Test public void testDefaultConfiguration() { - EhConfigurationTestReader reader = - new EhConfigurationTestReader("ehcache.001.xml"); + EhCacheConfigurationReader reader = createReader("ehcache.001.xml"); Configuration c = createConfiguration(reader); checkDefaultConfiguration(c); checkCacheConfiguration(c, "sonia.test.cache.001", 1000l, 30l, 60l); - } /** @@ -91,8 +80,7 @@ public class EhConfigurationReaderTest @Test public void testGlobalAttributes() { - EhConfigurationTestReader reader = - new EhConfigurationTestReader("ehcache.006.xml"); + EhCacheConfigurationReader reader = createReader("ehcache.006.xml"); Configuration c = createConfiguration(reader); assertFalse(c.getUpdateCheck()); @@ -107,7 +95,7 @@ public class EhConfigurationReaderTest public void testMergeAndOverride() { //J- - EhConfigurationTestReader reader = new EhConfigurationTestReader( + EhCacheConfigurationReader reader = createReader( "ehcache.001.xml", Iterators.forArray("ehcache.002.xml", "ehcache.003.xml"), "ehcache.004.xml" @@ -129,8 +117,8 @@ public class EhConfigurationReaderTest @Test public void testMergeWithManualConfiguration() { - EhConfigurationTestReader reader = - new EhConfigurationTestReader("ehcache.001.xml", null, "ehcache.002.xml"); + EhCacheConfigurationReader reader = createReader("ehcache.001.xml", null, + "ehcache.002.xml"); Configuration c = createConfiguration(reader); @@ -147,9 +135,9 @@ public class EhConfigurationReaderTest @Test public void testMergeWithModuleConfigurations() { - EhConfigurationTestReader reader = - new EhConfigurationTestReader("ehcache.001.xml", - Iterators.forArray("ehcache.002.xml", "ehcache.003.xml")); + EhCacheConfigurationReader reader = createReader("ehcache.001.xml", + Iterators.forArray("ehcache.002.xml", + "ehcache.003.xml")); Configuration c = createConfiguration(reader); @@ -167,9 +155,9 @@ public class EhConfigurationReaderTest @Test(expected = IllegalStateException.class) public void testMissingDefaultConfiguration() { - EhConfigurationTestReader reader = new EhConfigurationTestReader(); + EhCacheConfigurationReader reader = createReader(); - reader.read(); + reader.doRead(); } /** @@ -180,7 +168,7 @@ public class EhConfigurationReaderTest public void testOverrideDefaultConfiguration() { //J- - EhConfigurationTestReader reader = new EhConfigurationTestReader( + EhCacheConfigurationReader reader = createReader( "ehcache.001.xml", Iterators.forArray("ehcache.005.xml") ); @@ -197,8 +185,8 @@ public class EhConfigurationReaderTest @Test public void testOverrideGlobalAttributes() { - EhConfigurationTestReader reader = - new EhConfigurationTestReader("ehcache.006.xml", null, "ehcache.007.xml"); + EhCacheConfigurationReader reader = createReader("ehcache.006.xml", null, + "ehcache.007.xml"); Configuration c = createConfiguration(reader); assertTrue(c.getUpdateCheck()); @@ -270,7 +258,7 @@ public class EhConfigurationReaderTest try { - content = reader.read(); + content = reader.doRead(); config = ConfigurationFactory.parseConfiguration(content); } finally @@ -283,178 +271,67 @@ public class EhConfigurationReaderTest return config; } - //~--- inner classes -------------------------------------------------------- - /** - * Class description + * Method description * * - * @version Enter version here..., 13/03/19 - * @author Enter your name here... + * @param defaultConfiguration + * + * @return */ - private class EhConfigurationTestReader extends EhCacheConfigurationReader + private EhCacheConfigurationReader createReader(String defaultConfiguration) { - - /** - * Constructs ... - * - */ - public EhConfigurationTestReader() {} - - /** - * Constructs ... - * - * - * @param defaultConfiguration - */ - public EhConfigurationTestReader(String defaultConfiguration) - { - this.defaultConfiguration = defaultConfiguration; - } - - /** - * Constructs ... - * - * - * @param defaultConfiguration - * @param moduleConfigurations - */ - public EhConfigurationTestReader(String defaultConfiguration, - Iterator moduleConfigurations) - { - this.defaultConfiguration = defaultConfiguration; - this.moduleConfigurations = moduleConfigurations; - } - - /** - * Constructs ... - * - * - * @param defaultConfiguration - * @param moduleConfigurations - * @param manualConfiguration - */ - public EhConfigurationTestReader(String defaultConfiguration, - Iterator moduleConfigurations, String manualConfiguration) - { - this.defaultConfiguration = defaultConfiguration; - this.moduleConfigurations = moduleConfigurations; - this.manualConfiguration = manualConfiguration; - } - - //~--- get methods -------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - @Override - public URL getDefaultResource() - { - URL url = null; - - if (defaultConfiguration != null) - { - url = getResource(defaultConfiguration); - } - - return url; - } - - /** - * Method description - * - * - * @return - */ - @Override - public Iterator getModuleResources() - { - Iterator urlIterator; - - if (moduleConfigurations == null) - { - urlIterator = Iterators.emptyIterator(); - } - else - { - urlIterator = Iterators.transform(moduleConfigurations, - new Function() - { - - @Override - public URL apply(String resource) - { - return getResource(resource); - } - }); - } - - return urlIterator; - } - - /** - * Method description - * - * - * @return - */ - @Override - protected File getManualFileResource() - { - File file; - - if (manualConfiguration == null) - { - file = mock(File.class); - when(file.exists()).thenReturn(Boolean.FALSE); - } - else - { - try - { - file = tempFolder.newFile(); - - URL manual = getResource(manualConfiguration); - - Files.copy(Resources.newInputStreamSupplier(manual), file); - } - catch (IOException ex) - { - throw new RuntimeException("could not create manual config file", ex); - } - } - - return file; - } - - /** - * Method description - * - * - * @param name - * - * @return - */ - private URL getResource(String name) - { - return Resources.getResource("sonia/scm/cache/".concat(name)); - } - - //~--- fields ------------------------------------------------------------- - - /** Field description */ - private String defaultConfiguration; - - /** Field description */ - private String manualConfiguration; - - /** Field description */ - private Iterator moduleConfigurations; + return new EhCacheConfigurationReader( + new CacheConfigurationTestLoader(tempFolder, defaultConfiguration)); } + /** + * Method description + * + * + * @param defaultConfiguration + * @param moduleConfiguration + * @param manualConfiguration + * + * @return + */ + private EhCacheConfigurationReader createReader(String defaultConfiguration, + Iterator moduleConfiguration, String manualConfiguration) + { + return new EhCacheConfigurationReader( + new CacheConfigurationTestLoader( + tempFolder, defaultConfiguration, moduleConfiguration, + manualConfiguration)); + } + + /** + * Method description + * + * + * @param defaultConfiguration + * @param moduleConfiguration + * + * @return + */ + private EhCacheConfigurationReader createReader(String defaultConfiguration, + Iterator moduleConfiguration) + { + return new EhCacheConfigurationReader( + new CacheConfigurationTestLoader( + tempFolder, defaultConfiguration, moduleConfiguration)); + } + + /** + * Method description + * + * + * @return + */ + private EhCacheConfigurationReader createReader() + { + return new EhCacheConfigurationReader( + new CacheConfigurationTestLoader(tempFolder)); + } //~--- fields --------------------------------------------------------------- diff --git a/scm-webapp/src/test/java/sonia/scm/cache/GuavaConfigurationReaderTest.java b/scm-webapp/src/test/java/sonia/scm/cache/GuavaConfigurationReaderTest.java new file mode 100644 index 0000000000..185a78e6db --- /dev/null +++ b/scm-webapp/src/test/java/sonia/scm/cache/GuavaConfigurationReaderTest.java @@ -0,0 +1,247 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cache; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.common.collect.Iterators; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Iterator; + +/** + * + * @author Sebastian Sdorra + */ +public class GuavaConfigurationReaderTest +{ + + /** + * Method description + * + */ + @Test + public void testDefaultConfiguration() + { + GuavaCacheConfiguration cfg = + readConfiguration("gcache.001.xml").getDefaultCache(); + + assertCacheValues(cfg, 200l, 1200l, 2400l); + } + + /** + * Method description + * + */ + @Test + public void testMergeAndOverride() + { + //J- + GuavaCacheManagerConfiguration gcm = readConfiguration( + "gcache.001.xml", + Iterators.forArray("gcache.002.xml", "gcache.003.xml"), + "gcache.004.xml" + ); + //J+ + + // cache sonia.test.cache.001 override by cache.004.xml + assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 6l, 2l, 8l); + assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000l, 120l, 60l); + assertCacheValues(getCache(gcm, "sonia.test.cache.003"), 3000l, 120l, + 2400l); + } + + /** + * Method description + * + */ + @Test + public void testMergeWithManualConfiguration() + { + GuavaCacheManagerConfiguration gcm = readConfiguration("gcache.001.xml", + null, "gcache.002.xml"); + + // check default + + assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 1000l, 60l, 30l); + assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000l, 120l, 60l); + } + + /** + * Method description + * + */ + @Test + public void testMergeWithModuleConfigurations() + { + GuavaCacheManagerConfiguration gcm = readConfiguration("gcache.001.xml", + Iterators.forArray("gcache.002.xml", + "gcache.003.xml")); + + assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 1000l, 60l, 30l); + assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000l, 120l, 60l); + assertCacheValues(getCache(gcm, "sonia.test.cache.003"), 3000l, 120l, + 2400l); + } + + /** + * Method description + * + */ + @Test + public void testSimpleConfiguration() + { + GuavaCacheConfiguration cfg = + readConfiguration("gcache.001.xml").getCaches().get(0); + + assertCacheValues(cfg, 1000l, 60l, 30l); + } + + /** + * Method description + * + */ + @Test + public void testWithoutDefaultConfiguration() + { + GuavaCacheConfiguration cfg = + readConfiguration("gcache.002.xml").getCaches().get(0); + + assertCacheValues(cfg, 1000l, 120l, 60l); + } + + /** + * Method description + * + * + * @param cfg + * @param maximumSize + * @param expireAfterAccess + * @param expireAfterWrite + */ + private void assertCacheValues(GuavaCacheConfiguration cfg, long maximumSize, + long expireAfterAccess, long expireAfterWrite) + { + assertEquals(Long.valueOf(maximumSize), cfg.getMaximumSize()); + assertEquals(Long.valueOf(expireAfterAccess), cfg.getExpireAfterAccess()); + assertEquals(Long.valueOf(expireAfterWrite), cfg.getExpireAfterWrite()); + } + + /** + * Method description + * + * + * @param defaultConfiguration + * + * @return + */ + private GuavaCacheManagerConfiguration readConfiguration( + String defaultConfiguration) + { + return readConfiguration(defaultConfiguration, null, null); + } + + /** + * Method description + * + * + * @param defaultConfiguration + * @param moduleConfiguration + * + * @return + */ + private GuavaCacheManagerConfiguration readConfiguration( + String defaultConfiguration, Iterator moduleConfiguration) + { + return readConfiguration(defaultConfiguration, moduleConfiguration, null); + } + + /** + * Method description + * + * + * @param defaultConfiguration + * @param moduleConfiguration + * @param manualConfiguration + * + * @return + */ + private GuavaCacheManagerConfiguration readConfiguration( + String defaultConfiguration, Iterator moduleConfiguration, + String manualConfiguration) + { + return new GuavaCacheConfigurationReader( + new CacheConfigurationTestLoader( + tempFolder, defaultConfiguration, moduleConfiguration, + manualConfiguration)).doRead(); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param gcmc + * @param name + * + * @return + */ + private GuavaNamedCacheConfiguration getCache( + GuavaCacheManagerConfiguration gcmc, String name) + { + GuavaNamedCacheConfiguration cache = null; + + for (GuavaNamedCacheConfiguration gncc : gcmc.getCaches()) + { + if (name.equals(gncc.getName())) + { + cache = gncc; + } + } + + return cache; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); +} diff --git a/scm-webapp/src/test/resources/sonia/scm/cache/gcache.001.xml b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.001.xml new file mode 100644 index 0000000000..649229ccd0 --- /dev/null +++ b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.001.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/scm-webapp/src/test/resources/sonia/scm/cache/gcache.002.xml b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.002.xml new file mode 100644 index 0000000000..740c57ccba --- /dev/null +++ b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.002.xml @@ -0,0 +1,43 @@ + + + + + + + + + \ No newline at end of file diff --git a/scm-webapp/src/test/resources/sonia/scm/cache/gcache.003.xml b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.003.xml new file mode 100644 index 0000000000..6a35788b09 --- /dev/null +++ b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.003.xml @@ -0,0 +1,42 @@ + + + + + + + + diff --git a/scm-webapp/src/test/resources/sonia/scm/cache/gcache.004.xml b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.004.xml new file mode 100644 index 0000000000..04d2107c64 --- /dev/null +++ b/scm-webapp/src/test/resources/sonia/scm/cache/gcache.004.xml @@ -0,0 +1,43 @@ + + + + + + + + +