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;
}