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.ERRORS) { 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/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 testRemovedClass()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 testChangedSuper()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 testChangedAssignableReturn()106 public void testChangedAssignableReturn() { 107 String[] args = { "test/api/changed-assignable-return-1.xml", "test/api/changed-assignable-return-2.xml" }; 108 ApiCheck apiCheck = new ApiCheck(); 109 Report report = apiCheck.checkApi(args); 110 assertEquals(0, report.errors().size()); 111 } 112 testInsertedSuper()113 public void testInsertedSuper() { 114 String[] args = { "test/api/inserted-super-1.xml", "test/api/inserted-super-2.xml" }; 115 ApiCheck apiCheck = new ApiCheck(); 116 Report report = apiCheck.checkApi(args); 117 assertEquals(0, report.errors().size()); 118 } 119 testAddedInterface()120 public void testAddedInterface() { 121 String[] args = { "test/api/removed-interface.xml", "test/api/medium.xml" }; 122 ApiCheck apiCheck = new ApiCheck(); 123 Report report = apiCheck.checkApi(args); 124 assertEquals(1, report.errors().size()); 125 assertEquals(Errors.ADDED_INTERFACE, report.errors().iterator().next().error()); 126 } 127 testRemovedInterface()128 public void testRemovedInterface() { 129 String[] args = { "test/api/medium.xml", "test/api/removed-interface.xml" }; 130 ApiCheck apiCheck = new ApiCheck(); 131 Report report = apiCheck.checkApi(args); 132 assertEquals(1, report.errors().size()); 133 assertEquals(Errors.REMOVED_INTERFACE, report.errors().iterator().next().error()); 134 } 135 testChangedAbstractClass()136 public void testChangedAbstractClass() { 137 String[] args = { "test/api/medium.xml", "test/api/changed-abstract.xml" }; 138 ApiCheck apiCheck = new ApiCheck(); 139 Report report = apiCheck.checkApi(args); 140 assertEquals(1, report.errors().size()); 141 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 142 } 143 testChangedAbstractClass2()144 public void testChangedAbstractClass2() { 145 String[] args = { "test/api/changed-abstract.xml", "test/api/medium.xml" }; 146 ApiCheck apiCheck = new ApiCheck(); 147 Report report = apiCheck.checkApi(args); 148 assertEquals(1, report.errors().size()); 149 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 150 } 151 testChangedAbstractMethod()152 public void testChangedAbstractMethod() { 153 String[] args = { "test/api/medium.xml", "test/api/changed-abstract2.xml" }; 154 ApiCheck apiCheck = new ApiCheck(); 155 Report report = apiCheck.checkApi(args); 156 assertEquals(1, report.errors().size()); 157 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 158 } 159 testChangedAbstractMethod2()160 public void testChangedAbstractMethod2() { 161 String[] args = { "test/api/changed-abstract2.xml", "test/api/medium.xml" }; 162 ApiCheck apiCheck = new ApiCheck(); 163 Report report = apiCheck.checkApi(args); 164 assertEquals(1, report.errors().size()); 165 assertEquals(Errors.CHANGED_ABSTRACT, report.errors().iterator().next().error()); 166 } 167 testAddedPackage()168 public void testAddedPackage() { 169 String[] args = { "test/api/medium.xml", "test/api/added-package.xml" }; 170 ApiCheck apiCheck = new ApiCheck(); 171 Report report = apiCheck.checkApi(args); 172 assertEquals(1, report.errors().size()); 173 assertEquals(Errors.ADDED_PACKAGE, report.errors().iterator().next().error()); 174 } 175 testRemovedPackage()176 public void testRemovedPackage() { 177 String[] args = { "test/api/added-package.xml", "test/api/medium.xml" }; 178 ApiCheck apiCheck = new ApiCheck(); 179 Report report = apiCheck.checkApi(args); 180 assertEquals(1, report.errors().size()); 181 assertEquals(Errors.REMOVED_PACKAGE, report.errors().iterator().next().error()); 182 } 183 testChangedValue()184 public void testChangedValue() { 185 String[] args = { "test/api/constants.xml", "test/api/changed-value.xml" }; 186 ApiCheck apiCheck = new ApiCheck(); 187 Report report = apiCheck.checkApi(args); 188 assertEquals(1, report.errors().size()); 189 assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error()); 190 } 191 testChangedValue2()192 public void testChangedValue2() { 193 String[] args = { "test/api/constants.xml", "test/api/changed-value2.xml" }; 194 ApiCheck apiCheck = new ApiCheck(); 195 Report report = apiCheck.checkApi(args); 196 assertEquals(1, report.errors().size()); 197 assertEquals(Errors.CHANGED_VALUE, report.errors().iterator().next().error()); 198 } 199 testChangedType()200 public void testChangedType() { 201 String[] args = { "test/api/constants.xml", "test/api/changed-type.xml" }; 202 ApiCheck apiCheck = new ApiCheck(); 203 Report report = apiCheck.checkApi(args); 204 assertEquals(1, report.errors().size()); 205 assertEquals(Errors.CHANGED_TYPE, report.errors().iterator().next().error()); 206 } 207 testChangedFinalField()208 public void testChangedFinalField() { 209 String[] args = { "test/api/constants.xml", "test/api/changed-final.xml" }; 210 ApiCheck apiCheck = new ApiCheck(); 211 Report report = apiCheck.checkApi(args); 212 assertEquals(1, report.errors().size()); 213 assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error()); 214 } 215 testChangedFinalMethod()216 public void testChangedFinalMethod() { 217 String[] args = { "test/api/constants.xml", "test/api/changed-final2.xml" }; 218 ApiCheck apiCheck = new ApiCheck(); 219 Report report = apiCheck.checkApi(args); 220 assertEquals(1, report.errors().size()); 221 assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error()); 222 } 223 testChangedFinalClass()224 public void testChangedFinalClass() { 225 String[] args = { "test/api/constants.xml", "test/api/changed-final3.xml" }; 226 ApiCheck apiCheck = new ApiCheck(); 227 Report report = apiCheck.checkApi(args); 228 assertEquals(1, report.errors().size()); 229 assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error()); 230 } 231 testChangedFinalClass2()232 public void testChangedFinalClass2() { 233 String[] args = { "test/api/changed-final3.xml", "test/api/constants.xml" }; 234 ApiCheck apiCheck = new ApiCheck(); 235 Report report = apiCheck.checkApi(args); 236 assertEquals(1, report.errors().size()); 237 assertEquals(Errors.CHANGED_FINAL, report.errors().iterator().next().error()); 238 } 239 testAddedField()240 public void testAddedField() { 241 String[] args = { "test/api/constants.xml", "test/api/added-field.xml" }; 242 ApiCheck apiCheck = new ApiCheck(); 243 Report report = apiCheck.checkApi(args); 244 assertEquals(1, report.errors().size()); 245 assertEquals(Errors.ADDED_FIELD, report.errors().iterator().next().error()); 246 } 247 testRemovedField()248 public void testRemovedField() { 249 String[] args = { "test/api/added-field.xml", "test/api/constants.xml" }; 250 ApiCheck apiCheck = new ApiCheck(); 251 Report report = apiCheck.checkApi(args); 252 assertEquals(1, report.errors().size()); 253 assertEquals(Errors.REMOVED_FIELD, report.errors().iterator().next().error()); 254 } 255 testChangedStaticMethod()256 public void testChangedStaticMethod() { 257 String[] args = { "test/api/constants.xml", "test/api/changed-static.xml" }; 258 ApiCheck apiCheck = new ApiCheck(); 259 Report report = apiCheck.checkApi(args); 260 assertEquals(1, report.errors().size()); 261 assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error()); 262 } 263 testChangedStaticClass()264 public void testChangedStaticClass() { 265 String[] args = { "test/api/constants.xml", "test/api/changed-static2.xml" }; 266 ApiCheck apiCheck = new ApiCheck(); 267 Report report = apiCheck.checkApi(args); 268 assertEquals(1, report.errors().size()); 269 assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error()); 270 } 271 testChangedStaticField()272 public void testChangedStaticField() { 273 String[] args = { "test/api/constants.xml", "test/api/changed-static3.xml" }; 274 ApiCheck apiCheck = new ApiCheck(); 275 Report report = apiCheck.checkApi(args); 276 assertEquals(1, report.errors().size()); 277 assertEquals(Errors.CHANGED_STATIC, report.errors().iterator().next().error()); 278 } 279 testChangedTransient()280 public void testChangedTransient() { 281 String[] args = { "test/api/constants.xml", "test/api/changed-transient.xml" }; 282 ApiCheck apiCheck = new ApiCheck(); 283 Report report = apiCheck.checkApi(args); 284 assertEquals(1, report.errors().size()); 285 assertEquals(Errors.CHANGED_TRANSIENT, report.errors().iterator().next().error()); 286 } 287 testChangedSynchronized()288 public void testChangedSynchronized() { 289 String[] args = { "test/api/constants.xml", "test/api/changed-synchronized.xml" }; 290 ApiCheck apiCheck = new ApiCheck(); 291 Report report = apiCheck.checkApi(args); 292 assertEquals(0, report.errors().size()); 293 } 294 testChangedVolatile()295 public void testChangedVolatile() { 296 String[] args = { "test/api/constants.xml", "test/api/changed-volatile.xml" }; 297 ApiCheck apiCheck = new ApiCheck(); 298 Report report = apiCheck.checkApi(args); 299 assertEquals(1, report.errors().size()); 300 assertEquals(Errors.CHANGED_VOLATILE, report.errors().iterator().next().error()); 301 } 302 testChangedNative()303 public void testChangedNative() { 304 String[] args = { "test/api/constants.xml", "test/api/changed-native.xml" }; 305 ApiCheck apiCheck = new ApiCheck(); 306 Report report = apiCheck.checkApi(args); 307 assertEquals(1, report.errors().size()); 308 assertEquals(Errors.CHANGED_NATIVE, report.errors().iterator().next().error()); 309 } 310 testChangedScopeMethod()311 public void testChangedScopeMethod() { 312 String[] args = { "test/api/constants.xml", "test/api/changed-scope.xml" }; 313 ApiCheck apiCheck = new ApiCheck(); 314 Report report = apiCheck.checkApi(args); 315 assertEquals(1, report.errors().size()); 316 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 317 } 318 testChangedScopeClass()319 public void testChangedScopeClass() { 320 String[] args = { "test/api/changed-scope.xml", "test/api/constants.xml" }; 321 ApiCheck apiCheck = new ApiCheck(); 322 Report report = apiCheck.checkApi(args); 323 assertEquals(1, report.errors().size()); 324 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 325 } 326 testChangedScopeClass2()327 public void testChangedScopeClass2() { 328 String[] args = { "test/api/constants.xml", "test/api/changed-scope2.xml" }; 329 ApiCheck apiCheck = new ApiCheck(); 330 Report report = apiCheck.checkApi(args); 331 assertEquals(1, report.errors().size()); 332 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 333 } 334 testChangedScopeField()335 public void testChangedScopeField() { 336 String[] args = { "test/api/constants.xml", "test/api/changed-scope3.xml" }; 337 ApiCheck apiCheck = new ApiCheck(); 338 Report report = apiCheck.checkApi(args); 339 assertEquals(1, report.errors().size()); 340 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 341 } 342 testChangedConstructorScope()343 public void testChangedConstructorScope() { 344 String[] args = { "test/api/constants.xml", "test/api/changed-scope4.xml" }; 345 ApiCheck apiCheck = new ApiCheck(); 346 Report report = apiCheck.checkApi(args); 347 assertEquals(1, report.errors().size()); 348 assertEquals(Errors.CHANGED_SCOPE, report.errors().iterator().next().error()); 349 } 350 testChangedMethodThrows()351 public void testChangedMethodThrows() { 352 String[] args = { "test/api/throws.xml", "test/api/removed-exception.xml" }; 353 ApiCheck apiCheck = new ApiCheck(); 354 Report report = apiCheck.checkApi(args); 355 assertEquals(1, report.errors().size()); 356 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 357 } 358 testChangedMethodThrows2()359 public void testChangedMethodThrows2() { 360 String[] args = { "test/api/removed-exception.xml", "test/api/throws.xml" }; 361 ApiCheck apiCheck = new ApiCheck(); 362 Report report = apiCheck.checkApi(args); 363 assertEquals(1, report.errors().size()); 364 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 365 } 366 testChangedConstructorThrows()367 public void testChangedConstructorThrows() { 368 String[] args = { "test/api/throws.xml", "test/api/added-exception.xml" }; 369 ApiCheck apiCheck = new ApiCheck(); 370 Report report = apiCheck.checkApi(args); 371 assertEquals(1, report.errors().size()); 372 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 373 } 374 testChangedConstructorThrows2()375 public void testChangedConstructorThrows2() { 376 String[] args = { "test/api/added-exception.xml", "test/api/throws.xml" }; 377 ApiCheck apiCheck = new ApiCheck(); 378 Report report = apiCheck.checkApi(args); 379 assertEquals(1, report.errors().size()); 380 assertEquals(Errors.CHANGED_THROWS, report.errors().iterator().next().error()); 381 } 382 testChangedMethodDeprecated()383 public void testChangedMethodDeprecated() { 384 String[] args = { "test/api/constants.xml", "test/api/changed-deprecated.xml" }; 385 ApiCheck apiCheck = new ApiCheck(); 386 Report report = apiCheck.checkApi(args); 387 assertEquals(1, report.errors().size()); 388 assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error()); 389 } 390 testChangedConstructorDeprecated()391 public void testChangedConstructorDeprecated() { 392 String[] args = { "test/api/constants.xml", "test/api/changed-deprecated2.xml" }; 393 ApiCheck apiCheck = new ApiCheck(); 394 Report report = apiCheck.checkApi(args); 395 assertEquals(1, report.errors().size()); 396 assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error()); 397 } 398 testChangedFieldDeprecated()399 public void testChangedFieldDeprecated() { 400 String[] args = { "test/api/constants.xml", "test/api/changed-deprecated3.xml" }; 401 ApiCheck apiCheck = new ApiCheck(); 402 Report report = apiCheck.checkApi(args); 403 assertEquals(1, report.errors().size()); 404 assertEquals(Errors.CHANGED_DEPRECATED, report.errors().iterator().next().error()); 405 } 406 testChangedClassToInterface()407 public void testChangedClassToInterface() { 408 String[] args = { "test/api/changed-class-info2.xml", "test/api/changed-class-info.xml" }; 409 ApiCheck apiCheck = new ApiCheck(); 410 Report report = apiCheck.checkApi(args); 411 assertEquals(1, report.errors().size()); 412 assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error()); 413 } 414 testChangedInterfaceToClass()415 public void testChangedInterfaceToClass() { 416 String[] args = { "test/api/changed-class-info.xml", "test/api/changed-class-info2.xml" }; 417 ApiCheck apiCheck = new ApiCheck(); 418 Report report = apiCheck.checkApi(args); 419 assertEquals(1, report.errors().size()); 420 assertEquals(Errors.CHANGED_CLASS, report.errors().iterator().next().error()); 421 } 422 } 423