1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
35
36 public class ExternalLoggerTestCase extends TestCase implements LogChute
37 {
38
39 private String logString = null;
40 private VelocityEngine ve = null;
41
42
43
44
45 public ExternalLoggerTestCase(String name)
46 {
47 super(name);
48 }
49
50 public void setUp()
51 throws Exception
52 {
53
54
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
65 }
66
67 public static Test suite ()
68 {
69 return new TestSuite(ExternalLoggerTestCase.class);
70 }
71
72
73
74
75 public void testExternalLogger ()
76 {
77
78
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
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
128 log(level, message);
129 }
130
131 public boolean isLevelEnabled(int level)
132 {
133 return true;
134 }
135 }