1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package doclava;
18
19import com.google.doclava.Errors;
20import com.google.doclava.Errors.Error;
21import com.google.doclava.Errors.ErrorMessage;
22import com.google.doclava.apicheck.ApiCheck;
23import com.google.doclava.apicheck.ApiCheck.Report;
24
25import junit.framework.TestCase;
26
27import java.util.Iterator;
28
29public class ApiCheckTest extends TestCase {
30  /**
31   * Clear all errors and make sure all future errors will be recorded.
32   */
33  public void setUp() {
34    Errors.clearErrors();
35    for (Errors.Error error : Errors.ERRORS) {
36      Errors.setErrorLevel(error.code, Errors.ERROR);
37    }
38  }
39
40  public void testEquivalentApi() {
41    String[] args = { "test/api/medium.xml", "test/api/medium.xml" };
42    ApiCheck apiCheck = new ApiCheck();
43    Report report = apiCheck.checkApi(args);
44    assertEquals(report.errors().size(), 0);
45  }
46
47  public void testMethodReturnTypeChanged() {
48    String[] args = { "test/api/return-type-changed-1.xml", "test/api/return-type-changed-2.xml" };
49    ApiCheck apiCheck = new ApiCheck();
50    Report report = apiCheck.checkApi(args);
51    assertEquals(1, report.errors().size());
52    assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error());
53  }
54
55  public void testMethodParameterChanged() {
56    String[] args = { "test/api/parameter-changed-1.xml", "test/api/parameter-changed-2.xml" };
57    ApiCheck apiCheck = new ApiCheck();
58    Report report = apiCheck.checkApi(args);
59    assertEquals(2, report.errors().size());
60
61    Iterator<ErrorMessage> errors = report.errors().iterator();
62    ErrorMessage m1 = errors.next();
63    ErrorMessage m2 = errors.next();
64    assertNotSame(m1.error(), m2.error());
65    assertTrue(m1.error().equals(Errors.ADDED_METHOD) || m1.error().equals(Errors.REMOVED_METHOD));
66    assertTrue(m2.error().equals(Errors.ADDED_METHOD) || m2.error().equals(Errors.REMOVED_METHOD));
67  }
68
69  public void testConstructorParameterChanged() {
70    String[] args = { "test/api/parameter-changed-1.xml", "test/api/parameter-changed-3.xml" };
71    ApiCheck apiCheck = new ApiCheck();
72    Report report = apiCheck.checkApi(args);
73    assertEquals(2, report.errors().size());
74    Iterator<ErrorMessage> errors = report.errors().iterator();
75    ErrorMessage m1 = errors.next();
76    ErrorMessage m2 = errors.next();
77    assertNotSame(m1.error(), m2.error());
78    assertTrue(m1.error().equals(Errors.ADDED_METHOD) || m1.error().equals(Errors.REMOVED_METHOD));
79    assertTrue(m2.error().equals(Errors.ADDED_METHOD) || m2.error().equals(Errors.REMOVED_METHOD));
80  }
81
82  public void testAddedClass() {
83    String[] args = { "test/api/simple.xml", "test/api/add-class.xml" };
84    ApiCheck apiCheck = new ApiCheck();
85    Report report = apiCheck.checkApi(args);
86    assertEquals(1, report.errors().size());
87    assertEquals(Errors.ADDED_CLASS, report.errors().iterator().next().error());
88  }
89
90  public void testRemovedClass() {
91    String[] args = { "test/api/add-class.xml", "test/api/simple.xml" };
92    ApiCheck apiCheck = new ApiCheck();
93    Report report = apiCheck.checkApi(args);
94    assertEquals(1, report.errors().size());
95    assertEquals(Errors.REMOVED_CLASS, report.errors().iterator().next().error());
96  }
97
98  public void testChangedSuper() {
99    String[] args = { "test/api/simple.xml", "test/api/changed-super.xml" };
100    ApiCheck apiCheck = new ApiCheck();
101    Report report = apiCheck.checkApi(args);
102    assertEquals(1, report.errors().size());
103    assertEquals(Errors.CHANGED_SUPERCLASS, report.errors().iterator().next().error());
104  }
105
106  public void testAddedInterface() {
107    String[] args = { "test/api/removed-interface.xml", "test/api/medium.xml" };
108    ApiCheck apiCheck = new ApiCheck();
109    Report report = apiCheck.checkApi(args);
110    assertEquals(1, report.errors().size());
111    assertEquals(Errors.ADDED_INTERFACE, report.errors().iterator().next().error());
112  }
113
114  public void testRemovedInterface() {
115    String[] args = { "test/api/medium.xml", "test/api/removed-interface.xml" };
116    ApiCheck apiCheck = new ApiCheck();
117    Report report = apiCheck.checkApi(args);
118    assertEquals(1, report.errors().size());
119    assertEquals(Errors.REMOVED_INTERFACE, report.errors().iterator().next().error());
120  }
121
122  public void testChangedAbstractClass() {
123    String[] args = { "test/api/medium.xml", "test/api/changed-abstract.xml" };
124    ApiCheck apiCheck = new ApiCheck();
125    Report report = apiCheck.checkApi(args);
126    assertEquals(1, report.errors().size());
127    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
128  }
129
130  public void testChangedAbstractClass2() {
131    String[] args = { "test/api/changed-abstract.xml", "test/api/medium.xml" };
132    ApiCheck apiCheck = new ApiCheck();
133    Report report = apiCheck.checkApi(args);
134    assertEquals(1, report.errors().size());
135    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
136  }
137
138  public void testChangedAbstractMethod() {
139    String[] args = { "test/api/medium.xml", "test/api/changed-abstract2.xml" };
140    ApiCheck apiCheck = new ApiCheck();
141    Report report = apiCheck.checkApi(args);
142    assertEquals(1, report.errors().size());
143    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
144  }
145
146  public void testChangedAbstractMethod2() {
147    String[] args = { "test/api/changed-abstract2.xml", "test/api/medium.xml" };
148    ApiCheck apiCheck = new ApiCheck();
149    Report report = apiCheck.checkApi(args);
150    assertEquals(1, report.errors().size());
151    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
152  }
153
154  public void testAddedPackage() {
155    String[] args = { "test/api/medium.xml", "test/api/added-package.xml" };
156    ApiCheck apiCheck = new ApiCheck();
157    Report report = apiCheck.checkApi(args);
158    assertEquals(1, report.errors().size());
159    assertEquals(Errors.ADDED_PACKAGE, report.errors().iterator().next().error());
160  }
161
162  public void testRemovedPackage() {
163    String[] args = { "test/api/added-package.xml", "test/api/medium.xml" };
164    ApiCheck apiCheck = new ApiCheck();
165    Report report = apiCheck.checkApi(args);
166    assertEquals(1, report.errors().size());
167    assertEquals(Errors.REMOVED_PACKAGE, report.errors().iterator().next().error());
168  }
169
170  public void testChangedValue() {
171    String[] args = { "test/api/constants.xml", "test/api/changed-value.xml" };
172    ApiCheck apiCheck = new ApiCheck();
173    Report report = apiCheck.checkApi(args);
174    assertEquals(1, report.errors().size());
175    assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error());
176  }
177
178  public void testChangedValue2() {
179    String[] args = { "test/api/constants.xml", "test/api/changed-value2.xml" };
180    ApiCheck apiCheck = new ApiCheck();
181    Report report = apiCheck.checkApi(args);
182    assertEquals(1, report.errors().size());
183    assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error());
184  }
185
186  public void testChangedType() {
187    String[] args = { "test/api/constants.xml", "test/api/changed-type.xml" };
188    ApiCheck apiCheck = new ApiCheck();
189    Report report = apiCheck.checkApi(args);
190    assertEquals(1, report.errors().size());
191    assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error());
192  }
193
194  public void testChangedFinalField() {
195    String[] args = { "test/api/constants.xml", "test/api/changed-final.xml" };
196    ApiCheck apiCheck = new ApiCheck();
197    Report report = apiCheck.checkApi(args);
198    assertEquals(1, report.errors().size());
199    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
200  }
201
202  public void testChangedFinalMethod() {
203    String[] args = { "test/api/constants.xml", "test/api/changed-final2.xml" };
204    ApiCheck apiCheck = new ApiCheck();
205    Report report = apiCheck.checkApi(args);
206    assertEquals(1, report.errors().size());
207    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
208  }
209
210  public void testChangedFinalClass() {
211    String[] args = { "test/api/constants.xml", "test/api/changed-final3.xml" };
212    ApiCheck apiCheck = new ApiCheck();
213    Report report = apiCheck.checkApi(args);
214    assertEquals(1, report.errors().size());
215    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
216  }
217
218  public void testChangedFinalClass2() {
219    String[] args = { "test/api/changed-final3.xml", "test/api/constants.xml" };
220    ApiCheck apiCheck = new ApiCheck();
221    Report report = apiCheck.checkApi(args);
222    assertEquals(1, report.errors().size());
223    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
224  }
225
226  public void testAddedField() {
227    String[] args = { "test/api/constants.xml", "test/api/added-field.xml" };
228    ApiCheck apiCheck = new ApiCheck();
229    Report report = apiCheck.checkApi(args);
230    assertEquals(1, report.errors().size());
231    assertEquals(Errors.ADDED_FIELD, report.errors().iterator().next().error());
232  }
233
234  public void testRemovedField() {
235    String[] args = { "test/api/added-field.xml", "test/api/constants.xml" };
236    ApiCheck apiCheck = new ApiCheck();
237    Report report = apiCheck.checkApi(args);
238    assertEquals(1, report.errors().size());
239    assertEquals(Errors.REMOVED_FIELD, report.errors().iterator().next().error());
240  }
241
242  public void testChangedStaticMethod() {
243    String[] args = { "test/api/constants.xml", "test/api/changed-static.xml" };
244    ApiCheck apiCheck = new ApiCheck();
245    Report report = apiCheck.checkApi(args);
246    assertEquals(1, report.errors().size());
247    assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error());
248  }
249
250  public void testChangedStaticClass() {
251    String[] args = { "test/api/constants.xml", "test/api/changed-static2.xml" };
252    ApiCheck apiCheck = new ApiCheck();
253    Report report = apiCheck.checkApi(args);
254    assertEquals(1, report.errors().size());
255    assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error());
256  }
257
258  public void testChangedStaticField() {
259    String[] args = { "test/api/constants.xml", "test/api/changed-static3.xml" };
260    ApiCheck apiCheck = new ApiCheck();
261    Report report = apiCheck.checkApi(args);
262    assertEquals(1, report.errors().size());
263    assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error());
264  }
265
266  public void testChangedTransient() {
267    String[] args = { "test/api/constants.xml", "test/api/changed-transient.xml" };
268    ApiCheck apiCheck = new ApiCheck();
269    Report report = apiCheck.checkApi(args);
270    assertEquals(1, report.errors().size());
271    assertEquals(Errors.CHANGED_TRANSIENT, report.errors().iterator().next().error());
272  }
273
274  public void testChangedSynchronized() {
275    String[] args = { "test/api/constants.xml", "test/api/changed-synchronized.xml" };
276    ApiCheck apiCheck = new ApiCheck();
277    Report report = apiCheck.checkApi(args);
278    assertEquals(1, report.errors().size());
279    assertEquals(Errors.CHANGED_SYNCHRONIZED, report.errors().iterator().next().error());
280  }
281
282  public void testChangedVolatile() {
283    String[] args = { "test/api/constants.xml", "test/api/changed-volatile.xml" };
284    ApiCheck apiCheck = new ApiCheck();
285    Report report = apiCheck.checkApi(args);
286    assertEquals(1, report.errors().size());
287    assertEquals(Errors.CHANGED_VOLATILE, report.errors().iterator().next().error());
288  }
289
290  public void testChangedNative() {
291    String[] args = { "test/api/constants.xml", "test/api/changed-native.xml" };
292    ApiCheck apiCheck = new ApiCheck();
293    Report report = apiCheck.checkApi(args);
294    assertEquals(1, report.errors().size());
295    assertEquals(Errors.CHANGED_NATIVE, report.errors().iterator().next().error());
296  }
297
298  public void testChangedScopeMethod() {
299    String[] args = { "test/api/constants.xml", "test/api/changed-scope.xml" };
300    ApiCheck apiCheck = new ApiCheck();
301    Report report = apiCheck.checkApi(args);
302    assertEquals(1, report.errors().size());
303    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
304  }
305
306  public void testChangedScopeClass() {
307    String[] args = { "test/api/changed-scope.xml", "test/api/constants.xml" };
308    ApiCheck apiCheck = new ApiCheck();
309    Report report = apiCheck.checkApi(args);
310    assertEquals(1, report.errors().size());
311    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
312  }
313
314  public void testChangedScopeClass2() {
315    String[] args = { "test/api/constants.xml", "test/api/changed-scope2.xml" };
316    ApiCheck apiCheck = new ApiCheck();
317    Report report = apiCheck.checkApi(args);
318    assertEquals(1, report.errors().size());
319    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
320  }
321
322  public void testChangedScopeField() {
323    String[] args = { "test/api/constants.xml", "test/api/changed-scope3.xml" };
324    ApiCheck apiCheck = new ApiCheck();
325    Report report = apiCheck.checkApi(args);
326    assertEquals(1, report.errors().size());
327    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
328  }
329
330  public void testChangedConstructorScope() {
331    String[] args = { "test/api/constants.xml", "test/api/changed-scope4.xml" };
332    ApiCheck apiCheck = new ApiCheck();
333    Report report = apiCheck.checkApi(args);
334    assertEquals(1, report.errors().size());
335    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
336  }
337
338  public void testChangedMethodThrows() {
339    String[] args = { "test/api/throws.xml", "test/api/removed-exception.xml" };
340    ApiCheck apiCheck = new ApiCheck();
341    Report report = apiCheck.checkApi(args);
342    assertEquals(1, report.errors().size());
343    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
344  }
345
346  public void testChangedMethodThrows2() {
347    String[] args = { "test/api/removed-exception.xml", "test/api/throws.xml" };
348    ApiCheck apiCheck = new ApiCheck();
349    Report report = apiCheck.checkApi(args);
350    assertEquals(1, report.errors().size());
351    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
352  }
353
354  public void testChangedConstructorThrows() {
355    String[] args = { "test/api/throws.xml", "test/api/added-exception.xml" };
356    ApiCheck apiCheck = new ApiCheck();
357    Report report = apiCheck.checkApi(args);
358    assertEquals(1, report.errors().size());
359    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
360  }
361
362  public void testChangedConstructorThrows2() {
363    String[] args = { "test/api/added-exception.xml", "test/api/throws.xml" };
364    ApiCheck apiCheck = new ApiCheck();
365    Report report = apiCheck.checkApi(args);
366    assertEquals(1, report.errors().size());
367    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
368  }
369
370  public void testChangedMethodDeprecated() {
371    String[] args = { "test/api/constants.xml", "test/api/changed-deprecated.xml" };
372    ApiCheck apiCheck = new ApiCheck();
373    Report report = apiCheck.checkApi(args);
374    assertEquals(1, report.errors().size());
375    assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error());
376  }
377
378  public void testChangedConstructorDeprecated() {
379    String[] args = { "test/api/constants.xml", "test/api/changed-deprecated2.xml" };
380    ApiCheck apiCheck = new ApiCheck();
381    Report report = apiCheck.checkApi(args);
382    assertEquals(1, report.errors().size());
383    assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error());
384  }
385
386  public void testChangedFieldDeprecated() {
387    String[] args = { "test/api/constants.xml", "test/api/changed-deprecated3.xml" };
388    ApiCheck apiCheck = new ApiCheck();
389    Report report = apiCheck.checkApi(args);
390    assertEquals(1, report.errors().size());
391    assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error());
392  }
393
394  public void testChangedClassToInterface() {
395    String[] args = { "test/api/changed-class-info2.xml", "test/api/changed-class-info.xml" };
396    ApiCheck apiCheck = new ApiCheck();
397    Report report = apiCheck.checkApi(args);
398    assertEquals(1, report.errors().size());
399    assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error());
400  }
401
402  public void testChangedInterfaceToClass() {
403    String[] args = { "test/api/changed-class-info.xml", "test/api/changed-class-info2.xml" };
404    ApiCheck apiCheck = new ApiCheck();
405    Report report = apiCheck.checkApi(args);
406    assertEquals(1, report.errors().size());
407    assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error());
408  }
409}