changeset 42:71b4ce25fa98

Improve the tests for EncodedValue and add Mockito
author volker
date Fri, 18 Mar 2011 18:06:34 +0100
parents 0dc9d18f2633
children 8d1881b53b96
files pom.xml src/test/java/org/icedrobot/daneel/dex/EncodedValueTest.java
diffstat 2 files changed, 38 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pom.xml	Fri Mar 18 01:49:52 2011 +0100
+++ b/pom.xml	Fri Mar 18 18:06:34 2011 +0100
@@ -98,5 +98,11 @@
       <artifactId>jopt-simple</artifactId>
       <version>3.2</version>
     </dependency>
+    <dependency>
+    	<groupId>org.mockito</groupId>
+    	<artifactId>mockito-all</artifactId>
+    	<version>1.8.5</version>
+    	<scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
--- a/src/test/java/org/icedrobot/daneel/dex/EncodedValueTest.java	Fri Mar 18 01:49:52 2011 +0100
+++ b/src/test/java/org/icedrobot/daneel/dex/EncodedValueTest.java	Fri Mar 18 18:06:34 2011 +0100
@@ -23,7 +23,10 @@
 
 import java.nio.ByteBuffer;
 
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
+import static org.mockito.Mockito.*;
 
 public class EncodedValueTest {
 
@@ -32,6 +35,9 @@
     private static final byte[] SEQ_SHORT2 = new byte[] { 34, -1, 1 };
     private static final byte[] SEQ_SHORT3 = new byte[] { 34, 0, 1 };
     private static final byte[] SEQ_SHORT4 = new byte[] { 34, -1, 127 };
+    private static final byte[] SEQ_INT = new byte[] { 4, -5 };
+    private static final byte[] SEQ_LONG = new byte[] { -26, 127, -1, -1, -1,
+            -1, -1, -1, -1 };
     private static final byte[] SEQ_CHAR1 = new byte[] { 3, -1 };
     private static final byte[] SEQ_CHAR2 = new byte[] { 35, -1, 1 };
     private static final byte[] SEQ_CHAR3 = new byte[] { 35, 0, 1 };
@@ -40,6 +46,23 @@
     private static final byte[] SEQ_BOOLEAN1 = new byte[] { 31 };
     private static final byte[] SEQ_BOOLEAN2 = new byte[] { 63 };
     private static final byte[] SEQ_NULL = new byte[] { 30 };
+    private static final byte[] SEQ_STRING = new byte[] { 23, 5 };
+    private static final byte[] SEQ_UNKNOWN = new byte[] { 1 };
+
+    private static final String SOME_STRING = "some string";
+
+    private static DexFile dexFile;
+
+    @BeforeClass
+    public static void setUpBeforeClass() throws Exception {
+        dexFile = mock(DexFile.class);
+        when(dexFile.getString(5)).thenReturn(SOME_STRING);
+    }
+
+    @AfterClass
+    public static void tearDownAfterClass() throws Exception {
+        dexFile = null;
+    }
 
     @Test
     public void testParse() {
@@ -48,6 +71,8 @@
         assertEquals(Short.valueOf((short) 511), parse(SEQ_SHORT2));
         assertEquals(Short.valueOf((short) 256), parse(SEQ_SHORT3));
         assertEquals(Short.valueOf(Short.MAX_VALUE), parse(SEQ_SHORT4));
+        assertEquals(Integer.valueOf((byte) -5), parse(SEQ_INT));
+        assertEquals(Long.valueOf((byte) -1), parse(SEQ_LONG));
         assertEquals(Character.valueOf((char) 255), parse(SEQ_CHAR1));
         assertEquals(Character.valueOf((char) 511), parse(SEQ_CHAR2));
         assertEquals(Character.valueOf((char) 256), parse(SEQ_CHAR3));
@@ -56,9 +81,15 @@
         assertEquals(Boolean.FALSE, parse(SEQ_BOOLEAN1));
         assertEquals(Boolean.TRUE, parse(SEQ_BOOLEAN2));
         assertNull(parse(SEQ_NULL));
+        assertEquals(SOME_STRING, parse(SEQ_STRING));
+    }
+    
+    @Test(expected = DexParseException.class)
+    public void testParseUnknown() {
+        parse(SEQ_UNKNOWN);
     }
 
     private static Object parse(byte[] seq) {
-        return EncodedValue.parse(ByteBuffer.wrap(seq), null);
+        return EncodedValue.parse(ByteBuffer.wrap(seq), dexFile);
     }
 }