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 17 package doclava; 18 19 import com.google.doclava.Errors; 20 import com.google.doclava.Errors.Error; 21 import com.google.doclava.Errors.ErrorMessage; 22 import com.google.doclava.apicheck.ApiCheck; 23 import com.google.doclava.apicheck.ApiCheck.Report; 24 25 import junit.framework.TestCase; 26 27 import java.util.Iterator; 28 29 public class ApiCheckTest extends TestCase { 30 /** 31 * Clear all errors and make sure all future errors will be recorded. 32 */ setUp()33 public void setUp() { 34 Errors.clearErrors(); 35 for (Errors.Error error : Errors.sErrors) { 36 Errors.setErrorLevel(error.code, Errors.ERROR); 37 } 38 } 39 testEquivalentApi()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 testMethodReturnTypeChanged()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 testMethodParameterChanged()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 testConstructorParameterChanged()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 testAddedClass()82 public void testAddedClass() { 83 String[] args = { "test/api/simple.xml", "test/api/added-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 testRemovedClass()90 public void testRemovedClass() { 91 String[] args = { "test/api/added-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 testRemovedDeprecatedClass()98 public void testRemovedDeprecatedClass() { 99 String[] args = { "test/api/added-deprecated-class.xml", "test/api/simple.xml" }; 100 ApiCheck apiCheck = new ApiCheck(); 101 Report report = apiCheck.checkApi(args); 102 assertEquals(1, report.errors().size()); 103 assertEquals(Errors.REMOVED_DEPRECATED_CLASS, report.errors().iterator().next().error()); 104 } 105 testChangedSuper()106 public void testChangedSuper() { 107 String[] args = { "test/api/simple.xml", "test/api/changed-super.xml" }; 108 ApiCheck apiCheck = new ApiCheck(); 109 Report report = apiCheck.checkApi(args); 110 assertEquals(1, report.errors().size()); 111 assertEquals(Errors.CHANGED_SUPERCLASS, report.errors().iterator().next().error()); 112 } 113 testChangedAssignableReturn()114 public void testChangedAssignableReturn() { 115 String[] args = { 116 "test/api/changed-assignable-return-1.xml", 117 "test/api/changed-assignable-return-2.xml" 118 }; 119 ApiCheck apiCheck = new ApiCheck(); 120 Report report = apiCheck.checkApi(args); 121 assertEquals(1, report.errors().size()); 122 assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error()); 123 } 124 testInsertedSuper()125 public void testInsertedSuper() { 126 String[] args = { "test/api/inserted-super-1.xml", "test/api/inserted-super-2.xml" }; 127 ApiCheck apiCheck = new ApiCheck(); 128 Report report = apiCheck.checkApi(args); 129 assertEquals(0, report.errors().size()); 130 } 131 testAddedInterface()132 public void testAddedInterface() { 133 String[] args = { "test/api/removed-interface.xml", "test/api/medium.xml" }; 134 ApiCheck apiCheck = new ApiCheck(); 135 Report report = apiCheck.checkApi(args); 136 assertEquals(1, report.errors().size()); 137 assertEquals(Errors.ADDED_INTERFACE, report.errors().iterator().next().error()); 138 } 139 testRemovedInterface()140 public void testRemovedInterface() { 141 String[] args = { "test/api/medium.xml", "test/api/removed-interface.xml" }; 142 ApiCheck apiCheck = new ApiCheck(); 143 Report report = apiCheck.checkApi(args); 144 assertEquals(1, report.errors().size()); 145 assertEquals(Errors.REMOVED_INTERFACE, report.errors().iterator().next().error()); 146 } 147 testChangedAbstractClass()148 public void testChangedAbstractClass() { 149 String[] args = { "test/api/medium.xml", "test/api/changed-abstract.xml" }; 150 ApiCheck apiCheck = new ApiCheck(); 151 Report report = apiCheck.checkApi(args); 152 assertEquals(1, report.errors().size()); 153 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 154 } 155 testChangedAbstractClass2()156 public void testChangedAbstractClass2() { 157 String[] args = { "test/api/changed-abstract.xml", "test/api/medium.xml" }; 158 ApiCheck apiCheck = new ApiCheck(); 159 Report report = apiCheck.checkApi(args); 160 assertEquals(1, report.errors().size()); 161 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 162 } 163 testChangedAbstractMethod()164 public void testChangedAbstractMethod() { 165 String[] args = { "test/api/medium.xml", "test/api/changed-abstract2.xml" }; 166 ApiCheck apiCheck = new ApiCheck(); 167 Report report = apiCheck.checkApi(args); 168 assertEquals(1, report.errors().size()); 169 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 170 } 171 testChangedAbstractMethod2()172 public void testChangedAbstractMethod2() { 173 String[] args = { "test/api/changed-abstract2.xml", "test/api/medium.xml" }; 174 ApiCheck apiCheck = new ApiCheck(); 175 Report report = apiCheck.checkApi(args); 176 assertEquals(1, report.errors().size()); 177 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 178 } 179 testAddedPackage()180 public void testAddedPackage() { 181 String[] args = { "test/api/medium.xml", "test/api/added-package.xml" }; 182 ApiCheck apiCheck = new ApiCheck(); 183 Report report = apiCheck.checkApi(args); 184 assertEquals(1, report.errors().size()); 185 assertEquals(Errors.ADDED_PACKAGE, report.errors().iterator().next().error()); 186 } 187 testRemovedPackage()188 public void testRemovedPackage() { 189 String[] args = { "test/api/added-package.xml", "test/api/medium.xml" }; 190 ApiCheck apiCheck = new ApiCheck(); 191 Report report = apiCheck.checkApi(args); 192 assertEquals(1, report.errors().size()); 193 assertEquals(Errors.REMOVED_PACKAGE, report.errors().iterator().next().error()); 194 } 195 testChangedValue()196 public void testChangedValue() { 197 String[] args = { "test/api/constants.xml", "test/api/changed-value.xml" }; 198 ApiCheck apiCheck = new ApiCheck(); 199 Report report = apiCheck.checkApi(args); 200 assertEquals(1, report.errors().size()); 201 assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error()); 202 } 203 testChangedValue2()204 public void testChangedValue2() { 205 String[] args = { "test/api/constants.xml", "test/api/changed-value2.xml" }; 206 ApiCheck apiCheck = new ApiCheck(); 207 Report report = apiCheck.checkApi(args); 208 assertEquals(1, report.errors().size()); 209 assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error()); 210 } 211 testChangedType()212 public void testChangedType() { 213 String[] args = { "test/api/constants.xml", "test/api/changed-type.xml" }; 214 ApiCheck apiCheck = new ApiCheck(); 215 Report report = apiCheck.checkApi(args); 216 assertEquals(1, report.errors().size()); 217 assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error()); 218 } 219 testAddedFinalField()220 public void testAddedFinalField() { 221 String[] args = { "test/api/constants.xml", "test/api/changed-final.xml" }; 222 ApiCheck apiCheck = new ApiCheck(); 223 Report report = apiCheck.checkApi(args); 224 assertEquals(1, report.errors().size()); 225 assertEquals(Errors.ADDED_FINAL, report.errors().iterator().next().error()); 226 } 227 testAddedFinalMethod()228 public void testAddedFinalMethod() { 229 String[] args = { "test/api/constants.xml", "test/api/changed-final2.xml" }; 230 ApiCheck apiCheck = new ApiCheck(); 231 Report report = apiCheck.checkApi(args); 232 assertEquals(1, report.errors().size()); 233 assertEquals(Errors.ADDED_FINAL, report.errors().iterator().next().error()); 234 } 235 testAddedFinalClass()236 public void testAddedFinalClass() { 237 String[] args = { "test/api/constants.xml", "test/api/changed-final3.xml" }; 238 ApiCheck apiCheck = new ApiCheck(); 239 Report report = apiCheck.checkApi(args); 240 // One error for the class, one for the constructor, one for the method. 241 assertEquals(3, report.errors().size()); 242 assertEquals(Errors.ADDED_FINAL, report.errors().iterator().next().error()); 243 } 244 testRemovedFinalClass()245 public void testRemovedFinalClass() { 246 String[] args = { "test/api/changed-final3.xml", "test/api/constants.xml" }; 247 ApiCheck apiCheck = new ApiCheck(); 248 Report report = apiCheck.checkApi(args); 249 // One error for the class, one for the constructor, one for the method. 250 assertEquals(3, report.errors().size()); 251 assertEquals(Errors.REMOVED_FINAL, report.errors().iterator().next().error()); 252 } 253 testAddedField()254 public void testAddedField() { 255 String[] args = { "test/api/constants.xml", "test/api/added-field.xml" }; 256 ApiCheck apiCheck = new ApiCheck(); 257 Report report = apiCheck.checkApi(args); 258 assertEquals(1, report.errors().size()); 259 assertEquals(Errors.ADDED_FIELD, report.errors().iterator().next().error()); 260 } 261 testRemovedField()262 public void testRemovedField() { 263 String[] args = { "test/api/added-field.xml", "test/api/constants.xml" }; 264 ApiCheck apiCheck = new ApiCheck(); 265 Report report = apiCheck.checkApi(args); 266 assertEquals(1, report.errors().size()); 267 assertEquals(Errors.REMOVED_FIELD, report.errors().iterator().next().error()); 268 } 269 testRemovedDeprecatedField()270 public void testRemovedDeprecatedField() { 271 String[] args = { "test/api/added-deprecated-field.xml", "test/api/constants.xml" }; 272 ApiCheck apiCheck = new ApiCheck(); 273 Report report = apiCheck.checkApi(args); 274 assertEquals(1, report.errors().size()); 275 assertEquals(Errors.REMOVED_DEPRECATED_FIELD, report.errors().iterator().next().error()); 276 } 277 testAddedMethod()278 public void testAddedMethod() { 279 String[] args = { "test/api/constants.xml", "test/api/added-method.xml" }; 280 ApiCheck apiCheck = new ApiCheck(); 281 Report report = apiCheck.checkApi(args); 282 assertEquals(1, report.errors().size()); 283 assertEquals(Errors.ADDED_METHOD, report.errors().iterator().next().error()); 284 } 285 testRemovedMethod()286 public void testRemovedMethod() { 287 String[] args = { "test/api/added-method.xml", "test/api/constants.xml" }; 288 ApiCheck apiCheck = new ApiCheck(); 289 Report report = apiCheck.checkApi(args); 290 assertEquals(1, report.errors().size()); 291 assertEquals(Errors.REMOVED_METHOD, report.errors().iterator().next().error()); 292 } 293 testRemovedDeprecatedMethod()294 public void testRemovedDeprecatedMethod() { 295 String[] args = { "test/api/added-deprecated-method.xml", "test/api/constants.xml" }; 296 ApiCheck apiCheck = new ApiCheck(); 297 Report report = apiCheck.checkApi(args); 298 assertEquals(1, report.errors().size()); 299 assertEquals(Errors.REMOVED_DEPRECATED_METHOD, report.errors().iterator().next().error()); 300 } 301 testChangedStaticMethod()302 public void testChangedStaticMethod() { 303 String[] args = { "test/api/constants.xml", "test/api/changed-static.xml" }; 304 ApiCheck apiCheck = new ApiCheck(); 305 Report report = apiCheck.checkApi(args); 306 assertEquals(1, report.errors().size()); 307 assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error()); 308 } 309 testChangedStaticClass()310 public void testChangedStaticClass() { 311 String[] args = { "test/api/constants.xml", "test/api/changed-static2.xml" }; 312 ApiCheck apiCheck = new ApiCheck(); 313 Report report = apiCheck.checkApi(args); 314 assertEquals(1, report.errors().size()); 315 assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error()); 316 } 317 testChangedStaticField()318 public void testChangedStaticField() { 319 String[] args = { "test/api/constants.xml", "test/api/changed-static3.xml" }; 320 ApiCheck apiCheck = new ApiCheck(); 321 Report report = apiCheck.checkApi(args); 322 assertEquals(1, report.errors().size()); 323 assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error()); 324 } 325 testChangedTransient()326 public void testChangedTransient() { 327 String[] args = { "test/api/constants.xml", "test/api/changed-transient.xml" }; 328 ApiCheck apiCheck = new ApiCheck(); 329 Report report = apiCheck.checkApi(args); 330 assertEquals(1, report.errors().size()); 331 assertEquals(Errors.CHANGED_TRANSIENT, report.errors().iterator().next().error()); 332 } 333 testChangedSynchronized()334 public void testChangedSynchronized() { 335 String[] args = { "test/api/constants.xml", "test/api/changed-synchronized.xml" }; 336 ApiCheck apiCheck = new ApiCheck(); 337 Report report = apiCheck.checkApi(args); 338 assertEquals(0, report.errors().size()); 339 } 340 testChangedVolatile()341 public void testChangedVolatile() { 342 String[] args = { "test/api/constants.xml", "test/api/changed-volatile.xml" }; 343 ApiCheck apiCheck = new ApiCheck(); 344 Report report = apiCheck.checkApi(args); 345 assertEquals(1, report.errors().size()); 346 assertEquals(Errors.CHANGED_VOLATILE, report.errors().iterator().next().error()); 347 } 348 testChangedNative()349 public void testChangedNative() { 350 String[] args = { "test/api/constants.xml", "test/api/changed-native.xml" }; 351 ApiCheck apiCheck = new ApiCheck(); 352 Report report = apiCheck.checkApi(args); 353 assertEquals(1, report.errors().size()); 354 assertEquals(Errors.CHANGED_NATIVE, report.errors().iterator().next().error()); 355 } 356 testChangedScopeMethod()357 public void testChangedScopeMethod() { 358 String[] args = { "test/api/constants.xml", "test/api/changed-scope.xml" }; 359 ApiCheck apiCheck = new ApiCheck(); 360 Report report = apiCheck.checkApi(args); 361 assertEquals(1, report.errors().size()); 362 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 363 } 364 testChangedScopeClass()365 public void testChangedScopeClass() { 366 String[] args = { "test/api/changed-scope.xml", "test/api/constants.xml" }; 367 ApiCheck apiCheck = new ApiCheck(); 368 Report report = apiCheck.checkApi(args); 369 assertEquals(1, report.errors().size()); 370 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 371 } 372 testChangedScopeClass2()373 public void testChangedScopeClass2() { 374 String[] args = { "test/api/constants.xml", "test/api/changed-scope2.xml" }; 375 ApiCheck apiCheck = new ApiCheck(); 376 Report report = apiCheck.checkApi(args); 377 assertEquals(1, report.errors().size()); 378 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 379 } 380 testChangedScopeField()381 public void testChangedScopeField() { 382 String[] args = { "test/api/constants.xml", "test/api/changed-scope3.xml" }; 383 ApiCheck apiCheck = new ApiCheck(); 384 Report report = apiCheck.checkApi(args); 385 assertEquals(1, report.errors().size()); 386 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 387 } 388 testChangedConstructorScope()389 public void testChangedConstructorScope() { 390 String[] args = { "test/api/constants.xml", "test/api/changed-scope4.xml" }; 391 ApiCheck apiCheck = new ApiCheck(); 392 Report report = apiCheck.checkApi(args); 393 assertEquals(1, report.errors().size()); 394 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 395 } 396 testChangedMethodThrows()397 public void testChangedMethodThrows() { 398 String[] args = { "test/api/throws.xml", "test/api/removed-exception.xml" }; 399 ApiCheck apiCheck = new ApiCheck(); 400 Report report = apiCheck.checkApi(args); 401 assertEquals(1, report.errors().size()); 402 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 403 } 404 testChangedMethodThrows2()405 public void testChangedMethodThrows2() { 406 String[] args = { "test/api/removed-exception.xml", "test/api/throws.xml" }; 407 ApiCheck apiCheck = new ApiCheck(); 408 Report report = apiCheck.checkApi(args); 409 assertEquals(1, report.errors().size()); 410 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 411 } 412 testChangedConstructorThrows()413 public void testChangedConstructorThrows() { 414 String[] args = { "test/api/throws.xml", "test/api/added-exception.xml" }; 415 ApiCheck apiCheck = new ApiCheck(); 416 Report report = apiCheck.checkApi(args); 417 assertEquals(1, report.errors().size()); 418 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 419 } 420 testChangedConstructorThrows2()421 public void testChangedConstructorThrows2() { 422 String[] args = { "test/api/added-exception.xml", "test/api/throws.xml" }; 423 ApiCheck apiCheck = new ApiCheck(); 424 Report report = apiCheck.checkApi(args); 425 assertEquals(1, report.errors().size()); 426 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 427 } 428 testChangedMethodDeprecated()429 public void testChangedMethodDeprecated() { 430 String[] args = { "test/api/constants.xml", "test/api/changed-deprecated.xml" }; 431 ApiCheck apiCheck = new ApiCheck(); 432 Report report = apiCheck.checkApi(args); 433 assertEquals(1, report.errors().size()); 434 assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error()); 435 } 436 testChangedConstructorDeprecated()437 public void testChangedConstructorDeprecated() { 438 String[] args = { "test/api/constants.xml", "test/api/changed-deprecated2.xml" }; 439 ApiCheck apiCheck = new ApiCheck(); 440 Report report = apiCheck.checkApi(args); 441 assertEquals(1, report.errors().size()); 442 assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error()); 443 } 444 testChangedFieldDeprecated()445 public void testChangedFieldDeprecated() { 446 String[] args = { "test/api/constants.xml", "test/api/changed-deprecated3.xml" }; 447 ApiCheck apiCheck = new ApiCheck(); 448 Report report = apiCheck.checkApi(args); 449 assertEquals(1, report.errors().size()); 450 assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error()); 451 } 452 testChangedClassToInterface()453 public void testChangedClassToInterface() { 454 String[] args = { "test/api/changed-class-info2.xml", "test/api/changed-class-info.xml" }; 455 ApiCheck apiCheck = new ApiCheck(); 456 Report report = apiCheck.checkApi(args); 457 assertEquals(1, report.errors().size()); 458 assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error()); 459 } 460 testChangedInterfaceToClass()461 public void testChangedInterfaceToClass() { 462 String[] args = { "test/api/changed-class-info.xml", "test/api/changed-class-info2.xml" }; 463 ApiCheck apiCheck = new ApiCheck(); 464 Report report = apiCheck.checkApi(args); 465 assertEquals(1, report.errors().size()); 466 assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error()); 467 } 468 } 469