/*
* 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 StringUtilsTest {
@Test
public void testIsUUID() {
assertFalse("Null is not an UUID", StringUtils.isUUID((String) null));
assertFalse("= is not an UUID", StringUtils.isUUID("=="));
assertTrue("This is a UUID", StringUtils.isUUID("460beaa419d26e36e040007f01092ed1"));
}
@Test
public void testEncodeHex() {
assertNull(StringUtils.encodeHex(null));
BigInteger x = new BigInteger("10");
String uuid = "3b0ae7d419d26e36e040007f0101785c";
assertEquals("RAW 16", 16, StringUtils.encodeHex(uuid).length);
assertEquals("RAW 16", uuid, ByteUtils.toHex(StringUtils.encodeHex(uuid)));
assertTrue("Number has been converted", 10 == StringUtils.encodeHex(ByteUtils.toHex(x.toByteArray()))[0]);
String enc = "QCcyTE4v9EgCGo+kPGeI6LZ5XK0+3JLa7dC90DwKabHd9gtBd+/pJg==";
byte[] encbytes = enc.getBytes();
assertEquals("Encrypted data is treated ok", enc, new String(StringUtils.encodeHex(enc)));
assertEquals("Encrypted data is treated ok", encbytes.length, StringUtils.encodeHex(ByteUtils.toHex(encbytes)).length);
assertEquals("Encrypted data is treated ok", encbytes[0], StringUtils.encodeHex(ByteUtils.toHex(encbytes))[0]);
}
@Test
public void getFilename() {
assertNull(StringUtils.getFilename(null));
assertEquals("windows paths are removed", "beach.jpg", StringUtils.getFilename("c:\\temp\\other\\beach.jpg"));
assertEquals("unix paths are removed", "beach.jpg", StringUtils.getFilename("/tmp/usr/beach.jpg"));
}
@Test
public void betweenDelimiters() {
assertTrue("Null string cannot be splitted", StringUtils.splitBetweenDelimiters(null, ",", ";").size() == 0);
assertTrue("split between different delimiters", StringUtils.splitBetweenDelimiters("hi, how are you? - said peter", ",", "-").size() == 3);
assertEquals("Correct first part", "hi" , StringUtils.splitBetweenDelimiters("hi, how are you? - said peter", ",", "-").get(0));
assertEquals("Correct first part", " how are you? " , StringUtils.splitBetweenDelimiters("hi, how are you? - said peter", ",", "-").get(1));
assertEquals("Correct first part", " said peter" , StringUtils.splitBetweenDelimiters("hi, how are you? - said peter", ",", "-").get(2));
assertTrue("split between same delimiters", StringUtils.splitBetweenDelimiters("hi, how are you, my friend", ",", ",").size() == 3);
}
@Test
public void toPaddedHexString() {
assertEquals("00", StringUtils.toPaddedHexString(0));
assertEquals("01", StringUtils.toPaddedHexString(1));
assertEquals("0a", StringUtils.toPaddedHexString(10));
assertEquals("10", StringUtils.toPaddedHexString(16));
}
}