01: /**
02: *
03: */package org.drools.xml;
04:
05: import java.io.IOException;
06: import java.io.InputStreamReader;
07:
08: import junit.framework.Assert;
09:
10: import org.drools.compiler.DrlParser;
11: import org.drools.lang.DrlDumper;
12: import org.drools.lang.descr.PackageDescr;
13:
14: /**
15: * Helper Class for both xml and drl Dump Tests
16: *
17: * @author fernandomeyer
18: */
19: public class DumperTestHelper extends Assert {
20:
21: public static void XmlFile(String filename) throws Exception {
22:
23: XmlPackageReader xmlPackageReader = new XmlPackageReader();
24: xmlPackageReader.read(new InputStreamReader(
25: DumperTestHelper.class.getResourceAsStream(filename)));
26: final PackageDescr pkgOriginal = xmlPackageReader
27: .getPackageDescr();
28:
29: final XmlDumper dumper = new XmlDumper();
30: final String result = dumper.dump(pkgOriginal);
31:
32: String buffer = readFile(filename);
33:
34: assertEqualsIgnoreWhitespace(buffer, result);
35: assertNotNull(result);
36: }
37:
38: public static void DrlFile(String filename) throws Exception {
39:
40: DrlParser parser = new DrlParser();
41: final PackageDescr pkgOriginal = parser
42: .parse(new InputStreamReader(DumperTestHelper.class
43: .getResourceAsStream(filename)));
44: final DrlDumper dumper = new DrlDumper();
45: final String result = dumper.dump(pkgOriginal);
46:
47: parser = new DrlParser();
48: String buffer = readFile(filename);
49: assertEqualsIgnoreWhitespace(buffer.toString(), result);
50:
51: }
52:
53: private static void assertEqualsIgnoreWhitespace(
54: final String expected, final String actual) {
55: final String cleanExpected = expected.replaceAll("\\s+", "");
56: final String cleanActual = actual.replaceAll("\\s+", "");
57: assertEquals(cleanExpected, cleanActual);
58: }
59:
60: private static String readFile(final String file)
61: throws IOException {
62: final InputStreamReader reader = new InputStreamReader(
63: DumperTestHelper.class.getResourceAsStream(file));
64: final StringBuffer text = new StringBuffer();
65: final char[] buf = new char[1024];
66: int len = 0;
67:
68: while ((len = reader.read(buf)) >= 0) {
69: text.append(buf, 0, len);
70: }
71: return text.toString();
72: }
73: }
|