KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > test > AbstractLoggingTest


1 package org.grlea.log.test;
2
3 // $Id: AbstractLoggingTest.java,v 1.3 2006/07/13 12:44:56 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

6 // Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

18 import org.grlea.log.SimpleLog;
19
20 import junit.framework.TestCase;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 /**
31  * <p></p>
32  *
33  * @author Graham Lea
34  * @version $Revision: 1.3 $
35  */

36 public abstract class
37 AbstractLoggingTest
38 extends TestCase
39 {
40    private ByteArrayOutputStream JavaDoc outputStream;
41    protected SimpleLog log;
42    protected Properties JavaDoc properties;
43
44    public
45    AbstractLoggingTest(String JavaDoc name)
46    {
47       super(name);
48    }
49
50    protected void
51    setUp()
52    {
53       outputStream = new ByteArrayOutputStream JavaDoc(512);
54       System.setErr(new PrintStream JavaDoc(outputStream));
55
56       properties = new Properties JavaDoc();
57       log = new SimpleLog(properties);
58    }
59
60    protected void
61    tearDown()
62    {
63       log = null;
64       properties = null;
65       outputStream = null;
66    }
67
68    protected void
69    checkOutput(String JavaDoc[] expectedOutputLineParts)
70    throws IOException JavaDoc
71    {
72       byte[] output = outputStream.toByteArray();
73       ByteArrayInputStream JavaDoc byteIn = new ByteArrayInputStream JavaDoc(output);
74       InputStreamReader JavaDoc streamReader = new InputStreamReader JavaDoc(byteIn);
75       BufferedReader JavaDoc in = new BufferedReader JavaDoc(streamReader);
76       String JavaDoc outputLine;
77       int lineNumber = 0;
78       while ((outputLine = in.readLine()) != null)
79       {
80          if (lineNumber >= expectedOutputLineParts.length)
81          {
82             fail("More output lines than expected.\nExtra line: " + outputLine);
83          }
84
85          String JavaDoc expectedOutputLinePart = expectedOutputLineParts[lineNumber];
86          boolean linePartFound = outputLine.indexOf(expectedOutputLinePart) != -1;
87          assertEquals("'" + expectedOutputLinePart + "' found in '" + outputLine + "'",
88                       true, linePartFound);
89
90          lineNumber++;
91       }
92
93       assertEquals("output lines", expectedOutputLineParts.length, lineNumber);
94    }
95 }
Popular Tags