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/StartingNodeTest.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.ParentStartingNodeException;
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 StartingNode 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 final.</li>
51   * <li>1.0.0 Moved to version 1.</li>
52   * <li>1.1.0 Exception hierarchy changed.</li>
53   * <li>1.2.0 Symmetric and congruent tests.</li>
54   * <li>1.3.0 addParent.</li>
55   * <li>1.3.1 GraphToken renamed by GraphNode</li>
56   * <li>1.3.2 Token constant renamed to Node.</li>
57   * <li>1.4.0 GrammarReader separated from Graph.</li>
58   * <li>1.5.0 Node.</li>
59   * <li>1.5.1 GraphNode Hierarchy.</li>
60   * </ul>
61   *
62   * @author Andres Gomez Casanova <a
63   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
64   * @version 1.5.1 2010-08-29
65   */
66  @RunWith(RandomTestRunner.class)
67  public final class StartingNodeTest {
68      /**
69       * Default constructor.
70       */
71      public StartingNodeTest() {
72          // Nothing.
73      }
74  
75      /**
76       * Tests that an StartingNode cannot have parents.
77       *
78       * @throws AbstractZemucanException
79       *             Never.
80       */
81      @Test
82      public void testAddParent() throws AbstractZemucanException {
83          final StartingNode actual = new StartingNode(new Graph("test", false));
84  
85          Exception e = null;
86          try {
87              actual.addParent(null);
88          } catch (final Exception exception) {
89              e = exception;
90          }
91          Assert.assertTrue("ParentStartingNodeException",
92                  e instanceof ParentStartingNodeException);
93      }
94  
95      /**
96       * Tests if two consecutive calls returns the same value.
97       *
98       * @throws AbstractZemucanException
99       *             Never
100      */
101     @Test
102     public void testCongruent() throws AbstractZemucanException {
103         final String node = "test"; //$NON-NLS-1$
104 
105         final GraphNode node1 = new GraphNode(node, new Graph("test", false));
106         final int val1 = node1.hashCode();
107         final int val2 = node1.hashCode();
108 
109         final boolean condition = val1 == val2;
110 
111         Assert.assertTrue("Two consecutive calls return the same value", //$NON-NLS-1$
112                 condition);
113     }
114 
115     /**
116      * Test that the hash code is different to hashCode of EndingNode.
117      *
118      * @throws AbstractZemucanException
119      *             Never.
120      */
121     @Test
122     public void testDifferentStartingNodeHashCode()
123             throws AbstractZemucanException {
124         final Graph graph = new Graph("test", false);
125 
126         final StartingNode actual = new StartingNode(graph);
127         final EndingNode notExpected = new EndingNode(graph);
128 
129         final boolean condition = notExpected.hashCode() == actual.hashCode();
130 
131         Assert.assertFalse("hashcode for two different objects", condition); //$NON-NLS-1$
132     }
133 
134     /**
135      * Test the equals method with a graph node non reserved.
136      *
137      * @throws AbstractZemucanException
138      *             Never.
139      */
140     @Test
141     public void testEqualsNodeNotReserved() throws AbstractZemucanException {
142         final String node = "StartingNode"; //$NON-NLS-1$
143         final Graph graph = new Graph("test", false);
144 
145         final StartingNode actual = new StartingNode(graph);
146         final GraphNode notExpected = new GraphNode(node, graph);
147 
148         Assert.assertFalse("Equals for a non reserved object", //$NON-NLS-1$
149                 actual.equals(notExpected));
150         Assert.assertFalse("Equals for a non reserved object 2", //$NON-NLS-1$
151                 notExpected.equals(actual));
152     }
153 
154     /**
155      * Test the equals method with a graph node reserved.
156      *
157      * @throws AbstractZemucanException
158      *             Never.
159      */
160     @Test
161     public void testEqualsNodeReserved() throws AbstractZemucanException {
162         final String node = "StartingNode"; //$NON-NLS-1$
163         final Graph graph = new Graph("test", false);
164 
165         final StartingNode actual = new StartingNode(graph);
166         final GraphNode notExpected = new GraphNode(node, graph);
167 
168         Assert.assertFalse("equals for a reserved object", //$NON-NLS-1$
169                 actual.equals(notExpected));
170         Assert.assertFalse("equals for a reserved object 2", //$NON-NLS-1$
171                 notExpected.equals(actual));
172     }
173 
174     /**
175      * Test that the hash code is different to 1, 0 and -1.
176      *
177      * @throws AbstractZemucanException
178      *             Never.
179      */
180     @Test
181     public void testHashCode() throws AbstractZemucanException {
182         final StartingNode actual = new StartingNode(new Graph("test", false));
183 
184         Assert.assertTrue("hashcode different to 1", //$NON-NLS-1$
185                 1 != actual.hashCode());
186         Assert.assertTrue("hashcode different to 0", //$NON-NLS-1$
187                 0 != actual.hashCode());
188         Assert.assertTrue("hashcode different to -1", //$NON-NLS-1$
189                 -1 != actual.hashCode());
190     }
191 
192     /**
193      * Test the equals method to a null.
194      *
195      * @throws AbstractZemucanException
196      *             Never.
197      */
198     @Test
199     public void testNullEqualsObject() throws AbstractZemucanException {
200         final StartingNode actual = new StartingNode(new Graph("test", false));
201 
202         final StartingNode nullObject = null;
203 
204         final boolean condition = actual.equals(nullObject);
205 
206         Assert.assertFalse("Equals with a null value", condition); //$NON-NLS-1$
207     }
208 
209     /**
210      * Test the equals method to a different type of object.
211      *
212      * @throws AbstractZemucanException
213      *             Never.
214      */
215     @Test
216     public void testOtherTypeEqualsObject() throws AbstractZemucanException {
217         final Graph graph = new Graph("test", false);
218 
219         final StartingNode actual = new StartingNode(graph);
220         final EndingNode notExpected = new EndingNode(graph);
221 
222         Assert.assertFalse("equals for two different objects", //$NON-NLS-1$
223                 actual.equals(notExpected));
224         Assert.assertFalse("equals for two different objects 2", //$NON-NLS-1$
225                 notExpected.equals(actual));
226     }
227 
228     /**
229      * Test the equals method to the same object.
230      *
231      * @throws AbstractZemucanException
232      *             Never.
233      */
234     @Test
235     public void testSameEqualsObject() throws AbstractZemucanException {
236         final Graph graph = new Graph("test", false);
237 
238         final StartingNode actual = new StartingNode(graph);
239         final StartingNode expected = new StartingNode(graph);
240 
241         Assert.assertTrue("equals fot two identical objects", //$NON-NLS-1$
242                 actual.equals(expected));
243         Assert.assertTrue("equals fot two identical objects 2", //$NON-NLS-1$
244                 expected.equals(actual));
245     }
246 
247     /**
248      * Test that the hash code is the same.
249      *
250      * @throws AbstractZemucanException
251      *             Never.
252      */
253     @Test
254     public void testSameHashCode() throws AbstractZemucanException {
255         final Graph graph = new Graph("test", false);
256 
257         final StartingNode actual = new StartingNode(graph);
258         final StartingNode expected = new StartingNode(graph);
259 
260         Assert.assertEquals("hashcode for two identical objects", //$NON-NLS-1$
261                 expected.hashCode(), actual.hashCode());
262     }
263 
264     /**
265      * Tests the symmetry of equals.
266      *
267      * @throws AbstractZemucanException
268      *             Never.
269      */
270     @Test
271     public void testSymmetric() throws AbstractZemucanException {
272         final StartingNode actual = new StartingNode(new Graph("test", false));
273 
274         final boolean value = actual.equals(actual);
275 
276         Assert.assertTrue("Symmetry", value); //$NON-NLS-1$
277     }
278 
279     /**
280      * Test the toString method.
281      *
282      * @throws AbstractZemucanException
283      *             Never.
284      */
285     @Test
286     public void testToString() throws AbstractZemucanException {
287         final StartingNode actual = new StartingNode(new Graph("test", false));
288         final String expected = '{' + Constants.STARTING_NODE + '}';
289 
290         Assert.assertEquals("toString", expected, actual.toString()); //$NON-NLS-1$
291     }
292 }