Permalink
Browse files

Now fully allows setting an alternate config file using a command line

setting; includes tests.
  • Loading branch information...
1 parent 4a96b27 commit 1b9d1f69934d5b647b36c978ffbcf6963b73c176 Janne Jalkanen committed Jan 21, 2012
@@ -64,22 +64,13 @@
*/
public static final String SYSTEM_PROPERTY = "speed4j.properties";
- private static Map<String,StopWatchFactory> c_factories = new HashMap<String,StopWatchFactory>();
+ private static Map<String,StopWatchFactory> c_factories;
/**
* This is the Log that this factory is associated to.
*/
private Log m_log;
- /**
- * When the class is instantiated, try to set up the configuration from the config
- * file.
- */
- static
- {
- configure();
- }
-
private static InputStream findConfigFile( String... alternatives )
{
for( String name : alternatives )
@@ -99,16 +90,25 @@ private static InputStream findConfigFile( String... alternatives )
return null;
}
+ private static void configure()
+ {
+ String propertyFile = System.getProperty( SYSTEM_PROPERTY, PROPERTYFILENAME );
+
+ InputStream in = findConfigFile( propertyFile,
+ "/com/ecyrd/speed4j/default_speed4j.properties");
+
+ configure(in);
+ }
+
/**
* Load configuration file, try to parse it and do something useful.
*
* @throws ConfigurationException If configuration fails.
*/
@SuppressWarnings( "unchecked" )
- private static void configure() throws ConfigurationException
+ private static void configure(InputStream in) throws ConfigurationException
{
- InputStream in = findConfigFile( System.getProperty( SYSTEM_PROPERTY, PROPERTYFILENAME ),
- "/com/ecyrd/speed4j/default_speed4j.properties");
+ c_factories = new HashMap<String, StopWatchFactory>();
try
{
@@ -266,6 +266,8 @@ public static StopWatchFactory getDefault()
*/
public static void shutdown()
{
+ if( c_factories == null ) return; // Nothing to do
+
for( Iterator<Entry<String, StopWatchFactory>> i = c_factories.entrySet().iterator(); i.hasNext() ; )
{
Map.Entry<String,StopWatchFactory> e = i.next();
@@ -290,6 +292,12 @@ public static StopWatchFactory getInstance(Log logger)
return new StopWatchFactory(logger);
}
+ private static synchronized Map<String, StopWatchFactory> getFactories()
+ {
+ if( c_factories == null ) configure();
+ return c_factories;
+ }
+
/**
* Returns a StopWatchFactory that has been configured previously. May return
* null, if the factory has not been configured.
@@ -299,7 +307,7 @@ public static StopWatchFactory getInstance(Log logger)
*/
public static StopWatchFactory getInstance(String loggerName) throws ConfigurationException
{
- StopWatchFactory swf = c_factories.get(loggerName);
+ StopWatchFactory swf = getFactories().get(loggerName);
if( swf == null ) throw new ConfigurationException("No logger by the name "+loggerName+" found.");
@@ -15,45 +15,73 @@
*/
package com.ecyrd.speed4j;
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
-import com.ecyrd.speed4j.StopWatch;
-import com.ecyrd.speed4j.StopWatchFactory;
import com.ecyrd.speed4j.log.PeriodicalLog;
import com.ecyrd.speed4j.log.Slf4jLog;
public class StopWatchFactoryTest
{
-
+ /**
+ * Sets up a test property file to check for settings.
+ */
+ @Before
+ public void setConfig()
+ {
+ System.setProperty( "speed4j.properties", "test_speed4j.properties" );
+ }
+
+ @After
+ public void cleanup()
+ {
+ // Clean up the StopWatchFactory, just in case.
+ System.gc();
+ }
+
+ /**
+ * Test that we can find a test factory with the special settings.
+ */
+ @Test
+ public void testLoggerSetting()
+ {
+ StopWatchFactory swf = StopWatchFactory.getInstance( "testFactory" );
+
+ assertNotNull("StopWatchFactory not found", swf);
+ }
+
/* This succeeds if it passes quietly. */
@Test
public void test() throws InterruptedException
{
StopWatchFactory swf = StopWatchFactory.getDefault();
-
+
int iterations = 120;
-
+
for( int i = 0; i < iterations; i++ )
{
StopWatch sw = swf.getStopWatch("foo");
-
+
Thread.sleep(10+ (long)(Math.random() * 10));
-
+
sw.stop("iteration:success");
}
-
+
}
-
+
@Test
public void testSlf4jLog() throws InterruptedException
{
Slf4jLog log = new Slf4jLog();
log.setSlf4jLogname("foo");
StopWatchFactory swf = StopWatchFactory.getInstance(log);
-
+
int iterations = 100;
-
+
for( int i = 0; i < iterations; i++ )
{
StopWatch sw = swf.getStopWatch("foo");
@@ -62,7 +90,7 @@ public void testSlf4jLog() throws InterruptedException
sw.stop("iteration:success");
}
-
+
}
@Test
@@ -73,22 +101,22 @@ public void testPeriodicalLog() throws InterruptedException
log.setPeriod(5);
log.setName("testLog");
log.setJmx("iteration:1,iteration:2,iteration:3,iteration:4,iteration:N");
-
+
StopWatchFactory swf = StopWatchFactory.getInstance(log);
-
+
int iterations = 1000;
-
+
for( int i = 0; i < iterations; i++ )
{
StopWatch sw = swf.getStopWatch("foo");
long waitPeriod = (long)(Math.random() * 10);
-
+
Thread.sleep(10+waitPeriod);
sw.stop("iteration:"+waitPeriod);
}
-
+
}
}
@@ -0,0 +1,6 @@
+#
+# This is a test file for StopWatchFactoryTest.
+#
+
+speed4j.testFactory=com.ecyrd.speed4j.log.Slf4jLog
+speed4j.testFactory.slf4jLogname=com.example.mylog

0 comments on commit 1b9d1f6

Please sign in to comment.