Package org.internna.iwebmvc.utils

Source Code of org.internna.iwebmvc.utils.ByteUtilsTest

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.utils;

import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;

public class ByteUtilsTest {

    @Test
    public void hasLength() {
        assertFalse("Null array", ByteUtils.hasLength(null));
        assertFalse("Empty array", ByteUtils.hasLength(new byte[0]));
        assertTrue("A byte array", ByteUtils.hasLength(new byte[]{1}));
    }

    @Test
    public void hasExactLength() {
        assertFalse("Null array of length 1", ByteUtils.hasExactLength(null, 1));
        assertTrue("Null array", ByteUtils.hasExactLength(null, 0));
        assertFalse("Empty array of length 1", ByteUtils.hasExactLength(new byte[0], 1));
        assertTrue("Empty array", ByteUtils.hasExactLength(new byte[0], 0));
        assertFalse("A byte array of length 2", ByteUtils.hasExactLength(new byte[]{1}, 2));
        assertTrue("A correct byte array", ByteUtils.hasExactLength(new byte[]{1}, 1));
    }

    @Test
    public void testToHex() {
        assertNull("Null uuid", ByteUtils.toHex(null));
        BigInteger x = new BigInteger("11");
        assertEquals("11 converts to 0b", "0b", ByteUtils.toHex(x.toByteArray()));
        x = new BigInteger("91341076760875801477766333660764240022");
        assertEquals("Number has been converted", "44b7a6c7f58a5dcdf1594ad7003af896", ByteUtils.toHex(x.toByteArray()));
        byte[] hex = StringUtils.encodeHex("44b7a6c7f58a5dcdf1594ad7003af896");
        assertEquals("An hex array is not recoded", "44b7a6c7f58a5dcdf1594ad7003af896", ByteUtils.toHex(hex));
    }

    @Test
    public void testIsUUID() {
        byte[] y = new byte[12];
        assertFalse("Not a raw(16)", ByteUtils.isUUID(y));
        byte[] z = new byte[] {66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 65};
        assertTrue("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0a", ByteUtils.isUUID(z));
    }

    @Test
    public void testHashCode() throws Exception {
        byte[] uuid = StringUtils.encodeHex("3b0ae7d419d26e36e040007f0101785c");
        byte[] uuid2 = StringUtils.encodeHex("987654321098765432109876543210aa");
        assertEquals("Same bye[] produces same hashcode", ByteUtils.hashCode(uuid), ByteUtils.hashCode(uuid));
        assertFalse("Two different UUIDs hashcodes are different", ByteUtils.hashCode(uuid) == ByteUtils.hashCode(uuid2));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testHashCodeNull() throws Exception {
        assertTrue("Null hascode", ByteUtils.hashCode(null) == 0);
        fail("Null hascode");
    }
}
TOP

Related Classes of org.internna.iwebmvc.utils.ByteUtilsTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.