001: /*
002: * Copyright 2006 Holger West, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package log4j;
017:
018: import java.net.URL;
019: import java.sql.Connection;
020: import java.sql.Statement;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.log4j.xml.DOMConfigurator;
025: import org.exolab.castor.jdo.Database;
026: import org.exolab.castor.jdo.JDOManager;
027:
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: /**
033: * Test for <code>CastorAppender</code> to showcase its usage.
034: *
035: * @author <a href="mailto:holger.west@syscon-informatics.de">Holger West</a>
036: */
037: public final class TestCastorAppender extends TestCase {
038: // -----------------------------------------------------------------------------------
039:
040: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
041: * Logging </a> instance used for all logging. */
042: private static final Log LOG = LogFactory
043: .getLog(TestCastorAppender.class);
044:
045: /** Example JDO configuration. */
046: private static final String JDO_CONF = "/log4j/example-jdo-conf.xml";
047:
048: /** Example LOG4J configuration. */
049: private static final String XML_CONF = "/log4j/example-log4j.xml";
050:
051: // -----------------------------------------------------------------------------------
052:
053: /**
054: * Initialize LOG4J logging with example configuration and create JUNIT test suite.
055: *
056: * @return JUNIT test suite.
057: */
058: public static Test suite() {
059: // load specific resource for testing
060: URL url = TestCastorAppender.class.getResource(XML_CONF);
061: DOMConfigurator.configure(url);
062:
063: TestSuite suite = new TestSuite("CastorAppender tests");
064:
065: suite.addTest(new TestCastorAppender("testAppender"));
066:
067: return suite;
068: }
069:
070: // -----------------------------------------------------------------------------------
071:
072: /**
073: * Constructs a new TestCase with the given name.
074: *
075: * @param name The name for the test.
076: */
077: public TestCastorAppender(final String name) {
078: super (name);
079: }
080:
081: // -----------------------------------------------------------------------------------
082:
083: public void setUp() throws Exception {
084: JDOManager.loadConfiguration(TestCastorAppender.class
085: .getResource(JDO_CONF).toString());
086: JDOManager jdo = JDOManager.createInstance("LOGGING");
087: Database db = jdo.getDatabase();
088: db.begin();
089: Connection connection = db.getJdbcConnection();
090: Statement statement = connection.createStatement();
091: statement.executeUpdate("DROP TABLE LOG_EXTENSION");
092: statement.executeUpdate("DROP TABLE LOG_EXCEPTION");
093: statement.executeUpdate("DROP TABLE LOG");
094:
095: statement
096: .executeUpdate("CREATE TABLE LOG (LOG_ID INT NOT NULL, "
097: + " LOG_TIMESTAMP TIMESTAMP NOT NULL, "
098: + " LOG_CLASS VARCHAR ( 100) NOT NULL, "
099: + " LOG_LEVEL VARCHAR ( 10) NOT NULL, "
100: + " LOG_THREAD VARCHAR ( 100) NOT NULL, "
101: + " LOG_MESSAGE VARCHAR (1000) DEFAULT NULL, "
102: + " LOG_COUNT INT NOT NULL)");
103: statement
104: .executeUpdate("ALTER TABLE LOG ADD PRIMARY KEY (LOG_ID)");
105: statement.executeUpdate("CREATE TABLE LOG_EXCEPTION ( "
106: + " LOGE_ID INT NOT NULL, "
107: + " LOGE_LOG_ID INT NOT NULL, "
108: + " LOGE_STACKTRACE BLOB NOT NULL)");
109: statement
110: .executeUpdate("ALTER TABLE LOG_EXCEPTION ADD PRIMARY KEY (LOGE_ID)");
111: statement
112: .executeUpdate("ALTER TABLE LOG_EXCEPTION ADD CONSTRAINT FK_LOGE_LOG_ID"
113: + " FOREIGN KEY (LOGE_LOG_ID) REFERENCES LOG (LOG_ID)");
114: statement
115: .executeUpdate("CREATE TABLE LOG_EXTENSION (LOGX_LOG_ID INT NOT NULL, LOGX_TYPE VARCHAR(100) NOT NULL, LOGX_VALUE VARCHAR(100) NOT NULL)");
116: statement
117: .executeUpdate("ALTER TABLE LOG_EXTENSION ADD PRIMARY KEY (LOGX_LOG_ID)");
118: statement
119: .executeUpdate("ALTER TABLE LOG_EXTENSION ADD CONSTRAINT FK_LOGX_LOG_ID "
120: + " FOREIGN KEY (LOGX_LOG_ID) REFERENCES LOG (LOG_ID)");
121:
122: statement.executeQuery("select count(*) from LOG");
123: db.commit();
124: db.close();
125: }
126:
127: /**
128: * Test CastorAppender for LOG4J.
129: *
130: * @throws Exception If anything went wrong in the test.
131: */
132: public void testAppender() throws Exception {
133: // JDOManager.loadConfiguration(
134: // TestCastorAppender.class.getResource(JDO_CONF).toString());
135:
136: try {
137: Integer.parseInt("cc");
138: } catch (Exception e) {
139: LOG.error("exception", e);
140: }
141: LOG.error(null);
142: LOG.warn("This is only a message");
143:
144: LogReferenceExtension ext = new LogReferenceExtension();
145: ext.setMessage("this is an extension");
146: ext.setType(this .getClass().toString());
147: ext.setValue("123456789");
148: LOG.error(ext);
149:
150: // flush the buffer before exit !!!!
151: CastorAppender.flush();
152: }
153:
154: // -----------------------------------------------------------------------------------
155: }
|