001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: import net.sourceforge.chaperon.build.EmptyList;
016: import net.sourceforge.chaperon.model.symbol.Nonterminal;
017: import net.sourceforge.chaperon.model.symbol.Terminal;
018:
019: public class SymbolTestCase extends TestCase {
020: public SymbolTestCase() {
021: super ("SymbolTestCase");
022: }
023:
024: public static void assertNotEquals(String message, Object a,
025: Object b) {
026: if ((a == null) || (a == null))
027: return;
028:
029: if (a.equals(b)) {
030: String formatted = "";
031: if (message != null)
032: formatted = message + " ";
033:
034: fail(formatted + "<" + a + "> equals <" + b + ">");
035: }
036: }
037:
038: public void runTest() throws Throwable {
039: // Test for terminals
040: Terminal a = new Terminal("a");
041: Terminal b = new Terminal("b");
042:
043: assertEquals("Test if symbol names are equal", "a", a.getName());
044: assertEquals("Test if symbol names are equal", "b", b.getName());
045:
046: assertEquals("Test if symbols are equal", a, a);
047: assertNotEquals("Test if symbols are not equal", a, b);
048: assertEquals("Test if hashcodes are equals", a.hashCode(), a
049: .hashCode());
050:
051: Terminal a2 = new Terminal("a");
052:
053: assertEquals("Test if symbols are equal", a, a2);
054: assertNotEquals("Test if symbols are not equal", a2, b);
055: assertEquals("Test if hashcodes are equals", a.hashCode(), a2
056: .hashCode());
057: assertTrue("Test if hashcodes are no equal", a2.hashCode() != b
058: .hashCode());
059:
060: // Test for nonterminals
061: Nonterminal A = new Nonterminal("A");
062: Nonterminal B = new Nonterminal("B");
063:
064: assertEquals("Test if symbol names are equal", "A", A.getName());
065: assertEquals("Test if symbol names are equal", "B", B.getName());
066:
067: assertEquals("Test if symbols are equal", A, A);
068: assertNotEquals("Test if symbols are not equal", A, B);
069: assertEquals("Test if hashcodes are equals", A.hashCode(), A
070: .hashCode());
071:
072: Nonterminal A2 = new Nonterminal("A");
073:
074: assertEquals("Test if symbols are equal", A, A2);
075: assertNotEquals("Test if symbols are not equal", A2, B);
076: assertEquals("Test if hashcodes are equal", A.hashCode(), A2
077: .hashCode());
078: assertTrue("Test if hashcodes are no equal", A2.hashCode() != B
079: .hashCode());
080:
081: // Test for emptylist symbols
082: EmptyList emptylist = new EmptyList();
083: EmptyList emptylist2 = new EmptyList();
084:
085: assertEquals("Test if symbols are equal", emptylist, emptylist);
086: assertEquals("Test if symbols are equal", emptylist, emptylist2);
087:
088: // Composite tests
089: Terminal a3 = new Terminal("A");
090: Nonterminal A3 = new Nonterminal("a");
091:
092: assertNotEquals("Test if symbols are not equal", a3, A);
093: assertNotEquals("Test if symbols are not equal", a, A3);
094: assertNotEquals("Test if symbols are not equal", a, emptylist);
095: assertNotEquals("Test if symbols are not equal", A, emptylist);
096: assertTrue("Test if hashcodes are no equal",
097: a.hashCode() != emptylist.hashCode());
098: assertTrue("Test if hashcodes are no equal",
099: A.hashCode() != emptylist.hashCode());
100:
101: try {
102: Terminal a4 = new Terminal(null);
103: fail("Test if exception occurs");
104: } catch (Exception e) {
105: }
106:
107: try {
108: Nonterminal A4 = new Nonterminal(null);
109: fail("Test if exception occurs");
110: } catch (Exception e) {
111: }
112:
113: try {
114: Terminal a4 = new Terminal("");
115: fail("Test if exception occurs");
116: } catch (Exception e) {
117: }
118:
119: try {
120: Nonterminal A4 = new Nonterminal("");
121: fail("Test if exception occurs");
122: } catch (Exception e) {
123: }
124: }
125:
126: public static Test suite() {
127: TestSuite suite = new TestSuite("Symbol tests");
128: suite.addTest(new SymbolTestCase());
129: return suite;
130: }
131: }
|