View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 10:15:32 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1913 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/graph/src/test/java/name/angoca/zemucan/core/graph/model/EndingNodeTest.java $:
28   */
29  package name.angoca.zemucan.core.graph.model;
30  
31  import name.angoca.zemucan.AbstractZemucanException;
32  import name.angoca.zemucan.core.graph.api.ChildEndingNodeException;
33  import name.angoca.zemucan.tools.Constants;
34  import name.angoca.zemucan.tools.test.RandomTestRunner;
35  
36  import org.junit.Assert;
37  import org.junit.Test;
38  import org.junit.runner.RunWith;
39  
40  /**
41   * These are the tests for the EndingNode class.
42   * <p>
43   * <b>Control Version</b>
44   * <p>
45   * <ul>
46   * <li>0.0.1 Class creation.</li>
47   * <li>0.0.2 Recommendations from PMD.</li>
48   * <li>0.0.3 Organized.</li>
49   * <li>0.1.0 jUnit 4 annotations.</li>
50   * <li>0.1.1 Messages for the tests.</li>
51   * <li>0.1.2 final.</li>
52   * <li>1.0.0 Moved to version 1.</li>
53   * <li>1.1.0 Exception hierarchy changed.</li>
54   * <li>1.2.0 Symmetric and congruent test, and transitivity.</li>
55   * <li>1.2.1 Tests corrections.</li>
56   * <li>1.3.0 addWay test.</li>
57   * <li>1.3.1 GraphToken renamed by GraphNode</li>
58   * <li>1.3.2 Token constant renamed to Node.</li>
59   * <li>1.4.0 GrammarReader separated from Graph.</li>
60   * <li>1.5.0 Node.</li>
61   * <li>1.5.1 Nake method.</li>
62   * <li>1.5.2 GraphNode Hierarchy.</li>
63   * </ul>
64   *
65   * @author Andres Gomez Casanova <a
66   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
67   * @version 1.5.2 2010-08-29
68   */
69  @RunWith(RandomTestRunner.class)
70  public final class EndingNodeTest {
71      /**
72       * Word 'EndingNode'.
73       */
74      private static final String ENDING_NODE = "EndingNode"; //$NON-NLS-1$
75  
76      /**
77       * Default constructor.
78       */
79      public EndingNodeTest() {
80          // Nothing.
81      }
82  
83      /**
84       * Tests that an EndingNode cannot have children.
85       *
86       * @throws AbstractZemucanException
87       *             Never.
88       */
89      @Test
90      public void testAddWay() throws AbstractZemucanException {
91          final EndingNode actual = new EndingNode(new Graph("test", false));
92  
93          Exception e = null;
94          try {
95              actual.addChild(null);
96          } catch (final Exception exception) {
97              e = exception;
98          }
99          Assert.assertTrue("ChildEndingNodeException",
100                 e instanceof ChildEndingNodeException);
101     }
102 
103     /**
104      * Tests if two consecutive calls returns the same value.
105      *
106      * @throws AbstractZemucanException
107      *             Never
108      */
109     @Test
110     public void testCongruent() throws AbstractZemucanException {
111         final Graph graph = new Graph("test", false);
112 
113         final EndingNode node1 = new EndingNode(graph);
114         final int val1 = node1.hashCode();
115         final int val2 = node1.hashCode();
116 
117         final boolean val = val1 == val2;
118 
119         Assert.assertTrue("Two consecutive calls return the same value", val); //$NON-NLS-1$
120     }
121 
122     /**
123      * Test that the hash code is different to hashCode of StartingNode.
124      *
125      * @throws AbstractZemucanException
126      *             Never
127      */
128     @Test
129     public void testDifferentStartingNodeHashCode()
130             throws AbstractZemucanException {
131         final Graph graph = new Graph("test", false);
132 
133         final EndingNode actual = new EndingNode(graph);
134         final StartingNode notExpected = new StartingNode(graph);
135 
136         final boolean condition = notExpected.hashCode() == actual.hashCode();
137 
138         Assert.assertFalse(
139                 "EndingNode has a hashcode different to StartingNode", //$NON-NLS-1$
140                 condition);
141     }
142 
143     /**
144      * Test the equals method with a graph node non reserved.
145      *
146      * @throws AbstractZemucanException
147      *             Never
148      */
149     @Test
150     public void testEqualsNodeNotReserved() throws AbstractZemucanException {
151         final String node = EndingNodeTest.ENDING_NODE;
152         final Graph graph = new Graph("test", false);
153 
154         final EndingNode actual = new EndingNode(graph);
155         final GraphNode notExpected = new GraphNode(node, graph);
156 
157         Assert.assertFalse(
158                 "Equals between EndingNode and a non reserved GraphNode", //$NON-NLS-1$
159                 actual.equals(notExpected));
160         Assert.assertFalse(
161                 "Equals between EndingNode and a non reserved GraphNode 2", //$NON-NLS-1$
162                 notExpected.equals(actual));
163     }
164 
165     /**
166      * Test the equals method with a graph node reserved.
167      *
168      * @throws AbstractZemucanException
169      *             Never
170      */
171     @Test
172     public void testEqualsNodeReserved() throws AbstractZemucanException {
173         final String node = EndingNodeTest.ENDING_NODE;
174         final Graph graph = new Graph("test", false);
175 
176         final EndingNode actual = new EndingNode(graph);
177         final GraphNode notExpected = new GraphNode(node, graph);
178 
179         Assert.assertFalse(
180                 "Equals between EndingNode and a reserved GraphNode", //$NON-NLS-1$
181                 actual.equals(notExpected));
182         Assert.assertFalse(
183                 "Equals between EndingNode and a reserved GraphNode 2", //$NON-NLS-1$
184                 notExpected.equals(actual));
185     }
186 
187     /**
188      * Test that the hash code is different to 1, 0 and -1.
189      *
190      * @throws AbstractZemucanException
191      *             Never
192      */
193     @Test
194     public void testHashCode() throws AbstractZemucanException {
195         final EndingNode actual = new EndingNode(new Graph("test", false));
196 
197         Assert.assertTrue("HashCode different to 1", //$NON-NLS-1$
198                 1 != actual.hashCode());
199         Assert.assertTrue("HashCode different to 0", //$NON-NLS-1$
200                 0 != actual.hashCode());
201         Assert.assertTrue("HashCode different to -1", //$NON-NLS-1$
202                 -1 != actual.hashCode());
203     }
204 
205     /**
206      * Test the equals method to a null.
207      *
208      * @throws AbstractZemucanException
209      *             Never
210      */
211     @Test
212     public void testNullEqualsObject() throws AbstractZemucanException {
213         final EndingNode actual = new EndingNode(new Graph("test", false));
214 
215         final EndingNode nullObject = null;
216 
217         final boolean condition = actual.equals(nullObject);
218 
219         Assert.assertFalse("Equals in EndingNode with null", condition); //$NON-NLS-1$
220     }
221 
222     /**
223      * Test the equals method to a different type of object.
224      *
225      * @throws AbstractZemucanException
226      *             Never
227      */
228     @Test
229     public void testOtherTypeEqualsObject() throws AbstractZemucanException {
230         final Graph graph = new Graph("test", false);
231 
232         final EndingNode actual = new EndingNode(graph);
233         final StartingNode notExpected = new StartingNode(graph);
234 
235         Assert.assertFalse("Equals between EndingNode and StartingNode", //$NON-NLS-1$
236                 actual.equals(notExpected));
237         Assert.assertFalse("Equals between EndingNode and StartingNode 2", //$NON-NLS-1$
238                 notExpected.equals(actual));
239     }
240 
241     /**
242      * Test the equals method to the same object.
243      *
244      * @throws AbstractZemucanException
245      *             Never
246      */
247     @Test
248     public void testSameEqualsObject() throws AbstractZemucanException {
249         final Graph graph = new Graph("test", false);
250 
251         final EndingNode actual = new EndingNode(graph);
252         final EndingNode expected = new EndingNode(graph);
253 
254         Assert.assertTrue("Two EndingNode are equals", //$NON-NLS-1$
255                 actual.equals(expected));
256         Assert.assertTrue("Two EndingNode are equals 2", //$NON-NLS-1$
257                 expected.equals(actual));
258     }
259 
260     /**
261      * Test that the hash code is the same.
262      *
263      * @throws AbstractZemucanException
264      *             Never
265      */
266     @Test
267     public void testSameHashCode() throws AbstractZemucanException {
268         final Graph graph = new Graph("test", false);
269 
270         final EndingNode actual = new EndingNode(graph);
271         final EndingNode expected = new EndingNode(graph);
272 
273         Assert.assertEquals(
274                 "Two endingNode have the prodcue the same hashcode.", //$NON-NLS-1$
275                 expected.hashCode(), actual.hashCode());
276     }
277 
278     /**
279      * Tests the symmetry of equals.
280      *
281      * @throws AbstractZemucanException
282      *             Never.
283      */
284     @Test
285     public void testSymmetric() throws AbstractZemucanException {
286         final EndingNode actual = new EndingNode(new Graph("test", false));
287 
288         final boolean value = actual.equals(actual);
289 
290         Assert.assertTrue("Symmetry", value); //$NON-NLS-1$
291     }
292 
293     /**
294      * Test the toString method.
295      *
296      * @throws AbstractZemucanException
297      *             Never
298      */
299     @Test
300     public void testToString() throws AbstractZemucanException {
301         final EndingNode actual = new EndingNode(new Graph("test", false));
302         final String expected = '{' + Constants.ENDING_NODE + '}';
303 
304         Assert.assertEquals("Description of an EndingNode", expected, //$NON-NLS-1$
305                 actual.toString());
306     }
307 }