| 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.io.ByteArrayInputStream; |
|---|
| 36 | import java.io.ByteArrayOutputStream; |
|---|
| 37 | import java.io.ObjectInputStream; |
|---|
| 38 | import java.io.ObjectOutputStream; |
|---|
| 39 | import java.util.Collections; |
|---|
| 40 | import java.util.Date; |
|---|
| 41 | import java.util.HashMap; |
|---|
| 42 | import java.util.Map; |
|---|
| 43 | import java.util.logging.Level; |
|---|
| 44 | import junit.framework.TestCase; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Tests the LogMessageInfo class. |
|---|
| 48 | * |
|---|
| 49 | * @author Michael Griffel |
|---|
| 50 | */ |
|---|
| 51 | public class LogMessageInfoTest |
|---|
| 52 | extends TestCase |
|---|
| 53 | { |
|---|
| 54 | private static final TstLogMessage CODE = TstLogMessage.TEST_MESSAGE; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Tests that it is possible to use the int representation of the log |
|---|
| 58 | * message in a switch statement. |
|---|
| 59 | */ |
|---|
| 60 | public void testMisc () |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | switch (CODE.toInt()) |
|---|
| 64 | { |
|---|
| 65 | case TstLogMessage.TestMessage.INT_VALUE: |
|---|
| 66 | // expected |
|---|
| 67 | break; |
|---|
| 68 | default: |
|---|
| 69 | fail("Unexpected int " + CODE.toInt() + ", expected " |
|---|
| 70 | + TstLogMessage.TestMessage.INT_VALUE); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Tests the method {@link TstLogMessage#fromString(String)}. |
|---|
| 76 | */ |
|---|
| 77 | public void testFromString () |
|---|
| 78 | { |
|---|
| 79 | assertSame("fromString method should return the same instance", CODE, |
|---|
| 80 | TstLogMessage.fromString(CODE.toString())); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Tests the method {@link TstLogMessage#fromInt(int)}. |
|---|
| 85 | */ |
|---|
| 86 | public void testFromInt () |
|---|
| 87 | { |
|---|
| 88 | assertSame("fromInt method should return the same instance", CODE, |
|---|
| 89 | TstLogMessage.fromInt(CODE.toInt())); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Tests the method {@link LogMessageInfoImpl#toInt()}. |
|---|
| 94 | */ |
|---|
| 95 | public void testToInt () |
|---|
| 96 | { |
|---|
| 97 | assertEquals("constant int representation", |
|---|
| 98 | TstLogMessage.TestMessage.INT_VALUE, |
|---|
| 99 | TstLogMessage.TEST_MESSAGE.toInt()); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Tests the method {@link LogMessageInfoImpl#toString()}. |
|---|
| 104 | */ |
|---|
| 105 | public void testToString () |
|---|
| 106 | { |
|---|
| 107 | assertEquals("string representation should be the message symbol", |
|---|
| 108 | TstLogMessage.TEST_MESSAGE.getSymbol(), |
|---|
| 109 | TstLogMessage.TEST_MESSAGE.toString()); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Tests the method {@link LogMessageInfoImpl#getSymbol()}. |
|---|
| 114 | */ |
|---|
| 115 | public void testGetSymbol () |
|---|
| 116 | { |
|---|
| 117 | assertEquals("message symbol", |
|---|
| 118 | "FWK_TST_TEST_MESSAGE", TstLogMessage.TEST_MESSAGE.getSymbol()); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Tests the method {@link LogMessageInfoImpl#getLogLevel()}. |
|---|
| 123 | */ |
|---|
| 124 | public void testGetLogLevel () |
|---|
| 125 | { |
|---|
| 126 | assertEquals("default log level should be OFF", |
|---|
| 127 | Level.INFO, TstLogMessage.TEST_MESSAGE.getLogLevel()); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Tests the method {@link LogMessageInfoImpl#getMessagePattern()}. |
|---|
| 132 | */ |
|---|
| 133 | public void testGetMessagePattern () |
|---|
| 134 | { |
|---|
| 135 | final String regex = ".*\\{0\\}.*\\{1,date\\}.*\\{1,time\\}.*"; |
|---|
| 136 | assertTrue("message pattern test", |
|---|
| 137 | TstLogMessage.TEST_MESSAGE.getMessagePattern().matches(regex)); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Tests the method {@link LogMessageInfoImpl#formatMessage(Map, StringBuffer)}. |
|---|
| 142 | */ |
|---|
| 143 | public void testFormatMessage () |
|---|
| 144 | { |
|---|
| 145 | final Map parameters = new HashMap(); |
|---|
| 146 | parameters.put(TstLogMessage.TestMessage.PARAM_FOO, |
|---|
| 147 | Collections.singletonList("param")); |
|---|
| 148 | parameters.put(TstLogMessage.TestMessage.PARAM_NOW, |
|---|
| 149 | Collections.singletonList(new Date())); |
|---|
| 150 | System.out.println("message: " |
|---|
| 151 | + TstLogMessage.TEST_MESSAGE.formatMessage(parameters, null)); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Tests the method {@link LogMessageInfoImpl#getSolution()}. |
|---|
| 156 | */ |
|---|
| 157 | public void testGetSolution () |
|---|
| 158 | { |
|---|
| 159 | assertNotNull("test solution", TstLogMessage.TEST_MESSAGE.getSolution()); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Tests the method {@link LogMessageInfoImpl#getBusinessImpact()}. |
|---|
| 164 | */ |
|---|
| 165 | public void testGetBusinessImpact () |
|---|
| 166 | { |
|---|
| 167 | assertEquals("test business impact", BusinessImpact.UNDEFINED, |
|---|
| 168 | TstLogMessage.TEST_MESSAGE.getBusinessImpact()); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Tests the method {@link Object#hashCode()}. |
|---|
| 173 | */ |
|---|
| 174 | public void testHashCode () |
|---|
| 175 | { |
|---|
| 176 | assertEquals("fromString method should return the same hashCode", |
|---|
| 177 | CODE.hashCode(), |
|---|
| 178 | TstLogMessage.fromString(CODE.toString()).hashCode()); |
|---|
| 179 | assertEquals("fromInt method should return the same hashCode", |
|---|
| 180 | CODE.hashCode(), |
|---|
| 181 | TstLogMessage.fromInt(CODE.toInt()).hashCode()); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | * Tests the method readResolve(). |
|---|
| 186 | * @throws Exception in case of an unexpected error. |
|---|
| 187 | */ |
|---|
| 188 | public void testSerialize () |
|---|
| 189 | throws Exception |
|---|
| 190 | { |
|---|
| 191 | final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
|---|
| 192 | final ObjectOutputStream objOut = new ObjectOutputStream(bOut); |
|---|
| 193 | |
|---|
| 194 | objOut.writeObject(CODE); |
|---|
| 195 | objOut.flush(); |
|---|
| 196 | |
|---|
| 197 | final ByteArrayInputStream bIn |
|---|
| 198 | = new ByteArrayInputStream(bOut.toByteArray()); |
|---|
| 199 | final ObjectInputStream objIn = new ObjectInputStream(bIn); |
|---|
| 200 | |
|---|
| 201 | final TstLogMessage p = (TstLogMessage) objIn.readObject(); |
|---|
| 202 | |
|---|
| 203 | assertEquals("Object must be equal after serialization", p, CODE); |
|---|
| 204 | assertSame("Object must be same(!) after serialization", p, CODE); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Test the usage of the test message w/ exception wrapper. |
|---|
| 209 | */ |
|---|
| 210 | public void testSampleError () |
|---|
| 211 | { |
|---|
| 212 | final String parameter = "bar"; |
|---|
| 213 | final SampleError error = new SampleError(parameter); |
|---|
| 214 | error.log(); |
|---|
| 215 | assertEquals("The parameter foo should not be modified", |
|---|
| 216 | parameter, |
|---|
| 217 | error.getParameter(TstLogMessage.TestMessage.PARAM_FOO).get(0)); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * Tests the serializable implementation. |
|---|
| 222 | * @throws Exception in case of an unexpected error. |
|---|
| 223 | */ |
|---|
| 224 | public void testSerializable () |
|---|
| 225 | throws Exception |
|---|
| 226 | { |
|---|
| 227 | final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
|---|
| 228 | final ObjectOutputStream objOut = new ObjectOutputStream(bOut); |
|---|
| 229 | ByteArrayInputStream bIn; |
|---|
| 230 | ObjectInputStream objIn; |
|---|
| 231 | |
|---|
| 232 | objOut.writeObject(TstLogMessage.TEST_MESSAGE); |
|---|
| 233 | objOut.flush(); |
|---|
| 234 | bIn = new ByteArrayInputStream(bOut.toByteArray()); |
|---|
| 235 | objIn = new ObjectInputStream(bIn); |
|---|
| 236 | final TstLogMessage messageRead = (TstLogMessage) objIn.readObject(); |
|---|
| 237 | |
|---|
| 238 | assertSame("Values or reference changed during serialization.", |
|---|
| 239 | TstLogMessage.TEST_MESSAGE, messageRead); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | /** |
|---|
| 243 | * Tests the if the special audit log event is generated. |
|---|
| 244 | */ |
|---|
| 245 | public void testAuditLogEvent () |
|---|
| 246 | { |
|---|
| 247 | final String dummyToString = "Dummy Audit Log Event"; |
|---|
| 248 | final AuditPrincipal principal = new AuditPrincipal() |
|---|
| 249 | { |
|---|
| 250 | private static final long serialVersionUID = 1L; |
|---|
| 251 | |
|---|
| 252 | public String toString () |
|---|
| 253 | { |
|---|
| 254 | return dummyToString; |
|---|
| 255 | } |
|---|
| 256 | }; |
|---|
| 257 | final AuditLogEvent event |
|---|
| 258 | = TstLogMessage.TestAuditMessage.create(principal); |
|---|
| 259 | assertEquals("Expected dummy to string representation", dummyToString, |
|---|
| 260 | event.getAuditPrincipal().toString()); |
|---|
| 261 | event.log(); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /** |
|---|
| 265 | * Sample exception using test log message. |
|---|
| 266 | * |
|---|
| 267 | * @author Michael Griffel |
|---|
| 268 | */ |
|---|
| 269 | public static final class SampleError |
|---|
| 270 | extends BaseException |
|---|
| 271 | { |
|---|
| 272 | static final long serialVersionUID = 1; |
|---|
| 273 | |
|---|
| 274 | /** |
|---|
| 275 | * Constructor. |
|---|
| 276 | * @param foo The parameter value for foo. |
|---|
| 277 | */ |
|---|
| 278 | public SampleError (String foo) |
|---|
| 279 | { |
|---|
| 280 | super(TstLogMessage.TEST_MESSAGE); |
|---|
| 281 | addParameter(TstLogMessage.TestMessage.PARAM_FOO, foo); |
|---|
| 282 | addParameter(TstLogMessage.TestMessage.PARAM_NOW, new Date()); |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | } |
|---|