Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
85   157   10   8.5
0   119   0.12   10
10     1  
1    
 
  TraceListenerTest       Line # 33 85 10 100% 1.0
 
  (7)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.bo.log;
17   
18    import java.io.ByteArrayOutputStream;
19    import java.io.UnsupportedEncodingException;
20   
21    import org.apache.log4j.Level;
22    import org.apache.log4j.Logger;
23    import org.apache.log4j.PatternLayout;
24    import org.apache.log4j.WriterAppender;
25    import org.qedeq.base.trace.Trace;
26    import org.qedeq.kernel.bo.test.QedeqBoTestCase;
27   
28    /**
29    * Test class.
30    *
31    * @author Michael Meyling
32    */
 
33    public class TraceListenerTest extends QedeqBoTestCase {
34   
35    private TraceListener listener;
36    private ByteArrayOutputStream out = new ByteArrayOutputStream();
37    private boolean oldTrace;
38   
 
39  7 toggle protected void setUp() throws Exception {
40  7 super.setUp();
41  7 out.reset();
42  7 Logger rootLogger = Logger.getRootLogger();
43  7 rootLogger.removeAllAppenders();
44  7 rootLogger.setLevel(Level.DEBUG);
45  7 rootLogger.addAppender(new WriterAppender(
46    new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN), out));
47  7 oldTrace = Trace.isTraceOn();
48  7 Trace.setTraceOn(true);
49  7 listener = new TraceListener();
50  7 QedeqLog.getInstance().addLog(listener);
51    // LogFactory.getFactory().getInstance("log");
52    }
53   
 
54  7 toggle protected void tearDown() throws Exception {
55  7 QedeqLog.getInstance().removeLog(listener);
56  7 Trace.setTraceOn(oldTrace);
57  7 super.tearDown();
58    }
59   
 
60  1 toggle public void testLogFailureReply() throws Exception{
61  1 listener.logFailureReply("failure and error", "http://mysite.org/mymodule.xml", "was this logged?");
62  1 String out = getLogfileContents();
63  1 assertTrue(out.trim().endsWith("was this logged?"));
64  1 assertTrue(0 <= out.indexOf("reply"));
65  1 assertTrue(0 <= out.indexOf("failure and error"));
66  1 assertTrue(0 <= out.indexOf("http://mysite.org/mymodule.xml"));
67  1 int pos = out.length();
68  1 listener.logFailureReply("failure and error", "http://mysite.org/mymodule.xml", "this is the end");
69  1 out = getLogfileContents();
70  1 assertTrue(out.trim().endsWith("this is the end"));
71  1 assertTrue(pos <= out.indexOf("reply", pos));
72  1 assertTrue(pos <= out.indexOf("failure and error", pos));
73  1 assertTrue(-1 == out.indexOf("http://mysite.org/mymodule.xml", pos));
74    }
75   
 
76  14 toggle private String getLogfileContents() throws UnsupportedEncodingException {
77  14 return out.toString("UTF-8");
78    }
79   
 
80  1 toggle public void testLogSuccessfulReply() throws Exception{
81  1 listener.logSuccessfulReply("successfully loaded", "http://mysite.org/mymodule.xml");
82  1 String out = getLogfileContents();
83  1 assertTrue(0 <= out.indexOf("successfully loaded"));
84  1 assertTrue(0 <= out.indexOf("http://mysite.org/mymodule.xml"));
85  1 int pos = out.length();
86  1 listener.logSuccessfulReply("successfully loaded", "http://mysite.org/mymodule.xml");
87  1 out = getLogfileContents();
88  1 assertTrue(-1 == out.indexOf("http://mysite.org/mymodule.xml", pos));
89  1 assertTrue(pos <= out.indexOf("successfully loaded", pos));
90    }
91   
 
92  1 toggle public void testLogMessageState() throws Exception{
93  1 listener.logMessageState("loading 10 percent complete", "http://mysite.org/mymodule.xml");
94  1 String out = getLogfileContents();
95  1 assertTrue(0 <= out.indexOf("loading 10 percent complete"));
96  1 assertTrue(0 <= out.indexOf("http://mysite.org/mymodule.xml"));
97    // System.out.println(out);
98  1 int pos = out.length();
99  1 listener.logMessageState("loading 20 percent complete", "http://mysite.org/mymodule.xml");
100  1 out = getLogfileContents();
101  1 assertTrue(pos <= out.indexOf("loading 20 percent complete", pos));
102  1 assertTrue(-1 == out.indexOf("http://mysite.org/mymodule.xml", pos));
103    }
104   
 
105  1 toggle public void testLogFailureState() throws Exception{
106  1 listener.logFailureState("error state occured", "http://mysite.org/mymodule.xml", "was this logged?");
107  1 String out = getLogfileContents();
108  1 assertTrue(out.trim().endsWith("was this logged?"));
109  1 assertTrue(0 <= out.indexOf("failure"));
110  1 assertTrue(0 <= out.indexOf("error state occured"));
111  1 assertTrue(0 <= out.indexOf("http://mysite.org/mymodule.xml"));
112  1 int pos = out.length();
113  1 listener.logFailureState("error state occured", "http://mysite.org/mymodule.xml", "was this logged!");
114  1 out = getLogfileContents();
115  1 assertTrue(out.trim().endsWith("was this logged!"));
116  1 assertTrue(pos <= out.indexOf("failure", pos));
117  1 assertTrue(pos <= out.indexOf("error state occured", pos));
118  1 assertTrue(-1 == out.indexOf("http://mysite.org/mymodule.xml", pos));
119    }
120   
 
121  1 toggle public void testLogSuccesfulState() throws Exception{
122  1 listener.logSuccessfulState("we are the champions", "http://mysite.org/mymodule.xml");
123  1 String out = getLogfileContents();
124  1 assertTrue(0 <= out.indexOf("success"));
125  1 assertTrue(0 <= out.indexOf("we are the champions"));
126  1 assertTrue(0 <= out.indexOf("http://mysite.org/mymodule.xml"));
127  1 int pos = out.length();
128  1 listener.logSuccessfulState("rain begins to fall", "http://mysite.org/mymodule.xml");
129  1 out = getLogfileContents();
130  1 assertTrue(pos <= out.indexOf("success", pos));
131  1 assertTrue(pos <= out.indexOf("rain begins to fall", pos));
132  1 assertTrue(-1 == out.indexOf("http://mysite.org/mymodule.xml", pos));
133    }
134   
 
135  1 toggle public void testLogRequest() throws Exception{
136  1 listener.logRequest("validating", "http://mysite.org/mymodule.xml");
137  1 String out = getLogfileContents();
138  1 assertTrue(0 <= out.indexOf("validating"));
139  1 assertTrue(0 <= out.indexOf("http://mysite.org/mymodule.xml"));
140  1 int pos = out.length();
141  1 listener.logRequest("validated", "http://mysite.org/mymodule.xml");
142  1 out = getLogfileContents();
143  1 assertTrue(pos <= out.indexOf("validated", pos));
144  1 assertTrue(-1 == out.indexOf("http://mysite.org/mymodule.xml", pos));
145    }
146   
 
147  1 toggle public void testLogMessage() throws Exception{
148  1 listener.logMessage("we are on a yellow submarine");
149  1 String out = getLogfileContents();
150  1 assertTrue(0 <= out.indexOf("we are on a yellow submarine"));
151  1 int pos = out.length();
152  1 listener.logMessage("we are on a blue submarine");
153  1 out = getLogfileContents();
154  1 assertTrue(pos <= out.indexOf("we are on a blue submarine", pos));
155    }
156   
157    }