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