KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > ExternalLoggerTest


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.velocity.app.VelocityEngine;
20 import org.apache.velocity.runtime.RuntimeServices;
21
22 import org.apache.velocity.runtime.log.LogSystem;
23
24 import junit.framework.TestCase;
25
26 /**
27  * Tests if we can hand Velocity an arbitrary class for logging.
28  *
29  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
30  * @version $Id: ExternalLoggerTest.java,v 1.4.10.1 2004/03/03 23:23:04 geirm Exp $
31  */

32 public class ExternalLoggerTest extends TestCase implements LogSystem
33 {
34    
35     private String JavaDoc logString = null;
36     private VelocityEngine ve = null;
37
38     /**
39      * Default constructor.
40      */

41     public ExternalLoggerTest()
42     {
43         super("LoggerTest");
44
45         try
46         {
47             /*
48              * use an alternative logger. Set it up here and pass it in.
49              */

50             
51             ve = new VelocityEngine();
52             ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
53             ve.init();
54         }
55         catch (Exception JavaDoc e)
56         {
57             System.err.println("Cannot setup LoggerTest : " + e);
58             System.exit(1);
59         }
60     }
61
62     public void init( RuntimeServices rs )
63     {
64         // do nothing with it
65
}
66
67     public static junit.framework.Test suite ()
68     {
69         return new ExternalLoggerTest();
70     }
71
72     /**
73      * Runs the test.
74      */

75     public void runTest ()
76     {
77         /*
78          * simply log something and see if we get it.
79          */

80
81         logString = null;
82
83         String JavaDoc testString = "This is a test.";
84
85         ve.warn( testString );
86
87         if (logString == null || !logString.equals( VelocityEngine.WARN_PREFIX + testString ) )
88         {
89             fail("Didn't recieve log message.");
90         }
91     }
92
93     public void logVelocityMessage(int level, String JavaDoc message)
94     {
95         String JavaDoc out = "";
96
97         /*
98          * Start with the appropriate prefix
99          */

100         switch( level )
101         {
102             case LogSystem.DEBUG_ID :
103                 out = VelocityEngine.DEBUG_PREFIX;
104                 break;
105             case LogSystem.INFO_ID :
106                 out = VelocityEngine.INFO_PREFIX;
107                 break;
108             case LogSystem.WARN_ID :
109                 out = VelocityEngine.WARN_PREFIX;
110                 break;
111             case LogSystem.ERROR_ID :
112                 out = VelocityEngine.ERROR_PREFIX;
113                 break;
114             default :
115                 out = VelocityEngine.UNKNOWN_PREFIX;
116                 break;
117         }
118
119         logString = out + message;
120     }
121 }
122
Popular Tags