⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64test.java

📁 一个很实用的东东
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		try {
			Base64 b64 = new Base64();
			byte[] result = b64.decode( bArray );
            
			assertTrue( "The result should be empty as the test encoded content did " +
				"not contain any valid base 64 characters", result.length == 0);
		} 
		catch( Exception e ) {
			exceptionThrown = true;
		}

		assertFalse( "Exception was thrown when trying to decode " +
					"invalid base64 encoded data - RFC 2045 requires that all " +
					"non base64 character be discarded, an exception should not" +
					" have been thrown", exceptionThrown );
          

	}
    
	public void testIgnoringNonBase64InDecode() throws Exception {
		assertEquals("The quick brown fox jumped over the lazy dogs.",new String(Base64.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes())));
	}

    public void testObjectDecodeWithInvalidParameter() throws Exception {
        boolean exceptionThrown = false;

        Base64 b64 = new Base64();

        try {
            Object o = new String( "Yadayadayada" );
            b64.decode( o );
        } catch( Exception e ) {
            exceptionThrown = true;
        }

        assertTrue( "decode(Object) didn't throw an exception when passed a " +
                    "String object", exceptionThrown );
    }

    public void testObjectDecodeWithValidParameter() throws Exception {

        String original = "Hello World!";
        byte[] bArray = 
            Base64.encodeBase64( (new String(original)).getBytes() );
        Object o = bArray;
        
        Base64 b64 = new Base64();
        Object oDecoded = b64.decode( o );
        byte[] baDecoded = (byte[]) oDecoded;
        String dest = new String( baDecoded );

        assertTrue( "dest string down not equal original",
                    dest.equals( original ) );
    }

    public void testObjectEncodeWithInvalidParameter() throws Exception {
        boolean exceptionThrown = false;

        Base64 b64 = new Base64();

        try {
            Object o = new String( "Yadayadayada" );
            b64.encode( o );
        } catch( Exception e ) {
            exceptionThrown = true;
        }

        assertTrue( "encode(Object) didn't throw an exception when passed a " +
                    "String object", exceptionThrown );
    }

    public void testObjectEncodeWithValidParameter() throws Exception {

        String original = "Hello World!";
        byte[] origBytes = original.getBytes();
        Object origObj = origBytes;

        Base64 b64 = new Base64();
        Object oEncoded = b64.encode( origObj );
        byte[] bArray = 
            Base64.decodeBase64( (byte[]) oEncoded );
        String dest = new String( bArray );

        assertTrue( "dest string down not equal original",
                    dest.equals( original ) );
    }

    public void testDecodeWithWhitespace() throws Exception {

        String orig = "I am a late night coder.";

        byte[] encodedArray = Base64.encodeBase64( orig.getBytes() );
        StringBuffer intermediate = 
            new StringBuffer( new String(encodedArray) );

        intermediate.insert( 2, ' ' );
        intermediate.insert( 5, '\t' );
        intermediate.insert( 10, '\r' );
        intermediate.insert( 15, '\n' );

        byte[] encodedWithWS = intermediate.toString().getBytes();
        byte[] decodedWithWS = Base64.decodeBase64( encodedWithWS );

        String dest = new String( decodedWithWS );

        assertTrue( "Dest string doesn't equals the original", 
                    dest.equals( orig ) );
    }

    public void testDiscardWhitespace() throws Exception {

        String orig = "I am a late night coder.";

        byte[] encodedArray = Base64.encodeBase64( orig.getBytes() );
        StringBuffer intermediate = 
            new StringBuffer( new String(encodedArray) );

        intermediate.insert( 2, ' ' );
        intermediate.insert( 5, '\t' );
        intermediate.insert( 10, '\r' );
        intermediate.insert( 15, '\n' );

        byte[] encodedWithWS = intermediate.toString().getBytes();
        byte[] encodedNoWS = Base64.discardWhitespace( encodedWithWS );
        byte[] decodedWithWS = Base64.decodeBase64( encodedWithWS );
        byte[] decodedNoWS = Base64.decodeBase64( encodedNoWS );

        String destFromWS = new String( decodedWithWS );
        String destFromNoWS = new String( decodedNoWS );

        assertTrue( "Dest string doesn't eausl original", 
                destFromWS.equals( orig ) );
        assertTrue( "Dest string doesn't eausl original", 
                destFromNoWS.equals( orig ) );
    }

    // -------------------------------------------------------- Private Methods

    private String toString(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for(int i=0;i<data.length;i++) {
            buf.append(data[i]);
            if(i != data.length-1) {
                buf.append(",");
            }
        }
        return buf.toString();
    }

    // ------------------------------------------------------------------------

    private Random _random = new Random();

    /**
     * @return Returns the _random.
     */
    public Random getRandom() {
        return this._random;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -