1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 signature.comparator; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertNull; 22 23 import java.io.IOException; 24 25 import org.junit.Test; 26 27 import signature.comparator.util.AbstractComparatorTest; 28 import signature.compare.model.IApiDelta; 29 import signature.compare.model.IClassDefinitionDelta; 30 import signature.compare.model.DeltaType; 31 import signature.converter.util.CompilationUnit; 32 import signature.model.IApi; 33 34 public abstract class MethodCompareTests extends AbstractComparatorTest { 35 36 37 @Test 38 public void compareEqualClasses() throws IOException{ 39 CompilationUnit A = new CompilationUnit("a.A", 40 "package a; " + 41 "public class A {" + 42 " public void m(){}" + 43 "}"); 44 IApi fromApi = convert(A); 45 IApi toApi = convert(A); 46 assertNull(compare(fromApi, toApi)); 47 } 48 49 @Test 50 public void compareMissingMethod() throws IOException{ 51 CompilationUnit A = new CompilationUnit("a.A", 52 "package a; " + 53 "public class A {" + 54 " public void m(){}" + 55 "}"); 56 CompilationUnit AMissing = new CompilationUnit("a.A", 57 "package a; " + 58 "public class A {" + 59 "}"); 60 IApi fromApi = convert(A); 61 IApi toApi = convert(AMissing); 62 IApiDelta delta = compare(fromApi, toApi); 63 assertNotNull(delta); 64 IClassDefinitionDelta classDelta = delta.getPackageDeltas().iterator().next().getClassDeltas().iterator().next(); 65 assertEquals(1, classDelta.getMethodDeltas().size()); 66 assertEquals(DeltaType.REMOVED, classDelta.getMethodDeltas().iterator().next().getType()); 67 } 68 69 @Test 70 public void compareAddedMethod() throws IOException{ 71 CompilationUnit A = new CompilationUnit("a.A", 72 "package a; " + 73 "public class A {" + 74 " public void m(){}" + 75 "}"); 76 CompilationUnit AMissing = new CompilationUnit("a.A", 77 "package a; " + 78 "public class A {" + 79 "}"); 80 IApi fromApi = convert(AMissing); 81 IApi toApi = convert(A); 82 IApiDelta delta = compare(fromApi, toApi); 83 assertNotNull(delta); 84 IClassDefinitionDelta classDelta = delta.getPackageDeltas().iterator().next().getClassDeltas().iterator().next(); 85 assertEquals(1, classDelta.getMethodDeltas().size()); 86 assertEquals(DeltaType.ADDED, classDelta.getMethodDeltas().iterator().next().getType()); 87 } 88 89 @Test 90 public void compareChangedMethod() throws IOException{ 91 CompilationUnit A = new CompilationUnit("a.A", 92 "package a; " + 93 "public class A {" + 94 " public void m(){}" + 95 "}"); 96 CompilationUnit AMissing = new CompilationUnit("a.A", 97 "package a; " + 98 "public class A {" + 99 " public void m() throws Exception {}" + 100 "}"); 101 IApi fromApi = convert(AMissing); 102 IApi toApi = convert(A); 103 IApiDelta delta = compare(fromApi, toApi); 104 assertNotNull(delta); 105 IClassDefinitionDelta classDelta = delta.getPackageDeltas().iterator().next().getClassDeltas().iterator().next(); 106 assertEquals(1, classDelta.getMethodDeltas().size()); 107 assertEquals(DeltaType.CHANGED, classDelta.getMethodDeltas().iterator().next().getType()); 108 } 109 110 @Test 111 public void compareAddedParameterMethod() throws IOException{ 112 CompilationUnit A = new CompilationUnit("a.A", 113 "package a; " + 114 "public class A {" + 115 " public void m(){}" + 116 "}"); 117 CompilationUnit AMissing = new CompilationUnit("a.A", 118 "package a; " + 119 "public class A {" + 120 " public void m(int i) {}" + 121 "}"); 122 IApi fromApi = convert(AMissing); 123 IApi toApi = convert(A); 124 IApiDelta delta = compare(fromApi, toApi); 125 assertNotNull(delta); 126 IClassDefinitionDelta classDelta = delta.getPackageDeltas().iterator().next().getClassDeltas().iterator().next(); 127 assertEquals(2, classDelta.getMethodDeltas().size()); //one added , one removed 128 } 129 130 @Test 131 public void compareExceptions0() throws IOException{ 132 CompilationUnit E0 = new CompilationUnit("a.E0", 133 "package a; " + 134 "public class E0 extends Exception {}"); 135 CompilationUnit E1 = new CompilationUnit("a.E1", 136 "package a; " + 137 "public class E1 extends E0 {}"); 138 CompilationUnit C0 = new CompilationUnit("a.C0", 139 "package a; " + 140 "public class C0 {" + 141 " public void m() throws E0 {}" + 142 "}"); 143 CompilationUnit C0_E1 = new CompilationUnit("a.C0", 144 "package a; " + 145 "public class C0{" + 146 " public void m() throws E0, E1 {}" + 147 "}"); 148 IApi fromApi = convert(E0, E1, C0); 149 IApi toApi = convert(E0, E1, C0_E1); 150 IApiDelta apiDelta = compare(fromApi, toApi); 151 assertNull(apiDelta); 152 } 153 154 @Test 155 public void compareExceptions1() throws IOException{ 156 CompilationUnit E0 = new CompilationUnit("a.E0", 157 "package a; " + 158 "public class E0 extends Exception {}"); 159 CompilationUnit E1 = new CompilationUnit("a.E1", 160 "package a; " + 161 "public class E1 extends Exception {}"); 162 CompilationUnit C0 = new CompilationUnit("a.C0", 163 "package a; " + 164 "public class C0 {" + 165 " public void m() throws E0 {}" + 166 "}"); 167 CompilationUnit C0_E1 = new CompilationUnit("a.C0", 168 "package a; " + 169 "public class C0{" + 170 " public void m() throws E0, E1 {}" + 171 "}"); 172 IApi fromApi = convert(E0, E1, C0); 173 IApi toApi = convert(E0, E1, C0_E1); 174 IApiDelta apiDelta = compare(fromApi, toApi); 175 assertNotNull(apiDelta); 176 } 177 178 @Test 179 public void compareRuntimeExceptions() throws IOException{ 180 CompilationUnit E0 = new CompilationUnit("a.E0", 181 "package a; " + 182 "public class E0 extends RuntimeException {}"); 183 CompilationUnit C0 = new CompilationUnit("a.C0", 184 "package a; " + 185 "public class C0{" + 186 " public void m() {}" + 187 "}"); 188 CompilationUnit C0_E0 = new CompilationUnit("a.C0", 189 "package a; " + 190 "public class C0 {" + 191 " public void m() throws E0 {}" + 192 "}"); 193 IApi fromApi = convert(E0, C0); 194 IApi toApi = convert(E0, C0_E0); 195 IApiDelta apiDelta = compare(fromApi, toApi); 196 assertNull(apiDelta); 197 } 198 199 @Test 200 public void compareAnnotations() throws IOException{ 201 CompilationUnit C0 = new CompilationUnit("a.C0", 202 "package a; " + 203 "public class C0{" + 204 " public void m(int i) {}" + 205 "}"); 206 CompilationUnit C1 = new CompilationUnit("a.C0", 207 "package a; " + 208 "public class C0 {" + 209 " public void m(@Deprecated int i) {}" + 210 "}"); 211 IApi fromApi = convert(C0); 212 IApi toApi = convert(C1); 213 IApiDelta apiDelta = compare(fromApi, toApi); 214 assertNotNull(apiDelta); 215 } 216 217 @Test 218 public void compareMissingDefaultConstructor() throws IOException{ 219 CompilationUnit C0 = new CompilationUnit("a.C0", 220 "package a; " + 221 "public class C0{" + 222 " public C0() {}" + 223 "}"); 224 CompilationUnit C1 = new CompilationUnit("a.C0", 225 "package a; " + 226 "public class C0 {}"); 227 IApi fromApi = convert(C0); 228 IApi toApi = convert(C1); 229 IApiDelta apiDelta = compare(fromApi, toApi); 230 assertNull(apiDelta); 231 } 232 233 @Test 234 public void compareMissingAbstractMethod() throws IOException{ 235 CompilationUnit I = new CompilationUnit("a.I", 236 "package a; " + 237 "public interface I{" + 238 " void m();" + 239 "}"); 240 CompilationUnit C0 = new CompilationUnit("a.C0", 241 "package a; " + 242 "public abstract class C0 implements I{" + 243 " public abstract void m(); " + 244 "}"); 245 CompilationUnit C1 = new CompilationUnit("a.C0", 246 "package a; " + 247 "public abstract class C0 implements I{}"); 248 IApi fromApi = convert(C0, I); 249 IApi toApi = convert(C1, I); 250 IApiDelta apiDelta = compare(fromApi, toApi); 251 assertNull(apiDelta); 252 } 253 254 @Test 255 public void compareMissingInheritedMethod() throws IOException{ 256 CompilationUnit I = new CompilationUnit("a.I", 257 "package a; " + 258 "public class I{" + 259 " public void m(){};" + 260 "}"); 261 CompilationUnit C0 = new CompilationUnit("a.C0", 262 "package a; " + 263 "public class C0 extends I{" + 264 " public void m(){}; " + 265 "}"); 266 CompilationUnit C1 = new CompilationUnit("a.C0", 267 "package a; " + 268 "public class C0 extends I{}"); 269 IApi fromApi = convert(C0, I); 270 IApi toApi = convert(C1, I); 271 IApiDelta apiDelta = compare(fromApi, toApi); 272 assertNull(apiDelta); 273 } 274 275 @Test 276 public void compareMissingInheritedMethodGeneric0() throws IOException{ 277 CompilationUnit I = new CompilationUnit("a.I", 278 "package a; " + 279 "public class I<T>{" + 280 " public void m(T t){};" + 281 "}"); 282 CompilationUnit C0 = new CompilationUnit("a.C0", 283 "package a; " + 284 "public class C0<T> extends I<T>{" + 285 " public void m(T t){}; " + 286 "}"); 287 CompilationUnit C1 = new CompilationUnit("a.C0", 288 "package a; " + 289 "public class C0<T> extends I<T>{}"); 290 IApi fromApi = convert(C0, I); 291 IApi toApi = convert(C1, I); 292 IApiDelta apiDelta = compare(fromApi, toApi); 293 assertNull(apiDelta); 294 } 295 296 @Test 297 public void compareMissingInheritedMethodGeneric1() throws IOException{ 298 CompilationUnit I = new CompilationUnit("a.I", 299 "package a; " + 300 "public class I<T,S>{" + 301 " public void m(S s){};" + 302 "}"); 303 CompilationUnit C0 = new CompilationUnit("a.C0", 304 "package a; " + 305 "public class C0<Q, R> extends I<Q,R>{" + 306 " public void m(R t){}; " + 307 "}"); 308 CompilationUnit C1 = new CompilationUnit("a.C0", 309 "package a; " + 310 "public class C0<Y,Z> extends I<Y,Z>{}"); 311 IApi fromApi = convert(C0, I); 312 IApi toApi = convert(C1, I); 313 IApiDelta apiDelta = compare(fromApi, toApi); 314 assertNull(apiDelta); 315 } 316 317 @Test 318 public void compareMissingInheritedMethodGeneric2() throws IOException{ 319 CompilationUnit I = new CompilationUnit("a.I", 320 "package a; " + 321 "public class I<T,S>{" + 322 " public void m(S s){};" + 323 "}"); 324 CompilationUnit J = new CompilationUnit("a.J", 325 "package a; " + 326 "public class J<W> extends I<Number,W>{" + 327 " public void m(W w){};" + 328 "}"); 329 CompilationUnit C0 = new CompilationUnit("a.C0", 330 "package a; " + 331 "public class C0<Q> extends J<Q>{" + 332 " public void m(Q t){}; " + 333 "}"); 334 CompilationUnit C1 = new CompilationUnit("a.C0", 335 "package a; " + 336 "public class C0<Y> extends J<Y>{}"); 337 IApi fromApi = convert(C0, I, J); 338 IApi toApi = convert(C1, I, J); 339 IApiDelta apiDelta = compare(fromApi, toApi); 340 assertNull(apiDelta); 341 } 342 343 344 345 @Test 346 public void compareMissingInheritedMethodGeneric3() throws IOException{ 347 CompilationUnit Q = new CompilationUnit("a.Q", 348 "package a; " + 349 "public class Q<S,T>{ " + 350 " public void m(T s){} " + 351 "}"); 352 353 CompilationUnit W = new CompilationUnit("a.W", 354 "package a; " + 355 "public class W<A,B> extends Q<A,A>{}"); 356 CompilationUnit E0 = new CompilationUnit("a.E", 357 "package a; " + 358 "public class E<C,D> extends W<C,C>{" + 359 " public void m(C s){}" + 360 "}"); 361 CompilationUnit E1 = new CompilationUnit("a.E", 362 "package a; " + 363 "public class E<C,D> extends W<C,C>{}"); 364 IApi fromApi = convert(E0, Q, W); 365 IApi toApi = convert(E1, Q, W); 366 IApiDelta apiDelta = compare(fromApi, toApi); 367 assertNull(apiDelta); 368 } 369 370 @Test 371 public void compareMissingInheritedMethodGeneric4() throws IOException{ 372 CompilationUnit Q = new CompilationUnit("a.Q", 373 "package a; " + 374 "public class Q<S,T>{ " + 375 " public void m(T t, S s){} " + 376 "}"); 377 378 CompilationUnit W = new CompilationUnit("a.W", 379 "package a; " + 380 "public class W<A,B> extends Q<A,A>{}"); 381 CompilationUnit E0 = new CompilationUnit("a.E", 382 "package a; " + 383 "public class E<C,D> extends W<C,C>{" + 384 " public void m(C s, C c){}" + 385 "}"); 386 CompilationUnit E1 = new CompilationUnit("a.E", 387 "package a; " + 388 "public class E<C,D> extends W<C,C>{}"); 389 IApi fromApi = convert(E0, Q, W); 390 IApi toApi = convert(E1, Q, W); 391 IApiDelta apiDelta = compare(fromApi, toApi); 392 assertNull(apiDelta); 393 } 394 395 @Test 396 public void compareMissingInheritedMethodGeneric5() throws IOException{ 397 CompilationUnit Q = new CompilationUnit("a.Q", 398 "package a; " + 399 "public class Q{}"); 400 401 CompilationUnit I = new CompilationUnit("a.I", 402 "package a; " + 403 "public class I<S,T>{" + 404 " public void m(T s){};" + 405 "}"); 406 CompilationUnit C0 = new CompilationUnit("a.C0", 407 "package a; " + 408 "public class C0<P> extends I<P, Q>{" + 409 " public void m(Q t){}; " + 410 "}"); 411 CompilationUnit C1 = new CompilationUnit("a.C0", 412 "package a; " + 413 "public class C0<Y> extends I<Y, Q>{}"); 414 IApi fromApi = convert(C0, I, Q); 415 IApi toApi = convert(C1, I, Q); 416 IApiDelta apiDelta = compare(fromApi, toApi); 417 assertNull(apiDelta); 418 } 419 420 421 @Test 422 public void substitutionTest() throws IOException{ 423 CompilationUnit NUMBER = new CompilationUnit("a.Number", 424 "package a; " + 425 "public class Number{}"); 426 CompilationUnit Q = new CompilationUnit("a.A", 427 "package a; " + 428 "public class A<T>{ " + 429 " public void m(T t){} " + 430 "}"); 431 CompilationUnit E0 = new CompilationUnit("a.E", 432 "package a; " + 433 "public class E extends A<Number>{" + 434 " public void m(Number n){}" + 435 "}"); 436 CompilationUnit E1 = new CompilationUnit("a.E", 437 "package a; " + 438 "public class E extends A<Number>{}"); 439 IApi fromApi = convert(E0, Q, NUMBER); 440 IApi toApi = convert(E1, Q, NUMBER); 441 442 IApiDelta apiDelta = compare(fromApi, toApi); 443 assertNull(apiDelta); 444 } 445 446 @Test 447 public void substitutionArrayTest() throws IOException{ 448 CompilationUnit NUMBER = new CompilationUnit("a.Number", 449 "package a; " + 450 "public class Number{}"); 451 CompilationUnit Q = new CompilationUnit("a.A", 452 "package a; " + 453 "public class A<T>{ " + 454 " public void m(T[] t){} " + 455 "}"); 456 CompilationUnit E0 = new CompilationUnit("a.E", 457 "package a; " + 458 "public class E extends A<Number>{" + 459 " public void m(Number[] n){}" + 460 "}"); 461 CompilationUnit E1 = new CompilationUnit("a.E", 462 "package a; " + 463 "public class E extends A<Number>{}"); 464 IApi fromApi = convert(E0, Q, NUMBER); 465 IApi toApi = convert(E1, Q, NUMBER); 466 467 IApiDelta apiDelta = compare(fromApi, toApi); 468 assertNull(apiDelta); 469 } 470 471 @Test 472 public void substitutionNestedTypes() throws IOException{ 473 CompilationUnit NUMBER = new CompilationUnit("a.Number", 474 "package a; " + 475 "public class Number{}"); 476 CompilationUnit Q = new CompilationUnit("a.A", 477 "package a; " + 478 "public class A<T>{ " + 479 " public void m(A<A<T[]>> t){} " + 480 "}"); 481 CompilationUnit E0 = new CompilationUnit("a.E", 482 "package a; " + 483 "public class E extends A<Number>{" + 484 " public void m(A<A<Number[]>> n){}" + 485 "}"); 486 CompilationUnit E1 = new CompilationUnit("a.E", 487 "package a; " + 488 "public class E extends A<Number>{}"); 489 IApi fromApi = convert(E0, Q, NUMBER); 490 IApi toApi = convert(E1, Q, NUMBER); 491 492 IApiDelta apiDelta = compare(fromApi, toApi); 493 assertNull(apiDelta); 494 } 495 } 496