1 package net.sf.logdistiller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.BufferedReader;
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.net.URL;
25 import java.text.ParseException;
26 import java.util.HashMap;
27
28 import org.apache.commons.io.IOUtils;
29
30 import junit.framework.TestCase;
31
32
33
34
35
36
37 public class LogEventTestCase
38 extends TestCase
39 {
40 protected final LogType logtype;
41
42 protected LogType.Description description;
43
44 protected LogEvent.Factory factory;
45
46 protected LogEventTestCase( String logtype )
47 {
48 super( logtype );
49 this.logtype = LogTypes.getLogType( logtype );
50 this.description = this.logtype.newDescription( new HashMap<String, String>() );
51 this.description.setExtensions( new Attributes.Extension[0] );
52 }
53
54
55
56
57
58
59
60
61
62 protected URL getResource( String name )
63 {
64 URL url = getClass().getResource( name );
65 if ( url == null )
66 {
67 throw new RuntimeException( "resource '" + name + "' not found in package "
68 + getClass().getPackage().getName() );
69 }
70 return url;
71 }
72
73
74
75
76 protected LogEvent.Factory newFactory( String name )
77 throws IOException
78 {
79 URL url = getResource( name );
80 String source = getClass().getPackage().getName().replace( '.', '/' ) + '/' + name;
81 return description.newFactory( new InputStreamReader( url.openStream() ), source );
82 }
83
84 private final static String HEADER = "====== log event ======";
85
86
87
88
89 public static int dump( LogEvent.Factory factory, PrintWriter out )
90 throws IOException, ParseException
91 {
92 LogEvent le;
93 int count = 0;
94 while ( ( le = factory.nextEvent() ) != null )
95 {
96 out.println( HEADER );
97 le.dump( out );
98 count++;
99 }
100 return count;
101 }
102
103
104
105
106 public static int dump( LogEvent.Factory factory, File file )
107 throws IOException, ParseException
108 {
109 PrintWriter out = null;
110 try
111 {
112 out = new PrintWriter( new FileWriter( file ) );
113 return dump( factory, out );
114 }
115 finally
116 {
117 IOUtils.closeQuietly( out );
118 }
119 }
120
121
122
123
124 public static String readDump( BufferedReader reader )
125 throws IOException
126 {
127 StringWriter buffer = new StringWriter();
128 PrintWriter out = new PrintWriter( buffer );
129 String line = reader.readLine();
130 if ( line == null )
131 {
132 return null;
133 }
134 if ( !HEADER.equals( line ) )
135 {
136 throw new IllegalStateException( "expected '" + HEADER + "' but got '" + line + "'" );
137 }
138 while ( !startsWith( reader, HEADER ) && ( ( line = reader.readLine() ) != null ) )
139 {
140 out.println( line );
141 }
142 out.close();
143 return buffer.toString();
144 }
145
146
147
148
149 private static boolean startsWith( BufferedReader reader, String content )
150 throws IOException
151 {
152 reader.mark( content.length() );
153 for ( int i = 0; i < content.length(); i++ )
154 {
155 if ( reader.read() != content.charAt( i ) )
156 {
157 reader.reset();
158 return false;
159 }
160 }
161 reader.reset();
162 return true;
163 }
164
165 public void checkDump( LogEvent.Factory factory, BufferedReader reader )
166 throws IOException, ParseException
167 {
168 LogEvent le = null;
169 while ( ( le = factory.nextEvent() ) != null )
170 {
171 String dump = readDump( reader );
172 System.out.println( dump );
173 assertEquals( dump, le.dump() );
174 }
175 assertEquals( "not any log event available, but dump still has content", null, readDump( reader ) );
176 }
177
178 public void checkDump( LogEvent.Factory factory, String name )
179 throws IOException, ParseException
180 {
181 BufferedReader reader = new BufferedReader( new InputStreamReader( getResource( name ).openStream() ) );
182 checkDump( factory, reader );
183 }
184 }