KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > SymbolTestCase


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.test;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import net.sourceforge.chaperon.build.EmptyList;
16 import net.sourceforge.chaperon.model.symbol.Nonterminal;
17 import net.sourceforge.chaperon.model.symbol.Terminal;
18
19 public class SymbolTestCase extends TestCase
20 {
21   public SymbolTestCase()
22   {
23     super("SymbolTestCase");
24   }
25
26   public static void assertNotEquals(String JavaDoc message, Object JavaDoc a, Object JavaDoc b)
27   {
28     if ((a==null) || (a==null))
29       return;
30
31     if (a.equals(b))
32     {
33       String JavaDoc formatted = "";
34       if (message!=null)
35         formatted = message+" ";
36
37       fail(formatted+"<"+a+"> equals <"+b+">");
38     }
39   }
40
41   public void runTest() throws Throwable JavaDoc
42   {
43     // Test for terminals
44
Terminal a = new Terminal("a");
45     Terminal b = new Terminal("b");
46
47     assertEquals("Test if symbol names are equal", "a", a.getName());
48     assertEquals("Test if symbol names are equal", "b", b.getName());
49
50     assertEquals("Test if symbols are equal", a, a);
51     assertNotEquals("Test if symbols are not equal", a, b);
52     assertEquals("Test if hashcodes are equals", a.hashCode(), a.hashCode());
53
54     Terminal a2 = new Terminal("a");
55
56     assertEquals("Test if symbols are equal", a, a2);
57     assertNotEquals("Test if symbols are not equal", a2, b);
58     assertEquals("Test if hashcodes are equals", a.hashCode(), a2.hashCode());
59     assertTrue("Test if hashcodes are no equal", a2.hashCode()!=b.hashCode());
60
61     // Test for nonterminals
62
Nonterminal A = new Nonterminal("A");
63     Nonterminal B = new Nonterminal("B");
64
65     assertEquals("Test if symbol names are equal", "A", A.getName());
66     assertEquals("Test if symbol names are equal", "B", B.getName());
67
68     assertEquals("Test if symbols are equal", A, A);
69     assertNotEquals("Test if symbols are not equal", A, B);
70     assertEquals("Test if hashcodes are equals", A.hashCode(), A.hashCode());
71
72     Nonterminal A2 = new Nonterminal("A");
73
74     assertEquals("Test if symbols are equal", A, A2);
75     assertNotEquals("Test if symbols are not equal", A2, B);
76     assertEquals("Test if hashcodes are equal", A.hashCode(), A2.hashCode());
77     assertTrue("Test if hashcodes are no equal", A2.hashCode()!=B.hashCode());
78
79     // Test for emptylist symbols
80
EmptyList emptylist = new EmptyList();
81     EmptyList emptylist2 = new EmptyList();
82
83     assertEquals("Test if symbols are equal", emptylist, emptylist);
84     assertEquals("Test if symbols are equal", emptylist, emptylist2);
85
86     // Composite tests
87
Terminal a3 = new Terminal("A");
88     Nonterminal A3 = new Nonterminal("a");
89
90     assertNotEquals("Test if symbols are not equal", a3, A);
91     assertNotEquals("Test if symbols are not equal", a, A3);
92     assertNotEquals("Test if symbols are not equal", a, emptylist);
93     assertNotEquals("Test if symbols are not equal", A, emptylist);
94     assertTrue("Test if hashcodes are no equal", a.hashCode()!=emptylist.hashCode());
95     assertTrue("Test if hashcodes are no equal", A.hashCode()!=emptylist.hashCode());
96
97     try
98     {
99       Terminal a4 = new Terminal(null);
100       fail("Test if exception occurs");
101     }
102     catch (Exception JavaDoc e) {}
103
104     try
105     {
106       Nonterminal A4 = new Nonterminal(null);
107       fail("Test if exception occurs");
108     }
109     catch (Exception JavaDoc e) {}
110
111     try
112     {
113       Terminal a4 = new Terminal("");
114       fail("Test if exception occurs");
115     }
116     catch (Exception JavaDoc e) {}
117
118     try
119     {
120       Nonterminal A4 = new Nonterminal("");
121       fail("Test if exception occurs");
122     }
123     catch (Exception JavaDoc e) {}
124   }
125
126   public static Test suite()
127   {
128     TestSuite suite = new TestSuite("Symbol tests");
129     suite.addTest(new SymbolTestCase());
130     return suite;
131   }
132 }
133
Popular Tags