Author: julien_viet
Date: 2010-02-28 13:10:35 -0500 (Sun, 28 Feb 2010)
New Revision: 1902
Added:
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/LogConfigurator.java
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternElementType.java
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternFormatter.java
portal/trunk/component/test/core/src/main/resources/gatein-logging.properties
Modified:
portal/trunk/component/test/core/pom.xml
portal/trunk/component/test/organization/src/main/resources/conf/exo.portal.component.test.organization-configuration.xml
portal/trunk/pom.xml
Log:
GTNPORTAL-778 : Unit test logging
Modified: portal/trunk/component/test/core/pom.xml
===================================================================
--- portal/trunk/component/test/core/pom.xml 2010-02-27 22:03:28 UTC (rev 1901)
+++ portal/trunk/component/test/core/pom.xml 2010-02-28 18:10:35 UTC (rev 1902)
@@ -65,6 +65,24 @@
</dependencies>
<build>
+
+<!--
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <systemProperties>
+ <property>
+ <name>java.util.logging.config.class</name>
+ <value>org.exoplatform.component.test</value>
+ </property>
+ </systemProperties>
+ </configuration>
+ </plugin>
+ </plugins>
+-->
+
<resources>
<resource>
<directory>src/main/resources</directory>
Added:
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/LogConfigurator.java
===================================================================
---
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/LogConfigurator.java
(rev 0)
+++
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/LogConfigurator.java 2010-02-28
18:10:35 UTC (rev 1902)
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.component.test.logging;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+import java.util.logging.LogManager;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * <p>A logger configuration for the the java logging system. It attempts to load
the logging configuration from
+ * the classpath under the name <i>gatein-logging.properties</i>. If the
configuration cannot happen for some reason
+ * (the file cannot be loaded for instance) then an exception is thrown that will be
logged by the java logging
+ * system and configuration will happen by other means defined by the java logging system
(which means RTFM).</p>
+ *
+ * <p>When the property file is loaded, string interpolation happens on the values
using the format <i>${}</i>. When
+ * interpolation occurs it happens with the system properties. This is useful to
configure the output directory
+ * of the {@link java.util.logging.FileHandler} class with the maven target
directory.</p>
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class LogConfigurator
+{
+
+ /** The interpolation pattern. */
+ private static final Pattern INTERPOLATION_PATTERN =
Pattern.compile("\\$\\{([^}]+)\\}");
+
+ public LogConfigurator() throws Exception
+ {
+ LogManager manager = LogManager.getLogManager();
+
+ //
+ boolean configured = false;
+ URL url =
Thread.currentThread().getContextClassLoader().getResource("gatein-logging.properties");
+ if (url != null)
+ {
+ try
+ {
+ InputStream in = url.openStream();
+
+ //
+ Properties props = new Properties();
+ props.load(in);
+
+ //
+ for (Map.Entry<?, ?> entry : props.entrySet())
+ {
+ // A necessity here...
+ Map.Entry<String, String> entry2 = (Map.Entry<String,
String>)entry;
+
+ //
+ String value = entry2.getValue();
+ Matcher matcher = INTERPOLATION_PATTERN.matcher(value);
+ StringBuffer builder = new StringBuffer();
+ while (matcher.find())
+ {
+ String matched = matcher.group(1);
+ String repl = System.getProperty(matched);
+ if (repl == null)
+ {
+ repl = matched;
+ }
+ matcher.appendReplacement(builder, repl);
+ }
+ matcher.appendTail(builder);
+ entry2.setValue(builder.toString());
+ }
+
+ //
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ props.store(baos, null);
+ baos.close();
+ in = new ByteArrayInputStream(baos.toByteArray());
+
+ //
+ manager.readConfiguration(in);
+ configured = true;
+ }
+ catch (Throwable t)
+ {
+ throw new UndeclaredThrowableException(t, "Could not configure logging
" +
+ "from gatein-logging.properties file");
+ }
+ }
+
+ // In case something bad happened we reconfigure with default
+ if (!configured)
+ {
+ throw new Exception("Could not configure logging " +
+ "from gatein-logging.properties file");
+ }
+ }
+}
Added:
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternElementType.java
===================================================================
---
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternElementType.java
(rev 0)
+++
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternElementType.java 2010-02-28
18:10:35 UTC (rev 1902)
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.component.test.logging;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.logging.LogRecord;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public enum PatternElementType
+{
+
+ LEVEL('l') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getLevel().toString();
+ }},
+
+ MESSAGE('m') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getMessage();
+ }},
+
+ THREAD_NAME('t') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getThreadID();
+ }},
+
+ DATE('d') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getMillis();
+ }},
+
+ LOGGER_NAME('n') {
+ @Override
+ Object get(LogRecord record)
+ {
+ String name = record.getLoggerName();
+ return name.substring(name.lastIndexOf('.') + 1);
+ }},
+
+ LOGGER_FQN('N') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getLoggerName();
+ }},
+
+ CLASS_NAME('c') {
+ @Override
+ Object get(LogRecord record)
+ {
+ String sourceClassName = record.getSourceClassName();
+ return sourceClassName.substring(sourceClassName.lastIndexOf('.') + 1);
+ }},
+
+ CLASS_FQN('C') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getSourceClassName();
+ }},
+
+ METHOD_NAME('m') {
+ @Override
+ Object get(LogRecord record)
+ {
+ return record.getSourceMethodName();
+ }},
+
+ THROWABLE_MESSAGE('t') {
+ @Override
+ Object get(LogRecord record)
+ {
+ final Throwable t = record.getThrown();
+ return (t == null) ? "" : t.getMessage();
+ }},
+
+ THROWABLE_TRACE('T') {
+ @Override
+ Object get(LogRecord record)
+ {
+ String o = "";
+ final Throwable t = record.getThrown();
+ if (t != null)
+ {
+ StringWriter buffer = new StringWriter();
+ PrintWriter writer = new PrintWriter(buffer);
+ t.printStackTrace(writer);
+ writer.close();
+ o = buffer.toString();
+ }
+ return o;
+ }};
+
+ /** . */
+ private static final PatternElementType[] all = PatternElementType.values();
+
+ /** . */
+ public static final String LINE_SEPARATOR =
System.getProperty("line.separator");
+
+ /** . */
+ final char blah;
+
+ PatternElementType(char blah)
+ {
+ this.blah = blah;
+ }
+
+ abstract Object get(LogRecord record);
+
+ static Object[] getRecordValue(LogRecord record)
+ {
+ Object[] objs = new Object[all.length];
+ for (int i = 0;i < all.length;i++)
+ {
+ PatternElementType eltType = all[i];
+ objs[i] = eltType.get(record);
+ }
+ return objs;
+
+ }
+
+ static String computeMPFPattern(String format)
+ {
+ for (int i = 0;i < all.length;i++)
+ {
+ PatternElementType elt = all[i];
+ format = format.replaceAll("%" + elt.blah, "" + i);
+ }
+ return format;
+ }
+}
Added:
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternFormatter.java
===================================================================
---
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternFormatter.java
(rev 0)
+++
portal/trunk/component/test/core/src/main/java/org/exoplatform/component/test/logging/PatternFormatter.java 2010-02-28
18:10:35 UTC (rev 1902)
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.component.test.logging;
+
+import java.text.MessageFormat;
+import java.util.logging.Formatter;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+
+/**
+ * <p>A more flexible formatter than what is provided by the Java runtime. This log
formatter is used only for unit
+ * tests as it is not optimized for performances nor robustness.</p>
+ *
+ * <p>The formatter is parameterized with a pattern based on the pattern defined by
the {@link java.text.MessageFormat}
+ * class replacing the format elements by pattern element types. Each pattern element
type replaces an integer in the
+ * pattern string. A pattern element type symbolize an information computed from a {@link
LogRecord}
+ * object when one needs to be formatted.</p>
+ *
+ * <p>The existing pattern element types are:
+ * <ul>
+ * <li>l: the level returned by {@link LogRecord#getLevel()}</li>
+ * <li>m: the message returned by {@link LogRecord#getMessage()}</li>
+ * <li>t: the thread id returned by {@link LogRecord#getThreadID()}</li>
+ * <li>d: the number of milliseconds since 1970 returned by {@link
LogRecord#getMillis()}</li>
+ * <li>n: the logger name obtained as the simple name (in the sense of {@link
Class#getSimpleName()}) of the value
+ * returned by {@link LogRecord#getLoggerName()}</li>
+ * <li>N: the logger name returned by {@link LogRecord#getLoggerName()}</li>
+ * <li>c: the source class name obtained as the simple name (in the sense of {@link
Class#getSimpleName()}) of the value
+ * returned by {@link LogRecord#getSourceClassName()}</li>
+ * <li>N: the source class name returned by {@link
LogRecord#getSourceClassName()}</li>
+ * <li>t: the throwable message name returned by {@link LogRecord#getThrown()} when
it is not null or the empty string</li>
+ * <li>T: the formatted throwable stack trace returned by {@link
LogRecord#getThrown()} when it is not null or the empty string</li>
+ * </ul>
+ * </p>
+ *
+ * <p>The pattern is configurable by <i>pattern</p> property of the
formatter otheriwse the default pattern {@link #DEFAULT_PATTERN}
+ * is used.</p>.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class PatternFormatter extends Formatter
+{
+
+ /** . */
+ public static final String DEFAULT_PATTERN = "[{%d,date,HH:mm:ss-SSS}][{%l}] {%n}
{%m} {%T}";
+
+ /** . */
+ private final String mfPattern;
+
+ public PatternFormatter()
+ {
+ LogManager manager = LogManager.getLogManager();
+ String format = manager.getProperty(PatternFormatter.class.getName() +
".pattern");
+ if (format == null)
+ {
+ format = DEFAULT_PATTERN;
+ }
+ else
+ {
+ format = format.trim();
+ }
+ mfPattern = PatternElementType.computeMPFPattern(format);
+ }
+
+ @Override
+ public String format(LogRecord record)
+ {
+ MessageFormat mf = new MessageFormat(mfPattern);
+ Object[] objs = PatternElementType.getRecordValue(record);
+ return mf.format(objs) + PatternElementType.LINE_SEPARATOR;
+ }
+}
Added: portal/trunk/component/test/core/src/main/resources/gatein-logging.properties
===================================================================
--- portal/trunk/component/test/core/src/main/resources/gatein-logging.properties
(rev 0)
+++
portal/trunk/component/test/core/src/main/resources/gatein-logging.properties 2010-02-28
18:10:35 UTC (rev 1902)
@@ -0,0 +1,36 @@
+#
+# Copyright (C) 2009 eXo Platform SAS.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2.1 of
+# the License, or (at your option) any later version.
+#
+# This software is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this software; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+#
+
+handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
+
+# Console handler
+java.util.logging.ConsoleHandler.level = INFO
+java.util.logging.ConsoleHandler.formatter =
org.exoplatform.component.test.logging.PatternFormatter
+
+# File handler
+java.util.logging.FileHandler.level = ALL
+java.util.logging.FileHandler.formatter =
org.exoplatform.component.test.logging.PatternFormatter
+java.util.logging.FileHandler.pattern = ${basedir}/target/gateintest-%u.log
+
+# Default level is info
+.level = INFO
+
+# Configures GateIn logging as fine
+org.exoplatform.level = FINE
+org.gatein.level = FINE
Modified:
portal/trunk/component/test/organization/src/main/resources/conf/exo.portal.component.test.organization-configuration.xml
===================================================================
---
portal/trunk/component/test/organization/src/main/resources/conf/exo.portal.component.test.organization-configuration.xml 2010-02-27
22:03:28 UTC (rev 1901)
+++
portal/trunk/component/test/organization/src/main/resources/conf/exo.portal.component.test.organization-configuration.xml 2010-02-28
18:10:35 UTC (rev 1902)
@@ -135,7 +135,7 @@
<name>ref-addresses</name>
<description>ref-addresses</description>
<property name="driverClassName"
value="org.hsqldb.jdbcDriver"/>
- <property name="url"
value="jdbc:hsqldb:file:${gatein.test.tmp.dir}/idm"/>
+ <property name="url"
value="jdbc:hsqldb:file:${gatein.test.tmp.dir}/idmdb"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</properties-param>
Modified: portal/trunk/pom.xml
===================================================================
--- portal/trunk/pom.xml 2010-02-27 22:03:28 UTC (rev 1901)
+++ portal/trunk/pom.xml 2010-02-28 18:10:35 UTC (rev 1902)
@@ -110,6 +110,16 @@
<groupId>org.exoplatform.kernel</groupId>
<artifactId>exo.kernel.commons</artifactId>
<version>${org.exoplatform.kernel.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.exoplatform.kernel</groupId>
@@ -125,11 +135,31 @@
<groupId>org.exoplatform.kernel</groupId>
<artifactId>exo.kernel.component.common</artifactId>
<version>${org.exoplatform.kernel.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.exoplatform.kernel</groupId>
<artifactId>exo.kernel.container</artifactId>
<version>${org.exoplatform.kernel.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.exoplatform.core</groupId>
@@ -140,6 +170,16 @@
<groupId>org.exoplatform.core</groupId>
<artifactId>exo.core.component.database</artifactId>
<version>${org.exoplatform.core.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.exoplatform.core</groupId>
@@ -150,6 +190,16 @@
<groupId>org.exoplatform.core</groupId>
<artifactId>exo.core.component.organization.jdbc</artifactId>
<version>${org.exoplatform.core.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.exoplatform.core</groupId>
@@ -170,6 +220,16 @@
<groupId>org.exoplatform.jcr</groupId>
<artifactId>exo.jcr.component.ext</artifactId>
<version>${org.exoplatform.jcr.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.exoplatform.ws</groupId>
@@ -292,6 +352,16 @@
<groupId>org.picketlink.idm</groupId>
<artifactId>picketlink-idm-hibernate</artifactId>
<version>${org.picketlink.idm}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.picketlink.idm</groupId>
@@ -483,7 +553,7 @@
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
- <artifactId>slf4j-simple</artifactId>
+ <artifactId>slf4j-jdk14</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
@@ -551,6 +621,12 @@
<name>java.naming.factory.initial</name>
<value>org.exoplatform.services.naming.SimpleContextFactory</value>
</property>
+
+ <property>
+ <name>java.util.logging.config.class</name>
+
<value>org.exoplatform.component.test.LogConfigurator</value>
+ </property>
+
</systemProperties>
</configuration>
</plugin>
@@ -580,18 +656,6 @@
</execution>
</executions>
</plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <systemProperties>
- <property>
-
<name>com.arjuna.ats.arjuna.objectstore.objectStoreDir</name>
- <value>${project.build.directory}</value>
- </property>
- </systemProperties>
- </configuration>
- </plugin>
</plugins>
</build>
@@ -616,6 +680,10 @@
<configuration>
<systemProperties>
<property>
+ <name>java.util.logging.config.class</name>
+
<value>org.exoplatform.component.test.logging.LogConfigurator</value>
+ </property>
+ <property>
<name>com.arjuna.ats.arjuna.objectstore.objectStoreDir</name>
<value>${project.build.directory}</value>
</property>
@@ -625,7 +693,7 @@
</property>
<property>
<name>gatein.test.datasource.url</name>
-
<value>jdbc:hsqldb:file:${project.build.directory}/idm</value>
+
<value>jdbc:hsqldb:file:${project.build.directory}/gateindb</value>
</property>
<property>
<name>gatein.test.datasource.username</name>
@@ -659,6 +727,10 @@
<configuration>
<systemProperties>
<property>
+ <name>java.util.logging.config.class</name>
+
<value>org.exoplatform.component.test.logging.LogConfigurator</value>
+ </property>
+ <property>
<name>com.arjuna.ats.arjuna.objectstore.objectStoreDir</name>
<value>${project.build.directory}</value>
</property>