commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joc...@apache.org
Subject svn commit: r1653544 - in /commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl: InitializableObject.java Log4jLoggerModuleTest.java LoggerModuleTest.java log/ log/SimpleLoggerFactory.java
Date Wed, 21 Jan 2015 14:26:59 GMT
Author: jochen
Date: Wed Jan 21 14:26:58 2015
New Revision: 1653544

URL: http://svn.apache.org/r1653544
Log:
LoggerModuleTest is now working when called from Surefire.

Added:
    commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/LoggerModuleTest.java
      - copied, changed from r1651363, commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/Log4jLoggerModuleTest.java
    commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/
    commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/SimpleLoggerFactory.java
  (with props)
Removed:
    commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/Log4jLoggerModuleTest.java
Modified:
    commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/InitializableObject.java

Modified: commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/InitializableObject.java
URL: http://svn.apache.org/viewvc/commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/InitializableObject.java?rev=1653544&r1=1653543&r2=1653544&view=diff
==============================================================================
--- commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/InitializableObject.java
(original)
+++ commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/InitializableObject.java
Wed Jan 21 14:26:58 2015
@@ -18,24 +18,25 @@ package org.apache.commons.inject.impl;
 
 import org.apache.commons.inject.api.ILifecycleListener;
 import org.apache.commons.inject.api.InjLogger;
-import org.apache.log4j.Logger;
+import org.apache.commons.inject.impl.log.SimpleLoggerFactory.Level;
+import org.apache.commons.inject.impl.log.SimpleLoggerFactory.SimpleLogger;
 
 public class InitializableObject implements ILifecycleListener {
-	@InjLogger(id="MyLogger") private Logger log;
+	@InjLogger(id="MyLogger") private SimpleLogger log;
 
 	@Override
 	public void start() {
-		log.debug("start: ->");
-		log.debug("start: <-");
+		log.log("start: ->");
+		log.log("start: <-");
 	}
 
 	public void run() {
-		log.debug("run: Running");
+		log.log(Level.INFO, "run: Running");
 	}
 	
 	@Override
 	public void shutdown() {
-		log.debug("shutdown: ->");
-		log.debug("shutdown: <-");
+		log.log("shutdown: ->");
+		log.log("shutdown: <-");
 	}
 }
\ No newline at end of file

