KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > log > TestLog


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package se.anatom.ejbca.log;
15
16 import java.util.Collection JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.log4j.Logger;
26 import org.ejbca.core.ejb.log.ILogSessionHome;
27 import org.ejbca.core.ejb.log.ILogSessionRemote;
28 import org.ejbca.core.model.log.Admin;
29 import org.ejbca.core.model.log.CsvLogExporter;
30 import org.ejbca.core.model.log.ILogExporter;
31 import org.ejbca.core.model.log.LogConfiguration;
32 import org.ejbca.core.model.log.LogEntry;
33 import org.ejbca.util.query.BasicMatch;
34 import org.ejbca.util.query.LogMatch;
35 import org.ejbca.util.query.Query;
36
37 /**
38  * Tests the log modules entity and session beans.
39  *
40  * @version $Id: TestLog.java,v 1.6 2006/12/20 08:35:06 anatom Exp $
41  */

42 public class TestLog extends TestCase {
43     private static Logger log = Logger.getLogger(TestLog.class);
44
45     private ILogSessionRemote cacheAdmin;
46
47     private static ILogSessionHome cacheHome;
48
49     private Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
50
51     /**
52      * Creates a new TestLog object.
53      *
54      * @param name name
55      */

56     public TestLog(String JavaDoc name) {
57         super(name);
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61
62         log.debug(">setUp()");
63
64         if (cacheAdmin == null) {
65             if (cacheHome == null) {
66                 Context JavaDoc jndiContext = getInitialContext();
67                 Object JavaDoc obj1 = jndiContext.lookup("LogSession");
68                 cacheHome = (ILogSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, ILogSessionHome.class);
69
70             }
71
72             cacheAdmin = cacheHome.create();
73         }
74
75
76         log.debug("<setUp()");
77     }
78
79     protected void tearDown() throws Exception JavaDoc {
80     }
81
82     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
83         log.debug(">getInitialContext");
84
85         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
86         log.debug("<getInitialContext");
87
88         return ctx;
89     }
90
91
92     /**
93      * tests adding a log configuration and checks if it can be read again.
94      *
95      * @throws Exception error
96      */

97     public void test01AddLogConfiguration() throws Exception JavaDoc {
98         log.debug(">test01AddLogConfiguration()");
99
100         LogConfiguration logconf = new LogConfiguration();
101         logconf.setLogEvent(LogEntry.EVENT_INFO_DATABASE, false);
102         logconf.setLogEvent(LogEntry.EVENT_ERROR_DATABASE, true);
103
104         cacheAdmin.saveLogConfiguration(admin, "CN=TEST".hashCode(), logconf);
105
106         LogConfiguration logconf2 = cacheAdmin.loadLogConfiguration("CN=TEST".hashCode());
107         assertTrue("Couldn't retrieve correct log confirguration data from database.", !logconf2.getLogEvent(LogEntry.EVENT_INFO_DATABASE).booleanValue());
108         assertTrue("Couldn't retrieve correct log confirguration data from database.", logconf2.getLogEvent(LogEntry.EVENT_ERROR_DATABASE).booleanValue());
109
110         log.debug("<test01AddLogConfiguration()");
111     }
112
113     /**
114      * tests adds some log events and checks that they have been stored
115      * correctly.
116      *
117      * @throws Exception error
118      */

119     public void test02AddAndCheckLogEvents() throws Exception JavaDoc {
120         log.debug(">test02AddAndCheckLogEvents()");
121
122         cacheAdmin.log(admin, "CN=TEST".hashCode(), LogEntry.MODULE_LOG, new Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_UNKNOWN, "Test");
123
124
125         Query query = new Query(Query.TYPE_LOGQUERY);
126         query.add(LogMatch.MATCH_WITH_COMMENT,BasicMatch.MATCH_TYPE_EQUALS,"Test");
127         Collection JavaDoc result = cacheAdmin.query(query, "", "caid=" + Integer.toString("CN=TEST".hashCode()));
128         Iterator JavaDoc iter = result.iterator();
129         boolean found = false;
130         while (iter.hasNext()) {
131             LogEntry entry = (LogEntry) iter.next();
132             if ( (entry.getComment() != null) && (entry.getComment().equals("Test")) ) {
133                 found = true;
134             }
135         }
136         assertTrue("Couldn't retrieve correct log data from database.", found);
137         
138        ILogExporter exporter = new CsvLogExporter();
139        exporter.setEntries(result);
140        byte[] export = exporter.export();
141        assertNotNull(export);
142        String JavaDoc str = new String JavaDoc(export);
143        //assertEquals("foo", str);
144
int ind = str.indexOf("Test\t");
145        assertTrue(ind > 0);
146
147         log.debug("<test02AddAndCheckLogEvents()");
148     }
149
150
151 }
152
Popular Tags