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,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();
}