Copied: commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/LoggerModuleTest.java
(from r1651363, commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/Log4jLoggerModuleTest.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/LoggerModuleTest.java?p2=commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/LoggerModuleTest.java&p1=commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/Log4jLoggerModuleTest.java&r1=1651363&r2=1653544&rev=1653544&view=diff
==============================================================================
--- commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/Log4jLoggerModuleTest.java
(original)
+++ commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/LoggerModuleTest.java
Wed Jan 21 14:26:58 2015
@@ -16,56 +16,30 @@
 */
 package org.apache.commons.inject.impl;
 
-import java.util.ArrayList;
-import java.util.List;
-
+import org.apache.commons.inject.api.AbstractLoggerInjectingModule;
 import org.apache.commons.inject.api.CommonsInject;
 import org.apache.commons.inject.api.IInjector;
 import org.apache.commons.inject.api.ILifecycleController;
-import org.apache.commons.inject.api.Log4jLoggerModule;
 import org.apache.commons.inject.api.PostConstructModule;
 import org.apache.commons.inject.api.bind.IBinder;
 import org.apache.commons.inject.api.bind.IModule;
 import org.apache.commons.inject.api.bind.Scopes;
-import org.apache.log4j.AppenderSkeleton;
-import org.apache.log4j.BasicConfigurator;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
-import org.apache.log4j.spi.LoggingEvent;
+import org.apache.commons.inject.impl.log.SimpleLoggerFactory;
+import org.apache.commons.inject.impl.log.SimpleLoggerFactory.SimpleLogger;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class Log4jLoggerModuleTest {
-	private final List<String> messages = new ArrayList<String>();
-	private void configureLog4j() {
-		BasicConfigurator.configure();
-		final Logger root = Logger.getRootLogger();
-		root.removeAllAppenders();
-		root.addAppender(new AppenderSkeleton(true) {
-			
-			@Override
-			public boolean requiresLayout() {
-				return false;
-			}
-			
-			@Override
-			public void close() {
-				// Does nothing.
-			}
-			
-			@Override
-			protected void append(LoggingEvent pEvent) {
-				final Level level = pEvent.getLevel();
-				messages.add(level.toString() + "; " + pEvent.getMessage());
-			}
-		});
-	}
-
+public class LoggerModuleTest {
 	@Test
 	public void testLoggerModule() {
-		configureLog4j();
+		final SimpleLoggerFactory factory = new SimpleLoggerFactory();
 		final PostConstructModule module0 = new PostConstructModule();
-		final IModule module1 = new Log4jLoggerModule();
+		final IModule module1 = new AbstractLoggerInjectingModule<SimpleLogger>(){
+			@Override
+			protected SimpleLogger newLogger(String pId) {
+				return factory.getLogger(pId);
+			}
+		};
 		final IModule module2 = new IModule(){
 			@Override
 			public void configure(IBinder pBinder) {
@@ -78,11 +52,11 @@ public class Log4jLoggerModuleTest {
 		controller.start();
 		io.run();
 		controller.shutdown();
-		Assert.assertEquals(5, messages.size());
-		Assert.assertEquals("DEBUG; start: ->", messages.get(0));
-		Assert.assertEquals("DEBUG; start: <-", messages.get(1));
-		Assert.assertEquals("DEBUG; run: Running", messages.get(2));
-		Assert.assertEquals("DEBUG; shutdown: ->", messages.get(3));
-		Assert.assertEquals("DEBUG; shutdown: <-", messages.get(4));
+		Assert.assertEquals(5, factory.getNumEvents());
+		Assert.assertEquals("DEBUG MyLogger start: ->", factory.getMessage(0));
+		Assert.assertEquals("DEBUG MyLogger start: <-", factory.getMessage(1));
+		Assert.assertEquals("INFO  MyLogger run: Running", factory.getMessage(2));
+		Assert.assertEquals("DEBUG MyLogger shutdown: ->", factory.getMessage(3));
+		Assert.assertEquals("DEBUG MyLogger shutdown: <-", factory.getMessage(4));
 	}
 }

Added: commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/SimpleLoggerFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/SimpleLoggerFactory.java?rev=1653544&view=auto
==============================================================================
--- commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/SimpleLoggerFactory.java
(added)
+++ commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/SimpleLoggerFactory.java
Wed Jan 21 14:26:58 2015
@@ -0,0 +1,65 @@
+package org.apache.commons.inject.impl.log;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class SimpleLoggerFactory {
+	public enum Level {
+		TRACE("TRACE"),
+		DEBUG("DEBUG"),
+		INFO("INFO "),
+		WARN("WARN "),
+		ERROR("ERROR"),
+		FATAL("FATAL");
+		final String value;
+		private Level(String pValue) {
+			value = pValue;
+		}
+		public String getValue() {
+			return value;
+		}
+	}
+	public interface SimpleLogger {
+		void log(Level pLevel, String pMsg);
+		void log(String pMsg);
+	}
+
+	private final List<String> messages = new ArrayList<String>();
+
+	public int getNumEvents() {
+		synchronized(messages) {
+			return messages.size();
+		}
+	}
+
+	public SimpleLogger getLogger(final String pId) {
+		return new SimpleLogger() {
+			private final String id = pId;
+
+			@Override
+			public void log(Level pLevel, String pMsg) {
+				final StringBuilder sb = new StringBuilder();
+				sb.append(pLevel.getValue());
+				sb.append(' ');
+				sb.append(id);
+				sb.append(' ');
+				sb.append(pMsg);
+				synchronized (messages) {
+					messages.add(sb.toString());
+				}
+			}
+
+			@Override
+			public void log(String pMsg) {
+				log(Level.DEBUG, pMsg);
+			}
+			
+		};
+	}
+
+	public Object getMessage(int i) {
+		synchronized(messages) {
+			return messages.get(i);
+		}
+	}
+}

Propchange: commons/sandbox/commons-inject/trunk/src/test/java/org/apache/commons/inject/impl/log/SimpleLoggerFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message