OSDN Git Service

add test case
authortama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Wed, 3 Dec 2008 08:08:18 +0000 (08:08 +0000)
committertama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Wed, 3 Dec 2008 08:08:18 +0000 (08:08 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/stigmata/plugins/trunk@334 acee48c3-7b26-0410-bdac-b3d0e5314bbc

wsp/pom.xml
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/Opcode.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeExtractionFinishListener.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeManager.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPattern.java
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/CurrentDepthTest.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/LabelOpcodeTest.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeManagerTest.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeTest.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternTest.java

index 224b92f..2c37e1b 100644 (file)
       <version>2.0.0-SNAPSHOT</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>\r
+      <groupId>asm</groupId>\r
+      <artifactId>asm-all</artifactId>\r
+      <version>2.2.3</version>\r
+      <scope>compile</scope>\r
+    </dependency>\r
   </dependencies>
 </project>
index 00358fb..29fe415 100644 (file)
@@ -65,6 +65,9 @@ public class Opcode implements Serializable{
     }
 
     public void addLabel(Label label){
+        if(label == null){
+            throw new NullPointerException();
+        }
         if(category != Category.BRANCH){
             throw new IllegalStateException("this method allows only branch category");
         }
@@ -72,14 +75,25 @@ public class Opcode implements Serializable{
     }
 
     public void setLabels(Label[] labelArray){
+        if(labelArray == null){
+            throw new NullPointerException();
+        }
         if(category != Category.BRANCH){
             throw new IllegalStateException("this method allows only branch category");
         }
+        labels.clear();
         for(Label label: labelArray){
+            if(label == null){
+                throw new NullPointerException();
+            }
             labels.add(label);
         }
     }
 
+    public Label getLabel(int index){
+        return labels.get(index);
+    }
+
     public Iterator<Label> labels(){
         return Collections.unmodifiableList(labels).iterator();
     }
index 459d422..ae4b256 100644 (file)
@@ -1,7 +1,16 @@
 package jp.sourceforge.stigmata.birthmarks.wsp;
 
+/*
+ * $Id$
+ */
+
 import java.util.List;
 
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
 public interface OpcodeExtractionFinishListener{
     public void finishExtractionOpcodes(List<Opcode> opcodes);
 }
index 3c79490..508e5c2 100644 (file)
@@ -25,7 +25,7 @@ public class OpcodeManager{
 
     static{
         try{
-            URL location = StackPatternBasedBirthmarkExtractor.class.getResource("/META-INF/bytecode.def");
+            URL location = OpcodeManager.class.getResource("/META-INF/bytecode.def");
             BufferedReader in = new BufferedReader(new InputStreamReader(location.openStream()));
             CsvParser parser = new CsvParser(in);
             while(parser.hasNext()){
index 25660d1..ca50313 100644 (file)
@@ -1,10 +1,19 @@
 package jp.sourceforge.stigmata.birthmarks.wsp;
 
+/*
+ * $Id$
+ */
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
 public class StackPattern implements Iterable<CurrentDepth>{
     private List<CurrentDepth> depthList = new ArrayList<CurrentDepth>();
     private int weight = -1;
diff --git a/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/CurrentDepthTest.java b/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/CurrentDepthTest.java
new file mode 100644 (file)
index 0000000..9f813c6
--- /dev/null
@@ -0,0 +1,31 @@
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+/*
+ * $Id$
+ */
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class CurrentDepthTest{
+    private CurrentDepth depth;
+    private Opcode opcode;
+
+    @Before
+    public void setup(){
+        opcode = new Opcode(4, "iconst_1", 0, 1, Opcode.Category.NORMAL);
+        depth = new CurrentDepth(3, opcode);
+    }
+
+    @Test
+    public void testBasic(){
+        Assert.assertEquals(3, depth.getDepth());
+        Assert.assertEquals(opcode, depth.getOpcode());
+    }
+}
diff --git a/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/LabelOpcodeTest.java b/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/LabelOpcodeTest.java
new file mode 100644 (file)
index 0000000..63aa486
--- /dev/null
@@ -0,0 +1,34 @@
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+/*
+ * $Id$
+ */
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.objectweb.asm.Label;
+
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class LabelOpcodeTest{
+    private LabelOpcode opcode;
+    private Label label;
+
+    @Before
+    public void setup(){
+        label = new Label();
+        opcode = new LabelOpcode(label);
+    }
+
+    @Test
+    public void testBasic(){
+        Assert.assertEquals(Opcode.Category.TARGETER, opcode.getCategory());
+        Assert.assertEquals(-1, opcode.getOpcode());
+        Assert.assertEquals("targeter", opcode.getName());
+        Assert.assertEquals(label, opcode.getLabel());
+    }
+}
diff --git a/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeManagerTest.java b/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeManagerTest.java
new file mode 100644 (file)
index 0000000..d799ae2
--- /dev/null
@@ -0,0 +1,25 @@
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+/*
+ * $Id$
+ */
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class OpcodeManagerTest{
+    @Test
+    public void testBasic(){
+        Assert.assertNotNull(OpcodeManager.getInstance());
+
+        Opcode opcode = OpcodeManager.getInstance().getOpcode(182);
+
+        Assert.assertEquals(182, opcode.getOpcode());
+        Assert.assertEquals("invokevirtual", opcode.getName());
+    }
+}
diff --git a/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeTest.java b/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeTest.java
new file mode 100644 (file)
index 0000000..1e4908c
--- /dev/null
@@ -0,0 +1,141 @@
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+/*
+ * $Id$
+ */
+
+import java.util.Iterator;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.objectweb.asm.Label;
+
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class OpcodeTest{
+    private Opcode opcode;
+
+    @Before
+    public void setup(){
+        opcode = new Opcode(26, "iload_0", 0, 1, Opcode.Category.NORMAL);
+    }
+
+    @Test
+    public void testBasic(){
+        Assert.assertEquals(26, opcode.getOpcode());
+        Assert.assertEquals("iload_0", opcode.getName());
+        Assert.assertEquals(0, opcode.getArgumentCount());
+        Assert.assertEquals(1, opcode.getAct());
+        Assert.assertEquals(Opcode.Category.NORMAL, opcode.getCategory());
+    }
+
+    @Test
+    public void testSelfConstructor(){
+        Opcode o = new Opcode(opcode);
+        Assert.assertEquals(26, o.getOpcode());
+        Assert.assertEquals("iload_0", o.getName());
+        Assert.assertEquals(0, o.getArgumentCount());
+        Assert.assertEquals(1, o.getAct());
+        Assert.assertEquals(Opcode.Category.NORMAL, o.getCategory());
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testSetActThrowException(){
+        opcode.setAct(1);
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testAddLabelThrowException(){
+        opcode.addLabel(new Label());
+    }
+
+    @Test(expected=NullPointerException.class)
+    public void testAddLabelNullPointer(){
+        opcode.addLabel(null);
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testSetLabelsThrowException(){
+        opcode.setLabels(new Label[] { new Label() });
+    }
+
+    @Test(expected=NullPointerException.class)
+    public void testSetLabelsNullPointer1(){
+        opcode.setLabels(null);
+    }
+
+    @Test
+    public void testAddLabel(){
+        opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
+        Label label1 = new Label();
+        Label label2 = new Label();
+        Label label3 = new Label();
+
+        opcode.addLabel(label1);
+        opcode.addLabel(label2);
+        opcode.addLabel(label3);
+
+        Assert.assertEquals(label1, opcode.getLabel(0));
+        Assert.assertEquals(label2, opcode.getLabel(1));
+        Assert.assertEquals(label3, opcode.getLabel(2));
+
+        Iterator<Label> iterator = opcode.labels();
+        Assert.assertTrue(iterator.hasNext());
+        Assert.assertEquals(label1, iterator.next());
+        Assert.assertTrue(iterator.hasNext());
+        Assert.assertEquals(label2, iterator.next());
+        Assert.assertTrue(iterator.hasNext());
+        Assert.assertEquals(label3, iterator.next());
+        Assert.assertFalse(iterator.hasNext());
+    }
+
+    @Test
+    public void testSetLabels(){
+        opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
+        Label label1 = new Label();
+        Label label2 = new Label();
+        Label label3 = new Label();
+
+        opcode.addLabel(label1);
+        opcode.setLabels(new Label[] { label1, label2, label3, });
+
+        Assert.assertEquals(label1, opcode.getLabel(0));
+        Assert.assertEquals(label2, opcode.getLabel(1));
+        Assert.assertEquals(label3, opcode.getLabel(2));
+
+        Iterator<Label> iterator = opcode.labels();
+        Assert.assertTrue(iterator.hasNext());
+        Assert.assertEquals(label1, iterator.next());
+        Assert.assertTrue(iterator.hasNext());
+        Assert.assertEquals(label2, iterator.next());
+        Assert.assertTrue(iterator.hasNext());
+        Assert.assertEquals(label3, iterator.next());
+        Assert.assertFalse(iterator.hasNext());
+    }
+
+    @Test(expected=NullPointerException.class)
+    public void testSetLabelsThrownNullPointerException(){
+        opcode = new Opcode(154, "ifne", 2, -1, "BRANCH");
+
+        opcode.setLabels(new Label[] { null, });
+    }
+
+    /**
+     * this test will be thrown IllegalStateException.
+     * Because, IllegalStateException is checked before null check of array elements.
+     */
+    @Test(expected=IllegalStateException.class)
+    public void testSetLabelsNullPointer2(){
+        opcode.setLabels(new Label[] { null, });
+    }
+
+    @Test
+    public void testSetAct() throws Exception{
+        opcode = new Opcode(182, "invokevirtual", 2, 0, "INVOKE");
+        opcode.setAct(4);
+    }
+}
index e37a67a..3eb6146 100644 (file)
@@ -47,16 +47,16 @@ public class StackPatternTest{
     public void testCalculateWeightedCommonSubsequence(){
         StackPattern pattern2 = new StackPattern();
         
-        pattern2.update(new CurrentDepth(1, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7)));
-        pattern2.update(new CurrentDepth(2, new Opcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1)));
-        pattern2.update(new CurrentDepth(1, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4)));
-        pattern2.update(new CurrentDepth(1, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1)));
-        pattern2.update(new CurrentDepth(2, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7)));
-        pattern2.update(new CurrentDepth(3, new Opcode(  5, "iconst_2",     0,  1, Opcode.Category.NORMAL, 1)));
-        pattern2.update(new CurrentDepth(2, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4)));
-        pattern2.update(new CurrentDepth(2, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1)));
-        pattern2.update(new CurrentDepth(1, new Opcode( 96, "iadd",         0, -1, Opcode.Category.NORMAL, 3)));
-        pattern2.update(new CurrentDepth(0, new Opcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2)));
+        pattern2.update(1, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
+        pattern2.update(2, new Opcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1));
+        pattern2.update(1, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
+        pattern2.update(1, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
+        pattern2.update(2, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
+        pattern2.update(3, new Opcode(  5, "iconst_2",     0,  1, Opcode.Category.NORMAL, 1));
+        pattern2.update(2, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
+        pattern2.update(2, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
+        pattern2.update(1, new Opcode( 96, "iadd",         0, -1, Opcode.Category.NORMAL, 3));
+        pattern2.update(0, new Opcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2));
 
         Assert.assertEquals(21, pattern.calculateWeight(pattern2));
         Assert.assertEquals(21, pattern2.calculateWeight(pattern));