001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.output.test;
056:
057: import java.io.ByteArrayOutputStream;
058: import java.io.File;
059: import java.io.FileInputStream;
060: import java.io.IOException;
061: import java.io.OutputStreamWriter;
062: import junit.framework.TestCase;
063: import org.apache.log.Hierarchy;
064: import org.apache.log.LogTarget;
065: import org.apache.log.Logger;
066: import org.apache.log.Priority;
067: import org.apache.log.format.RawFormatter;
068: import org.apache.log.output.AbstractOutputTarget;
069: import org.apache.log.output.MemoryTarget;
070: import org.apache.log.output.io.FileTarget;
071: import org.apache.log.output.io.SafeFileTarget;
072: import org.apache.log.output.io.StreamTarget;
073: import org.apache.log.output.io.WriterTarget;
074:
075: /**
076: * Test suite for the formatters.
077: * TODO: Incorporate testing for ContextStack and ContextMap
078: *
079: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
080: */
081: public final class OutputTargetTestCase extends TestCase {
082: private static String M1 = "meep meep!";
083: private static String M2 = "marina";
084: private static String M3 = "spin and then fall down";
085:
086: private static String HEAD = "";
087: private static String TAIL = "";
088:
089: private static String R1 = M1;
090: private static String R2 = M2;
091: private static String R3 = M3;
092:
093: private static String OUTPUT = HEAD + R1 + R2 + R3 + TAIL;
094:
095: private static RawFormatter FORMATTER = new RawFormatter();
096:
097: private final File m_logFile;
098:
099: public OutputTargetTestCase(final String name) throws IOException {
100: super (name);
101:
102: m_logFile = (new File("test/log/logfile.txt"))
103: .getCanonicalFile();
104: }
105:
106: private String getResult(final ByteArrayOutputStream output) {
107: final String result = output.toString();
108: output.reset();
109: return result;
110: }
111:
112: public void testStreamTarget() throws Exception {
113: final ByteArrayOutputStream output = new ByteArrayOutputStream();
114: final StreamTarget target = new StreamTarget(output, FORMATTER);
115: doStreamTest(output, target);
116: /*
117: final ExtendedPatternFormatter formatter =
118: new ExtendedPatternFormatter( "%{method} from %{thread}\n" );
119: final StreamTarget target2 = new StreamTarget( System.out, formatter );
120: final Logger logger = getNewLogger( target2 );
121: logger.debug( M1 );
122: */
123: }
124:
125: public void testWriterTarget() throws Exception {
126: final ByteArrayOutputStream output = new ByteArrayOutputStream();
127: final OutputStreamWriter writer = new OutputStreamWriter(output);
128: final WriterTarget target = new WriterTarget(writer, FORMATTER);
129: doStreamTest(output, target);
130: }
131:
132: public void testFileTarget() throws Exception {
133: final FileTarget target = new FileTarget(m_logFile, false,
134: FORMATTER);
135:
136: final Logger logger = getNewLogger(target);
137: logger.debug(M1);
138: logger.debug(M2);
139: logger.debug(M3);
140: target.close();
141:
142: final String data = getFileContents(m_logFile);
143: assertEquals("Targets file output", OUTPUT, data);
144: assertTrue("Deleting logfile", m_logFile.delete());
145:
146: logger.debug(M1);
147: assertTrue("Write after close()", !m_logFile.exists());
148: }
149:
150: public void testSafeFileTarget() throws Exception {
151: final SafeFileTarget target = new SafeFileTarget(m_logFile,
152: false, FORMATTER);
153:
154: final Logger logger = getNewLogger(target);
155: logger.debug(M1);
156:
157: final String data1 = getFileContents(m_logFile);
158: assertEquals("Targets file output", HEAD + R1, data1);
159: assertTrue("Deleting logfile", m_logFile.delete());
160:
161: logger.debug(M2);
162: logger.debug(M3);
163: target.close();
164:
165: final String data2 = getFileContents(m_logFile);
166: assertEquals("Targets file output", R2 + R3 + TAIL, data2);
167: assertTrue("Deleting logfile", m_logFile.delete());
168:
169: logger.debug(M1);
170: assertTrue("Write after close()", !m_logFile.exists());
171: }
172:
173: /**
174: * Test that MemoryTarget triggers on buffer size hit.
175: *
176: * @exception Exception if an error occurs
177: */
178: public void testMemoryTarget1() throws Exception {
179: final ByteArrayOutputStream output = new ByteArrayOutputStream();
180: final StreamTarget other = new StreamTarget(output, FORMATTER);
181: final MemoryTarget target = new MemoryTarget(other, 1,
182: Priority.FATAL_ERROR);
183: doStreamOutputTest(output, target);
184: }
185:
186: /**
187: * Test that MemoryTarget triggers on priority correctly.
188: *
189: * @exception Exception if an error occurs
190: */
191: public void testMemoryTarget2() throws Exception {
192: final ByteArrayOutputStream output = new ByteArrayOutputStream();
193: final StreamTarget other = new StreamTarget(output, FORMATTER);
194: final MemoryTarget target = new MemoryTarget(other, 10,
195: Priority.DEBUG);
196: doStreamOutputTest(output, target);
197: }
198:
199: /**
200: * Test that MemoryTarget triggers on priority correctly.
201: *
202: * @exception Exception if an error occurs
203: */
204: public void testMemoryTarget3() throws Exception {
205: final ByteArrayOutputStream output = new ByteArrayOutputStream();
206: final StreamTarget other = new StreamTarget(output, FORMATTER);
207: final MemoryTarget target = new MemoryTarget(other, 10,
208: Priority.FATAL_ERROR);
209:
210: final Logger logger = getNewLogger(target);
211:
212: //Head output should not be pushed yet
213: final String head = getResult(output);
214: assertEquals("Targets Head output", "", head);
215:
216: //Not pushed yet
217: logger.debug(M1);
218: final String result1 = getResult(output);
219: assertEquals("Targets R1 debug output", "", result1);
220:
221: target.push();
222: final String resultPP = getResult(output);
223: assertEquals("Targets HEAD+R1 debug output", HEAD + R1,
224: resultPP);
225:
226: logger.debug(M2);
227: final String result2 = getResult(output);
228:
229: logger.debug(M3);
230: final String result3 = getResult(output);
231:
232: //fatal error triggers a push
233: logger.fatalError(M3);
234: final String result4 = getResult(output);
235:
236: assertEquals("Targets R2 debug output", "", result2);
237: assertEquals("Targets R3 debug output", "", result3);
238: assertEquals("Targets R3 debug output", R2 + R3 + R3, result4);
239: }
240:
241: private Logger getNewLogger(final LogTarget target) {
242: final Hierarchy hierarchy = new Hierarchy();
243: final Logger logger = hierarchy.getLoggerFor("myCategory");
244: logger.setLogTargets(new LogTarget[] { target });
245: return logger;
246: }
247:
248: private String getFileContents(final File file) throws IOException {
249: final FileInputStream input = new FileInputStream(file);
250: final StringBuffer sb = new StringBuffer();
251:
252: int ch = input.read();
253: while (-1 != ch) {
254: sb.append((char) ch);
255: ch = input.read();
256: }
257:
258: input.close();
259:
260: return sb.toString();
261: }
262:
263: private Logger doStreamOutputTest(
264: final ByteArrayOutputStream output, final LogTarget target) {
265: final Logger logger = getNewLogger(target);
266:
267: final String head = getResult(output);
268:
269: logger.debug(M1);
270: final String result1 = getResult(output);
271:
272: logger.debug(M2);
273: final String result2 = getResult(output);
274:
275: logger.debug(M3);
276: final String result3 = getResult(output);
277:
278: assertEquals("Targets Head output", HEAD, head);
279: assertEquals("Targets R1 debug output", R1, result1);
280: assertEquals("Targets R2 debug output", R2, result2);
281: assertEquals("Targets R3 debug output", R3, result3);
282:
283: return logger;
284: }
285:
286: private void doStreamTest(final ByteArrayOutputStream output,
287: final AbstractOutputTarget target) {
288: final Logger logger = doStreamOutputTest(output, target);
289:
290: target.close();
291: final String tail = getResult(output);
292: assertEquals("Targets Tail output", TAIL, tail);
293:
294: logger.debug(M1);
295: final String noresult = getResult(output);
296: //final String errorResult = getErrorResult( errorOutput );
297:
298: assertEquals("Write after close()", "", noresult);
299: //assertEquals( "Epecting error", "", errorResult );
300: }
301: }
|