| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 47 | | | {@link } |
| 48 | | | |
| 49 | | | @author |
| 50 | | | |
| 51 | 100 | | public class LogFormatterOutputTest |
| 52 | | | extends TestCase |
| 53 | | | { |
| 54 | 75 | | private static final String CLASSNAME |
| 55 | | | = LogFormatterOutputTest.class.getName(); |
| 56 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 57 | | | |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | public void testLogEvent () |
| 62 | | | { |
| 63 | 100 | | TstLogMessage.TestMessage.log("foo", Date.now().toUtilDate()); |
| 64 | 100 | | } |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | public void testLogEventWithImpliedParams () |
| 70 | | | { |
| 71 | 100 | | TssLogMessage.ImpliedParametersLog.log(new RuntimeException("Foo!")); |
| 72 | 100 | | } |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | | | public void testLogException () |
| 79 | | | { |
| 80 | 100 | | TstLogMessage.TestMessage.log("foo", Date.now().toUtilDate(), |
| 81 | | | new Exception("This is the top level exception")); |
| 82 | 100 | | } |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | public void testLogThrowable () |
| 89 | | | { |
| 90 | 100 | | final Throwable th = new Exception ( |
| 91 | | | "This is a top level test exception"); |
| 92 | 100 | | logger.logp(Level.SEVERE, CLASSNAME, "logException", "Logging exception", |
| 93 | | | th); |
| 94 | 100 | | } |
| 95 | | | |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | public void testLogNestedThrowable () |
| 100 | | | { |
| 101 | 100 | | final Throwable th = new Exception ( |
| 102 | | | "This is a top level test exception"); |
| 103 | 100 | | th.initCause(new Throwable("This is a nestedException")); |
| 104 | 100 | | logger.logp(Level.SEVERE, CLASSNAME, "logException", "Logging exception", |
| 105 | | | th); |
| 106 | 100 | | } |
| 107 | | | |
| 108 | | | |
| 109 | | | public void testServerExceptionLogWithNestedNullPointer () |
| 110 | | | { |
| 111 | 100 | | new InternalErrorException( |
| 112 | | | "dump stack trace w/ nested NullPointerException", |
| 113 | | | new NullPointerException()).log(); |
| 114 | 100 | | } |
| 115 | | | |
| 116 | | | public void testServerExceptionLogWithNestedInternalErrorAndNullPointer () |
| 117 | | | { |
| 118 | 100 | | final NullPointerException e = new NullPointerException("root"); |
| 119 | 100 | | final InternalErrorException x = new InternalErrorException("middle", e); |
| 120 | 100 | (1) | System.err.println("---------------------------------------------------"); |
| 121 | 100 | | new InternalErrorException( |
| 122 | | | "dump stack trace w/ nested InternalErrorException and " |
| 123 | | | + "NullPointerException", |
| 124 | | | x).log(); |
| 125 | 100 | (2) | System.err.println("---------------------------------------------------"); |
| 126 | 100 | | } |
| 127 | | | |
| 128 | | | |
| 129 | | | public void testDeepNesting () |
| 130 | | | { |
| 131 | 100 | | final InternalErrorException inner |
| 132 | | | = new InternalErrorException("inner"); |
| 133 | 100 | | inner.addParameter("INNER-PARAMETER", "value"); |
| 134 | 100 | | final InternalErrorException middle |
| 135 | | | = new InternalErrorException("middle", inner); |
| 136 | 100 | | middle.addParameter("MIDDLE-PARAMETER", "this is just a value"); |
| 137 | 100 | | final Exception middleDefault |
| 138 | | | = new NullPointerException("middle-nullpointer"); |
| 139 | 100 | | middleDefault.initCause(middle); |
| 140 | 100 | | final Loggable outer |
| 141 | | | = new InternalErrorException("outer", middleDefault); |
| 142 | 100 | | outer.addParameter("OUTER-PARAMETER", "this is just an other value"); |
| 143 | 100 | | outer.log(); |
| 144 | 100 | | } |
| 145 | | | |
| 146 | | | |
| 147 | | | public void testNesting () |
| 148 | | | { |
| 149 | | | try |
| 150 | | | { |
| 151 | 0 | | a(); |
| 152 | | | } |
| 153 | 100 | | catch (HighLevelException e) |
| 154 | | | { |
| 155 | 100 | | new InternalErrorException("Test nesting.", e).log(); |
| 156 | 0 | | } |
| 157 | 100 | | } |
| 158 | | | |
| 159 | | | |
| 160 | | | public void testNestingWithSax () |
| 161 | | | { |
| 162 | 100 | | final InternalErrorException inner |
| 163 | | | = new InternalErrorException("inner"); |
| 164 | 100 | | inner.addParameter("INNER-PARAMETER", "value"); |
| 165 | 100 | | final InternalErrorException middle |
| 166 | | | = new InternalErrorException("middle", inner); |
| 167 | 100 | | middle.addParameter("MIDDLE-PARAMETER", "this is just a value"); |
| 168 | 100 | | final Exception middleDefault |
| 169 | | | = new SAXException("SAX Exception", middle); |
| 170 | 100 | | final Loggable outer |
| 171 | | | = new InternalErrorException("outer", middleDefault); |
| 172 | 100 | | outer.addParameter("OUTER-PARAMETER", "this is just an other value"); |
| 173 | 100 | | outer.log(); |
| 174 | 100 | | } |
| 175 | | | |
| 176 | | | static void a () |
| 177 | | | throws HighLevelException |
| 178 | | | { |
| 179 | | | try |
| 180 | | | { |
| 181 | 0 | | b(); |
| 182 | | | } |
| 183 | 100 | | catch (MidLevelException e) |
| 184 | | | { |
| 185 | 100 | | throw new HighLevelException(e); |
| 186 | 0 | | } |
| 187 | 0 | | } |
| 188 | | | |
| 189 | | | static void b () |
| 190 | | | throws MidLevelException |
| 191 | | | { |
| 192 | 0 | | c(); |
| 193 | 0 | | } |
| 194 | | | |
| 195 | | | |
| 196 | | | static void c () |
| 197 | | | throws MidLevelException |
| 198 | | | { |
| 199 | | | try |
| 200 | | | { |
| 201 | 0 | | d(); |
| 202 | | | } |
| 203 | 100 | | catch (LowLevelException e) |
| 204 | | | { |
| 205 | 100 | | throw new MidLevelException(e); |
| 206 | 0 | | } |
| 207 | 0 | | } |
| 208 | | | |
| 209 | | | static void d () |
| 210 | | | throws LowLevelException |
| 211 | | | { |
| 212 | 0 | | e(); |
| 213 | 0 | | } |
| 214 | | | |
| 215 | | | static void e () |
| 216 | | | throws LowLevelException |
| 217 | | | { |
| 218 | 100 | | 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 | 100 | | super(cause); |
| 229 | 100 | | } |
| 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 | 100 | | super(cause); |
| 240 | 100 | | } |
| 241 | | | } |
| 242 | | | |
| 243 | 100 | | static final class LowLevelException |
| 244 | | | extends Exception |
| 245 | | | { |
| 246 | | | private static final long serialVersionUID = 1L; |
| 247 | | | } |
| 248 | | | |
| 249 | | | } |