This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push: new 5b43e639 Added signature test cases for class/method, and bad signatures (#182) 5b43e639 is described below commit 5b43e639b0ae24547dcb052b9afc60a1822fa567 Author: nbauma109 <nbauma...@users.noreply.github.com> AuthorDate: Sat Apr 8 18:02:45 2023 +0200 Added signature test cases for class/method, and bad signatures (#182) * added signature test cases for class/method, and bad signatures * added tests for Type.getSignature * Tests signatures, including primitives, generics, and bad signatures for exceptions. --------- Co-authored-by: nbauma109 <nbauma...@github.com> Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com> --- .../apache/bcel/classfile/SignatureTestCase.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/test/java/org/apache/bcel/classfile/SignatureTestCase.java b/src/test/java/org/apache/bcel/classfile/SignatureTestCase.java new file mode 100644 index 00000000..43c835ff --- /dev/null +++ b/src/test/java/org/apache/bcel/classfile/SignatureTestCase.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.bcel.classfile; + +import org.apache.bcel.AbstractTestCase; +import org.apache.bcel.Repository; +import org.apache.bcel.generic.Type; +import org.junit.jupiter.api.Test; + +import java.io.DataInput; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; + +/** + * Tests signatures, including primitives, generics, and bad signatures for exceptions. + */ +public class SignatureTestCase extends AbstractTestCase { + + void testMethod(final JavaClass jc, final String expected, final Class<?> clazz, final String methodName, final Class<?>... paramTypes) throws Exception { + final Method method = jc.getMethod(clazz.getMethod(methodName, paramTypes)); + final String methodSignature = Optional.ofNullable(method.getGenericSignature()).orElse(method.getSignature()); + final String translatedMethodSignature = Signature.translate(methodSignature); + assertEquals(expected, translatedMethodSignature); + } + + @Test + public void testString() throws Exception { + final JavaClass jc = Repository.lookupClass(String.class); + final Signature classSignature = (Signature) findAttribute("Signature", jc.getAttributes()); + final String translatedSignature = Signature.translate(classSignature.getSignature()); + assertEquals("java.lang.Object", translatedSignature); + testMethod(jc, "()java.lang.String", String.class, "trim"); + + } + + @Test + public void testMap() throws Exception { + final JavaClass jc = Repository.lookupClass(Map.class); + final Signature classSignature = (Signature) findAttribute("Signature", jc.getAttributes()); + final String translatedSignature = Signature.translate(classSignature.getSignature()); + assertEquals("<K, V>java.lang.Object", translatedSignature); + testMethod(jc, "(java.lang.Object)V", Map.class, "get", Object.class); + testMethod(jc, "(K, V)V", Map.class, "put", Object.class, Object.class); + } + + @Test + public void testBadSignatures() throws Exception { + assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate("<")); + assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate("<>")); + } + + @Test + public void testType() throws Exception { + assertEquals("(I)I", Type.getSignature(Math.class.getMethod("abs", int.class))); + assertEquals("(J)J", Type.getSignature(Math.class.getMethod("abs", long.class))); + assertEquals("(D)D", Type.getSignature(Math.class.getMethod("abs", double.class))); + assertEquals("(F)F", Type.getSignature(Math.class.getMethod("abs", float.class))); + assertEquals("(Ljava/lang/String;)[Ljava/lang/String;", Type.getSignature(String.class.getMethod("split", String.class))); + assertEquals("(I)C", Type.getSignature(String.class.getMethod("charAt", int.class))); + assertEquals("()Ljava/lang/String;", Type.getSignature(String.class.getMethod("trim"))); + assertEquals("()V", Type.getSignature(Object.class.getMethod("notify"))); + assertEquals("(Ljava/lang/Object;)Z", Type.getSignature(Object.class.getMethod("equals", Object.class))); + assertEquals("()B", Type.getSignature(DataInput.class.getMethod("readByte"))); + assertEquals("()S", Type.getSignature(DataInput.class.getMethod("readShort"))); + } +}