1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package name.angoca.db2sa.core.syntax;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import name.angoca.db2sa.core.lexical.Token;
35 import name.angoca.db2sa.core.lexical.exceptions.InvalidTokenException;
36
37 import org.junit.Assert;
38 import org.junit.Test;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public final class GraphAnswerTest {
56
57
58
59
60 private final String CREATE = "create";
61
62
63
64 private final String TABLE = "table";
65
66
67
68 private final String TABLESPACE = "tablespace";
69
70
71
72 private final String CREATE_TABLE = "create table";
73
74
75
76 private final String CREATE_TABLESPACE = "create talespace";
77
78
79
80 private final String TABLENAME = "<tableName>";
81
82
83
84 private final String TABLESPACENAME = "<tablespaceName>";
85
86
87
88
89 public GraphAnswerTest() {
90
91 }
92
93
94
95
96
97
98
99 @Test
100 public final void testHashCode() throws InvalidTokenException {
101 final String phrase1 = this.CREATE;
102 final String option1 = this.TABLE;
103 final String option2 = this.TABLESPACE;
104
105 final List<Token> phrases = new ArrayList<Token>();
106 phrases.add(new Token(phrase1));
107 final List<Token> options = new ArrayList<Token>();
108 options.add(new Token(option1));
109 options.add(new Token(option2));
110 final GraphAnswer object = new GraphAnswer(phrases, options);
111
112 Assert.assertTrue("HashCode different to zero",
113 object.hashCode() != 0);
114 Assert.assertTrue("HashCode different to one",
115 object.hashCode() != 1);
116 Assert.assertTrue("HashCode different to minus one",
117 object.hashCode() != -1);
118 }
119
120
121
122
123
124
125
126 @Test
127 public final void testHashCodeIdentical() throws InvalidTokenException {
128 final String phrase1 = this.CREATE;
129 final String option1 = this.TABLE;
130 final String option2 = this.TABLESPACE;
131
132 final List<Token> phrases1 = new ArrayList<Token>();
133 phrases1.add(new Token(phrase1));
134 final List<Token> options1 = new ArrayList<Token>();
135 options1.add(new Token(option1));
136 options1.add(new Token(option2));
137 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
138
139 final List<Token> phrases2 = new ArrayList<Token>();
140 phrases2.add(new Token(phrase1));
141 final List<Token> options2 = new ArrayList<Token>();
142 options2.add(new Token(option1));
143 options2.add(new Token(option2));
144 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
145 Assert.assertEquals("Two identical objects with the same hash code",
146 expected.hashCode(), actual.hashCode());
147 }
148
149
150
151
152
153
154
155 @Test
156 public final void testHashCodeDifferent() throws InvalidTokenException {
157 final String phrase1 = this.CREATE_TABLE;
158 final String option11 = this.TABLE;
159 final String option12 = this.TABLESPACE;
160
161 final String phrase2 = this.CREATE_TABLESPACE;
162 final String option21 = this.TABLENAME;
163 final String option22 = this.TABLESPACENAME;
164
165 final List<Token> phrases1 = new ArrayList<Token>();
166 phrases1.add(new Token(phrase1));
167 final List<Token> options1 = new ArrayList<Token>();
168 options1.add(new Token(option11));
169 options1.add(new Token(option12));
170 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
171
172 final List<Token> phrases2 = new ArrayList<Token>();
173 phrases2.add(new Token(phrase2));
174 final List<Token> options2 = new ArrayList<Token>();
175 options2.add(new Token(option21));
176 options2.add(new Token(option22));
177 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
178
179
180 Assert.assertTrue("Two different objects with different hash code",
181 expected.hashCode() != actual.hashCode());
182 }
183
184
185
186
187
188
189
190 @Test
191 public final void testEqualsNull() throws InvalidTokenException {
192 final String phrase1 = this.CREATE;
193 final String option1 = this.TABLE;
194 final String option2 = this.TABLESPACE;
195
196 final List<Token> phrases = new ArrayList<Token>();
197 phrases.add(new Token(phrase1));
198 final List<Token> options = new ArrayList<Token>();
199 options.add(new Token(option1));
200 options.add(new Token(option2));
201 final GraphAnswer object = new GraphAnswer(phrases, options);
202
203 final GraphAnswer nullObject = null;
204
205 final boolean condition = object.equals(nullObject);
206
207 Assert.assertFalse("Equals different to null", condition);
208 }
209
210
211
212
213
214
215
216 @Test
217 public final void testEqualsObjectTrue() throws InvalidTokenException {
218 final String phrase1 = this.CREATE;
219 final String option1 = this.TABLE;
220 final String option2 = this.TABLESPACE;
221
222 final List<Token> phrases1 = new ArrayList<Token>();
223 phrases1.add(new Token(phrase1));
224 final List<Token> options1 = new ArrayList<Token>();
225 options1.add(new Token(option1));
226 options1.add(new Token(option2));
227 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
228
229 final List<Token> phrases2 = new ArrayList<Token>();
230 phrases2.add(new Token(phrase1));
231 final List<Token> options2 = new ArrayList<Token>();
232 options2.add(new Token(option1));
233 options2.add(new Token(option2));
234 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
235 Assert.assertTrue("Two identical objects with are equals",
236 expected.equals(actual));
237 }
238
239
240
241
242
243
244
245 @Test
246 public final void testEqualsObjectFalse() throws InvalidTokenException {
247 final String phrase1 = this.CREATE_TABLE;
248 final String option11 = this.TABLE;
249 final String option12 = this.TABLESPACE;
250
251 final String phrase2 = this.CREATE_TABLESPACE;
252 final String option21 = this.TABLENAME;
253 final String option22 = this.TABLESPACENAME;
254
255 final List<Token> phrases1 = new ArrayList<Token>();
256 phrases1.add(new Token(phrase1));
257 final List<Token> options1 = new ArrayList<Token>();
258 options1.add(new Token(option11));
259 options1.add(new Token(option12));
260 final GraphAnswer expected = new GraphAnswer(phrases1, options1);
261
262 final List<Token> phrases2 = new ArrayList<Token>();
263 phrases2.add(new Token(phrase2));
264 final List<Token> options2 = new ArrayList<Token>();
265 options2.add(new Token(option21));
266 options2.add(new Token(option22));
267 final GraphAnswer actual = new GraphAnswer(phrases2, options2);
268 Assert.assertFalse("Two different objects are not equals",
269 expected.equals(actual));
270 }
271
272
273
274
275
276
277
278 @Test
279 public final void testToString() throws InvalidTokenException {
280 final String phrase1 = this.CREATE;
281 final String option1 = this.TABLE;
282 final String option2 = this.TABLESPACE;
283
284 final List<Token> phrases1 = new ArrayList<Token>();
285 phrases1.add(new Token(phrase1));
286 final List<Token> options1 = new ArrayList<Token>();
287 options1.add(new Token(option1));
288 options1.add(new Token(option2));
289 final GraphAnswer answer = new GraphAnswer(phrases1, options1);
290 final String actual = answer.toString();
291
292 final String expected = "[T[create]],{T[table]T[tablespace]}";
293
294 Assert.assertEquals("Method toString", expected, actual);
295 }
296 }