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/AboutNodeTest.java $:
28   */
29  package name.angoca.zemucan.core.graph.model;
30  
31  import java.io.File;
32  
33  import name.angoca.zemucan.AbstractZemucanException;
34  import name.angoca.zemucan.tools.Constants;
35  import name.angoca.zemucan.tools.test.RandomTestRunner;
36  
37  import org.junit.Assert;
38  import org.junit.Test;
39  import org.junit.runner.RunWith;
40  
41  /**
42   * These are the tests for the AboutNode class.
43   * <p>
44   * <b>Control Version</b>
45   * <p>
46   * <ul>
47   * <li>0.1.0 Class creation</li>
48   * <li>1.0.1 GraphToken renamed by GraphNode</li>
49   * <li>1.1.0 Node.</li>
50   * <li>1.1.1 ToString and methods name.</li>
51   * <li>1.1.2 Extra tokens problem.</li>
52   * <li>1.1.3 GraphNode Hierarchy.</li>
53   * </ul>
54   *
55   * @author Andres Gomez Casanova <a
56   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
57   * @version 1.1.3 2010-08-29
58   */
59  @RunWith(RandomTestRunner.class)
60  public final class AboutNodeTest {
61      /**
62       * About node string.
63       */
64      private static final String ABOUT_NODE = "AboutNode";
65  
66      /**
67       * Default constructor.
68       */
69      public AboutNodeTest() {
70          // Nothing.
71      }
72  
73      /**
74       * Test the toString method with about information file.
75       *
76       * @throws AbstractZemucanException
77       *             Never
78       */
79      @Test
80      public void testAboutInformationFromFile()
81              throws AbstractZemucanException {
82          final AboutNode actual = new AboutNode(new Graph("", false));
83  
84          final String expected = AboutNode.readAboutInformation() + "::" + '{'
85                  + Constants.ABOUT_CONTENT_TOKEN_ID + "\n0\n0\nfalse|false}"; //$NON-NLS-1$
86  
87          Assert.assertEquals(
88                  "Description of an AboutNode with about information from a file", //$NON-NLS-1$
89                  expected, actual.toString());
90      }
91  
92      /**
93       * Tests if two consecutive calls returns the same value.
94       *
95       * @throws AbstractZemucanException
96       *             Never
97       */
98      @Test
99      public void testCongruent() throws AbstractZemucanException {
100         final AboutNode node1 = new AboutNode(new Graph("test", false));
101         final int val1 = node1.hashCode();
102         final int val2 = node1.hashCode();
103 
104         final boolean val = val1 == val2;
105 
106         Assert.assertTrue("Two consecutive calls return the same value", val); //$NON-NLS-1$
107     }
108 
109     /**
110      * Test the toString method with no about information file.
111      *
112      * @throws AbstractZemucanException
113      *             Never
114      */
115     @Test
116     public void testDefaultAboutInformation()
117             throws AbstractZemucanException {
118         final File file1 = new File(AboutNode.ABOUT_FILE);
119         final File file2 = new File(AboutNode.ABOUT_FILE + '2');
120 
121         file1.renameTo(file2);
122         final AboutNode actualNode = new AboutNode(new Graph("test", false));
123         file2.renameTo(file1);
124 
125         final String expected = AboutNode.IDENTIFICATION + "::" + '{'
126                 + Constants.ABOUT_CONTENT_TOKEN_ID + "\n0\n0\nfalse|false}"; //$NON-NLS-1$
127 
128         final String actual = actualNode.toString();
129 
130         Assert.assertEquals("Description of an AboutNode with default value", //$NON-NLS-1$
131                 expected, actual);
132     }
133 
134     /**
135      * Test that the hash code is different to hashCode of StartingNode.
136      *
137      * @throws AbstractZemucanException
138      *             Never
139      */
140     @Test
141     public void testDifferentStartingNodeHashCode()
142             throws AbstractZemucanException {
143         final Graph graph = new Graph("test", false);
144 
145         final AboutNode actual = new AboutNode(graph);
146         final StartingNode notExpected = new StartingNode(graph);
147 
148         final boolean condition = notExpected.hashCode() == actual.hashCode();
149 
150         Assert.assertFalse(
151                 "AboutNode has a hashcode different to StartingNode", //$NON-NLS-1$
152                 condition);
153     }
154 
155     /**
156      * Test the equals method with a graph node non reserved.
157      *
158      * @throws AbstractZemucanException
159      *             Never
160      */
161     @Test
162     public void testEqualsNodeNotReserved() throws AbstractZemucanException {
163         final String node = AboutNodeTest.ABOUT_NODE;
164         final Graph graph = new Graph("test", false);
165 
166         final AboutNode actual = new AboutNode(graph);
167         final GraphNode notExpected = new GraphNode(node, graph);
168 
169         Assert.assertFalse(
170                 "Equals between AboutNode and a non reserved GraphNode", //$NON-NLS-1$
171                 actual.equals(notExpected));
172         Assert.assertFalse(
173                 "Equals between AboutNode and a non reserved GraphNode 2", //$NON-NLS-1$
174                 notExpected.equals(actual));
175     }
176 
177     /**
178      * Test the equals method with a graph node reserved.
179      *
180      * @throws AbstractZemucanException
181      *             Never
182      */
183     @Test
184     public void testEqualsNodeReserved() throws AbstractZemucanException {
185         final String node = AboutNodeTest.ABOUT_NODE;
186         final Graph graph = new Graph("test", false);
187 
188         final AboutNode actual = new AboutNode(graph);
189         final GraphNode notExpected = new GraphNode(node, graph);
190 
191         Assert.assertFalse("Equals between AboutNode and a reserved GraphNode", //$NON-NLS-1$
192                 actual.equals(notExpected));
193         Assert.assertFalse(
194                 "Equals between AboutNode and a reserved GraphNode 2", //$NON-NLS-1$
195                 notExpected.equals(actual));
196     }
197 
198     /**
199      * Test that the hash code is different to 1, 0 and -1.
200      *
201      * @throws AbstractZemucanException
202      *             Never
203      */
204     @Test
205     public void testHashCode() throws AbstractZemucanException {
206         final AboutNode actual = new AboutNode(new Graph("test", false));
207 
208         Assert.assertTrue("HashCode different to 1", //$NON-NLS-1$
209                 1 != actual.hashCode());
210         Assert.assertTrue("HashCode different to 0", //$NON-NLS-1$
211                 0 != actual.hashCode());
212         Assert.assertTrue("HashCode different to -1", //$NON-NLS-1$
213                 -1 != actual.hashCode());
214     }
215 
216     /**
217      * Test the equals method to a null.
218      *
219      * @throws AbstractZemucanException
220      *             Never
221      */
222     @Test
223     public void testNullEqualsObject() throws AbstractZemucanException {
224         final AboutNode actual = new AboutNode(new Graph("test", false));
225 
226         final AboutNode nullObject = null;
227 
228         final boolean condition = actual.equals(nullObject);
229 
230         Assert.assertFalse("Equals in AboutNode with null", condition); //$NON-NLS-1$
231     }
232 
233     /**
234      * Test the equals method to a different type of object.
235      *
236      * @throws AbstractZemucanException
237      *             Never
238      */
239     @Test
240     public void testOtherTypeEqualsObject() throws AbstractZemucanException {
241         final Graph graph = new Graph("test", false);
242 
243         final AboutNode actual = new AboutNode(graph);
244         final StartingNode notExpected = new StartingNode(graph);
245 
246         Assert.assertFalse("Equals between AboutNode and StartingNode", //$NON-NLS-1$
247                 actual.equals(notExpected));
248         Assert.assertFalse("Equals between AboutNode and StartingNode 2", //$NON-NLS-1$
249                 notExpected.equals(actual));
250     }
251 
252     /**
253      * Test the equals method to the same object.
254      *
255      * @throws AbstractZemucanException
256      *             Never
257      */
258     @Test
259     public void testSameEqualsObject() throws AbstractZemucanException {
260         final AboutNode actual = new AboutNode(new Graph("test", false));
261         final AboutNode expected = new AboutNode(new Graph("test", false));
262 
263         Assert.assertTrue("Two AboutNode are equals", //$NON-NLS-1$
264                 actual.equals(expected));
265         Assert.assertTrue("Two AboutNode are equals 2", //$NON-NLS-1$
266                 expected.equals(actual));
267     }
268 
269     /**
270      * Test that the hash code is the same.
271      *
272      * @throws AbstractZemucanException
273      *             Never
274      */
275     @Test
276     public void testSameHashCode() throws AbstractZemucanException {
277         final AboutNode actual = new AboutNode(new Graph("test", false));
278         final AboutNode expected = new AboutNode(new Graph("test", false));
279 
280         Assert.assertEquals("Two AboutNode generate the same hashcode.", //$NON-NLS-1$
281                 expected.hashCode(), actual.hashCode());
282     }
283 
284     /**
285      * Tests the symmetry of equals.
286      *
287      * @throws AbstractZemucanException
288      *             Never.
289      */
290     @Test
291     public void testSymmetric() throws AbstractZemucanException {
292         final AboutNode actual = new AboutNode(new Graph("test", false));
293 
294         final boolean value = actual.equals(actual);
295 
296         Assert.assertTrue("Symmetry", value); //$NON-NLS-1$
297     }
298 }