1 13 14 package se.anatom.ejbca.log; 15 16 import java.util.Collection ; 17 import java.util.Date ; 18 import java.util.Iterator ; 19 20 import javax.naming.Context ; 21 import javax.naming.NamingException ; 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 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 56 public TestLog(String name) { 57 super(name); 58 } 59 60 protected void setUp() throws Exception { 61 62 log.debug(">setUp()"); 63 64 if (cacheAdmin == null) { 65 if (cacheHome == null) { 66 Context jndiContext = getInitialContext(); 67 Object 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 { 80 } 81 82 private Context getInitialContext() throws NamingException { 83 log.debug(">getInitialContext"); 84 85 Context ctx = new javax.naming.InitialContext (); 86 log.debug("<getInitialContext"); 87 88 return ctx; 89 } 90 91 92 97 public void test01AddLogConfiguration() throws Exception { 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 119 public void test02AddAndCheckLogEvents() throws Exception { 120 log.debug(">test02AddAndCheckLogEvents()"); 121 122 cacheAdmin.log(admin, "CN=TEST".hashCode(), LogEntry.MODULE_LOG, new Date (), 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 result = cacheAdmin.query(query, "", "caid=" + Integer.toString("CN=TEST".hashCode())); 128 Iterator 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 str = new String (export); 143 int ind = str.indexOf("Test\t"); 145 assertTrue(ind > 0); 146 147 log.debug("<test02AddAndCheckLogEvents()"); 148 } 149 150 151 } 152 | Popular Tags |