KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > test > DBTargetTestCase


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

17 package org.apache.log.output.test;
18
19 import junit.framework.TestCase;
20 import org.apache.log.Hierarchy;
21 import org.apache.log.LogTarget;
22 import org.apache.log.Logger;
23 import org.apache.log.output.db.ColumnInfo;
24 import org.apache.log.output.db.ColumnType;
25 import org.apache.log.output.db.DefaultDataSource;
26 import org.apache.log.output.db.DefaultJDBCTarget;
27 import org.apache.log.output.db.NormalizedJDBCTarget;
28
29 /**
30  * Test suite for the DB output target.
31  *
32  * @author Peter Donald
33  */

34 public final class DBTargetTestCase
35     extends TestCase
36 {
37     private String JavaDoc m_connectString;
38     private String JavaDoc m_userName;
39     private String JavaDoc m_userPassword;
40     private boolean m_doDBTest;
41
42     public DBTargetTestCase( final String JavaDoc name )
43         throws Exception JavaDoc
44     {
45         super( name );
46     }
47
48     public void setUp() throws Exception JavaDoc
49     {
50         String JavaDoc driverName = System.getProperty("test.db.driver");
51         m_connectString = System.getProperty("test.db.jdbc", "");
52         m_userName = System.getProperty( "test.db.user", "" );
53         m_userPassword = System.getProperty( "test.db.pword", "" );
54         m_doDBTest = System.getProperty( "test.db.run", "false" ).equalsIgnoreCase("true");
55
56         if (m_doDBTest)
57         {
58             Class.forName(driverName);
59         }
60         else
61         {
62             System.out.println("[WARNING] Database Testing is not being done");
63             System.out.println();
64             System.out.println("To enable database testing, please provide the");
65             System.out.println("following properties:");
66             System.out.println();
67             System.out.println( "test.db.driver -> Class name for the JDBC driver" );
68             System.out.println( "test.db.jdbc -> JDBC connect string" );
69             System.out.println( "test.db.user -> User ID" );
70             System.out.println( "test.db.pword -> User password" );
71             System.out.println( "test.db.run -> \"true\"" );
72         }
73     }
74
75     public void testBasicTarget()
76         throws Exception JavaDoc
77     {
78         if (! m_doDBTest) return;
79
80         final DefaultDataSource dataSource =
81             new DefaultDataSource( m_connectString, m_userName, m_userPassword );
82
83         final ColumnInfo[] columns =
84             {
85                 new ColumnInfo( "TIME", ColumnType.TIME, null ),
86                 new ColumnInfo( "PRIORITY", ColumnType.PRIORITY, null ),
87                 new ColumnInfo( "CATEGORY", ColumnType.CATEGORY, null ),
88                 new ColumnInfo( "HOSTNAME", ColumnType.STATIC, "helm.realityforge.net" ),
89                 new ColumnInfo( "MESSAGE", ColumnType.MESSAGE, null )
90             };
91
92         final DefaultJDBCTarget target =
93             new DefaultJDBCTarget( dataSource, "log_entrys", columns );
94
95         final Logger logger = getNewLogger( target );
96         logger.debug( "Hello" );
97     }
98
99     public void testNumericConstants()
100         throws Exception JavaDoc
101     {
102         if ( !m_doDBTest ) return;
103
104         final DefaultDataSource dataSource =
105                 new DefaultDataSource( m_connectString, m_userName, m_userPassword );
106
107         final ColumnInfo[] columns =
108             {
109                 new ColumnInfo( "TIME", ColumnType.TIME, null ),
110                 new ColumnInfo( "PRIORITY", ColumnType.PRIORITY, null ),
111                 new ColumnInfo( "CATEGORY", ColumnType.CATEGORY, null ),
112                 new ColumnInfo( "HOSTNAME", ColumnType.STATIC, "helm.realityforge.net" ),
113                 new ColumnInfo( "MESSAGE", ColumnType.MESSAGE, null )
114             };
115
116         final NormalizedJDBCTarget target =
117             new NormalizedJDBCTarget( dataSource, "log_entrys2", columns );
118
119         final Logger logger = getNewLogger( target );
120         logger.debug( "Hello" );
121         logger.info( "Hello info" );
122         logger.error( "Hello error" );
123         logger.fatalError( "Hello fatalError" );
124     }
125
126     private Logger getNewLogger( final LogTarget target )
127     {
128         final Hierarchy hierarchy = new Hierarchy();
129         final Logger logger = hierarchy.getLoggerFor( "myCategory" );
130         logger.setLogTargets( new LogTarget[]{target} );
131         return logger;
132     }
133 }
134
Popular Tags