Title: [waffle-scm] [239] trunk/core: Renamed ConsoleMonitor to AbstractWritingMonitor.

Diff

Modified: trunk/core/pom.xml (238 => 239)

--- trunk/core/pom.xml	2007-07-08 17:54:11 UTC (rev 238)
+++ trunk/core/pom.xml	2007-07-09 10:51:06 UTC (rev 239)
@@ -38,6 +38,10 @@
       <artifactId>xstream</artifactId>
     </dependency>
     <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+    </dependency>
+    <dependency>
       <groupId>velocity</groupId>
       <artifactId>velocity</artifactId>
     </dependency>

Modified: trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java (238 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java	2007-07-08 17:54:11 UTC (rev 238)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoComponentRegistry.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -10,7 +10,12 @@
  *****************************************************************************/
 package org.codehaus.waffle.context.pico;
 
+import java.util.Enumeration;
+
+import javax.servlet.ServletContext;
+
 import ognl.TypeConverter;
+
 import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.WaffleException;
 import org.codehaus.waffle.action.ActionMethodExecutor;
@@ -26,10 +31,10 @@
 import org.codehaus.waffle.bind.BindErrorMessageResolver;
 import org.codehaus.waffle.bind.DataBinder;
 import org.codehaus.waffle.bind.DefaultBindErrorMessageResolver;
+import org.codehaus.waffle.bind.IntrospectingRequestAttributeBinder;
 import org.codehaus.waffle.bind.OgnlDataBinder;
 import org.codehaus.waffle.bind.OgnlTypeConverter;
 import org.codehaus.waffle.bind.RequestAttributeBinder;
-import org.codehaus.waffle.bind.IntrospectingRequestAttributeBinder;
 import org.codehaus.waffle.context.ContextContainerFactory;
 import org.codehaus.waffle.controller.ContextControllerDefinitionFactory;
 import org.codehaus.waffle.controller.ContextPathControllerNameResolver;
@@ -37,8 +42,8 @@
 import org.codehaus.waffle.controller.ControllerNameResolver;
 import org.codehaus.waffle.i18n.DefaultMessageResources;
 import org.codehaus.waffle.i18n.MessageResources;
-import org.codehaus.waffle.monitor.ConsoleMonitor;
 import org.codehaus.waffle.monitor.Monitor;
+import org.codehaus.waffle.monitor.SilentMonitor;
 import org.codehaus.waffle.validation.DefaultValidator;
 import org.codehaus.waffle.validation.Validator;
 import org.codehaus.waffle.view.DefaultDispatchAssistant;
@@ -52,9 +57,6 @@
 import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
 import org.picocontainer.defaults.DefaultPicoContainer;
 
-import javax.servlet.ServletContext;
-import java.util.Enumeration;
-
 /**
  * PicoContainer-based implementation of Waffle's ComponentRegistry
  *
@@ -85,7 +87,7 @@
         register(MessageResources.class, DefaultMessageResources.class, servletContext);
         register(MethodDefinitionFinder.class, AnnotatedMethodDefinitionFinder.class, servletContext);
         register(MethodNameResolver.class, RequestParameterMethodNameResolver.class, servletContext);
-        register(Monitor.class, ConsoleMonitor.class, servletContext);
+        register(Monitor.class, SilentMonitor.class, servletContext);
         register(RequestAttributeBinder.class, IntrospectingRequestAttributeBinder.class, servletContext);
         register(TypeConverter.class, OgnlTypeConverter.class, servletContext);
         register(Validator.class, DefaultValidator.class, servletContext);

Added: trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java (0 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -0,0 +1,55 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Mauro Talevi                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.monitor;
+
+import static org.codehaus.waffle.monitor.MonitorLevel.DEBUG;
+import static org.codehaus.waffle.monitor.MonitorLevel.INFO;
+
+import java.util.Set;
+
+import org.codehaus.waffle.action.MethodDefinition;
+
+/**
+ * Abstract implementation of Monitor that delegates writing to concrete subclasses.
+ * 
+ * @author Mauro Talevi
+ */
+public abstract class AbstractWritingMonitor implements Monitor {
+
+    /**
+     * Writes message for a given level. Concrete implementations should provide writing functionality.
+     * 
+     * @param level
+     * @param message
+     */
+    protected abstract void write(MonitorLevel level, String message);
+
+    public void defaultActionMethodFound(MethodDefinition methodDefinition) {
+        write(INFO, "Default ActionMethod found: " + methodDefinition);
+    }
+
+    public void defaultActionMethodCached(Class<?> controllerType, MethodDefinition methodDefinition) {
+        write(DEBUG, "Default ActionMethod cached for controller " + controllerType.getName() + ": " + methodDefinition);
+    }
+
+    public void pragmaticActionMethodFound(MethodDefinition methodDefinition) {
+        write(INFO, "Pragmatic ActionMethod found: " + methodDefinition);
+    }
+
+    public void actionMethodFound(MethodDefinition methodDefinition) {
+        write(INFO, "ActionMethod found:  " + methodDefinition);
+    }
+
+    public void methodNameResolved(String methodName, String methodKey, Set<String> keys) {
+        write(INFO, "Method name '" + methodName + "' found for key '" + methodKey + "' among keys " + keys);
+    }
+
+}

Added: trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java (0 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -0,0 +1,73 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Mauro Talevi                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.monitor;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Writing monitor that uses Commons-Logging to log events
+ * 
+ * @author Mauro Talevi
+ */
+public class CommonsLoggingMonitor extends AbstractWritingMonitor {
+
+    private final Log log;
+
+    /**
+     * Creates a default CommonsLoggingMonitor.
+     */
+    public CommonsLoggingMonitor() {
+        this(CommonsLoggingMonitor.class.getName());
+    }
+
+    /**
+     * Creates a CommonsLoggingMonitor with a given Log instance name. It uses the [EMAIL PROTECTED] LogFactory LogFactory} to
+     * create the Log instance.
+     * 
+     * @param logName the name of the Log
+     */
+    public CommonsLoggingMonitor(String logName) {
+        this(LogFactory.getLog(logName));
+    }
+
+    /**
+     * Creates a CommonsLoggingMonitor with a given Log instance
+     * 
+     * @param log the Log to write to
+     */
+    public CommonsLoggingMonitor(Log log) {
+        this.log = log;
+    }
+
+    @Override
+    protected void write(MonitorLevel level, String message) {
+        switch (level) {
+            case ERROR:
+                if (log.isErrorEnabled())
+                    log.error(message);
+                break;
+            case INFO:
+                if (log.isInfoEnabled())
+                    log.info(message);
+                break;
+            case WARN:
+                if (log.isWarnEnabled())
+                    log.warn(message);
+                break;
+            case DEBUG:
+                if (log.isDebugEnabled())
+                    log.debug(message);
+                break;
+        }
+    }
+
+}

Deleted: trunk/core/src/main/java/org/codehaus/waffle/monitor/ConsoleMonitor.java (238 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/ConsoleMonitor.java	2007-07-08 17:54:11 UTC (rev 238)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/ConsoleMonitor.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -1,49 +0,0 @@
-/*****************************************************************************
- * Copyright (C) 2005,2006 Michael Ward                                      *
- * All rights reserved.                                                      *
- * ------------------------------------------------------------------------- *
- * The software in this package is published under the terms of the BSD      *
- * style license a copy of which has been included with this distribution in *
- * the LICENSE.txt file.                                                     *
- *                                                                           *
- * Original code by: Mauro Talevi                                            *
- *****************************************************************************/
-package org.codehaus.waffle.monitor;
-
-import java.util.Set;
-
-import org.codehaus.waffle.action.MethodDefinition;
-
-/**
- * Implementation of Monitor that writes to console
- * 
- * @author Mauro Talevi
- */
-public class ConsoleMonitor implements Monitor {
-
-    protected void write(String message) {
-        System.out.println(message);
-    }
-
-    public void defaultActionMethodFound(MethodDefinition methodDefinition) {
-        write("Default ActionMethod found: "+methodDefinition);
-    }
-
-    public void defaultActionMethodCached(Class controllerType, MethodDefinition methodDefinition) {
-        write("Default ActionMethod cached for controller "+ controllerType.getName()+": "+methodDefinition);        
-    }
-
-    public void pragmaticActionMethodFound(MethodDefinition methodDefinition) {
-        write("Pragmatic ActionMethod found: "+methodDefinition);
-    }
-
-    public void actionMethodFound(MethodDefinition methodDefinition) {        
-        write("ActionMethod found:  "+methodDefinition);
-    }
-
-    public void methodNameResolved(String methodName, String methodKey, Set<String> keys) {
-        write("Method name '"+methodName+"' found for key '"+methodKey+"' among keys "+keys);
-    }
-
-
-}

Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/Monitor.java (238 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/Monitor.java	2007-07-08 17:54:11 UTC (rev 238)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/Monitor.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -26,7 +26,7 @@
 
     void defaultActionMethodFound(MethodDefinition methodDefinition);
 
-    void defaultActionMethodCached(Class controllerType, MethodDefinition methodDefinition);
+    void defaultActionMethodCached(Class<?> controllerType, MethodDefinition methodDefinition);
 
     void pragmaticActionMethodFound(MethodDefinition methodDefinition);
 

Added: trunk/core/src/main/java/org/codehaus/waffle/monitor/MonitorLevel.java (0 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/MonitorLevel.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/MonitorLevel.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -0,0 +1,35 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Mauro Talevi                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.monitor;
+
+/**
+ * Enum that represent monitoring levels
+ * 
+ * @author Mauro Talevi
+ */
+public enum MonitorLevel {
+
+    ERROR("Error"),
+    INFO("Info"), 
+    WARN("Warn"),
+    DEBUG("Debug");
+
+    private final String level;
+
+    MonitorLevel(String level) {
+        this.level = level;
+    }
+
+    public String toString(){
+        return level;
+    }
+    
+}

Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java (238 => 239)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java	2007-07-08 17:54:11 UTC (rev 238)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -12,13 +12,13 @@
 
 
 /**
- * Silent Monitor is a console monitor that writes nothing.
+ * SilentMonitor is a writing monitor that writes nothing.
  * 
  * @author Mauro Talevi
  */
-public class SilentMonitor extends ConsoleMonitor {
+public class SilentMonitor extends AbstractWritingMonitor {
 
-    protected void write(String message) {
+    protected void write(MonitorLevel level, String message) {
         // write nothing
     }
     

Modified: trunk/core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java (238 => 239)

--- trunk/core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java	2007-07-08 17:54:11 UTC (rev 238)
+++ trunk/core/src/test/java/org/codehaus/waffle/context/pico/PicoComponentRegistryTest.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -25,7 +25,7 @@
 import org.codehaus.waffle.controller.ControllerNameResolver;
 import org.codehaus.waffle.i18n.DefaultMessageResources;
 import org.codehaus.waffle.i18n.MessageResources;
-import org.codehaus.waffle.monitor.ConsoleMonitor;
+import org.codehaus.waffle.monitor.AbstractWritingMonitor;
 import org.codehaus.waffle.monitor.Monitor;
 import org.codehaus.waffle.testmodel.StubActionMethodExecutor;
 import org.codehaus.waffle.testmodel.StubActionMethodResponseHandler;
@@ -113,7 +113,7 @@
         assertTrue(componentRegistry.getActionMethodResponseHandler() instanceof DefaultActionMethodResponseHandler);
         assertTrue(componentRegistry.getMethodNameResolver() instanceof RequestParameterMethodNameResolver);
         assertTrue(componentRegistry.getMessageResources() instanceof DefaultMessageResources);
-        assertTrue(componentRegistry.getMonitor() instanceof ConsoleMonitor);
+        assertTrue(componentRegistry.getMonitor() instanceof AbstractWritingMonitor);
         assertTrue(componentRegistry.getViewDispatcher() instanceof DefaultViewDispatcher);
         assertTrue(componentRegistry.getTypeConverter() instanceof OgnlTypeConverter);
         assertTrue(componentRegistry.getViewResolver() instanceof DefaultViewResolver);

Added: trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java (0 => 239)

--- trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -0,0 +1,68 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Mauro Talevi                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.monitor;
+
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.Method;
+import java.util.Set;
+
+import org.codehaus.waffle.action.MethodDefinition;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * 
+ * @author Mauro Talevi
+ */
[EMAIL PROTECTED](JMock.class)
+public class AbstractWritingMonitorTest {
+
+    private final Mockery mockery = new JUnit4Mockery();
+
+    @Test
+    public void canWriteEvents() {
+        final StringBuffer sb = new StringBuffer();
+        final AbstractWritingMonitor monitor = new AbstractWritingMonitor() {
+
+            @Override
+            protected void write(MonitorLevel level, String message) {
+                sb.append(message).append("\n");
+            }
+
+        };
+        MethodDefinition methodDefinition = mockMethodDefinition();
+        monitor.actionMethodFound(methodDefinition);
+        monitor.defaultActionMethodCached(Object.class, methodDefinition);
+        monitor.defaultActionMethodFound(methodDefinition);
+        monitor.methodNameResolved("methodName", "methodKey", mockSet());
+        monitor.pragmaticActionMethodFound(methodDefinition);
+        assertEquals(5, sb.toString().split("\n").length);
+    }
+
+    private MethodDefinition mockMethodDefinition() {
+        try {
+            Method method = Object.class.getMethod("toString", (Class[]) null);
+            return new MethodDefinition(method);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private Set<String> mockSet() {
+        return mockery.mock(Set.class);
+    }
+
+}

Added: trunk/core/src/test/java/org/codehaus/waffle/monitor/CommonsLoggingMonitorTest.java (0 => 239)

--- trunk/core/src/test/java/org/codehaus/waffle/monitor/CommonsLoggingMonitorTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/monitor/CommonsLoggingMonitorTest.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -0,0 +1,81 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Mauro Talevi                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.monitor;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.commons.logging.Log;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * 
+ * @author Mauro Talevi
+ */
[EMAIL PROTECTED](JMock.class)
+public class CommonsLoggingMonitorTest {
+
+    private final Mockery mockery = new JUnit4Mockery();
+
+    @Test
+    public void canInstantiateUsingDefaultConstructor() {
+        CommonsLoggingMonitor monitor = new CommonsLoggingMonitor();
+        assertNotNull(monitor);
+    }
+    
+    @Test
+    public void canWriteWhenLoggingEnabled() {
+        final Log log = mockery.mock(Log.class);
+        final String message = "message";
+        mockery.checking(new Expectations() {
+            {
+                one(log).isDebugEnabled(); will(returnValue(true));
+                one(log).debug(message);
+                one(log).isErrorEnabled(); will(returnValue(true));
+                one(log).error(message);
+                one(log).isInfoEnabled(); will(returnValue(true));
+                one(log).info(message);
+                one(log).isWarnEnabled(); will(returnValue(true));
+                one(log).warn(message);
+            }
+        });
+
+        final CommonsLoggingMonitor monitor = new CommonsLoggingMonitor(log);
+        monitor.write(MonitorLevel.DEBUG, message);
+        monitor.write(MonitorLevel.ERROR, message);
+        monitor.write(MonitorLevel.INFO, message);
+        monitor.write(MonitorLevel.WARN, message);
+    }
+
+    @Test
+    public void cannotWriteWhenLoggingNotEnabled() {
+        final Log log = mockery.mock(Log.class);
+        final String message = "message";
+        mockery.checking(new Expectations() {
+            {
+                one(log).isDebugEnabled(); will(returnValue(false));
+                one(log).isErrorEnabled(); will(returnValue(false));
+                one(log).isInfoEnabled(); will(returnValue(false));
+                one(log).isWarnEnabled(); will(returnValue(false));
+            }
+        });
+
+        final CommonsLoggingMonitor monitor = new CommonsLoggingMonitor(log);
+        monitor.write(MonitorLevel.DEBUG, message);
+        monitor.write(MonitorLevel.ERROR, message);
+        monitor.write(MonitorLevel.INFO, message);
+        monitor.write(MonitorLevel.WARN, message);
+    }
+}

Added: trunk/core/src/test/java/org/codehaus/waffle/monitor/MonitorLevelTest.java (0 => 239)

--- trunk/core/src/test/java/org/codehaus/waffle/monitor/MonitorLevelTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/monitor/MonitorLevelTest.java	2007-07-09 10:51:06 UTC (rev 239)
@@ -0,0 +1,31 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Mauro Talevi                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.monitor;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * 
+ * @author Mauro Talevi
+ */
+public class MonitorLevelTest {
+
+    @Test
+    public void enumsHHaveValidStringRepresentations() {
+        assertEquals(MonitorLevel.DEBUG.toString(), "Debug");
+        assertEquals(MonitorLevel.ERROR.toString(), "Error");
+        assertEquals(MonitorLevel.INFO.toString(), "Info");
+        assertEquals(MonitorLevel.WARN.toString(), "Warn");
+    }
+
+}


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to