StringUtils.parseLocaleString parses variant correctly when variant contains country code

This commit also includes a JUnit 4 style revision of StringUtilsTests and ObjectUtilsTests.

Issue: SPR-11806
This commit is contained in:
Juergen Hoeller 2014-05-19 22:29:19 +02:00
parent 1285467fe6
commit 295a6aeed6
3 changed files with 219 additions and 63 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -51,7 +51,6 @@ import java.util.TreeSet;
* @author Rick Evans * @author Rick Evans
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 16 April 2001 * @since 16 April 2001
* @see org.apache.commons.lang.StringUtils
*/ */
public abstract class StringUtils { public abstract class StringUtils {
@ -696,7 +695,7 @@ public abstract class StringUtils {
if (parts.length > 2) { if (parts.length > 2) {
// There is definitely a variant, and it is everything after the country // There is definitely a variant, and it is everything after the country
// code sans the separator between the country code and the variant. // code sans the separator between the country code and the variant.
int endIndexOfCountryCode = localeString.lastIndexOf(country) + country.length(); int endIndexOfCountryCode = localeString.indexOf(country, language.length()) + country.length();
// Strip off any leading '_' and whitespace, what's left is the variant. // Strip off any leading '_' and whitespace, what's left is the variant.
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode)); variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
if (variant.startsWith("_")) { if (variant.startsWith("_")) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,7 @@ package org.springframework.util;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.core.task.TaskRejectedException; import org.springframework.core.task.TaskRejectedException;
@ -31,8 +31,9 @@ import static org.junit.Assert.*;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
*/ */
public final class ObjectUtilsTests extends TestCase { public class ObjectUtilsTests {
@Test
public void testIsCheckedException() { public void testIsCheckedException() {
assertTrue(ObjectUtils.isCheckedException(new Exception())); assertTrue(ObjectUtils.isCheckedException(new Exception()));
assertTrue(ObjectUtils.isCheckedException(new SQLException())); assertTrue(ObjectUtils.isCheckedException(new SQLException()));
@ -45,6 +46,7 @@ public final class ObjectUtilsTests extends TestCase {
assertTrue(ObjectUtils.isCheckedException(new Throwable())); assertTrue(ObjectUtils.isCheckedException(new Throwable()));
} }
@Test
public void testIsCompatibleWithThrowsClause() { public void testIsCompatibleWithThrowsClause() {
Class<?>[] empty = new Class[0]; Class<?>[] empty = new Class[0];
Class<?>[] exception = new Class[] {Exception.class}; Class<?>[] exception = new Class[] {Exception.class};
@ -76,6 +78,7 @@ public final class ObjectUtilsTests extends TestCase {
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), throwable)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), throwable));
} }
@Test
public void testToObjectArray() { public void testToObjectArray() {
int[] a = new int[] {1, 2, 3, 4, 5}; int[] a = new int[] {1, 2, 3, 4, 5};
Integer[] wrapper = (Integer[]) ObjectUtils.toObjectArray(a); Integer[] wrapper = (Integer[]) ObjectUtils.toObjectArray(a);
@ -85,18 +88,21 @@ public final class ObjectUtilsTests extends TestCase {
} }
} }
@Test
public void testToObjectArrayWithNull() { public void testToObjectArrayWithNull() {
Object[] objects = ObjectUtils.toObjectArray(null); Object[] objects = ObjectUtils.toObjectArray(null);
assertNotNull(objects); assertNotNull(objects);
assertEquals(0, objects.length); assertEquals(0, objects.length);
} }
@Test
public void testToObjectArrayWithEmptyPrimitiveArray() { public void testToObjectArrayWithEmptyPrimitiveArray() {
Object[] objects = ObjectUtils.toObjectArray(new byte[] {}); Object[] objects = ObjectUtils.toObjectArray(new byte[] {});
assertNotNull(objects); assertNotNull(objects);
assertEquals(0, objects.length); assertEquals(0, objects.length);
} }
@Test
public void testToObjectArrayWithNonArrayType() { public void testToObjectArrayWithNonArrayType() {
try { try {
ObjectUtils.toObjectArray("Not an []"); ObjectUtils.toObjectArray("Not an []");
@ -106,11 +112,13 @@ public final class ObjectUtilsTests extends TestCase {
} }
} }
@Test
public void testToObjectArrayWithNonPrimitiveArray() { public void testToObjectArrayWithNonPrimitiveArray() {
String[] source = new String[] {"Bingo"}; String[] source = new String[] {"Bingo"};
assertEquals(source, ObjectUtils.toObjectArray(source)); assertArrayEquals(source, ObjectUtils.toObjectArray(source));
} }
@Test
public void testAddObjectToArraySunnyDay() { public void testAddObjectToArraySunnyDay() {
String[] array = new String[] {"foo", "bar"}; String[] array = new String[] {"foo", "bar"};
String newElement = "baz"; String newElement = "baz";
@ -119,6 +127,7 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(newElement, newArray[2]); assertEquals(newElement, newArray[2]);
} }
@Test
public void testAddObjectToArrayWhenEmpty() { public void testAddObjectToArrayWhenEmpty() {
String[] array = new String[0]; String[] array = new String[0];
String newElement = "foo"; String newElement = "foo";
@ -127,6 +136,7 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(newElement, newArray[0]); assertEquals(newElement, newArray[0]);
} }
@Test
public void testAddObjectToSingleNonNullElementArray() { public void testAddObjectToSingleNonNullElementArray() {
String existingElement = "foo"; String existingElement = "foo";
String[] array = new String[] {existingElement}; String[] array = new String[] {existingElement};
@ -137,6 +147,7 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(newElement, newArray[1]); assertEquals(newElement, newArray[1]);
} }
@Test
public void testAddObjectToSingleNullElementArray() { public void testAddObjectToSingleNullElementArray() {
String[] array = new String[] {null}; String[] array = new String[] {null};
String newElement = "bar"; String newElement = "bar";
@ -146,6 +157,7 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(newElement, newArray[1]); assertEquals(newElement, newArray[1]);
} }
@Test
public void testAddObjectToNullArray() throws Exception { public void testAddObjectToNullArray() throws Exception {
String newElement = "foo"; String newElement = "foo";
String[] newArray = ObjectUtils.addObjectToArray(null, newElement); String[] newArray = ObjectUtils.addObjectToArray(null, newElement);
@ -153,45 +165,53 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(newElement, newArray[0]); assertEquals(newElement, newArray[0]);
} }
@Test
public void testAddNullObjectToNullArray() throws Exception { public void testAddNullObjectToNullArray() throws Exception {
Object[] newArray = ObjectUtils.addObjectToArray(null, null); Object[] newArray = ObjectUtils.addObjectToArray(null, null);
assertEquals(1, newArray.length); assertEquals(1, newArray.length);
assertEquals(null, newArray[0]); assertEquals(null, newArray[0]);
} }
@Test
public void testNullSafeEqualsWithArrays() throws Exception { public void testNullSafeEqualsWithArrays() throws Exception {
assertTrue(ObjectUtils.nullSafeEquals(new String[] {"a", "b", "c"}, new String[] {"a", "b", "c"})); assertTrue(ObjectUtils.nullSafeEquals(new String[] {"a", "b", "c"}, new String[] {"a", "b", "c"}));
assertTrue(ObjectUtils.nullSafeEquals(new int[] {1, 2, 3}, new int[] {1, 2, 3})); assertTrue(ObjectUtils.nullSafeEquals(new int[] {1, 2, 3}, new int[] {1, 2, 3}));
} }
@Test
public void testHashCodeWithBooleanFalse() { public void testHashCodeWithBooleanFalse() {
int expected = Boolean.FALSE.hashCode(); int expected = Boolean.FALSE.hashCode();
assertEquals(expected, ObjectUtils.hashCode(false)); assertEquals(expected, ObjectUtils.hashCode(false));
} }
@Test
public void testHashCodeWithBooleanTrue() { public void testHashCodeWithBooleanTrue() {
int expected = Boolean.TRUE.hashCode(); int expected = Boolean.TRUE.hashCode();
assertEquals(expected, ObjectUtils.hashCode(true)); assertEquals(expected, ObjectUtils.hashCode(true));
} }
@Test
public void testHashCodeWithDouble() { public void testHashCodeWithDouble() {
double dbl = 9830.43; double dbl = 9830.43;
int expected = (new Double(dbl)).hashCode(); int expected = (new Double(dbl)).hashCode();
assertEquals(expected, ObjectUtils.hashCode(dbl)); assertEquals(expected, ObjectUtils.hashCode(dbl));
} }
@Test
public void testHashCodeWithFloat() { public void testHashCodeWithFloat() {
float flt = 34.8f; float flt = 34.8f;
int expected = (new Float(flt)).hashCode(); int expected = (new Float(flt)).hashCode();
assertEquals(expected, ObjectUtils.hashCode(flt)); assertEquals(expected, ObjectUtils.hashCode(flt));
} }
@Test
public void testHashCodeWithLong() { public void testHashCodeWithLong() {
long lng = 883l; long lng = 883l;
int expected = (new Long(lng)).hashCode(); int expected = (new Long(lng)).hashCode();
assertEquals(expected, ObjectUtils.hashCode(lng)); assertEquals(expected, ObjectUtils.hashCode(lng));
} }
@Test
public void testIdentityToString() { public void testIdentityToString() {
Object obj = new Object(); Object obj = new Object();
String expected = obj.getClass().getName() + "@" + ObjectUtils.getIdentityHexString(obj); String expected = obj.getClass().getName() + "@" + ObjectUtils.getIdentityHexString(obj);
@ -199,90 +219,112 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testIdentityToStringWithNullObject() { public void testIdentityToStringWithNullObject() {
assertEquals("", ObjectUtils.identityToString(null)); assertEquals("", ObjectUtils.identityToString(null));
} }
@Test
public void testIsArrayOfPrimitivesWithBooleanArray() { public void testIsArrayOfPrimitivesWithBooleanArray() {
assertTrue(ClassUtils.isPrimitiveArray(boolean[].class)); assertTrue(ClassUtils.isPrimitiveArray(boolean[].class));
} }
@Test
public void testIsArrayOfPrimitivesWithObjectArray() { public void testIsArrayOfPrimitivesWithObjectArray() {
assertFalse(ClassUtils.isPrimitiveArray(Object[].class)); assertFalse(ClassUtils.isPrimitiveArray(Object[].class));
} }
@Test
public void testIsArrayOfPrimitivesWithNonArray() { public void testIsArrayOfPrimitivesWithNonArray() {
assertFalse(ClassUtils.isPrimitiveArray(String.class)); assertFalse(ClassUtils.isPrimitiveArray(String.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithBooleanPrimitiveClass() { public void testIsPrimitiveOrWrapperWithBooleanPrimitiveClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(boolean.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(boolean.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithBooleanWrapperClass() { public void testIsPrimitiveOrWrapperWithBooleanWrapperClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Boolean.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Boolean.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithBytePrimitiveClass() { public void testIsPrimitiveOrWrapperWithBytePrimitiveClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(byte.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(byte.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithByteWrapperClass() { public void testIsPrimitiveOrWrapperWithByteWrapperClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Byte.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Byte.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithCharacterClass() { public void testIsPrimitiveOrWrapperWithCharacterClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Character.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Character.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithCharClass() { public void testIsPrimitiveOrWrapperWithCharClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(char.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(char.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithDoublePrimitiveClass() { public void testIsPrimitiveOrWrapperWithDoublePrimitiveClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(double.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(double.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithDoubleWrapperClass() { public void testIsPrimitiveOrWrapperWithDoubleWrapperClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Double.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Double.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithFloatPrimitiveClass() { public void testIsPrimitiveOrWrapperWithFloatPrimitiveClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(float.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(float.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithFloatWrapperClass() { public void testIsPrimitiveOrWrapperWithFloatWrapperClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Float.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Float.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithIntClass() { public void testIsPrimitiveOrWrapperWithIntClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(int.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(int.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithIntegerClass() { public void testIsPrimitiveOrWrapperWithIntegerClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Integer.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Integer.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithLongPrimitiveClass() { public void testIsPrimitiveOrWrapperWithLongPrimitiveClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(long.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(long.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithLongWrapperClass() { public void testIsPrimitiveOrWrapperWithLongWrapperClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Long.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Long.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithNonPrimitiveOrWrapperClass() { public void testIsPrimitiveOrWrapperWithNonPrimitiveOrWrapperClass() {
assertFalse(ClassUtils.isPrimitiveOrWrapper(Object.class)); assertFalse(ClassUtils.isPrimitiveOrWrapper(Object.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithShortPrimitiveClass() { public void testIsPrimitiveOrWrapperWithShortPrimitiveClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(short.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(short.class));
} }
@Test
public void testIsPrimitiveOrWrapperWithShortWrapperClass() { public void testIsPrimitiveOrWrapperWithShortWrapperClass() {
assertTrue(ClassUtils.isPrimitiveOrWrapper(Short.class)); assertTrue(ClassUtils.isPrimitiveOrWrapper(Short.class));
} }
@Test
public void testNullSafeHashCodeWithBooleanArray() { public void testNullSafeHashCodeWithBooleanArray() {
int expected = 31 * 7 + Boolean.TRUE.hashCode(); int expected = 31 * 7 + Boolean.TRUE.hashCode();
expected = 31 * expected + Boolean.FALSE.hashCode(); expected = 31 * expected + Boolean.FALSE.hashCode();
@ -293,10 +335,12 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithBooleanArrayEqualToNull() { public void testNullSafeHashCodeWithBooleanArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((boolean[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((boolean[]) null));
} }
@Test
public void testNullSafeHashCodeWithByteArray() { public void testNullSafeHashCodeWithByteArray() {
int expected = 31 * 7 + 8; int expected = 31 * 7 + 8;
expected = 31 * expected + 10; expected = 31 * expected + 10;
@ -307,10 +351,12 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithByteArrayEqualToNull() { public void testNullSafeHashCodeWithByteArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((byte[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((byte[]) null));
} }
@Test
public void testNullSafeHashCodeWithCharArray() { public void testNullSafeHashCodeWithCharArray() {
int expected = 31 * 7 + 'a'; int expected = 31 * 7 + 'a';
expected = 31 * expected + 'E'; expected = 31 * expected + 'E';
@ -321,10 +367,12 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithCharArrayEqualToNull() { public void testNullSafeHashCodeWithCharArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((char[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((char[]) null));
} }
@Test
public void testNullSafeHashCodeWithDoubleArray() { public void testNullSafeHashCodeWithDoubleArray() {
long bits = Double.doubleToLongBits(8449.65); long bits = Double.doubleToLongBits(8449.65);
int expected = 31 * 7 + (int) (bits ^ (bits >>> 32)); int expected = 31 * 7 + (int) (bits ^ (bits >>> 32));
@ -337,10 +385,12 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithDoubleArrayEqualToNull() { public void testNullSafeHashCodeWithDoubleArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((double[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((double[]) null));
} }
@Test
public void testNullSafeHashCodeWithFloatArray() { public void testNullSafeHashCodeWithFloatArray() {
int expected = 31 * 7 + Float.floatToIntBits(9.6f); int expected = 31 * 7 + Float.floatToIntBits(9.6f);
expected = 31 * expected + Float.floatToIntBits(7.4f); expected = 31 * expected + Float.floatToIntBits(7.4f);
@ -351,10 +401,12 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithFloatArrayEqualToNull() { public void testNullSafeHashCodeWithFloatArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((float[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((float[]) null));
} }
@Test
public void testNullSafeHashCodeWithIntArray() { public void testNullSafeHashCodeWithIntArray() {
int expected = 31 * 7 + 884; int expected = 31 * 7 + 884;
expected = 31 * expected + 340; expected = 31 * expected + 340;
@ -365,10 +417,12 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithIntArrayEqualToNull() { public void testNullSafeHashCodeWithIntArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((int[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((int[]) null));
} }
@Test
public void testNullSafeHashCodeWithLongArray() { public void testNullSafeHashCodeWithLongArray() {
long lng = 7993l; long lng = 7993l;
int expected = 31 * 7 + (int) (lng ^ (lng >>> 32)); int expected = 31 * 7 + (int) (lng ^ (lng >>> 32));
@ -381,15 +435,18 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithLongArrayEqualToNull() { public void testNullSafeHashCodeWithLongArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((long[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((long[]) null));
} }
@Test
public void testNullSafeHashCodeWithObject() { public void testNullSafeHashCodeWithObject() {
String str = "Luke"; String str = "Luke";
assertEquals(str.hashCode(), ObjectUtils.nullSafeHashCode(str)); assertEquals(str.hashCode(), ObjectUtils.nullSafeHashCode(str));
} }
@Test
public void testNullSafeHashCodeWithObjectArray() { public void testNullSafeHashCodeWithObjectArray() {
int expected = 31 * 7 + "Leia".hashCode(); int expected = 31 * 7 + "Leia".hashCode();
expected = 31 * expected + "Han".hashCode(); expected = 31 * expected + "Han".hashCode();
@ -400,68 +457,80 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithObjectArrayEqualToNull() { public void testNullSafeHashCodeWithObjectArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((Object[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((Object[]) null));
} }
@Test
public void testNullSafeHashCodeWithObjectBeingBooleanArray() { public void testNullSafeHashCodeWithObjectBeingBooleanArray() {
Object array = new boolean[] {true, false}; Object array = new boolean[] {true, false};
int expected = ObjectUtils.nullSafeHashCode((boolean[]) array); int expected = ObjectUtils.nullSafeHashCode((boolean[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingByteArray() { public void testNullSafeHashCodeWithObjectBeingByteArray() {
Object array = new byte[] {6, 39}; Object array = new byte[] {6, 39};
int expected = ObjectUtils.nullSafeHashCode((byte[]) array); int expected = ObjectUtils.nullSafeHashCode((byte[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingCharArray() { public void testNullSafeHashCodeWithObjectBeingCharArray() {
Object array = new char[] {'l', 'M'}; Object array = new char[] {'l', 'M'};
int expected = ObjectUtils.nullSafeHashCode((char[]) array); int expected = ObjectUtils.nullSafeHashCode((char[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingDoubleArray() { public void testNullSafeHashCodeWithObjectBeingDoubleArray() {
Object array = new double[] {68930.993, 9022.009}; Object array = new double[] {68930.993, 9022.009};
int expected = ObjectUtils.nullSafeHashCode((double[]) array); int expected = ObjectUtils.nullSafeHashCode((double[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingFloatArray() { public void testNullSafeHashCodeWithObjectBeingFloatArray() {
Object array = new float[] {9.9f, 9.54f}; Object array = new float[] {9.9f, 9.54f};
int expected = ObjectUtils.nullSafeHashCode((float[]) array); int expected = ObjectUtils.nullSafeHashCode((float[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingIntArray() { public void testNullSafeHashCodeWithObjectBeingIntArray() {
Object array = new int[] {89, 32}; Object array = new int[] {89, 32};
int expected = ObjectUtils.nullSafeHashCode((int[]) array); int expected = ObjectUtils.nullSafeHashCode((int[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingLongArray() { public void testNullSafeHashCodeWithObjectBeingLongArray() {
Object array = new long[] {4389, 320}; Object array = new long[] {4389, 320};
int expected = ObjectUtils.nullSafeHashCode((long[]) array); int expected = ObjectUtils.nullSafeHashCode((long[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingObjectArray() { public void testNullSafeHashCodeWithObjectBeingObjectArray() {
Object array = new Object[] {"Luke", "Anakin"}; Object array = new Object[] {"Luke", "Anakin"};
int expected = ObjectUtils.nullSafeHashCode((Object[]) array); int expected = ObjectUtils.nullSafeHashCode((Object[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectBeingShortArray() { public void testNullSafeHashCodeWithObjectBeingShortArray() {
Object array = new short[] {5, 3}; Object array = new short[] {5, 3};
int expected = ObjectUtils.nullSafeHashCode((short[]) array); int expected = ObjectUtils.nullSafeHashCode((short[]) array);
assertEqualHashCodes(expected, array); assertEqualHashCodes(expected, array);
} }
@Test
public void testNullSafeHashCodeWithObjectEqualToNull() { public void testNullSafeHashCodeWithObjectEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((Object) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((Object) null));
} }
@Test
public void testNullSafeHashCodeWithShortArray() { public void testNullSafeHashCodeWithShortArray() {
int expected = 31 * 7 + 70; int expected = 31 * 7 + 70;
expected = 31 * expected + 8; expected = 31 * expected + 8;
@ -472,156 +541,187 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
public void testNullSafeHashCodeWithShortArrayEqualToNull() { public void testNullSafeHashCodeWithShortArrayEqualToNull() {
assertEquals(0, ObjectUtils.nullSafeHashCode((short[]) null)); assertEquals(0, ObjectUtils.nullSafeHashCode((short[]) null));
} }
@Test
public void testNullSafeToStringWithBooleanArray() { public void testNullSafeToStringWithBooleanArray() {
boolean[] array = {true, false}; boolean[] array = {true, false};
assertEquals("{true, false}", ObjectUtils.nullSafeToString(array)); assertEquals("{true, false}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithBooleanArrayBeingEmpty() { public void testNullSafeToStringWithBooleanArrayBeingEmpty() {
boolean[] array = {}; boolean[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithBooleanArrayEqualToNull() { public void testNullSafeToStringWithBooleanArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((boolean[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((boolean[]) null));
} }
@Test
public void testNullSafeToStringWithByteArray() { public void testNullSafeToStringWithByteArray() {
byte[] array = {5, 8}; byte[] array = {5, 8};
assertEquals("{5, 8}", ObjectUtils.nullSafeToString(array)); assertEquals("{5, 8}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithByteArrayBeingEmpty() { public void testNullSafeToStringWithByteArrayBeingEmpty() {
byte[] array = {}; byte[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithByteArrayEqualToNull() { public void testNullSafeToStringWithByteArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((byte[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((byte[]) null));
} }
@Test
public void testNullSafeToStringWithCharArray() { public void testNullSafeToStringWithCharArray() {
char[] array = {'A', 'B'}; char[] array = {'A', 'B'};
assertEquals("{'A', 'B'}", ObjectUtils.nullSafeToString(array)); assertEquals("{'A', 'B'}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithCharArrayBeingEmpty() { public void testNullSafeToStringWithCharArrayBeingEmpty() {
char[] array = {}; char[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithCharArrayEqualToNull() { public void testNullSafeToStringWithCharArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((char[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((char[]) null));
} }
@Test
public void testNullSafeToStringWithDoubleArray() { public void testNullSafeToStringWithDoubleArray() {
double[] array = {8594.93, 8594023.95}; double[] array = {8594.93, 8594023.95};
assertEquals("{8594.93, 8594023.95}", ObjectUtils.nullSafeToString(array)); assertEquals("{8594.93, 8594023.95}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithDoubleArrayBeingEmpty() { public void testNullSafeToStringWithDoubleArrayBeingEmpty() {
double[] array = {}; double[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithDoubleArrayEqualToNull() { public void testNullSafeToStringWithDoubleArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((double[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((double[]) null));
} }
@Test
public void testNullSafeToStringWithFloatArray() { public void testNullSafeToStringWithFloatArray() {
float[] array = {8.6f, 43.8f}; float[] array = {8.6f, 43.8f};
assertEquals("{8.6, 43.8}", ObjectUtils.nullSafeToString(array)); assertEquals("{8.6, 43.8}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithFloatArrayBeingEmpty() { public void testNullSafeToStringWithFloatArrayBeingEmpty() {
float[] array = {}; float[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithFloatArrayEqualToNull() { public void testNullSafeToStringWithFloatArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((float[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((float[]) null));
} }
@Test
public void testNullSafeToStringWithIntArray() { public void testNullSafeToStringWithIntArray() {
int[] array = {9, 64}; int[] array = {9, 64};
assertEquals("{9, 64}", ObjectUtils.nullSafeToString(array)); assertEquals("{9, 64}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithIntArrayBeingEmpty() { public void testNullSafeToStringWithIntArrayBeingEmpty() {
int[] array = {}; int[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithIntArrayEqualToNull() { public void testNullSafeToStringWithIntArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((int[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((int[]) null));
} }
@Test
public void testNullSafeToStringWithLongArray() { public void testNullSafeToStringWithLongArray() {
long[] array = {434l, 23423l}; long[] array = {434l, 23423l};
assertEquals("{434, 23423}", ObjectUtils.nullSafeToString(array)); assertEquals("{434, 23423}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithLongArrayBeingEmpty() { public void testNullSafeToStringWithLongArrayBeingEmpty() {
long[] array = {}; long[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithLongArrayEqualToNull() { public void testNullSafeToStringWithLongArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((long[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((long[]) null));
} }
@Test
public void testNullSafeToStringWithPlainOldString() { public void testNullSafeToStringWithPlainOldString() {
assertEquals("I shoh love tha taste of mangoes", ObjectUtils.nullSafeToString("I shoh love tha taste of mangoes")); assertEquals("I shoh love tha taste of mangoes", ObjectUtils.nullSafeToString("I shoh love tha taste of mangoes"));
} }
@Test
public void testNullSafeToStringWithObjectArray() { public void testNullSafeToStringWithObjectArray() {
Object[] array = {"Han", new Long(43)}; Object[] array = {"Han", new Long(43)};
assertEquals("{Han, 43}", ObjectUtils.nullSafeToString(array)); assertEquals("{Han, 43}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithObjectArrayBeingEmpty() { public void testNullSafeToStringWithObjectArrayBeingEmpty() {
Object[] array = {}; Object[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithObjectArrayEqualToNull() { public void testNullSafeToStringWithObjectArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((Object[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((Object[]) null));
} }
@Test
public void testNullSafeToStringWithShortArray() { public void testNullSafeToStringWithShortArray() {
short[] array = {7, 9}; short[] array = {7, 9};
assertEquals("{7, 9}", ObjectUtils.nullSafeToString(array)); assertEquals("{7, 9}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithShortArrayBeingEmpty() { public void testNullSafeToStringWithShortArrayBeingEmpty() {
short[] array = {}; short[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithShortArrayEqualToNull() { public void testNullSafeToStringWithShortArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((short[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((short[]) null));
} }
@Test
public void testNullSafeToStringWithStringArray() { public void testNullSafeToStringWithStringArray() {
String[] array = {"Luke", "Anakin"}; String[] array = {"Luke", "Anakin"};
assertEquals("{Luke, Anakin}", ObjectUtils.nullSafeToString(array)); assertEquals("{Luke, Anakin}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithStringArrayBeingEmpty() { public void testNullSafeToStringWithStringArrayBeingEmpty() {
String[] array = {}; String[] array = {};
assertEquals("{}", ObjectUtils.nullSafeToString(array)); assertEquals("{}", ObjectUtils.nullSafeToString(array));
} }
@Test
public void testNullSafeToStringWithStringArrayEqualToNull() { public void testNullSafeToStringWithStringArrayEqualToNull() {
assertEquals("null", ObjectUtils.nullSafeToString((String[]) null)); assertEquals("null", ObjectUtils.nullSafeToString((String[]) null));
} }
enum Tropes { FOO, BAR, baz } @Test
public void testContainsConstant() { public void testContainsConstant() {
assertThat(ObjectUtils.containsConstant(Tropes.values(), "FOO"), is(true)); assertThat(ObjectUtils.containsConstant(Tropes.values(), "FOO"), is(true));
assertThat(ObjectUtils.containsConstant(Tropes.values(), "foo"), is(true)); assertThat(ObjectUtils.containsConstant(Tropes.values(), "foo"), is(true));
@ -636,13 +736,15 @@ public final class ObjectUtilsTests extends TestCase {
assertThat(ObjectUtils.containsConstant(Tropes.values(), "foo", true), is(false)); assertThat(ObjectUtils.containsConstant(Tropes.values(), "foo", true), is(false));
} }
@Test
public void testCaseInsensitiveValueOf() { public void testCaseInsensitiveValueOf() {
assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "foo"), is(Tropes.FOO)); assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "foo"), is(Tropes.FOO));
assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "BAR"), is(Tropes.BAR)); assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "BAR"), is(Tropes.BAR));
try { try {
ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "bogus"); ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "bogus");
fail("expected IllegalArgumentException"); fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ex) { }
catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), assertThat(ex.getMessage(),
is("constant [bogus] does not exist in enum type " + is("constant [bogus] does not exist in enum type " +
"org.springframework.util.ObjectUtilsTests$Tropes")); "org.springframework.util.ObjectUtilsTests$Tropes"));
@ -655,4 +757,7 @@ public final class ObjectUtilsTests extends TestCase {
assertTrue(array.hashCode() != actual); assertTrue(array.hashCode() != actual);
} }
enum Tropes { FOO, BAR, baz }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,29 +20,35 @@ import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import junit.framework.TestCase; import org.junit.Test;
import static org.junit.Assert.*;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
*/ */
public class StringUtilsTests extends TestCase { public class StringUtilsTests {
@Test
public void testHasTextBlank() throws Exception { public void testHasTextBlank() throws Exception {
String blank = " "; String blank = " ";
assertEquals(false, StringUtils.hasText(blank)); assertEquals(false, StringUtils.hasText(blank));
} }
@Test
public void testHasTextNullEmpty() throws Exception { public void testHasTextNullEmpty() throws Exception {
assertEquals(false, StringUtils.hasText(null)); assertEquals(false, StringUtils.hasText(null));
assertEquals(false, StringUtils.hasText("")); assertEquals(false, StringUtils.hasText(""));
} }
@Test
public void testHasTextValid() throws Exception { public void testHasTextValid() throws Exception {
assertEquals(true, StringUtils.hasText("t")); assertEquals(true, StringUtils.hasText("t"));
} }
@Test
public void testContainsWhitespace() throws Exception { public void testContainsWhitespace() throws Exception {
assertFalse(StringUtils.containsWhitespace(null)); assertFalse(StringUtils.containsWhitespace(null));
assertFalse(StringUtils.containsWhitespace("")); assertFalse(StringUtils.containsWhitespace(""));
@ -55,6 +61,7 @@ public class StringUtilsTests extends TestCase {
assertTrue(StringUtils.containsWhitespace("a b")); assertTrue(StringUtils.containsWhitespace("a b"));
} }
@Test
public void testTrimWhitespace() throws Exception { public void testTrimWhitespace() throws Exception {
assertEquals(null, StringUtils.trimWhitespace(null)); assertEquals(null, StringUtils.trimWhitespace(null));
assertEquals("", StringUtils.trimWhitespace("")); assertEquals("", StringUtils.trimWhitespace(""));
@ -67,6 +74,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("a b c", StringUtils.trimWhitespace(" a b c ")); assertEquals("a b c", StringUtils.trimWhitespace(" a b c "));
} }
@Test
public void testTrimAllWhitespace() throws Exception { public void testTrimAllWhitespace() throws Exception {
assertEquals("", StringUtils.trimAllWhitespace("")); assertEquals("", StringUtils.trimAllWhitespace(""));
assertEquals("", StringUtils.trimAllWhitespace(" ")); assertEquals("", StringUtils.trimAllWhitespace(" "));
@ -78,6 +86,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("abc", StringUtils.trimAllWhitespace(" a b c ")); assertEquals("abc", StringUtils.trimAllWhitespace(" a b c "));
} }
@Test
public void testTrimLeadingWhitespace() throws Exception { public void testTrimLeadingWhitespace() throws Exception {
assertEquals(null, StringUtils.trimLeadingWhitespace(null)); assertEquals(null, StringUtils.trimLeadingWhitespace(null));
assertEquals("", StringUtils.trimLeadingWhitespace("")); assertEquals("", StringUtils.trimLeadingWhitespace(""));
@ -90,6 +99,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("a b c ", StringUtils.trimLeadingWhitespace(" a b c ")); assertEquals("a b c ", StringUtils.trimLeadingWhitespace(" a b c "));
} }
@Test
public void testTrimTrailingWhitespace() throws Exception { public void testTrimTrailingWhitespace() throws Exception {
assertEquals(null, StringUtils.trimTrailingWhitespace(null)); assertEquals(null, StringUtils.trimTrailingWhitespace(null));
assertEquals("", StringUtils.trimTrailingWhitespace("")); assertEquals("", StringUtils.trimTrailingWhitespace(""));
@ -102,6 +112,7 @@ public class StringUtilsTests extends TestCase {
assertEquals(" a b c", StringUtils.trimTrailingWhitespace(" a b c ")); assertEquals(" a b c", StringUtils.trimTrailingWhitespace(" a b c "));
} }
@Test
public void testTrimLeadingCharacter() throws Exception { public void testTrimLeadingCharacter() throws Exception {
assertEquals(null, StringUtils.trimLeadingCharacter(null, ' ')); assertEquals(null, StringUtils.trimLeadingCharacter(null, ' '));
assertEquals("", StringUtils.trimLeadingCharacter("", ' ')); assertEquals("", StringUtils.trimLeadingCharacter("", ' '));
@ -114,6 +125,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("a b c ", StringUtils.trimLeadingCharacter(" a b c ", ' ')); assertEquals("a b c ", StringUtils.trimLeadingCharacter(" a b c ", ' '));
} }
@Test
public void testTrimTrailingCharacter() throws Exception { public void testTrimTrailingCharacter() throws Exception {
assertEquals(null, StringUtils.trimTrailingCharacter(null, ' ')); assertEquals(null, StringUtils.trimTrailingCharacter(null, ' '));
assertEquals("", StringUtils.trimTrailingCharacter("", ' ')); assertEquals("", StringUtils.trimTrailingCharacter("", ' '));
@ -126,6 +138,7 @@ public class StringUtilsTests extends TestCase {
assertEquals(" a b c", StringUtils.trimTrailingCharacter(" a b c ", ' ')); assertEquals(" a b c", StringUtils.trimTrailingCharacter(" a b c ", ' '));
} }
@Test
public void testCountOccurrencesOf() { public void testCountOccurrencesOf() {
assertTrue("nullx2 = 0", assertTrue("nullx2 = 0",
StringUtils.countOccurrencesOf(null, null) == 0); StringUtils.countOccurrencesOf(null, null) == 0);
@ -152,6 +165,7 @@ public class StringUtilsTests extends TestCase {
assertTrue("test last", StringUtils.countOccurrencesOf(s, "r") == 2); assertTrue("test last", StringUtils.countOccurrencesOf(s, "r") == 2);
} }
@Test
public void testReplace() throws Exception { public void testReplace() throws Exception {
String inString = "a6AazAaa77abaa"; String inString = "a6AazAaa77abaa";
String oldPattern = "aa"; String oldPattern = "aa";
@ -174,6 +188,7 @@ public class StringUtilsTests extends TestCase {
assertTrue("Replace non matched is equal", s.equals(inString)); assertTrue("Replace non matched is equal", s.equals(inString));
} }
@Test
public void testDelete() throws Exception { public void testDelete() throws Exception {
String inString = "The quick brown fox jumped over the lazy dog"; String inString = "The quick brown fox jumped over the lazy dog";
@ -200,6 +215,7 @@ public class StringUtilsTests extends TestCase {
assertTrue("Result is unchanged", nochange.equals(inString)); assertTrue("Result is unchanged", nochange.equals(inString));
} }
@Test
public void testDeleteAny() throws Exception { public void testDeleteAny() throws Exception {
String inString = "Able was I ere I saw Elba"; String inString = "Able was I ere I saw Elba";
@ -213,45 +229,51 @@ public class StringUtilsTests extends TestCase {
assertTrue("Result is unchanged", mismatch.equals(inString)); assertTrue("Result is unchanged", mismatch.equals(inString));
String whitespace = "This is\n\n\n \t a messagy string with whitespace\n"; String whitespace = "This is\n\n\n \t a messagy string with whitespace\n";
assertTrue("Has CR", whitespace.indexOf("\n") != -1); assertTrue("Has CR", whitespace.contains("\n"));
assertTrue("Has tab", whitespace.indexOf("\t") != -1); assertTrue("Has tab", whitespace.contains("\t"));
assertTrue("Has sp", whitespace.indexOf(" ") != -1); assertTrue("Has sp", whitespace.contains(" "));
String cleaned = StringUtils.deleteAny(whitespace, "\n\t "); String cleaned = StringUtils.deleteAny(whitespace, "\n\t ");
assertTrue("Has no CR", cleaned.indexOf("\n") == -1); assertTrue("Has no CR", !cleaned.contains("\n"));
assertTrue("Has no tab", cleaned.indexOf("\t") == -1); assertTrue("Has no tab", !cleaned.contains("\t"));
assertTrue("Has no sp", cleaned.indexOf(" ") == -1); assertTrue("Has no sp", !cleaned.contains(" "));
assertTrue("Still has chars", cleaned.length() > 10); assertTrue("Still has chars", cleaned.length() > 10);
} }
@Test
public void testQuote() { public void testQuote() {
assertEquals("'myString'", StringUtils.quote("myString")); assertEquals("'myString'", StringUtils.quote("myString"));
assertEquals("''", StringUtils.quote("")); assertEquals("''", StringUtils.quote(""));
assertNull(StringUtils.quote(null)); assertNull(StringUtils.quote(null));
} }
@Test
public void testQuoteIfString() { public void testQuoteIfString() {
assertEquals("'myString'", StringUtils.quoteIfString("myString")); assertEquals("'myString'", StringUtils.quoteIfString("myString"));
assertEquals("''", StringUtils.quoteIfString("")); assertEquals("''", StringUtils.quoteIfString(""));
assertEquals(new Integer(5), StringUtils.quoteIfString(new Integer(5))); assertEquals(new Integer(5), StringUtils.quoteIfString(5));
assertNull(StringUtils.quoteIfString(null)); assertNull(StringUtils.quoteIfString(null));
} }
@Test
public void testUnqualify() { public void testUnqualify() {
String qualified = "i.am.not.unqualified"; String qualified = "i.am.not.unqualified";
assertEquals("unqualified", StringUtils.unqualify(qualified)); assertEquals("unqualified", StringUtils.unqualify(qualified));
} }
@Test
public void testCapitalize() { public void testCapitalize() {
String capitalized = "i am not capitalized"; String capitalized = "i am not capitalized";
assertEquals("I am not capitalized", StringUtils.capitalize(capitalized)); assertEquals("I am not capitalized", StringUtils.capitalize(capitalized));
} }
@Test
public void testUncapitalize() { public void testUncapitalize() {
String capitalized = "I am capitalized"; String capitalized = "I am capitalized";
assertEquals("i am capitalized", StringUtils.uncapitalize(capitalized)); assertEquals("i am capitalized", StringUtils.uncapitalize(capitalized));
} }
@Test
public void testGetFilename() { public void testGetFilename() {
assertEquals(null, StringUtils.getFilename(null)); assertEquals(null, StringUtils.getFilename(null));
assertEquals("", StringUtils.getFilename("")); assertEquals("", StringUtils.getFilename(""));
@ -263,6 +285,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("myfile.txt", StringUtils.getFilename("mypath/myfile.txt")); assertEquals("myfile.txt", StringUtils.getFilename("mypath/myfile.txt"));
} }
@Test
public void testGetFilenameExtension() { public void testGetFilenameExtension() {
assertEquals(null, StringUtils.getFilenameExtension(null)); assertEquals(null, StringUtils.getFilenameExtension(null));
assertEquals(null, StringUtils.getFilenameExtension("")); assertEquals(null, StringUtils.getFilenameExtension(""));
@ -276,6 +299,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("txt", StringUtils.getFilenameExtension("/home/user/.m2/settings/myfile.txt")); assertEquals("txt", StringUtils.getFilenameExtension("/home/user/.m2/settings/myfile.txt"));
} }
@Test
public void testStripFilenameExtension() { public void testStripFilenameExtension() {
assertEquals(null, StringUtils.stripFilenameExtension(null)); assertEquals(null, StringUtils.stripFilenameExtension(null));
assertEquals("", StringUtils.stripFilenameExtension("")); assertEquals("", StringUtils.stripFilenameExtension(""));
@ -290,6 +314,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("/home/user/.m2/settings/myfile", StringUtils.stripFilenameExtension("/home/user/.m2/settings/myfile.txt")); assertEquals("/home/user/.m2/settings/myfile", StringUtils.stripFilenameExtension("/home/user/.m2/settings/myfile.txt"));
} }
@Test
public void testCleanPath() { public void testCleanPath() {
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile")); assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile"));
assertEquals("mypath/myfile", StringUtils.cleanPath("mypath\\myfile")); assertEquals("mypath/myfile", StringUtils.cleanPath("mypath\\myfile"));
@ -303,6 +328,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("file:///c:/path/to/the%20file.txt", StringUtils.cleanPath("file:///c:/some/../path/to/the%20file.txt")); assertEquals("file:///c:/path/to/the%20file.txt", StringUtils.cleanPath("file:///c:/some/../path/to/the%20file.txt"));
} }
@Test
public void testPathEquals() { public void testPathEquals() {
assertTrue("Must be true for the same strings", assertTrue("Must be true for the same strings",
StringUtils.pathEquals("/dummy1/dummy2/dummy3", StringUtils.pathEquals("/dummy1/dummy2/dummy3",
@ -348,6 +374,7 @@ public class StringUtilsTests extends TestCase {
"/dummy1/dummy2/dummy4")); "/dummy1/dummy2/dummy4"));
} }
@Test
public void testConcatenateStringArrays() { public void testConcatenateStringArrays() {
String[] input1 = new String[] {"myString2"}; String[] input1 = new String[] {"myString2"};
String[] input2 = new String[] {"myString1", "myString2"}; String[] input2 = new String[] {"myString1", "myString2"};
@ -357,11 +384,12 @@ public class StringUtilsTests extends TestCase {
assertEquals("myString1", result[1]); assertEquals("myString1", result[1]);
assertEquals("myString2", result[2]); assertEquals("myString2", result[2]);
assertEquals(input1, StringUtils.concatenateStringArrays(input1, null)); assertArrayEquals(input1, StringUtils.concatenateStringArrays(input1, null));
assertEquals(input2, StringUtils.concatenateStringArrays(null, input2)); assertArrayEquals(input2, StringUtils.concatenateStringArrays(null, input2));
assertNull(StringUtils.concatenateStringArrays(null, null)); assertNull(StringUtils.concatenateStringArrays(null, null));
} }
@Test
public void testMergeStringArrays() { public void testMergeStringArrays() {
String[] input1 = new String[] {"myString2"}; String[] input1 = new String[] {"myString2"};
String[] input2 = new String[] {"myString1", "myString2"}; String[] input2 = new String[] {"myString1", "myString2"};
@ -370,11 +398,12 @@ public class StringUtilsTests extends TestCase {
assertEquals("myString2", result[0]); assertEquals("myString2", result[0]);
assertEquals("myString1", result[1]); assertEquals("myString1", result[1]);
assertEquals(input1, StringUtils.mergeStringArrays(input1, null)); assertArrayEquals(input1, StringUtils.mergeStringArrays(input1, null));
assertEquals(input2, StringUtils.mergeStringArrays(null, input2)); assertArrayEquals(input2, StringUtils.mergeStringArrays(null, input2));
assertNull(StringUtils.mergeStringArrays(null, null)); assertNull(StringUtils.mergeStringArrays(null, null));
} }
@Test
public void testSortStringArray() { public void testSortStringArray() {
String[] input = new String[] {"myString2"}; String[] input = new String[] {"myString2"};
input = StringUtils.addStringToArray(input, "myString1"); input = StringUtils.addStringToArray(input, "myString1");
@ -386,6 +415,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("myString2", input[1]); assertEquals("myString2", input[1]);
} }
@Test
public void testRemoveDuplicateStrings() { public void testRemoveDuplicateStrings() {
String[] input = new String[] {"myString2", "myString1", "myString2"}; String[] input = new String[] {"myString2", "myString1", "myString2"};
input = StringUtils.removeDuplicateStrings(input); input = StringUtils.removeDuplicateStrings(input);
@ -393,6 +423,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("myString2", input[1]); assertEquals("myString2", input[1]);
} }
@Test
public void testSplitArrayElementsIntoProperties() { public void testSplitArrayElementsIntoProperties() {
String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""}; String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""};
Properties result = StringUtils.splitArrayElementsIntoProperties(input, "="); Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=");
@ -400,6 +431,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("\"value2\"", result.getProperty("key2")); assertEquals("\"value2\"", result.getProperty("key2"));
} }
@Test
public void testSplitArrayElementsIntoPropertiesAndDeletedChars() { public void testSplitArrayElementsIntoPropertiesAndDeletedChars() {
String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""}; String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""};
Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=", "\""); Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=", "\"");
@ -407,6 +439,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("value2", result.getProperty("key2")); assertEquals("value2", result.getProperty("key2"));
} }
@Test
public void testTokenizeToStringArray() { public void testTokenizeToStringArray() {
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ","); String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",");
assertEquals(3, sa.length); assertEquals(3, sa.length);
@ -414,6 +447,7 @@ public class StringUtilsTests extends TestCase {
sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("c")); sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("c"));
} }
@Test
public void testTokenizeToStringArrayWithNotIgnoreEmptyTokens() { public void testTokenizeToStringArrayWithNotIgnoreEmptyTokens() {
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false); String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false);
assertEquals(4, sa.length); assertEquals(4, sa.length);
@ -421,6 +455,7 @@ public class StringUtilsTests extends TestCase {
sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("") && sa[3].equals("c")); sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("") && sa[3].equals("c"));
} }
@Test
public void testTokenizeToStringArrayWithNotTrimTokens() { public void testTokenizeToStringArrayWithNotTrimTokens() {
String[] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true); String[] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true);
assertEquals(3, sa.length); assertEquals(3, sa.length);
@ -428,26 +463,21 @@ public class StringUtilsTests extends TestCase {
sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c")); sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c"));
} }
@Test
public void testCommaDelimitedListToStringArrayWithNullProducesEmptyArray() { public void testCommaDelimitedListToStringArrayWithNullProducesEmptyArray() {
String[] sa = StringUtils.commaDelimitedListToStringArray(null); String[] sa = StringUtils.commaDelimitedListToStringArray(null);
assertTrue("String array isn't null with null input", sa != null); assertTrue("String array isn't null with null input", sa != null);
assertTrue("String array length == 0 with null input", sa.length == 0); assertTrue("String array length == 0 with null input", sa.length == 0);
} }
@Test
public void testCommaDelimitedListToStringArrayWithEmptyStringProducesEmptyArray() { public void testCommaDelimitedListToStringArrayWithEmptyStringProducesEmptyArray() {
String[] sa = StringUtils.commaDelimitedListToStringArray(""); String[] sa = StringUtils.commaDelimitedListToStringArray("");
assertTrue("String array isn't null with null input", sa != null); assertTrue("String array isn't null with null input", sa != null);
assertTrue("String array length == 0 with null input", sa.length == 0); assertTrue("String array length == 0 with null input", sa.length == 0);
} }
private void testStringArrayReverseTransformationMatches(String[] sa) { @Test
String[] reverse =
StringUtils.commaDelimitedListToStringArray(StringUtils.arrayToCommaDelimitedString(sa));
assertEquals("Reverse transformation is equal",
Arrays.asList(sa),
Arrays.asList(reverse));
}
public void testDelimitedListToStringArrayWithComma() { public void testDelimitedListToStringArrayWithComma() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", ","); String[] sa = StringUtils.delimitedListToStringArray("a,b", ",");
assertEquals(2, sa.length); assertEquals(2, sa.length);
@ -455,6 +485,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("b", sa[1]); assertEquals("b", sa[1]);
} }
@Test
public void testDelimitedListToStringArrayWithSemicolon() { public void testDelimitedListToStringArrayWithSemicolon() {
String[] sa = StringUtils.delimitedListToStringArray("a;b", ";"); String[] sa = StringUtils.delimitedListToStringArray("a;b", ";");
assertEquals(2, sa.length); assertEquals(2, sa.length);
@ -462,6 +493,7 @@ public class StringUtilsTests extends TestCase {
assertEquals("b", sa[1]); assertEquals("b", sa[1]);
} }
@Test
public void testDelimitedListToStringArrayWithEmptyString() { public void testDelimitedListToStringArrayWithEmptyString() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", ""); String[] sa = StringUtils.delimitedListToStringArray("a,b", "");
assertEquals(3, sa.length); assertEquals(3, sa.length);
@ -470,28 +502,39 @@ public class StringUtilsTests extends TestCase {
assertEquals("b", sa[2]); assertEquals("b", sa[2]);
} }
@Test
public void testDelimitedListToStringArrayWithNullDelimiter() { public void testDelimitedListToStringArrayWithNullDelimiter() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", null); String[] sa = StringUtils.delimitedListToStringArray("a,b", null);
assertEquals(1, sa.length); assertEquals(1, sa.length);
assertEquals("a,b", sa[0]); assertEquals("a,b", sa[0]);
} }
@Test
public void testCommaDelimitedListToStringArrayMatchWords() { public void testCommaDelimitedListToStringArrayMatchWords() {
// Could read these from files // Could read these from files
String[] sa = new String[] {"foo", "bar", "big"}; String[] sa = new String[] {"foo", "bar", "big"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa); doTestCommaDelimitedListToStringArrayLegalMatch(sa);
testStringArrayReverseTransformationMatches(sa); doTestStringArrayReverseTransformationMatches(sa);
sa = new String[] {"a", "b", "c"}; sa = new String[] {"a", "b", "c"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa); doTestCommaDelimitedListToStringArrayLegalMatch(sa);
testStringArrayReverseTransformationMatches(sa); doTestStringArrayReverseTransformationMatches(sa);
// Test same words // Test same words
sa = new String[] {"AA", "AA", "AA", "AA", "AA"}; sa = new String[] {"AA", "AA", "AA", "AA", "AA"};
doTestCommaDelimitedListToStringArrayLegalMatch(sa); doTestCommaDelimitedListToStringArrayLegalMatch(sa);
testStringArrayReverseTransformationMatches(sa); doTestStringArrayReverseTransformationMatches(sa);
} }
private void doTestStringArrayReverseTransformationMatches(String[] sa) {
String[] reverse =
StringUtils.commaDelimitedListToStringArray(StringUtils.arrayToCommaDelimitedString(sa));
assertEquals("Reverse transformation is equal",
Arrays.asList(sa),
Arrays.asList(reverse));
}
@Test
public void testCommaDelimitedListToStringArraySingleString() { public void testCommaDelimitedListToStringArraySingleString() {
// Could read these from files // Could read these from files
String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur"; String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur";
@ -501,6 +544,7 @@ public class StringUtilsTests extends TestCase {
sa[0].equals(s)); sa[0].equals(s));
} }
@Test
public void testCommaDelimitedListToStringArrayWithOtherPunctuation() { public void testCommaDelimitedListToStringArrayWithOtherPunctuation() {
// Could read these from files // Could read these from files
String[] sa = new String[] {"xcvwert4456346&*.", "///", ".!", ".", ";"}; String[] sa = new String[] {"xcvwert4456346&*.", "///", ".!", ".", ";"};
@ -510,6 +554,7 @@ public class StringUtilsTests extends TestCase {
/** /**
* We expect to see the empty Strings in the output. * We expect to see the empty Strings in the output.
*/ */
@Test
public void testCommaDelimitedListToStringArrayEmptyStrings() { public void testCommaDelimitedListToStringArrayEmptyStrings() {
// Could read these from files // Could read these from files
String[] sa = StringUtils.commaDelimitedListToStringArray("a,,b"); String[] sa = StringUtils.commaDelimitedListToStringArray("a,,b");
@ -522,19 +567,20 @@ public class StringUtilsTests extends TestCase {
} }
private void doTestCommaDelimitedListToStringArrayLegalMatch(String[] components) { private void doTestCommaDelimitedListToStringArrayLegalMatch(String[] components) {
StringBuffer sbuf = new StringBuffer(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < components.length; i++) { for (int i = 0; i < components.length; i++) {
if (i != 0) { if (i != 0) {
sbuf.append(","); sb.append(",");
} }
sbuf.append(components[i]); sb.append(components[i]);
} }
String[] sa = StringUtils.commaDelimitedListToStringArray(sbuf.toString()); String[] sa = StringUtils.commaDelimitedListToStringArray(sb.toString());
assertTrue("String array isn't null with legal match", sa != null); assertTrue("String array isn't null with legal match", sa != null);
assertEquals("String array length is correct with legal match", components.length, sa.length); assertEquals("String array length is correct with legal match", components.length, sa.length);
assertTrue("Output equals input", Arrays.equals(sa, components)); assertTrue("Output equals input", Arrays.equals(sa, components));
} }
@Test
public void testEndsWithIgnoreCase() { public void testEndsWithIgnoreCase() {
String suffix = "fOo"; String suffix = "fOo";
assertTrue(StringUtils.endsWithIgnoreCase("foo", suffix)); assertTrue(StringUtils.endsWithIgnoreCase("foo", suffix));
@ -551,6 +597,8 @@ public class StringUtilsTests extends TestCase {
assertFalse(StringUtils.endsWithIgnoreCase("b", suffix)); assertFalse(StringUtils.endsWithIgnoreCase("b", suffix));
} }
@Test
public void testParseLocaleStringSunnyDay() throws Exception { public void testParseLocaleStringSunnyDay() throws Exception {
Locale expectedLocale = Locale.UK; Locale expectedLocale = Locale.UK;
Locale locale = StringUtils.parseLocaleString(expectedLocale.toString()); Locale locale = StringUtils.parseLocaleString(expectedLocale.toString());
@ -558,19 +606,20 @@ public class StringUtilsTests extends TestCase {
assertEquals(expectedLocale, locale); assertEquals(expectedLocale, locale);
} }
@Test
public void testParseLocaleStringWithMalformedLocaleString() throws Exception { public void testParseLocaleStringWithMalformedLocaleString() throws Exception {
Locale locale = StringUtils.parseLocaleString("_banjo_on_my_knee"); Locale locale = StringUtils.parseLocaleString("_banjo_on_my_knee");
assertNotNull("When given a malformed Locale string, must not return null.", locale); assertNotNull("When given a malformed Locale string, must not return null.", locale);
} }
@Test
public void testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale() throws Exception { public void testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale() throws Exception {
Locale locale = StringUtils.parseLocaleString(""); Locale locale = StringUtils.parseLocaleString("");
assertNull("When given an empty Locale string, must return null.", locale); assertNull("When given an empty Locale string, must return null.", locale);
} }
/** // SPR-8637
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-8637">See SPR-8637</a>. @Test
*/
public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception { public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception {
final String variant = "proper-northern"; final String variant = "proper-northern";
final String localeString = "en_GB_" + variant; final String localeString = "en_GB_" + variant;
@ -578,9 +627,8 @@ public class StringUtilsTests extends TestCase {
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
/** // SPR-3671
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3671">See SPR-3671</a>. @Test
*/
public void testParseLocaleWithMultiValuedVariant() throws Exception { public void testParseLocaleWithMultiValuedVariant() throws Exception {
final String variant = "proper_northern"; final String variant = "proper_northern";
final String localeString = "en_GB_" + variant; final String localeString = "en_GB_" + variant;
@ -588,9 +636,8 @@ public class StringUtilsTests extends TestCase {
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
/** // SPR-3671
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3671">See SPR-3671</a>. @Test
*/
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
final String variant = "proper northern"; final String variant = "proper northern";
final String localeString = "en GB " + variant; final String localeString = "en GB " + variant;
@ -598,9 +645,8 @@ public class StringUtilsTests extends TestCase {
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
/** // SPR-3671
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3671">See SPR-3671</a>. @Test
*/
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception {
final String variant = "proper northern"; final String variant = "proper northern";
final String localeString = "en_GB_" + variant; final String localeString = "en_GB_" + variant;
@ -608,9 +654,8 @@ public class StringUtilsTests extends TestCase {
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
/** // SPR-3671
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3671">See SPR-3671</a>. @Test
*/
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
final String variant = "proper northern"; final String variant = "proper northern";
final String localeString = "en GB " + variant; // lots of whitespace final String localeString = "en GB " + variant; // lots of whitespace
@ -618,9 +663,8 @@ public class StringUtilsTests extends TestCase {
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
/** // SPR-3671
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3671">See SPR-3671</a>. @Test
*/
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
final String variant = "proper_northern"; final String variant = "proper_northern";
final String localeString = "en_GB_____" + variant; // lots of underscores final String localeString = "en_GB_____" + variant; // lots of underscores
@ -628,9 +672,8 @@ public class StringUtilsTests extends TestCase {
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
/** // SPR-7779
* <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-7779">See SPR-7779</a>. @Test
*/
public void testParseLocaleWithInvalidCharacters() { public void testParseLocaleWithInvalidCharacters() {
try { try {
StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E"); StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E");
@ -641,11 +684,20 @@ public class StringUtilsTests extends TestCase {
} }
} }
/** // SPR-9420
* See SPR-9420. @Test
*/
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() { public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString()); assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString()); assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
} }
// SPR-11806
@Test
public void testParseLocaleWithVariantContainingCountryCode() {
String variant = "GBtest";
String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Variant containing country code not extracted correctly", variant, locale.getVariant());
}
} }