| Line | Hits | Note | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * $Id: XmlPrinterTest.java 1011 2008-06-16 17:57:36Z amandel $ | ||
| 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.logging; | ||
| 34 | |||
| 35 | import java.io.CharArrayWriter; | ||
| 36 | import java.io.PrintWriter; | ||
| 37 | import java.util.logging.Formatter; | ||
| 38 | import java.util.logging.Handler; | ||
| 39 | import java.util.logging.LogRecord; | ||
| 40 | import java.util.logging.Logger; | ||
| 41 | import org.jcoderz.commons.LogFormatterOutputTest; | ||
| 42 | |||
| 43 | |||
| 44 | /** | ||
| 45 | * This class is used for testing the XmlPrinter. It uses the handler being | ||
| 46 | * installed by the LogElementTest and sets a formatter using th XmlPrinter. | ||
| 47 | * The log messages are generated by the LogFormatterOutputTest. | ||
| 48 | * | ||
| 49 | */ | ||
| 50 | (1)(2) | public class XmlPrinterTest | |
| 51 | extends LogFormatterOutputTest | ||
| 52 | { | ||
| 53 | 75 | private static final String CLASSNAME = FormatTest.class.getName(); | |
| 54 | 100 | private static final Logger logger = Logger.getLogger(CLASSNAME); | |
| 55 | |||
| 56 | 100 | private static final Logger ROOT_LOGGER = Logger.getLogger(""); | |
| 57 | |||
| 58 | 100 | private Handler mHandler = null; | |
| 59 | |||
| 60 | (3) | private static class LogElementHandler | |
| 61 | extends Handler | ||
| 62 | { | ||
| 63 | /** | ||
| 64 | * Creates a new instance of this and sets teh xml formatter as formatter. | ||
| 65 | */ | ||
| 66 | private LogElementHandler () | ||
| 67 | 100 | { | |
| 68 | 100 | setFormatter(new XmlFormatter()); | |
| 69 | 100 | } | |
| 70 | |||
| 71 | /** | ||
| 72 | * Closes this Handler, this is a NOP. | ||
| 73 | * | ||
| 74 | * @see java.util.logging.Handler#close() | ||
| 75 | */ | ||
| 76 | public void close () | ||
| 77 | throws SecurityException | ||
| 78 | { | ||
| 79 | // nop | ||
| 80 | 0 | } | |
| 81 | |||
| 82 | /** | ||
| 83 | * Flushes this Handler, this is a NOP. | ||
| 84 | * | ||
| 85 | * @see java.util.logging.Handler#flush() | ||
| 86 | */ | ||
| 87 | public void flush () | ||
| 88 | { | ||
| 89 | // nop | ||
| 90 | 0 | } | |
| 91 | |||
| 92 | /** | ||
| 93 | * Just formats the LogRecord with the xml formatter. | ||
| 94 | * | ||
| 95 | * @see java.util.logging.Handler#publish(java.util.logging.LogRecord) | ||
| 96 | */ | ||
| 97 | public void publish (LogRecord record) | ||
| 98 | { | ||
| 99 | 100 | getFormatter().format(record); | |
| 100 | 100 | } | |
| 101 | } | ||
| 102 | |||
| 103 | (4) | private static class XmlFormatter | |
| 104 | extends Formatter | ||
| 105 | { | ||
| 106 | (5) | final PrintWriter mPrinter; | |
| 107 | (6) | final CharArrayWriter mCharWriter; | |
| 108 | 100 | (7) | XmlPrinter mXmlPrinter = null; |
| 109 | |||
| 110 | private XmlFormatter () | ||
| 111 | 100 | { | |
| 112 | 100 | mCharWriter = new CharArrayWriter(); | |
| 113 | 100 | mPrinter = new PrintWriter(mCharWriter); | |
| 114 | try | ||
| 115 | { | ||
| 116 | 100 | mXmlPrinter = new XmlPrinter(); | |
| 117 | } | ||
| 118 | 0 | catch (InstantiationException e) | |
| 119 | { | ||
| 120 | 0 | fail("Could not instantiate an Xml Printer"); | |
| 121 | 100 | } | |
| 122 | 100 | } | |
| 123 | |||
| 124 | /** {@inheritDoc} */ | ||
| 125 | public String format (LogRecord record) | ||
| 126 | { | ||
| 127 | 100 | mCharWriter.reset(); | |
| 128 | 100 | final LogItem element = new LogElement(record); | |
| 129 | 100 | mXmlPrinter.print(mPrinter, element); | |
| 130 | 100 | return mCharWriter.toString(); | |
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Creates a new test case instance. | ||
| 136 | */ | ||
| 137 | (8) | public XmlPrinterTest () | |
| 138 | { | ||
| 139 | 100 | super(); | |
| 140 | 100 | } | |
| 141 | |||
| 142 | (9) | public void testSimpleLogRecord () | |
| 143 | { | ||
| 144 | 100 | logger.info("testSimpleLogRecord: " + getClass().getName()); | |
| 145 | 100 | } | |
| 146 | |||
| 147 | |||
| 148 | /** | ||
| 149 | * Installs an additional Handler, which publishes RocRecords using | ||
| 150 | * LogElements. | ||
| 151 | * | ||
| 152 | * @see junit.framework.TestCase#setUp() | ||
| 153 | */ | ||
| 154 | protected void setUp () | ||
| 155 | { | ||
| 156 | 100 | mHandler = new LogElementHandler(); | |
| 157 | 100 | ROOT_LOGGER.addHandler(mHandler); | |
| 158 | 100 | } | |
| 159 | |||
| 160 | /** | ||
| 161 | * Deinstalls the additional Handler. | ||
| 162 | * | ||
| 163 | * @see junit.framework.TestCase#setUp() | ||
| 164 | */ | ||
| 165 | protected void tearDown () | ||
| 166 | { | ||
| 167 | 100 | if (mHandler != null) | |
| 168 | { | ||
| 169 | 100 | ROOT_LOGGER.removeHandler(mHandler); | |
| 170 | 100 | mHandler = null; | |
| 171 | } | ||
| 172 | 100 | } | |
| 173 | } |