001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.velocity.app.VelocityEngine;
027: import org.apache.velocity.runtime.RuntimeServices;
028: import org.apache.velocity.runtime.log.LogChute;
029:
030: /**
031: * Tests if we can hand Velocity an arbitrary class for logging.
032: *
033: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
034: * @version $Id: ExternalLoggerTestCase.java 463298 2006-10-12 16:10:32Z henning $
035: */
036: public class ExternalLoggerTestCase extends TestCase implements
037: LogChute {
038:
039: private String logString = null;
040: private VelocityEngine ve = null;
041:
042: /**
043: * Default constructor.
044: */
045: public ExternalLoggerTestCase(String name) {
046: super (name);
047: }
048:
049: public void setUp() throws Exception {
050: /*
051: * use an alternative logger. Set it up here and pass it in.
052: */
053:
054: ve = new VelocityEngine();
055: ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
056: ve.init();
057: }
058:
059: public void init(RuntimeServices rs) {
060: // do nothing with it
061: }
062:
063: public static Test suite() {
064: return new TestSuite(ExternalLoggerTestCase.class);
065: }
066:
067: /**
068: * Runs the test.
069: */
070: public void testExternalLogger() {
071: /*
072: * simply log something and see if we get it.
073: */
074:
075: logString = null;
076:
077: String testString = "This is a test.";
078:
079: ve.getLog().warn(testString);
080:
081: if (logString == null
082: || !logString.equals(WARN_PREFIX + testString)) {
083: fail("Didn't recieve log message.");
084: }
085: }
086:
087: public void log(int level, String message) {
088: String out = "";
089:
090: /*
091: * Start with the appropriate prefix
092: */
093: switch (level) {
094: case DEBUG_ID:
095: out = DEBUG_PREFIX;
096: break;
097: case INFO_ID:
098: out = INFO_PREFIX;
099: break;
100: case TRACE_ID:
101: out = TRACE_PREFIX;
102: break;
103: case WARN_ID:
104: out = WARN_PREFIX;
105: break;
106: case ERROR_ID:
107: out = ERROR_PREFIX;
108: break;
109: default:
110: out = INFO_PREFIX;
111: break;
112: }
113:
114: logString = out + message;
115: }
116:
117: public void log(int level, String message, Throwable t) {
118: // ignore the Throwable, we're not testing this method here
119: log(level, message);
120: }
121:
122: public boolean isLevelEnabled(int level) {
123: return true;
124: }
125: }
|