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