1 /* 2 * Copyright (C) 2019 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 com.android.class2nonsdklist; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.testng.Assert.assertThrows; 22 23 import com.android.annotationvisitor.AnnotationHandlerTestBase; 24 25 import org.junit.Test; 26 27 28 public class ApiComponentsTest extends AnnotationHandlerTestBase { 29 30 @Test testGetApiComponentsPackageFromSignature()31 public void testGetApiComponentsPackageFromSignature() throws SignatureSyntaxError { 32 ApiComponents api = ApiComponents.fromDexSignature("La/b/C;->foo()V"); 33 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 34 assertThat(packageAndClassName.packageName).isEqualTo("a.b"); 35 } 36 37 @Test testGetApiComponentsFromSignature()38 public void testGetApiComponentsFromSignature() throws SignatureSyntaxError { 39 ApiComponents api = ApiComponents.fromDexSignature("La/b/C;->foo(IJLfoo2/bar/Baz;)V"); 40 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 41 assertThat(packageAndClassName.className).isEqualTo("C"); 42 assertThat(api.getMemberName()).isEqualTo("foo"); 43 assertThat(api.getMethodParameterTypes()).isEqualTo("int, long, foo2.bar.Baz"); 44 } 45 46 @Test testInvalidDexSignatureInvalidClassFormat()47 public void testInvalidDexSignatureInvalidClassFormat() throws SignatureSyntaxError { 48 assertThrows(SignatureSyntaxError.class, () -> { 49 ApiComponents.fromDexSignature("a/b/C;->foo()V"); 50 }); 51 assertThrows(SignatureSyntaxError.class, () -> { 52 ApiComponents.fromDexSignature("La/b/C->foo()V"); 53 }); 54 } 55 56 @Test testInvalidDexSignatureInvalidParameterType()57 public void testInvalidDexSignatureInvalidParameterType() throws SignatureSyntaxError { 58 assertThrows(SignatureSyntaxError.class, () -> { 59 ApiComponents.fromDexSignature("a/b/C;->foo(foo)V"); 60 }); 61 } 62 63 @Test testInvalidDexSignatureInvalidReturnType()64 public void testInvalidDexSignatureInvalidReturnType() throws SignatureSyntaxError { 65 assertThrows(SignatureSyntaxError.class, () -> { 66 ApiComponents.fromDexSignature("a/b/C;->foo()foo"); 67 }); 68 } 69 70 @Test testInvalidDexSignatureMissingReturnType()71 public void testInvalidDexSignatureMissingReturnType() throws SignatureSyntaxError { 72 assertThrows(SignatureSyntaxError.class, () -> { 73 ApiComponents.fromDexSignature("a/b/C;->foo(I)"); 74 }); 75 } 76 77 @Test testInvalidDexSignatureMissingArrowOrColon()78 public void testInvalidDexSignatureMissingArrowOrColon() throws SignatureSyntaxError { 79 assertThrows(SignatureSyntaxError.class, () -> { 80 ApiComponents.fromDexSignature("La/b/C;foo()V"); 81 }); 82 } 83 84 @Test testGetApiComponentsFromFieldLink()85 public void testGetApiComponentsFromFieldLink() throws JavadocLinkSyntaxError { 86 ApiComponents api = ApiComponents.fromLinkTag("a.b.C#foo(int, long, foo2.bar.Baz)", 87 "La/b/C;->foo:I"); 88 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 89 assertThat(packageAndClassName.packageName).isEqualTo("a.b"); 90 assertThat(packageAndClassName.className).isEqualTo("C"); 91 assertThat(api.getMemberName()).isEqualTo("foo"); 92 } 93 94 @Test testGetApiComponentsLinkOnlyClass()95 public void testGetApiComponentsLinkOnlyClass() throws JavadocLinkSyntaxError { 96 ApiComponents api = ApiComponents.fromLinkTag("b.c.D", "La/b/C;->foo:I"); 97 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 98 assertThat(packageAndClassName.packageName).isEqualTo("b.c"); 99 assertThat(packageAndClassName.className).isEqualTo("D"); 100 assertThat(api.getMethodParameterTypes()).isEqualTo(""); 101 } 102 103 @Test testGetApiComponentsFromLinkOnlyClassDeducePackage()104 public void testGetApiComponentsFromLinkOnlyClassDeducePackage() throws JavadocLinkSyntaxError { 105 ApiComponents api = ApiComponents.fromLinkTag("D", "La/b/C;->foo:I"); 106 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 107 assertThat(packageAndClassName.packageName).isEqualTo("a.b"); 108 assertThat(packageAndClassName.className).isEqualTo("D"); 109 assertThat(api.getMemberName().isEmpty()).isTrue(); 110 assertThat(api.getMethodParameterTypes().isEmpty()).isTrue(); 111 } 112 113 @Test testGetApiComponentsParametersFromMethodLink()114 public void testGetApiComponentsParametersFromMethodLink() throws JavadocLinkSyntaxError { 115 ApiComponents api = ApiComponents.fromLinkTag("a.b.C#foo(int, long, foo2.bar.Baz)", 116 "La/b/C;->foo:I"); 117 assertThat(api.getMethodParameterTypes()).isEqualTo("int, long, foo2.bar.Baz"); 118 } 119 120 @Test testDeduceApiComponentsPackageFromLinkUsingContext()121 public void testDeduceApiComponentsPackageFromLinkUsingContext() throws JavadocLinkSyntaxError { 122 ApiComponents api = ApiComponents.fromLinkTag("C#foo(int, long, foo2.bar.Baz)", 123 "La/b/C;->foo:I"); 124 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 125 assertThat(packageAndClassName.packageName).isEqualTo("a.b"); 126 } 127 128 @Test testDeduceApiComponentsPackageAndClassFromLinkUsingContext()129 public void testDeduceApiComponentsPackageAndClassFromLinkUsingContext() 130 throws JavadocLinkSyntaxError { 131 ApiComponents api = ApiComponents.fromLinkTag("#foo(int, long, foo2.bar.Baz)", 132 "La/b/C;->foo:I"); 133 PackageAndClassName packageAndClassName = api.getPackageAndClassName(); 134 assertThat(packageAndClassName.packageName).isEqualTo("a.b"); 135 assertThat(packageAndClassName.className).isEqualTo("C"); 136 } 137 138 @Test testInvalidLinkTagUnclosedParenthesis()139 public void testInvalidLinkTagUnclosedParenthesis() throws JavadocLinkSyntaxError { 140 assertThrows(JavadocLinkSyntaxError.class, () -> { 141 ApiComponents.fromLinkTag("a.b.C#foo(int,float", "La/b/C;->foo()V"); 142 }); 143 } 144 145 }