added unit tests for GuavaCacheConfigurationReader

This commit is contained in:
Sebastian Sdorra
2013-04-11 15:28:47 +02:00
parent cab4f1a2d2
commit 55d5830ea8
12 changed files with 981 additions and 318 deletions

View File

@@ -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<URL> getModuleResources();
}

View File

@@ -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<URL> getModuleResources()
{
return CacheConfigurations.findModuleResources(
EhCacheConfigurationReader.class, moduleResources);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String defaultResource;
/** Field description */
private String manualFileResource;
/** Field description */
private String moduleResources;
}

View File

@@ -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<URL> it = getModuleResources();
Iterator<URL> 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<URL> 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<Id, Node> nodeMap = Maps.newLinkedHashMap();

View File

@@ -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

View File

@@ -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<URL> getModuleResources()
{
return CacheConfigurations.findModuleResources(
EhCacheConfigurationReader.class, MODULE_RESOURCES);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param caches
*
* @return
*/
private Map<String, GuavaNamedCacheConfiguration> createNamedCacheMap(
List<GuavaNamedCacheConfiguration> caches)
{
Map<String, GuavaNamedCacheConfiguration> 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<URL> it = getModuleResources();
Iterator<URL> 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<String, GuavaNamedCacheConfiguration> createNamedCacheMap(
List<GuavaNamedCacheConfiguration> caches)
{
Map<String, GuavaNamedCacheConfiguration> 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;
}

View File

@@ -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<String> 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<String> 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<URL> getModuleResources()
{
Iterator<URL> urlIterator;
if (moduleConfigurations == null)
{
urlIterator = Iterators.emptyIterator();
}
else
{
urlIterator = Iterators.transform(moduleConfigurations,
new Function<String, URL>()
{
@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<String> moduleConfigurations;
/** Field description */
private TemporaryFolder tempFolder;
}

View File

@@ -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<String> moduleConfigurations)
{
this.defaultConfiguration = defaultConfiguration;
this.moduleConfigurations = moduleConfigurations;
}
/**
* Constructs ...
*
*
* @param defaultConfiguration
* @param moduleConfigurations
* @param manualConfiguration
*/
public EhConfigurationTestReader(String defaultConfiguration,
Iterator<String> 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<URL> getModuleResources()
{
Iterator<URL> urlIterator;
if (moduleConfigurations == null)
{
urlIterator = Iterators.emptyIterator();
}
else
{
urlIterator = Iterators.transform(moduleConfigurations,
new Function<String, URL>()
{
@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<String> moduleConfigurations;
return new EhCacheConfigurationReader(
new CacheConfigurationTestLoader(tempFolder, defaultConfiguration));
}
/**
* Method description
*
*
* @param defaultConfiguration
* @param moduleConfiguration
* @param manualConfiguration
*
* @return
*/
private EhCacheConfigurationReader createReader(String defaultConfiguration,
Iterator<String> 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<String> moduleConfiguration)
{
return new EhCacheConfigurationReader(
new CacheConfigurationTestLoader(
tempFolder, defaultConfiguration, moduleConfiguration));
}
/**
* Method description
*
*
* @return
*/
private EhCacheConfigurationReader createReader()
{
return new EhCacheConfigurationReader(
new CacheConfigurationTestLoader(tempFolder));
}
//~--- fields ---------------------------------------------------------------

View File

@@ -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<String> moduleConfiguration)
{
return readConfiguration(defaultConfiguration, moduleConfiguration, null);
}
/**
* Method description
*
*
* @param defaultConfiguration
* @param moduleConfiguration
* @param manualConfiguration
*
* @return
*/
private GuavaCacheManagerConfiguration readConfiguration(
String defaultConfiguration, Iterator<String> 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();
}

View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--*
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
-->
<caches>
<defaultCache
maximumSize="200"
expireAfterAccess="1200"
expireAfterWrite="2400"
/>
<cache
name="sonia.test.cache.001"
maximumSize="1000"
expireAfterWrite="30"
expireAfterAccess="60"
/>
</caches>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--*
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
-->
<caches>
<cache
name="sonia.test.cache.002"
maximumSize="1000"
expireAfterWrite="60"
expireAfterAccess="120"
/>
</caches>

View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--*
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
-->
<caches>
<cache
name="sonia.test.cache.003"
maximumSize="3000"
expireAfterAccess="120"
expireAfterWrite="2400"
/>
</caches>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--*
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
-->
<caches>
<cache
name="sonia.test.cache.001"
maximumSize="6"
expireAfterAccess="2"
expireAfterWrite="8"
/>
</caches>