summaryrefslogtreecommitdiffstats
path: root/test/doclava/ApiCheckTest.java
blob: 76ab8642c7c49de242bbf91622098a0aa3716543 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package doclava;

import com.google.doclava.Errors;
import com.google.doclava.Errors.Error;
import com.google.doclava.Errors.ErrorMessage;
import com.google.doclava.apicheck.ApiCheck;
import com.google.doclava.apicheck.ApiCheck.Report;

import junit.framework.TestCase;

import java.util.Iterator;

public class ApiCheckTest extends TestCase {
  /**
   * Clear all errors and make sure all future errors will be recorded.
   */
  public void setUp() {
    Errors.clearErrors();
    for (Errors.Error error : Errors.ERRORS) {
      Errors.setErrorLevel(error.code, Errors.ERROR);
    }
  }
  
  public void testEquivalentApi() {
    String[] args = { "test/api/medium.xml", "test/api/medium.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(report.errors().size(), 0);
  }
  
  public void testMethodReturnTypeChanged() {
    String[] args = { "test/api/return-type-changed-1.xml", "test/api/return-type-changed-2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error());
  }
  
  public void testMethodParameterChanged() {
    String[] args = { "test/api/parameter-changed-1.xml", "test/api/parameter-changed-2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(2, report.errors().size());
    
    Iterator<ErrorMessage> errors = report.errors().iterator();
    ErrorMessage m1 = errors.next();
    ErrorMessage m2 = errors.next();
    assertNotSame(m1.error(), m2.error());
    assertTrue(m1.error().equals(Errors.ADDED_METHOD) || m1.error().equals(Errors.REMOVED_METHOD));
    assertTrue(m2.error().equals(Errors.ADDED_METHOD) || m2.error().equals(Errors.REMOVED_METHOD));
  }
  
  public void testConstructorParameterChanged() {
    String[] args = { "test/api/parameter-changed-1.xml", "test/api/parameter-changed-3.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(2, report.errors().size());
    Iterator<ErrorMessage> errors = report.errors().iterator();
    ErrorMessage m1 = errors.next();
    ErrorMessage m2 = errors.next();
    assertNotSame(m1.error(), m2.error());
    assertTrue(m1.error().equals(Errors.ADDED_METHOD) || m1.error().equals(Errors.REMOVED_METHOD));
    assertTrue(m2.error().equals(Errors.ADDED_METHOD) || m2.error().equals(Errors.REMOVED_METHOD));
  }
  
  public void testAddedClass() {
    String[] args = { "test/api/simple.xml", "test/api/add-class.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.ADDED_CLASS, report.errors().iterator().next().error());
  }
  
  public void testRemovedClass() {
    String[] args = { "test/api/add-class.xml", "test/api/simple.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.REMOVED_CLASS, report.errors().iterator().next().error());
  }
  
  public void testChangedSuper() {
    String[] args = { "test/api/simple.xml", "test/api/changed-super.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SUPERCLASS, report.errors().iterator().next().error());
  }
  
  public void testInsertedSuper() {
    String[] args = { "test/api/inserted-super-1.xml", "test/api/inserted-super-2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(0, report.errors().size());
  }

  public void testAddedInterface() {
    String[] args = { "test/api/removed-interface.xml", "test/api/medium.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.ADDED_INTERFACE, report.errors().iterator().next().error());
  }
  
  public void testRemovedInterface() {
    String[] args = { "test/api/medium.xml", "test/api/removed-interface.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.REMOVED_INTERFACE, report.errors().iterator().next().error());
  }
  
  public void testChangedAbstractClass() {
    String[] args = { "test/api/medium.xml", "test/api/changed-abstract.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
  }
  
  public void testChangedAbstractClass2() {
    String[] args = { "test/api/changed-abstract.xml", "test/api/medium.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
  }
  
  public void testChangedAbstractMethod() {
    String[] args = { "test/api/medium.xml", "test/api/changed-abstract2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
  }
  
  public void testChangedAbstractMethod2() {
    String[] args = { "test/api/changed-abstract2.xml", "test/api/medium.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error());
  }
  
  public void testAddedPackage() {
    String[] args = { "test/api/medium.xml", "test/api/added-package.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.ADDED_PACKAGE, report.errors().iterator().next().error());
  }
  
  public void testRemovedPackage() {
    String[] args = { "test/api/added-package.xml", "test/api/medium.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.REMOVED_PACKAGE, report.errors().iterator().next().error());
  }
    
  public void testChangedValue() {
    String[] args = { "test/api/constants.xml", "test/api/changed-value.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error());
  }
  
  public void testChangedValue2() {
    String[] args = { "test/api/constants.xml", "test/api/changed-value2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error());
  }
  
  public void testChangedType() {
    String[] args = { "test/api/constants.xml", "test/api/changed-type.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error());
  }
  
  public void testChangedFinalField() {
    String[] args = { "test/api/constants.xml", "test/api/changed-final.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
  }
  
  public void testChangedFinalMethod() {
    String[] args = { "test/api/constants.xml", "test/api/changed-final2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
  }
  
  public void testChangedFinalClass() {
    String[] args = { "test/api/constants.xml", "test/api/changed-final3.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
  }
  
  public void testChangedFinalClass2() {
    String[] args = { "test/api/changed-final3.xml", "test/api/constants.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error());
  }
  
  public void testAddedField() {
    String[] args = { "test/api/constants.xml", "test/api/added-field.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.ADDED_FIELD, report.errors().iterator().next().error());
  }
  
  public void testRemovedField() {
    String[] args = { "test/api/added-field.xml", "test/api/constants.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.REMOVED_FIELD, report.errors().iterator().next().error());
  }
  
  public void testChangedStaticMethod() {
    String[] args = { "test/api/constants.xml", "test/api/changed-static.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error());
  }
  
  public void testChangedStaticClass() {
    String[] args = { "test/api/constants.xml", "test/api/changed-static2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error());
  }
  
  public void testChangedStaticField() {
    String[] args = { "test/api/constants.xml", "test/api/changed-static3.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error());
  }
  
  public void testChangedTransient() {
    String[] args = { "test/api/constants.xml", "test/api/changed-transient.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_TRANSIENT, report.errors().iterator().next().error());
  }
  
  public void testChangedSynchronized() {
    String[] args = { "test/api/constants.xml", "test/api/changed-synchronized.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SYNCHRONIZED, report.errors().iterator().next().error());
  }

  public void testChangedVolatile() {
    String[] args = { "test/api/constants.xml", "test/api/changed-volatile.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_VOLATILE, report.errors().iterator().next().error());
  }
  
  public void testChangedNative() {
    String[] args = { "test/api/constants.xml", "test/api/changed-native.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_NATIVE, report.errors().iterator().next().error());
  }
  
  public void testChangedScopeMethod() {
    String[] args = { "test/api/constants.xml", "test/api/changed-scope.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
  }

  public void testChangedScopeClass() {
    String[] args = { "test/api/changed-scope.xml", "test/api/constants.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
  }
  
  public void testChangedScopeClass2() {
    String[] args = { "test/api/constants.xml", "test/api/changed-scope2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
  }
  
  public void testChangedScopeField() {
    String[] args = { "test/api/constants.xml", "test/api/changed-scope3.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
  }
  
  public void testChangedConstructorScope() {
    String[] args = { "test/api/constants.xml", "test/api/changed-scope4.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error());
  }
  
  public void testChangedMethodThrows() {
    String[] args = { "test/api/throws.xml", "test/api/removed-exception.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
  }
  
  public void testChangedMethodThrows2() {
    String[] args = { "test/api/removed-exception.xml", "test/api/throws.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
  }
  
  public void testChangedConstructorThrows() {
    String[] args = { "test/api/throws.xml", "test/api/added-exception.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
  }
  
  public void testChangedConstructorThrows2() {
    String[] args = { "test/api/added-exception.xml", "test/api/throws.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error());
  }
  
  public void testChangedMethodDeprecated() {
    String[] args = { "test/api/constants.xml", "test/api/changed-deprecated.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error());
  }
  
  public void testChangedConstructorDeprecated() {
    String[] args = { "test/api/constants.xml", "test/api/changed-deprecated2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error());
  }
  
  public void testChangedFieldDeprecated() {
    String[] args = { "test/api/constants.xml", "test/api/changed-deprecated3.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error());
  }
  
  public void testChangedClassToInterface() {
    String[] args = { "test/api/changed-class-info2.xml", "test/api/changed-class-info.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error());
  }
  
  public void testChangedInterfaceToClass() {
    String[] args = { "test/api/changed-class-info.xml", "test/api/changed-class-info2.xml" };
    ApiCheck apiCheck = new ApiCheck();
    Report report = apiCheck.checkApi(args);
    assertEquals(1, report.errors().size());
    assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error());
  }
}