| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons; |
|---|
| 34 | |
|---|
| 35 | import java.util.logging.Level; |
|---|
| 36 | import java.util.logging.Logger; |
|---|
| 37 | import junit.framework.TestCase; |
|---|
| 38 | |
|---|
| 39 | import org.jcoderz.commons.test.TssLogMessage; |
|---|
| 40 | import org.jcoderz.commons.types.Date; |
|---|
| 41 | import org.xml.sax.SAXException; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * JUnit test to demonstrate the different outputs of |
|---|
| 47 | * the {@link org.jcoderz.commons.LogFormatter}. |
|---|
| 48 | * |
|---|
| 49 | * @author Michael Griffel |
|---|
| 50 | */ |
|---|
| 51 | public class LogFormatterOutputTest |
|---|
| 52 | extends TestCase |
|---|
| 53 | { |
|---|
| 54 | private static final String CLASSNAME |
|---|
| 55 | = LogFormatterOutputTest.class.getName(); |
|---|
| 56 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Produces a log message from an exception w/ two parameters. |
|---|
| 60 | */ |
|---|
| 61 | public void testLogEvent () |
|---|
| 62 | { |
|---|
| 63 | TstLogMessage.TestMessage.log("foo", Date.now().toUtilDate()); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Produces a log message from an exception w/ two parameters. |
|---|
| 68 | */ |
|---|
| 69 | public void testLogEventWithImpliedParams () |
|---|
| 70 | { |
|---|
| 71 | TssLogMessage.ImpliedParametersLog.log(new RuntimeException("Foo!")); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Produces a log message from an exception w/ two parameters and exception |
|---|
| 76 | * as cause. |
|---|
| 77 | */ |
|---|
| 78 | public void testLogException () |
|---|
| 79 | { |
|---|
| 80 | TstLogMessage.TestMessage.log("foo", Date.now().toUtilDate(), |
|---|
| 81 | new Exception("This is the top level exception")); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Produces a log message from a standard log record with a Throwable. |
|---|
| 86 | * |
|---|
| 87 | */ |
|---|
| 88 | public void testLogThrowable () |
|---|
| 89 | { |
|---|
| 90 | final Throwable th = new Exception ( |
|---|
| 91 | "This is a top level test exception"); |
|---|
| 92 | logger.logp(Level.SEVERE, CLASSNAME, "logException", "Logging exception", |
|---|
| 93 | th); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Produces a log message from a standard log record with a nested Throwable. |
|---|
| 98 | */ |
|---|
| 99 | public void testLogNestedThrowable () |
|---|
| 100 | { |
|---|
| 101 | final Throwable th = new Exception ( |
|---|
| 102 | "This is a top level test exception"); |
|---|
| 103 | th.initCause(new Throwable("This is a nestedException")); |
|---|
| 104 | logger.logp(Level.SEVERE, CLASSNAME, "logException", "Logging exception", |
|---|
| 105 | th); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** Test singel nested exception. */ |
|---|
| 109 | public void testServerExceptionLogWithNestedNullPointer () |
|---|
| 110 | { |
|---|
| 111 | new InternalErrorException( |
|---|
| 112 | "dump stack trace w/ nested NullPointerException", |
|---|
| 113 | new NullPointerException()).log(); |
|---|
| 114 | } |
|---|
| 115 | /** Test singel nested exception. */ |
|---|
| 116 | public void testServerExceptionLogWithNestedInternalErrorAndNullPointer () |
|---|
| 117 | { |
|---|
| 118 | final NullPointerException e = new NullPointerException("root"); |
|---|
| 119 | final InternalErrorException x = new InternalErrorException("middle", e); |
|---|
| 120 | System.err.println("---------------------------------------------------"); |
|---|
| 121 | new InternalErrorException( |
|---|
| 122 | "dump stack trace w/ nested InternalErrorException and " |
|---|
| 123 | + "NullPointerException", |
|---|
| 124 | x).log(); |
|---|
| 125 | System.err.println("---------------------------------------------------"); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** Test deep nested exception. */ |
|---|
| 129 | public void testDeepNesting () |
|---|
| 130 | { |
|---|
| 131 | final InternalErrorException inner |
|---|
| 132 | = new InternalErrorException("inner"); |
|---|
| 133 | inner.addParameter("INNER-PARAMETER", "value"); |
|---|
| 134 | final InternalErrorException middle |
|---|
| 135 | = new InternalErrorException("middle", inner); |
|---|
| 136 | middle.addParameter("MIDDLE-PARAMETER", "this is just a value"); |
|---|
| 137 | final Exception middleDefault |
|---|
| 138 | = new NullPointerException("middle-nullpointer"); |
|---|
| 139 | middleDefault.initCause(middle); |
|---|
| 140 | final Loggable outer |
|---|
| 141 | = new InternalErrorException("outer", middleDefault); |
|---|
| 142 | outer.addParameter("OUTER-PARAMETER", "this is just an other value"); |
|---|
| 143 | outer.log(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** Test nested exceptions. */ |
|---|
| 147 | public void testNesting () |
|---|
| 148 | { |
|---|
| 149 | try |
|---|
| 150 | { |
|---|
| 151 | a(); |
|---|
| 152 | } |
|---|
| 153 | catch (HighLevelException e) |
|---|
| 154 | { |
|---|
| 155 | new InternalErrorException("Test nesting.", e).log(); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** Test deep nested with SAX exceptions exception. */ |
|---|
| 160 | public void testNestingWithSax () |
|---|
| 161 | { |
|---|
| 162 | final InternalErrorException inner |
|---|
| 163 | = new InternalErrorException("inner"); |
|---|
| 164 | inner.addParameter("INNER-PARAMETER", "value"); |
|---|
| 165 | final InternalErrorException middle |
|---|
| 166 | = new InternalErrorException("middle", inner); |
|---|
| 167 | middle.addParameter("MIDDLE-PARAMETER", "this is just a value"); |
|---|
| 168 | final Exception middleDefault |
|---|
| 169 | = new SAXException("SAX Exception", middle); |
|---|
| 170 | final Loggable outer |
|---|
| 171 | = new InternalErrorException("outer", middleDefault); |
|---|
| 172 | outer.addParameter("OUTER-PARAMETER", "this is just an other value"); |
|---|
| 173 | outer.log(); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | static void a () |
|---|
| 177 | throws HighLevelException |
|---|
| 178 | { |
|---|
| 179 | try |
|---|
| 180 | { |
|---|
| 181 | b(); |
|---|
| 182 | } |
|---|
| 183 | catch (MidLevelException e) |
|---|
| 184 | { |
|---|
| 185 | throw new HighLevelException(e); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | static void b () |
|---|
| 190 | throws MidLevelException |
|---|
| 191 | { |
|---|
| 192 | c(); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | static void c () |
|---|
| 197 | throws MidLevelException |
|---|
| 198 | { |
|---|
| 199 | try |
|---|
| 200 | { |
|---|
| 201 | d(); |
|---|
| 202 | } |
|---|
| 203 | catch (LowLevelException e) |
|---|
| 204 | { |
|---|
| 205 | throw new MidLevelException(e); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | static void d () |
|---|
| 210 | throws LowLevelException |
|---|
| 211 | { |
|---|
| 212 | e(); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | static void e () |
|---|
| 216 | throws LowLevelException |
|---|
| 217 | { |
|---|
| 218 | throw new LowLevelException(); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | static final class HighLevelException |
|---|
| 222 | extends Exception |
|---|
| 223 | { |
|---|
| 224 | private static final long serialVersionUID = 1L; |
|---|
| 225 | |
|---|
| 226 | HighLevelException (Throwable cause) |
|---|
| 227 | { |
|---|
| 228 | super(cause); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | static final class MidLevelException |
|---|
| 233 | extends Exception |
|---|
| 234 | { |
|---|
| 235 | private static final long serialVersionUID = 1L; |
|---|
| 236 | |
|---|
| 237 | MidLevelException (Throwable cause) |
|---|
| 238 | { |
|---|
| 239 | super(cause); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | static final class LowLevelException |
|---|
| 244 | extends Exception |
|---|
| 245 | { |
|---|
| 246 | private static final long serialVersionUID = 1L; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | } |
|---|