From 2771eb94dbe0b3709b6113cc1cc8997df7b825bc Mon Sep 17 00:00:00 2001 From: Egon Willighagen Date: Sat, 4 Jul 2009 18:06:35 +0200 Subject: [PATCH] Implemented instantiating custom loggers; example in the unit test class Signed-off-by: Rajarshi Guha --- .../cdk/tools/LoggingToolFactory.java | 61 +++++++++++------ .../cdk/tools/LoggingToolFactoryTest.java | 66 +++++++++++++++++++ 2 files changed, 107 insertions(+), 20 deletions(-) diff --git a/src/main/org/openscience/cdk/tools/LoggingToolFactory.java b/src/main/org/openscience/cdk/tools/LoggingToolFactory.java index 640c2e2abdd..1fc978d8163 100644 --- a/src/main/org/openscience/cdk/tools/LoggingToolFactory.java +++ b/src/main/org/openscience/cdk/tools/LoggingToolFactory.java @@ -48,7 +48,7 @@ public class LoggingToolFactory { public final static String STDOUT_LOGGING_TOOL_CLASS = "org.openscience.cdk.tools.SystemOutLoggingTool"; - private static Class loggingTool; + private static Class userSetILoggerTool; /** * Sets the {@link ILoggingTool} implementation to be used. @@ -56,10 +56,10 @@ public class LoggingToolFactory { * @param loggingTool The new {@link ILoggingTool}. * @see #getLoggingToolClass() */ - @TestMethod("testSetGetLoggingToolClass") + @TestMethod("testSetGetLoggingToolClass,testCustomLogger") public static void setLoggingToolClass( Class loggingTool) { - LoggingToolFactory.loggingTool = loggingTool; + LoggingToolFactory.userSetILoggerTool = loggingTool; } /** @@ -70,7 +70,7 @@ public static void setLoggingToolClass( */ @TestMethod("testSetGetLoggingToolClass") public static Class getLoggingToolClass() { - return LoggingToolFactory.loggingTool; + return LoggingToolFactory.userSetILoggerTool; } /** @@ -83,9 +83,18 @@ public static Class getLoggingToolClass() { */ @TestMethod("testCreateLoggingTool") public static ILoggingTool createLoggingTool(Class sourceClass) { - ILoggingTool tool = initializeLoggingTool( - sourceClass, DEFAULT_LOGGING_TOOL_CLASS - ); + ILoggingTool tool = null; + // first attempt the user set ILoggingTool + if (userSetILoggerTool != null) { + tool = instantiateWithCreateMethod( + sourceClass, userSetILoggerTool + ); + } + if (tool == null) { + tool = initializeLoggingTool( + sourceClass, DEFAULT_LOGGING_TOOL_CLASS + ); + } if (tool == null) { tool = initializeLoggingTool( sourceClass, STDOUT_LOGGING_TOOL_CLASS @@ -100,24 +109,36 @@ private static ILoggingTool initializeLoggingTool( Class possibleLoggingToolClass = sourceClass.getClassLoader() .loadClass(className); if (ILoggingTool.class.isAssignableFrom(possibleLoggingToolClass)) { - Method createMethod = possibleLoggingToolClass.getMethod( - "create", Class.class - ); - Object createdLoggingTool = createMethod.invoke( - null, sourceClass - ); - if (createdLoggingTool instanceof ILoggingTool) { - return (ILoggingTool)createdLoggingTool; - } else { - System.out.println("Expected ILoggingTool, but found a:" - + createdLoggingTool.getClass().getName()); - } + return instantiateWithCreateMethod(sourceClass, + possibleLoggingToolClass); } } catch (ClassNotFoundException e) { - } catch (IllegalAccessException e) { + } catch (SecurityException e) { + } catch (IllegalArgumentException e) { + } + return null; + } + + private static ILoggingTool instantiateWithCreateMethod( + Class sourceClass, Class loggingToolClass) { + Method createMethod; + try { + createMethod = loggingToolClass.getMethod( + "create", Class.class + ); + Object createdLoggingTool = createMethod.invoke( + null, sourceClass + ); + if (createdLoggingTool instanceof ILoggingTool) { + return (ILoggingTool)createdLoggingTool; + } else { + System.out.println("Expected ILoggingTool, but found a:" + + createdLoggingTool.getClass().getName()); + } } catch (SecurityException e) { } catch (NoSuchMethodException e) { } catch (IllegalArgumentException e) { + } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } return null; diff --git a/src/test/org/openscience/cdk/tools/LoggingToolFactoryTest.java b/src/test/org/openscience/cdk/tools/LoggingToolFactoryTest.java index 40d8d402ca1..0e70e5e0e19 100644 --- a/src/test/org/openscience/cdk/tools/LoggingToolFactoryTest.java +++ b/src/test/org/openscience/cdk/tools/LoggingToolFactoryTest.java @@ -50,4 +50,70 @@ public class LoggingToolFactoryTest { ); Assert.assertTrue(instance instanceof LoggingTool); } + + @Test public void testCustomLogger() { + LoggingToolFactory.setLoggingToolClass(CustomLogger.class); + ILoggingTool instance = LoggingToolFactory.createLoggingTool( + LoggingToolFactoryTest.class + ); + Assert.assertTrue(instance instanceof CustomLogger); + } + + /** + * Custom dummy logger used in the + * {@link LoggingToolFactoryTest#testCustomLogger()} test to see if + * the custom {@link ILoggingTool} is really being used. It does + * not really implement any method, as the test uses a mere + * instanceof call. + */ + private static class CustomLogger implements ILoggingTool { + + private CustomLogger(Class sourceClass) {} + + public static ILoggingTool create(Class sourceClass) { + return new CustomLogger(sourceClass); + } + + public void debug(Object object) {} + public void debug(Object object, Object object2) {} + public void debug(Object object, int number) {} + public void debug(Object object, double number) {} + public void debug(Object object, boolean bool) {} + public void debug(Object obj, Object obj2, Object obj3) {} + public void debug(Object obj, Object obj2, Object obj3, Object obj4) {} + public void debug(Object obj, Object obj2, Object obj3, Object obj4, + Object obj5) {} + public void dumpClasspath() {} + public void dumpSystemProperties() {} + public void error(Object object) {} + public void error(Object object, int number) {} + public void error(Object object, double number) {} + public void error(Object object, boolean bool) {} + public void error(Object object, Object object2) {} + public void error(Object obj, Object obj2, Object obj3) {} + public void error(Object obj, Object obj2, Object obj3, Object obj4) {} + public void error(Object obj, Object obj2, Object obj3, Object obj4, + Object obj5) {} + public void fatal(Object object) {} + public void info(Object object) {} + public void info(Object object, int number) {} + public void info(Object object, double number) {} + public void info(Object object, boolean bool) {} + public void info(Object object, Object object2) {} + public void info(Object obj, Object obj2, Object obj3) {} + public void info(Object obj, Object obj2, Object obj3, Object obj4) {} + public void info(Object obj, Object obj2, Object obj3, Object obj4, + Object obj5) {} + public boolean isDebugEnabled() { return true; } + public void setStackLength(int length) {} + public void warn(Object object) {} + public void warn(Object object, int number) {} + public void warn(Object object, boolean bool) {} + public void warn(Object object, double number) {} + public void warn(Object object, Object object2) {} + public void warn(Object obj, Object obj2, Object obj3) {} + public void warn(Object obj, Object obj2, Object obj3, Object obj4) {} + public void warn(Object obj, Object obj2, Object obj3, Object obj4, + Object obj5) {} + } }