001: package fit;
002:
003: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
004: // Released under the terms of the GNU General Public License version 2 or later.
005:
006: import junit.framework.*;
007: import java.util.*;
008: import java.text.DateFormat;
009: import java.io.*;
010:
011: public class FrameworkTest extends TestCase {
012:
013: public FrameworkTest(String name) {
014: super (name);
015: }
016:
017: public void testTypeAdapter() throws Exception {
018: TestFixture f = new TestFixture();
019: TypeAdapter a = TypeAdapter.on(f, f.getClass().getField(
020: "sampleInt"));
021: a.set(a.parse("123456"));
022: assertEquals(123456, f.sampleInt);
023: assertEquals("-234567", a.parse("-234567").toString());
024: a = TypeAdapter.on(f, f.getClass().getField("sampleInteger"));
025: a.set(a.parse("54321"));
026: assertEquals("54321", f.sampleInteger.toString());
027: a = TypeAdapter.on(f, f.getClass().getMethod("pi",
028: new Class[] {}));
029: assertEquals(3.14159, ((Double) a.invoke()).doubleValue(),
030: 0.00001);
031: assertEquals(new Double(3.14159862), a.invoke());
032: a = TypeAdapter.on(f, f.getClass().getField("ch"));
033: a.set(a.parse("abc"));
034: assertEquals('a', f.ch);
035: a = TypeAdapter.on(f, f.getClass().getField("name"));
036: a.set(a.parse("xyzzy"));
037: assertEquals("xyzzy", f.name);
038: a = TypeAdapter.on(f, f.getClass().getField("sampleFloat"));
039: a.set(a.parse("6.02e23"));
040: assertEquals(6.02e23, f.sampleFloat, 1e17);
041: a = TypeAdapter.on(f, f.getClass().getField("sampleArray"));
042: a.set(a.parse("1,2,3"));
043: assertEquals(1, f.sampleArray[0]);
044: assertEquals(2, f.sampleArray[1]);
045: assertEquals(3, f.sampleArray[2]);
046: assertEquals("1, 2, 3", a.toString(f.sampleArray));
047: assertTrue(a.equals(new int[] { 1, 2, 3 }, f.sampleArray));
048: a = TypeAdapter.on(f, f.getClass().getField("sampleDate"));
049: Date date = new GregorianCalendar(1949, 4, 26).getTime();
050: a.set(a.parse(DateFormat.getDateInstance().format(date)));
051: assertEquals(date, f.sampleDate);
052: a = TypeAdapter.on(f, f.getClass().getField("sampleByte"));
053: a.set(a.parse("123"));
054: assertEquals(123, f.sampleByte);
055: a = TypeAdapter.on(f, f.getClass().getField("sampleShort"));
056: a.set(a.parse("12345"));
057: assertEquals(12345, f.sampleShort);
058: }
059:
060: public void testScientificDouble() {
061: Double pi = new Double(3.141592865);
062: assertEquals(ScientificDouble.valueOf("3.14"), pi);
063: assertEquals(ScientificDouble.valueOf("3.142"), pi);
064: assertEquals(ScientificDouble.valueOf("3.1416"), pi);
065: assertEquals(ScientificDouble.valueOf("3.14159"), pi);
066: assertEquals(ScientificDouble.valueOf("3.141592865"), pi);
067: assertTrue(!ScientificDouble.valueOf("3.140").equals(pi));
068: assertTrue(!ScientificDouble.valueOf("3.144").equals(pi));
069: assertTrue(!ScientificDouble.valueOf("3.1414").equals(pi));
070: assertTrue(!ScientificDouble.valueOf("3.141592863").equals(pi));
071: assertEquals(ScientificDouble.valueOf("6.02e23"), new Double(
072: 6.02e23));
073: assertEquals(ScientificDouble.valueOf("6.02E23"), new Double(
074: 6.024E23));
075: assertEquals(ScientificDouble.valueOf("6.02e23"), new Double(
076: 6.016e23));
077: assertTrue(!ScientificDouble.valueOf("6.02e23").equals(
078: new Double(6.026e23)));
079: assertTrue(!ScientificDouble.valueOf("6.02e23").equals(
080: new Double(6.014e23)));
081: }
082:
083: public void testEscape() {
084: String junk = "!@#$%^*()_-+={}|[]\\:\";',./?`";
085: assertEquals(junk, Fixture.escape(junk));
086: assertEquals("", Fixture.escape(""));
087: assertEquals("<", Fixture.escape("<"));
088: assertEquals("<<", Fixture.escape("<<"));
089: assertEquals("x<", Fixture.escape("x<"));
090: assertEquals("&", Fixture.escape("&"));
091: assertEquals("<&<", Fixture.escape("<&<"));
092: assertEquals("&<&", Fixture.escape("&<&"));
093: assertEquals("a < b && c < d", Fixture
094: .escape("a < b && c < d"));
095: assertEquals("a<br />b", Fixture.escape("a\nb"));
096: }
097:
098: public void testRuns() throws Exception {
099: run("arithmetic", 37, 10, 0, 2);
100: run("CalculatorExample", 75, 9, 0, 0);
101: run("MusicExample", 95, 0, 0, 0);
102: }
103:
104: protected void run(String file, int right, int wrong, int ignores,
105: int exceptions) throws Exception {
106: String input = read(new File("../../examples/" + file + ".html"));
107: Fixture fixture = new Fixture();
108: Parse tables;
109: if (input.indexOf("<wiki>") >= 0) {
110: tables = new Parse(input, new String[] { "wiki", "table",
111: "tr", "td" });
112: fixture.doTables(tables.parts);
113: } else {
114: tables = new Parse(input, new String[] { "table", "tr",
115: "td" });
116: fixture.doTables(tables);
117: }
118: PrintWriter output = new PrintWriter(new BufferedWriter(
119: new FileWriter("output/test/" + file + ".html")));
120: tables.print(output);
121: output.close();
122: assertEquals(file + " right", right, fixture.counts.right);
123: assertEquals(file + " wrong", wrong, fixture.counts.wrong);
124: assertEquals(file + " ignores", ignores, fixture.counts.ignores);
125: assertEquals(file + " excpetions", exceptions,
126: fixture.counts.exceptions);
127: }
128:
129: protected String read(File input) throws IOException {
130: char chars[] = new char[(int) (input.length())];
131: FileReader in = new FileReader(input);
132: in.read(chars);
133: in.close();
134: return new String(chars);
135: }
136:
137: class TestFixture extends ColumnFixture { // used in testTypeAdapter
138: public byte sampleByte;
139: public short sampleShort;
140: public int sampleInt;
141: public Integer sampleInteger;
142: public float sampleFloat;
143:
144: public double pi() {
145: return 3.14159862;
146: }
147:
148: public char ch;
149: public String name;
150: public int[] sampleArray;
151: public Date sampleDate;
152: }
153: }
|