[Fb-contrib-commit] SF.net SVN: fb-contrib:[1805] trunk/fb-contrib/src
Brought to you by:
dbrosius
From: <dbr...@us...> - 2016-11-18 03:36:11
|
Revision: 1805 http://sourceforge.net/p/fb-contrib/code/1805 Author: dbrosius Date: 2016-11-18 03:36:09 +0000 (Fri, 18 Nov 2016) Log Message: ----------- add tests Added Paths: ----------- trunk/fb-contrib/src/test/ trunk/fb-contrib/src/test/java/ trunk/fb-contrib/src/test/java/com/ trunk/fb-contrib/src/test/java/com/mebigfatguy/ trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/ trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/ trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/RegisterUtilsTest.java trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureBuilderTest.java trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureUtilsTest.java Added: trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/RegisterUtilsTest.java =================================================================== --- trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/RegisterUtilsTest.java (rev 0) +++ trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/RegisterUtilsTest.java 2016-11-18 03:36:09 UTC (rev 1805) @@ -0,0 +1,132 @@ +package com.mebigfatguy.fbcontrib.utils; + +import edu.umd.cs.findbugs.FindBugs; +import edu.umd.cs.findbugs.visitclass.DismantleBytecode; + +import static org.apache.bcel.Constants.*; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.ConstantUtf8; +import org.apache.bcel.classfile.Method; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class RegisterUtilsTest { + + private static final int OPERAND = 12345; + + @Mock private DismantleBytecode dbc; + + @BeforeSuite + public void setUpClass() { + FindBugs.setHome("target/findbugs-3.0.1.jar"); + } + + @BeforeMethod + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(dbc.getRegisterOperand()).thenReturn(OPERAND); + } + + @DataProvider(name = "parameterRegisters") + Object[][] parameterRegisters() { + return new Object[][] { + // access flags, signature, expected registers + {ACC_PUBLIC | ACC_STATIC, "(JLjava/lang/String;[[I)V", new int[] { 0, 2, 3 }}, + {ACC_STATIC, "(Ljava/lang/Object;)Z", new int[] { 0 }}, + {ACC_PUBLIC, "(DJ[D[J)V", new int[] { 1, 3, 5, 6 }}, + {0, "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", new int[] { 1, 2 }}, + }; + } + + @DataProvider(name = "reg_ASTORE") + Object[][] reg_ASTORE() { + return new Object[][] { + {ASTORE, OPERAND}, {ASTORE_0, 0}, {ASTORE_1, 1}, {ASTORE_2, 2}, {ASTORE_3, 3}, {ASTORE_0 - 1, -1}, {ASTORE_3 + 1, -1}, + }; + } + + @DataProvider(name = "reg_ALOAD") + Object[][] reg_ALOAD() { + return new Object[][] { + {ALOAD_0, 0}, {ALOAD_1, 1}, {ALOAD_2, 2}, {ALOAD_3, 3}, {ALOAD_0 - 1, -1}, {ALOAD_3 + 1, -1}, + }; + } + + @DataProvider(name = "regStore") + Object[][] regStore() { + return new Object[][] { + {ASTORE, OPERAND}, {ISTORE, OPERAND}, {LSTORE, OPERAND}, {FSTORE, OPERAND}, {DSTORE, OPERAND}, + {ASTORE_0, 0}, {ASTORE_1, 1}, {ASTORE_2, 2}, {ASTORE_3, 3}, + {ISTORE_0, 0}, {ISTORE_1, 1}, {ISTORE_2, 2}, {ISTORE_3, 3}, + {LSTORE_0, 0}, {LSTORE_1, 1}, {LSTORE_2, 2}, {LSTORE_3, 3}, + {FSTORE_0, 0}, {FSTORE_1, 1}, {FSTORE_2, 2}, {FSTORE_3, 3}, + {DSTORE_0, 0}, {DSTORE_1, 1}, {DSTORE_2, 2}, {DSTORE_3, 3}, + {Integer.MIN_VALUE, -1}, {Integer.MAX_VALUE, -1}, + }; + } + + @DataProvider(name = "regLoad") + Object[][] regLoad() { + return new Object[][] { + {ALOAD, OPERAND}, {ILOAD, OPERAND}, {LLOAD, OPERAND}, {FLOAD, OPERAND}, {DLOAD, OPERAND}, + {ALOAD_0, 0}, {ALOAD_1, 1}, {ALOAD_2, 2}, {ALOAD_3, 3}, + {ILOAD_0, 0}, {ILOAD_1, 1}, {ILOAD_2, 2}, {ILOAD_3, 3}, + {LLOAD_0, 0}, {LLOAD_1, 1}, {LLOAD_2, 2}, {LLOAD_3, 3}, + {FLOAD_0, 0}, {FLOAD_1, 1}, {FLOAD_2, 2}, {FLOAD_3, 3}, + {DLOAD_0, 0}, {DLOAD_1, 1}, {DLOAD_2, 2}, {DLOAD_3, 3}, + {Integer.MIN_VALUE, -1}, {Integer.MAX_VALUE, -1}, + }; + } + + @Test(dataProvider = "parameterRegisters") + public void shouldGetParameterRegisters(int accessFlags, String signature, int[] expected) { + Method method = new Method(); + method.setAccessFlags(accessFlags); + + Constant[] cnst = new Constant[] { new ConstantUtf8(signature) }; + ConstantPool cp = new ConstantPool(cnst) { + + @Override + protected Object clone() throws CloneNotSupportedException { + throw new CloneNotSupportedException(); + } + + }; + method.setConstantPool(cp); + method.setSignatureIndex(0); + + int[] regs = RegisterUtils.getParameterRegisters(method); + + assertEquals(regs, expected); + } + + @Test(dataProvider = "reg_ASTORE") + public void shouldReturnOffsetWhenSeenASTORE(int seen, int expected) { + assertEquals(RegisterUtils.getAStoreReg(dbc, seen), expected); + } + + @Test(dataProvider = "reg_ALOAD") + public void shouldReturnOffsetWhenSeenALOAD(int seen, int expected) { + assertEquals(RegisterUtils.getALoadReg(dbc, seen), expected); + } + + @Test(dataProvider = "regStore") + public void shouldReturnStoreReg(int seen, int expected) { + assertEquals(RegisterUtils.getStoreReg(dbc, seen), expected); + } + + @Test(dataProvider = "regLoad") + public void shouldReturnLoadReg(int seen, int expected) { + assertEquals(RegisterUtils.getLoadReg(dbc, seen), expected); + } + +} Property changes on: trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/RegisterUtilsTest.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureBuilderTest.java =================================================================== --- trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureBuilderTest.java (rev 0) +++ trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureBuilderTest.java 2016-11-18 03:36:09 UTC (rev 1805) @@ -0,0 +1,68 @@ +/* + * fb-contrib - Auxiliary detectors for Java programs + * Copyright (C) 2005-2016 ThrawnCA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package com.mebigfatguy.fbcontrib.utils; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +public class SignatureBuilderTest { + + @Test + public void shouldDefaultToVoidReturnWithNoParameters() { + assertEquals(new SignatureBuilder().build(), "()V"); + } + + @Test + public void shouldConvertClassnamesToSignatures() { + assertEquals(new SignatureBuilder().withMethodName("classNames").withParamTypes("java.lang.String").withReturnType("java/util/Collection").build(), + "classNames(Ljava/lang/String;)Ljava/util/Collection;"); + } + + @Test + public void shouldPreserveSignatures() { + assertEquals(new SignatureBuilder().withMethodName("signatures").withParamTypes("Ljava/lang/Object;").withReturnType("Ljava/util/Date;").build(), + "signatures(Ljava/lang/Object;)Ljava/util/Date;"); + } + + @Test + public void shouldOverrideExistingParamTypes() { + assertEquals(new SignatureBuilder().withMethodName("overrideParams").withParamTypes("java/lang/Object") + .withParamTypes("java/lang/String", "java/lang/Boolean").build(), "overrideParams(Ljava/lang/String;Ljava/lang/Boolean;)V"); + } + + @Test + public void shouldAcceptPrimitiveTypes() { + assertEquals(new SignatureBuilder().withMethodName("primitives").withParamTypes("B", "S", "C", "I", "J", "java.lang.Object", "Z", "F") + .withReturnType("D").build(), "primitives(BSCIJLjava/lang/Object;ZF)D"); + } + + @Test + public void shouldAcceptClasses() { + assertEquals(new SignatureBuilder().withMethodName("classes").withParamTypes(String.class, Object.class).withReturnType(Integer.class).build(), + "classes(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Integer;"); + } + + @Test + public void shouldBlankReturnTypeWhenNeeded() { + assertEquals(new SignatureBuilder().withMethodName("noReturn").withParamTypes("java.lang.Object").withoutReturnType().build(), + "noReturn(Ljava/lang/Object;)"); + } + +} Property changes on: trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureBuilderTest.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureUtilsTest.java =================================================================== --- trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureUtilsTest.java (rev 0) +++ trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureUtilsTest.java 2016-11-18 03:36:09 UTC (rev 1805) @@ -0,0 +1,112 @@ +/* + * fb-contrib - Auxiliary detectors for Java programs + * Copyright (C) 2005-2016 ThrawnCA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package com.mebigfatguy.fbcontrib.utils; + +import static org.testng.Assert.assertEquals; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class SignatureUtilsTest { + + @DataProvider(name = "namesToSignatures") + public Object[][] namesToSignatures() { + String sigString = "Ljava/lang/String;"; + return new Object[][] { { "java.lang.String", sigString }, { "java/lang/String", sigString }, { sigString, sigString }, { "B", "B" }, { "S", "S" }, + { "C", "C" }, { "I", "I" }, { "J", "J" }, { "F", "F" }, { "D", "D" }, { "Z", "Z" }, { "", "" }, { null, null }, }; + } + + @DataProvider(name = "signaturesToSlashedNames") + public Object[][] signaturesToSlashedNames() { + String slashedString = "java/lang/String"; + return new Object[][] { { "Ljava/lang/String;", slashedString }, { slashedString, slashedString }, { "B", "B" }, { "S", "S" }, { "C", "C" }, + { "I", "I" }, { "J", "J" }, { "F", "F" }, { "D", "D" }, { "Z", "Z" }, { "", "" }, { null, null }, }; + } + + @DataProvider(name = "signaturesToDottedNames") + public Object[][] signaturesToDottedNames() { + String dottedString = "java.lang.String"; + return new Object[][] { { "Ljava/lang/String;", dottedString }, { "java/lang/String", dottedString }, { dottedString, dottedString }, { "B", "B" }, + { "S", "S" }, { "C", "C" }, { "I", "I" }, { "J", "J" }, { "F", "F" }, { "D", "D" }, { "Z", "Z" }, { "", "" }, }; + } + + @DataProvider(name = "namesToArrays") + public Object[][] namesToArrays() { + String sigStringArray = "[Ljava/lang/String;"; + return new Object[][] { { "java.lang.String", sigStringArray }, { "java/lang/String", sigStringArray }, { sigStringArray, "[[Ljava/lang/String;" }, + { "B", "[B" }, { "S", "[S" }, { "C", "[C" }, { "I", "[I" }, { "J", "[J" }, { "F", "[F" }, { "D", "[D" }, { "Z", "[Z" }, { "", "" }, + { null, null }, }; + } + + @Test + public void shouldGetParameterSlotsAndSignaturesForInstanceMethod() { + Map<Integer, String> expected = new HashMap<>(2); + expected.put(1, "I"); + expected.put(2, "Ljava/lang/Object;"); + assertEquals(SignatureUtils.getParameterSlotAndSignatures(false, "add(ILjava/lang/Object;)Ljava/lang/Object;"), expected); + } + + @Test + public void shouldGetParameterSlotsAndSignaturesForStaticMethod() { + Map<Integer, String> expected = new HashMap<>(2); + expected.put(0, "I"); + expected.put(1, "Ljava/lang/Object;"); + assertEquals(SignatureUtils.getParameterSlotAndSignatures(true, "add(ILjava/lang/Object;)Ljava/lang/Object;"), expected); + } + + @Test + public void shouldGetParameterSignatures() { + assertEquals(SignatureUtils.getParameterSignatures("add(ILjava/lang/Object;)Ljava/lang/Object;"), Arrays.asList("I", "Ljava/lang/Object;")); + } + + @Test + public void shouldCountParameterSignatures() { + assertEquals(SignatureUtils.getNumParameters("add(ILjava/lang/Object;)Ljava/lang/Object;"), 2); + } + + @Test + public void shouldIgnoreEclipseParameterSignatures() { + assertEquals(SignatureUtils.getParameterSignatures("wonky(!Ljava/lang/Object;++)Ljava/lang/Object;"), Arrays.asList("Ljava/lang/Object;")); + } + + @Test(dataProvider = "namesToSignatures") + public void shouldConvertClassnamesToSignatures(String input, String expected) { + assertEquals(SignatureUtils.classToSignature(input), expected); + } + + @Test(dataProvider = "signaturesToSlashedNames") + public void shouldConvertSignaturesToSlashedClassNames(String input, String expected) { + assertEquals(SignatureUtils.trimSignature(input), expected); + } + + @Test(dataProvider = "signaturesToDottedNames") + public void shouldConvertSignaturesToDottedClassNames(String input, String expected) { + assertEquals(SignatureUtils.stripSignature(input), expected); + } + + @Test(dataProvider = "namesToArrays") + public void shouldConvertClassnamesToArrays(String input, String expected) { + assertEquals(SignatureUtils.toArraySignature(input), expected); + } + +} Property changes on: trunk/fb-contrib/src/test/java/com/mebigfatguy/fbcontrib/utils/SignatureUtilsTest.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |