1 package org.apache.velocity.test;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.velocity.app.VelocityEngine;
27 import org.apache.velocity.runtime.RuntimeServices;
28 import org.apache.velocity.runtime.log.LogChute;
29
30 /**
31 * Tests if we can hand Velocity an arbitrary class for logging.
32 *
33 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
34 * @version $Id: ExternalLoggerTestCase.java 463298 2006-10-12 16:10:32Z henning $
35 */
36 public class ExternalLoggerTestCase extends TestCase implements LogChute
37 {
38
39 private String logString = null;
40 private VelocityEngine ve = null;
41
42 /**
43 * Default constructor.
44 */
45 public ExternalLoggerTestCase(String name)
46 {
47 super(name);
48 }
49
50 public void setUp()
51 throws Exception
52 {
53 /*
54 * use an alternative logger. Set it up here and pass it in.
55 */
56
57 ve = new VelocityEngine();
58 ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
59 ve.init();
60 }
61
62 public void init( RuntimeServices rs )
63 {
64 // do nothing with it
65 }
66
67 public static Test suite ()
68 {
69 return new TestSuite(ExternalLoggerTestCase.class);
70 }
71
72 /**
73 * Runs the test.
74 */
75 public void testExternalLogger ()
76 {
77 /*
78 * simply log something and see if we get it.
79 */
80
81 logString = null;
82
83 String testString = "This is a test.";
84
85 ve.getLog().warn(testString);
86
87 if (logString == null || !logString.equals(WARN_PREFIX + testString ) )
88 {
89 fail("Didn't recieve log message.");
90 }
91 }
92
93 public void log(int level, String message)
94 {
95 String out = "";
96
97 /*
98 * Start with the appropriate prefix
99 */
100 switch( level )
101 {
102 case DEBUG_ID :
103 out = DEBUG_PREFIX;
104 break;
105 case INFO_ID :
106 out = INFO_PREFIX;
107 break;
108 case TRACE_ID :
109 out = TRACE_PREFIX;
110 break;
111 case WARN_ID :
112 out = WARN_PREFIX;
113 break;
114 case ERROR_ID :
115 out = ERROR_PREFIX;
116 break;
117 default :
118 out = INFO_PREFIX;
119 break;
120 }
121
122 logString = out + message;
123 }
124
125 public void log(int level, String message, Throwable t)
126 {
127 // ignore the Throwable, we're not testing this method here
128 log(level, message);
129 }
130
131 public boolean isLevelEnabled(int level)
132 {
133 return true;
134 }
135 }