2013-05-08 68 views
0

我寫的JUnit下面提到的方法異常ArrayOutOfBound同時運行JUnit

有人可以提出適當的解決了這個

運行了JUnit之後我得到異常下面

java.lang.ArrayIndexOutOfBoundsException: -84 
    at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137) 
    at org.apache.commons.codec.binary.Base64.isArrayByteBase64(Base64.java:163) 

我測試類

public class DecodeToObjectTest extends TestCase { 


    public void testDecode() { 
     try { 
      String data="data"; 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       ObjectOutputStream oos = new ObjectOutputStream(baos); 

       oos.writeObject(data);  
      // convert String into InputStream 
      DecodeToObject.decode(new String(baos.toByteArray())); 


     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 

我需要測試的方法

public static 
    Object decode(
      String input) throws IOException, 
            ClassNotFoundException 
    { 
     Log log = LogFactory.getFactory().getInstance(
         "com.avocent.cps.report.service"); 

     try 
     { 
      boolean c = Base64.isArrayByteBase64(input.getBytes()); 

      if(log.isDebugEnabled()){ 
      log.debug("DecodeToObject :Entering Helper Input: " + input);   } 

      byte[] data = Base64.decodeBase64(input.getBytes()); 

      ObjectInputStream ois = new ObjectInputStream(
             new ByteArrayInputStream(data)); 

      if(log.isDebugEnabled()){ 
      log.debug("DecodeToObject :Object Input stream ois: " + ois);   } 

      Object obj = null; 

      obj = ois.readObject(); 


      if(log.isDebugEnabled()){ 
      log.debug(
       "DecodeToObject :Convering object to String: " 
       + obj.toString()); 
      } 
      ois.close(); 

       if(log.isDebugEnabled()){ 
         log.debug(
       "DecodeToObject :Decoded byte array to object successfully "); 
       } 

      return obj; 
     } 
     catch(ClassNotFoundException cnfe) 
     { 
       if(log.isErrorEnabled()){ 

      log.error("DecodeToObject : Error :"); 

        } 

      throw cnfe; 
     } 
     catch(IOException ioe) 
     { 

      if(log.isErrorEnabled()){ 
      log.error("DecodeToObject : Error :"); 

      } 

      throw ioe; 
     } 
    } 
+0

棧跟蹤是 – Programmer 2013-05-08 08:16:14

+0

現在我嘗試了byte [] temp = new byte [10]; \t \t \t/* ByteArrayOutputStream baos = new ByteArrayOutputStream(); \t \t ObjectOutputStream oos = new ObjectOutputStream(baos); \t \t oos.writeObject(data1); \t \t */ \t \t \t //轉換成字符串的InputStream \t \t \t 字節\t [] S = Base64.encodeBase64(溫度); \t \t \t DecodeToObject。decode(new String(s)); – Programmer 2013-05-08 09:14:23

回答

0

看來輸入不是Base64編碼。試試這個:

boolean c = Base64.isArrayByteBase64(input.getBytes()); 
if(log.isDebugEnabled()){ 
    log.debug("DecodeToObject :Entering Helper Input: " + input);   } 

if(c) { 
    byte[] data = Base64.decodeBase64(input.getBytes()); 
    // your code 
} 

但我想isArrayByteBase64()引發異常。看

source code
private static boolean isBase64(byte octect) { 
    if (octect == PAD) { 
     return true; 
    } else if (base64Alphabet[octect] == -1) { 
     return false; 
    } else { 
     return true; 
    } 
} 

靜態字節數組base64Alphabet在索引不具有元素「-84」,因此ArrayIndexOutOfBoundsException

+0

現在我嘗試了像byte [] temp = new byte [10]; \t \t \t/* ByteArrayOutputStream baos = new ByteArrayOutputStream(); \t \t ObjectOutputStream oos = new ObjectOutputStream(baos); \t \t oos.writeObject(data1); \t \t */ \t \t \t //轉換成字符串的InputStream \t \t \t 字節\t [] S = Base64.encodeBase64(溫度); \t \t \t DecodeToObject.decode(new String(s));它現在將erro設爲java.io.StreamCorruptedException:無效的流標頭:00000000 \t at java.io.ObjectInputStream.readStreamHeader(Unknown Source) – Programmer 2013-05-08 09:15:08

0

取而代之的是:

byte[] data = Base64.decodeBase64(input.getBytes()); 

試試這個:

byte[] s = Base64.encodeBase64URLSafe(input.getBytes()); 
byte[] data = Base64.decodeBase64(s); 
0

我遇到了同樣的異常。 JUnit測試在我的IDE上傳遞,但在Maven構建時失敗。

@RunWith(MockitoJUnitRunner.class)

切換到

@RunWith(PowerMockRunner.class)@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)來

解決了問題。