init
This commit is contained in:
65
test/org/apache/tomcat/util/buf/TestAscii.java
Normal file
65
test/org/apache/tomcat/util/buf/TestAscii.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestAscii {
|
||||
|
||||
@Test
|
||||
public void testParseLong1() {
|
||||
String value = "9223372036854775807"; // Long.MAX_VALUE
|
||||
byte[] bytes = value.getBytes();
|
||||
long result = Ascii.parseLong(bytes, 0, bytes.length);
|
||||
Assert.assertEquals(value, String.valueOf(result));
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testParseLong2() {
|
||||
byte[] bytes = "9223372036854775808".getBytes(); // Long.MAX_VALUE + 1
|
||||
long result = Ascii.parseLong(bytes, 0, bytes.length);
|
||||
Assert.fail("NumberFormatException expected, got: " + result);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testParseLong3() {
|
||||
byte[] bytes = "9223372036854775810".getBytes(); // Long.MAX_VALUE + 3
|
||||
long result = Ascii.parseLong(bytes, 0, bytes.length);
|
||||
Assert.fail("NumberFormatException expected, got: " + result);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testParseLong4() {
|
||||
BigInteger x = BigInteger.valueOf(5000000000L).shiftLeft(32);
|
||||
byte[] bytes = String.valueOf(x).getBytes();
|
||||
long result = Ascii.parseLong(bytes, 0, bytes.length);
|
||||
Assert.fail("NumberFormatException expected, got: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseLong5() {
|
||||
String value = "9223372036854775806"; // Long.MAX_VALUE - 1
|
||||
byte[] bytes = value.getBytes();
|
||||
long result = Ascii.parseLong(bytes, 0, bytes.length);
|
||||
Assert.assertEquals(value, String.valueOf(result));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
143
test/org/apache/tomcat/util/buf/TestB2CConverter.java
Normal file
143
test/org/apache/tomcat/util/buf/TestB2CConverter.java
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.MalformedInputException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestB2CConverter {
|
||||
|
||||
private static final byte[] UTF16_MESSAGE =
|
||||
new byte[] {-2, -1, 0, 65, 0, 66, 0, 67};
|
||||
|
||||
private static final byte[] UTF8_INVALID = new byte[] {-8, -69, -73, -77};
|
||||
|
||||
private static final byte[] UTF8_PARTIAL = new byte[] {-50};
|
||||
|
||||
@Test
|
||||
public void testSingleMessage() throws Exception {
|
||||
testMessages(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoMessage() throws Exception {
|
||||
testMessages(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManyMessage() throws Exception {
|
||||
testMessages(10);
|
||||
}
|
||||
|
||||
private void testMessages(int msgCount) throws Exception {
|
||||
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_16);
|
||||
|
||||
ByteChunk bc = new ByteChunk();
|
||||
CharChunk cc = new CharChunk(32);
|
||||
|
||||
|
||||
for (int i = 0; i < msgCount; i++) {
|
||||
bc.append(UTF16_MESSAGE, 0, UTF16_MESSAGE.length);
|
||||
conv.convert(bc, cc, true);
|
||||
Assert.assertEquals("ABC", cc.toString());
|
||||
bc.recycle();
|
||||
cc.recycle();
|
||||
conv.recycle();
|
||||
}
|
||||
|
||||
System.out.println(cc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLeftoverSize() {
|
||||
float maxLeftover = 0;
|
||||
String charsetName = "UNSET";
|
||||
for (Charset charset : Charset.availableCharsets().values()) {
|
||||
float leftover;
|
||||
if (charset.name().toLowerCase(Locale.ENGLISH).startsWith("x-")) {
|
||||
// Non-standard charset that browsers won't be using
|
||||
// Likely something used internally by the JRE
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
leftover = charset.newEncoder().maxBytesPerChar();
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
// Skip it
|
||||
continue;
|
||||
}
|
||||
if (leftover > maxLeftover) {
|
||||
maxLeftover = leftover;
|
||||
charsetName = charset.name();
|
||||
}
|
||||
}
|
||||
Assert.assertTrue("Limit needs to be at least " + maxLeftover +
|
||||
" (used in charset '" + charsetName + "')",
|
||||
maxLeftover <= B2CConverter.LEFTOVER_SIZE);
|
||||
}
|
||||
|
||||
@Test(expected=MalformedInputException.class)
|
||||
public void testBug54602a() throws Exception {
|
||||
// Check invalid input is rejected straight away
|
||||
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
|
||||
ByteChunk bc = new ByteChunk();
|
||||
CharChunk cc = new CharChunk();
|
||||
|
||||
bc.append(UTF8_INVALID, 0, UTF8_INVALID.length);
|
||||
cc.allocate(bc.getLength(), -1);
|
||||
|
||||
conv.convert(bc, cc, false);
|
||||
}
|
||||
|
||||
@Test(expected=MalformedInputException.class)
|
||||
public void testBug54602b() throws Exception {
|
||||
// Check partial input is rejected
|
||||
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
|
||||
ByteChunk bc = new ByteChunk();
|
||||
CharChunk cc = new CharChunk();
|
||||
|
||||
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
|
||||
cc.allocate(bc.getLength(), -1);
|
||||
|
||||
conv.convert(bc, cc, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug54602c() throws Exception {
|
||||
// Check partial input is rejected once it is known to be all available
|
||||
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
|
||||
ByteChunk bc = new ByteChunk();
|
||||
CharChunk cc = new CharChunk();
|
||||
|
||||
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
|
||||
cc.allocate(bc.getLength(), -1);
|
||||
|
||||
conv.convert(bc, cc, false);
|
||||
|
||||
Exception e = null;
|
||||
try {
|
||||
conv.convert(bc, cc, true);
|
||||
} catch (MalformedInputException mie) {
|
||||
e = mie;
|
||||
}
|
||||
Assert.assertNotNull(e);
|
||||
}
|
||||
}
|
||||
176
test/org/apache/tomcat/util/buf/TestByteChunk.java
Normal file
176
test/org/apache/tomcat/util/buf/TestByteChunk.java
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.tomcat.util.buf.ByteChunk.ByteOutputChannel;
|
||||
|
||||
/**
|
||||
* Test cases for {@link ByteChunk}.
|
||||
*/
|
||||
public class TestByteChunk {
|
||||
|
||||
@Test
|
||||
public void testConvertToBytes() throws UnsupportedEncodingException {
|
||||
String string = "HTTP/1.1 100 \r\n\r\n";
|
||||
byte[] bytes = ByteChunk.convertToBytes(string);
|
||||
byte[] expected = string.getBytes("ISO-8859-1");
|
||||
Assert.assertTrue(Arrays.equals(bytes, expected));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Test for {@code findByte} vs. {@code indexOf} methods difference.
|
||||
*
|
||||
* <p>
|
||||
* As discussed in the "Re: r944918" thread on dev@, {@code
|
||||
* ByteChunk.indexOf()} works for 0-127 ASCII chars only, and cannot find
|
||||
* any chars outside of the range. {@code ByteChunk.findByte()} works for
|
||||
* any ISO-8859-1 chars.
|
||||
*/
|
||||
@Test
|
||||
public void testFindByte() throws UnsupportedEncodingException {
|
||||
// 0xa0 = 160 = character
|
||||
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
|
||||
final int len = bytes.length;
|
||||
|
||||
// indexOf() does not work outside of 0-127
|
||||
Assert.assertEquals(5, ByteChunk.findByte(bytes, 0, len, (byte) '\u00a0'));
|
||||
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 0, len, '\u00a0'));
|
||||
|
||||
Assert.assertEquals(0, ByteChunk.findByte(bytes, 0, len, (byte) 'H'));
|
||||
Assert.assertEquals(0, ByteChunk.indexOf(bytes, 0, len, 'H'));
|
||||
|
||||
Assert.assertEquals(len - 1, ByteChunk.findByte(bytes, 0, len, (byte) 'd'));
|
||||
Assert.assertEquals(len - 1, ByteChunk.indexOf(bytes, 0, len, 'd'));
|
||||
|
||||
Assert.assertEquals(-1, ByteChunk.findByte(bytes, 0, len, (byte) 'x'));
|
||||
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 0, len, 'x'));
|
||||
|
||||
Assert.assertEquals(7, ByteChunk.findByte(bytes, 5, len, (byte) 'o'));
|
||||
Assert.assertEquals(7, ByteChunk.indexOf(bytes, 5, len, 'o'));
|
||||
|
||||
Assert.assertEquals(-1, ByteChunk.findByte(bytes, 2, 5, (byte) 'w'));
|
||||
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 5, 5, 'w'));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIndexOf_Char() throws UnsupportedEncodingException {
|
||||
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
|
||||
final int len = bytes.length;
|
||||
|
||||
ByteChunk bc = new ByteChunk();
|
||||
bc.setBytes(bytes, 0, len);
|
||||
|
||||
Assert.assertEquals(0, bc.indexOf('H', 0));
|
||||
Assert.assertEquals(6, bc.indexOf('w', 0));
|
||||
|
||||
// Does not work outside of 0-127
|
||||
Assert.assertEquals(-1, bc.indexOf('\u00a0', 0));
|
||||
|
||||
bc.setBytes(bytes, 6, 5);
|
||||
Assert.assertEquals(1, bc.indexOf('o', 0));
|
||||
|
||||
bc.setBytes(bytes, 6, 2);
|
||||
Assert.assertEquals(0, bc.indexOf('w', 0));
|
||||
Assert.assertEquals(-1, bc.indexOf('d', 0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIndexOf_String() throws UnsupportedEncodingException {
|
||||
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
|
||||
final int len = bytes.length;
|
||||
|
||||
ByteChunk bc = new ByteChunk();
|
||||
bc.setBytes(bytes, 0, len);
|
||||
|
||||
Assert.assertEquals(0, bc.indexOf("Hello", 0, "Hello".length(), 0));
|
||||
Assert.assertEquals(2, bc.indexOf("ll", 0, 2, 0));
|
||||
Assert.assertEquals(2, bc.indexOf("Hello", 2, 2, 0));
|
||||
|
||||
Assert.assertEquals(7, bc.indexOf("o", 0, 1, 5));
|
||||
|
||||
// Does not work outside of 0-127
|
||||
Assert.assertEquals(-1, bc.indexOf("\u00a0", 0, 1, 0));
|
||||
|
||||
bc.setBytes(bytes, 6, 5);
|
||||
Assert.assertEquals(1, bc.indexOf("o", 0, 1, 0));
|
||||
|
||||
bc.setBytes(bytes, 6, 2);
|
||||
Assert.assertEquals(0, bc.indexOf("wo", 0, 1, 0));
|
||||
Assert.assertEquals(-1, bc.indexOf("d", 0, 1, 0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindBytes() throws UnsupportedEncodingException {
|
||||
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
|
||||
final int len = bytes.length;
|
||||
|
||||
Assert.assertEquals(0, ByteChunk.findBytes(bytes, 0, len, new byte[] { 'H' }));
|
||||
Assert.assertEquals(5, ByteChunk.findBytes(bytes, 0, len, new byte[] {
|
||||
(byte) '\u00a0', 'x' }));
|
||||
Assert.assertEquals(5, ByteChunk.findBytes(bytes, 0, len - 4, new byte[] {
|
||||
'x', (byte) '\u00a0' }));
|
||||
Assert.assertEquals(len - 1, ByteChunk.findBytes(bytes, 2, len, new byte[] {
|
||||
'x', 'd' }));
|
||||
Assert.assertEquals(1, ByteChunk.findBytes(bytes, 0, len, new byte[] { 'o',
|
||||
'e' }));
|
||||
Assert.assertEquals(-1, ByteChunk.findBytes(bytes, 2, 5, new byte[] { 'w' }));
|
||||
}
|
||||
|
||||
|
||||
@Ignore // Requires a 6GB heap (on markt's desktop - YMMV)
|
||||
@Test
|
||||
public void testAppend() throws Exception {
|
||||
ByteChunk bc = new ByteChunk();
|
||||
bc.setByteOutputChannel(new Sink());
|
||||
// Defaults to no limit
|
||||
|
||||
byte data[] = new byte[32 * 1024 * 1024];
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
bc.append(data, 0, data.length);
|
||||
}
|
||||
|
||||
Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, bc.getBuffer().length);
|
||||
}
|
||||
|
||||
|
||||
public class Sink implements ByteOutputChannel {
|
||||
|
||||
@Override
|
||||
public void realWriteBytes(byte[] cbuf, int off, int len) throws IOException {
|
||||
// NO-OP
|
||||
}
|
||||
|
||||
@Override
|
||||
public void realWriteBytes(ByteBuffer from) throws IOException {
|
||||
// NO-OP
|
||||
}
|
||||
}
|
||||
}
|
||||
96
test/org/apache/tomcat/util/buf/TestCharChunk.java
Normal file
96
test/org/apache/tomcat/util/buf/TestCharChunk.java
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.tomcat.util.buf.CharChunk.CharOutputChannel;
|
||||
|
||||
/**
|
||||
* Test cases for {@link CharChunk}.
|
||||
*/
|
||||
public class TestCharChunk {
|
||||
|
||||
@Test
|
||||
public void testEndsWith() {
|
||||
CharChunk cc = new CharChunk();
|
||||
Assert.assertFalse(cc.endsWith("test"));
|
||||
cc.setChars("xxtestxx".toCharArray(), 2, 4);
|
||||
Assert.assertTrue(cc.endsWith(""));
|
||||
Assert.assertTrue(cc.endsWith("t"));
|
||||
Assert.assertTrue(cc.endsWith("st"));
|
||||
Assert.assertTrue(cc.endsWith("test"));
|
||||
Assert.assertFalse(cc.endsWith("x"));
|
||||
Assert.assertFalse(cc.endsWith("xxtest"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIndexOf_String() {
|
||||
char[] chars = "Hello\u00a0world".toCharArray();
|
||||
final int len = chars.length;
|
||||
|
||||
CharChunk cc = new CharChunk();
|
||||
cc.setChars(chars, 0, len);
|
||||
|
||||
Assert.assertEquals(0, cc.indexOf("Hello", 0, "Hello".length(), 0));
|
||||
Assert.assertEquals(2, cc.indexOf("ll", 0, 2, 0));
|
||||
Assert.assertEquals(2, cc.indexOf("Hello", 2, 2, 0));
|
||||
|
||||
Assert.assertEquals(7, cc.indexOf("o", 0, 1, 5));
|
||||
|
||||
// Does work outside of 0-127 (unlike ByteChunk)
|
||||
Assert.assertEquals(5, cc.indexOf("\u00a0", 0, 1, 0));
|
||||
|
||||
cc.setChars(chars, 6, 5);
|
||||
Assert.assertEquals(1, cc.indexOf("o", 0, 1, 0));
|
||||
|
||||
cc.setChars(chars, 6, 2);
|
||||
Assert.assertEquals(0, cc.indexOf("wo", 0, 1, 0));
|
||||
Assert.assertEquals(-1, cc.indexOf("d", 0, 1, 0));
|
||||
}
|
||||
|
||||
|
||||
@Ignore // Requires an 11GB heap (on markt's desktop - YMMV)
|
||||
@Test
|
||||
public void testAppend() throws Exception {
|
||||
CharChunk cc = new CharChunk();
|
||||
cc.setCharOutputChannel(new Sink());
|
||||
// Defaults to no limit
|
||||
|
||||
char data[] = new char[32 * 1024 * 1024];
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
cc.append(data, 0, data.length);
|
||||
}
|
||||
|
||||
Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, cc.getBuffer().length);
|
||||
}
|
||||
|
||||
|
||||
public class Sink implements CharOutputChannel {
|
||||
|
||||
@Override
|
||||
public void realWriteChars(char[] cbuf, int off, int len) throws IOException {
|
||||
// NO-OP
|
||||
}
|
||||
}
|
||||
}
|
||||
80
test/org/apache/tomcat/util/buf/TestCharsetCache.java
Normal file
80
test/org/apache/tomcat/util/buf/TestCharsetCache.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestCharsetCache {
|
||||
|
||||
@Test
|
||||
public void testAllKnownCharsets() {
|
||||
|
||||
Set<String> known = new HashSet<>();
|
||||
known.addAll(Arrays.asList(CharsetCache.LAZY_CHARSETS));
|
||||
Set<String> initial = new HashSet<>();
|
||||
initial.addAll(Arrays.asList(CharsetCache.INITIAL_CHARSETS));
|
||||
|
||||
List<String> cacheMisses = new ArrayList<>();
|
||||
|
||||
for (Charset charset: Charset.availableCharsets().values()) {
|
||||
String name = charset.name().toLowerCase(Locale.ENGLISH);
|
||||
|
||||
// No need to test the charsets that are pre-loaded
|
||||
if (initial.contains(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!known.contains(name)) {
|
||||
cacheMisses.add(name);
|
||||
}
|
||||
|
||||
for (String alias : charset.aliases()) {
|
||||
alias = alias.toLowerCase(Locale.ENGLISH);
|
||||
if (!known.contains(alias)) {
|
||||
cacheMisses.add(alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cacheMisses.size() != 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Collections.sort(cacheMisses);
|
||||
for (String name : cacheMisses) {
|
||||
if (sb.length() == 0) {
|
||||
sb.append('"');
|
||||
} else {
|
||||
sb.append(", \"");
|
||||
}
|
||||
sb.append(name.toLowerCase(Locale.ENGLISH));
|
||||
sb.append('"');
|
||||
}
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
|
||||
Assert.assertTrue(cacheMisses.size() == 0);
|
||||
}
|
||||
}
|
||||
141
test/org/apache/tomcat/util/buf/TestCharsetCachePerformance.java
Normal file
141
test/org/apache/tomcat/util/buf/TestCharsetCachePerformance.java
Normal file
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestCharsetCachePerformance {
|
||||
|
||||
@Test
|
||||
public void testNoCsCache() throws Exception {
|
||||
doTest(new NoCsCache());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFullCsCache() throws Exception {
|
||||
doTest(new FullCsCache());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLazyCsCache() throws Exception {
|
||||
doTest(new LazyCsCache());
|
||||
}
|
||||
|
||||
|
||||
private void doTest(CsCache cache) throws Exception {
|
||||
int threadCount = 10;
|
||||
int iterations = 10000000;
|
||||
String[] lookupNames = new String[] {
|
||||
"ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5" };
|
||||
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
threads[i] = new TestCsCacheThread(iterations, cache, lookupNames);
|
||||
}
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
threads[i].join();
|
||||
}
|
||||
|
||||
long endTime = System.nanoTime();
|
||||
|
||||
System.out.println(cache.getClass().getName() + ": " + (endTime - startTime) + "ns");
|
||||
}
|
||||
|
||||
|
||||
private static interface CsCache {
|
||||
Charset getCharset(String charsetName);
|
||||
}
|
||||
|
||||
|
||||
private static class NoCsCache implements CsCache {
|
||||
|
||||
@Override
|
||||
public Charset getCharset(String charsetName) {
|
||||
return Charset.forName(charsetName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class FullCsCache implements CsCache {
|
||||
|
||||
private static final Map<String,Charset> cache = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (Charset charset: Charset.availableCharsets().values()) {
|
||||
cache.put(charset.name().toLowerCase(Locale.ENGLISH), charset);
|
||||
for (String alias : charset.aliases()) {
|
||||
cache.put(alias.toLowerCase(Locale.ENGLISH), charset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Charset getCharset(String charsetName) {
|
||||
return cache.get(charsetName.toLowerCase(Locale.ENGLISH));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class LazyCsCache implements CsCache {
|
||||
|
||||
private CharsetCache cache = new CharsetCache();
|
||||
|
||||
@Override
|
||||
public Charset getCharset(String charsetName) {
|
||||
return cache.getCharset(charsetName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestCsCacheThread extends Thread {
|
||||
|
||||
private final int iterations;
|
||||
private final CsCache cache;
|
||||
private final String[] lookupNames;
|
||||
private final int lookupNamesCount;
|
||||
|
||||
public TestCsCacheThread(int iterations, CsCache cache, String[] lookupNames) {
|
||||
this.iterations = iterations;
|
||||
this.cache = cache;
|
||||
this.lookupNames = lookupNames;
|
||||
this.lookupNamesCount = lookupNames.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
cache.getCharset(lookupNames[i % lookupNamesCount]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
71
test/org/apache/tomcat/util/buf/TestHexUtils.java
Normal file
71
test/org/apache/tomcat/util/buf/TestHexUtils.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test cases for {@link HexUtils}.
|
||||
*/
|
||||
public class TestHexUtils {
|
||||
|
||||
private static final String TEST01_STRING = "Hello World";
|
||||
private static final byte[] TEST01_BYTES = TEST01_STRING.getBytes(StandardCharsets.UTF_8);
|
||||
private static final String TEST02_STRING = "foo";
|
||||
private static final byte[] TEST02_BYTES = TEST02_STRING.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
@Test
|
||||
public void testGetDec() {
|
||||
Assert.assertEquals(0, HexUtils.getDec('0'));
|
||||
Assert.assertEquals(9, HexUtils.getDec('9'));
|
||||
Assert.assertEquals(10, HexUtils.getDec('a'));
|
||||
Assert.assertEquals(15, HexUtils.getDec('f'));
|
||||
Assert.assertEquals(10, HexUtils.getDec('A'));
|
||||
Assert.assertEquals(15, HexUtils.getDec('F'));
|
||||
Assert.assertEquals(-1, HexUtils.getDec(0));
|
||||
Assert.assertEquals(-1, HexUtils.getDec('Z'));
|
||||
Assert.assertEquals(-1, HexUtils.getDec(255));
|
||||
Assert.assertEquals(-1, HexUtils.getDec(-60));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoundTrip01() {
|
||||
Assert.assertArrayEquals(TEST01_STRING, TEST01_BYTES,
|
||||
HexUtils.fromHexString(HexUtils.toHexString(TEST01_BYTES)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoundTrip02() {
|
||||
Assert.assertArrayEquals(TEST02_STRING, TEST02_BYTES,
|
||||
HexUtils.fromHexString(HexUtils.toHexString(TEST02_BYTES)));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testFromHex01() {
|
||||
HexUtils.fromHexString("Not a hex string");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testFromHex02() {
|
||||
// Odd number of hex characters
|
||||
HexUtils.fromHexString("aaa");
|
||||
}
|
||||
}
|
||||
69
test/org/apache/tomcat/util/buf/TestMessageBytes.java
Normal file
69
test/org/apache/tomcat/util/buf/TestMessageBytes.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestMessageBytes {
|
||||
|
||||
@Test
|
||||
public void testToStringFromNull() {
|
||||
MessageBytes mb = MessageBytes.newInstance();
|
||||
mb.toString();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToBytesFromNull() {
|
||||
MessageBytes mb = MessageBytes.newInstance();
|
||||
mb.toBytes();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToCharsFromNull() {
|
||||
MessageBytes mb = MessageBytes.newInstance();
|
||||
mb.toChars();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToStringAfterRecycle() {
|
||||
MessageBytes mb = MessageBytes.newInstance();
|
||||
mb.setString("foo");
|
||||
mb.recycle();
|
||||
mb.toString();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToBytesAfterRecycle() {
|
||||
MessageBytes mb = MessageBytes.newInstance();
|
||||
mb.setString("foo");
|
||||
mb.recycle();
|
||||
mb.toBytes();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToCharsAfterRecycle() {
|
||||
MessageBytes mb = MessageBytes.newInstance();
|
||||
mb.setString("foo");
|
||||
mb.recycle();
|
||||
mb.toChars();
|
||||
}
|
||||
}
|
||||
77
test/org/apache/tomcat/util/buf/TestStringUtils.java
Normal file
77
test/org/apache/tomcat/util/buf/TestStringUtils.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/*
|
||||
* None of these tests should throw a NPE.
|
||||
*/
|
||||
public class TestStringUtils {
|
||||
|
||||
@Test
|
||||
public void testNullArray() {
|
||||
Assert.assertEquals("", StringUtils.join((String[]) null));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullArrayCharStringBuilder() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringUtils.join((String[]) null, ',', sb);
|
||||
Assert.assertEquals("", sb.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullCollection() {
|
||||
Assert.assertEquals("", StringUtils.join((Collection<String>) null));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullCollectionChar() {
|
||||
Assert.assertEquals("", StringUtils.join(null, ','));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullIterableCharStringBuilder() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringUtils.join((Iterable<String>) null, ',', sb);
|
||||
Assert.assertEquals("", sb.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullArrayCharFunctionStringBuilder() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringUtils.join((String[]) null, ',', null, sb);
|
||||
Assert.assertEquals("", sb.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullIterableCharFunctionStringBuilder() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringUtils.join((Iterable<String>) null, ',', null, sb);
|
||||
Assert.assertEquals("", sb.toString());
|
||||
}
|
||||
}
|
||||
102
test/org/apache/tomcat/util/buf/TestUDecoder.java
Normal file
102
test/org/apache/tomcat/util/buf/TestUDecoder.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestUDecoder {
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringInvalid() {
|
||||
// %n rather than %nn should throw an IAE according to the Javadoc
|
||||
Exception exception = null;
|
||||
try {
|
||||
UDecoder.URLDecode("%5xxxxx");
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
Assert.assertTrue(exception instanceof IllegalArgumentException);
|
||||
|
||||
// Edge case trying to trigger ArrayIndexOutOfBoundsException
|
||||
exception = null;
|
||||
try {
|
||||
UDecoder.URLDecode("%5");
|
||||
} catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
Assert.assertTrue(exception instanceof IllegalArgumentException);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringValidIso88591Start() {
|
||||
String result = UDecoder.URLDecode("%41xxxx", StandardCharsets.ISO_8859_1);
|
||||
Assert.assertEquals("Axxxx", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringValidIso88591Middle() {
|
||||
String result = UDecoder.URLDecode("xx%41xx", StandardCharsets.ISO_8859_1);
|
||||
Assert.assertEquals("xxAxx", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringValidIso88591End() {
|
||||
String result = UDecoder.URLDecode("xxxx%41", StandardCharsets.ISO_8859_1);
|
||||
Assert.assertEquals("xxxxA", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringValidUtf8Start() {
|
||||
String result = UDecoder.URLDecode("%c3%aaxxxx", StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("\u00eaxxxx", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringValidUtf8Middle() {
|
||||
String result = UDecoder.URLDecode("xx%c3%aaxx", StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("xx\u00eaxx", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringValidUtf8End() {
|
||||
String result = UDecoder.URLDecode("xxxx%c3%aa", StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("xxxx\u00ea", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringNonAsciiValidNone() {
|
||||
String result = UDecoder.URLDecode("\u00eaxxxx", StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("\u00eaxxxx", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testURLDecodeStringNonAsciiValidUtf8() {
|
||||
String result = UDecoder.URLDecode("\u00ea%c3%aa", StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("\u00ea\u00ea", result);
|
||||
}
|
||||
}
|
||||
46
test/org/apache/tomcat/util/buf/TestUEncoder.java
Normal file
46
test/org/apache/tomcat/util/buf/TestUEncoder.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.tomcat.util.buf.UEncoder.SafeCharsSet;
|
||||
|
||||
/**
|
||||
* Test cases for {@link UEncoder}.
|
||||
*/
|
||||
public class TestUEncoder {
|
||||
|
||||
@Test
|
||||
public void testEncodeURLWithSlashInit() throws IOException {
|
||||
UEncoder urlEncoder = new UEncoder(SafeCharsSet.WITH_SLASH);
|
||||
|
||||
String s = "a+b/c/d+e.class";
|
||||
Assert.assertTrue(urlEncoder.encodeURL(s, 0, s.length()).equals(
|
||||
"a%2bb/c/d%2be.class"));
|
||||
Assert.assertTrue(urlEncoder.encodeURL(s, 2, s.length() - 2).equals(
|
||||
"b/c/d%2be.cla"));
|
||||
|
||||
s = new String(new char[] { 0xD801, 0xDC01 });
|
||||
Assert.assertTrue(urlEncoder.encodeURL(s, 0, s.length())
|
||||
.equals("%f0%90%90%81"));
|
||||
}
|
||||
}
|
||||
24
test/org/apache/tomcat/util/buf/TestUriUtil24.java
Normal file
24
test/org/apache/tomcat/util/buf/TestUriUtil24.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
public class TestUriUtil24 extends TesterUriUtilBase {
|
||||
|
||||
public TestUriUtil24() {
|
||||
super("$");
|
||||
}
|
||||
}
|
||||
24
test/org/apache/tomcat/util/buf/TestUriUtil26.java
Normal file
24
test/org/apache/tomcat/util/buf/TestUriUtil26.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
public class TestUriUtil26 extends TesterUriUtilBase {
|
||||
|
||||
public TestUriUtil26() {
|
||||
super("&");
|
||||
}
|
||||
}
|
||||
24
test/org/apache/tomcat/util/buf/TestUriUtil2A.java
Normal file
24
test/org/apache/tomcat/util/buf/TestUriUtil2A.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
public class TestUriUtil2A extends TesterUriUtilBase {
|
||||
|
||||
public TestUriUtil2A() {
|
||||
super("*");
|
||||
}
|
||||
}
|
||||
24
test/org/apache/tomcat/util/buf/TestUriUtil40.java
Normal file
24
test/org/apache/tomcat/util/buf/TestUriUtil40.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
public class TestUriUtil40 extends TesterUriUtilBase {
|
||||
|
||||
public TestUriUtil40() {
|
||||
super("@");
|
||||
}
|
||||
}
|
||||
678
test/org/apache/tomcat/util/buf/TestUtf8.java
Normal file
678
test/org/apache/tomcat/util/buf/TestUtf8.java
Normal file
File diff suppressed because it is too large
Load Diff
136
test/org/apache/tomcat/util/buf/TesterUriUtilBase.java
Normal file
136
test/org/apache/tomcat/util/buf/TesterUriUtilBase.java
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.apache.tomcat.util.buf;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
|
||||
|
||||
public abstract class TesterUriUtilBase {
|
||||
|
||||
private final String separator;
|
||||
|
||||
protected TesterUriUtilBase(String separator) {
|
||||
this.separator = separator;
|
||||
TomcatURLStreamHandlerFactory.register();
|
||||
System.setProperty("org.apache.tomcat.util.buf.UriUtil.WAR_SEPARATOR", separator);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBuildJarUrl01() throws MalformedURLException {
|
||||
File jarFile = new File("/patha/pathb!/pathc");
|
||||
String result = UriUtil.buildJarUrl(jarFile).toString();
|
||||
|
||||
int index = result.indexOf("!/");
|
||||
Assert.assertEquals(result, result.length() - 2, index);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBuildJarUrl02() throws MalformedURLException {
|
||||
File jarFile = new File("/patha/pathb*/pathc");
|
||||
String result = UriUtil.buildJarUrl(jarFile).toString();
|
||||
|
||||
int index = result.indexOf("!/");
|
||||
Assert.assertEquals(result, result.length() - 2, index);
|
||||
|
||||
index = result.indexOf("*/");
|
||||
Assert.assertEquals(result, -1, index);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBuildJarUrl03() throws MalformedURLException {
|
||||
File jarFile = new File("/patha/pathb^/pathc");
|
||||
String result = UriUtil.buildJarUrl(jarFile).toString();
|
||||
|
||||
int index = result.indexOf("!/");
|
||||
Assert.assertEquals(result, result.length() - 2, index);
|
||||
|
||||
index = result.indexOf("^/");
|
||||
Assert.assertEquals(result, -1, index);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBuildJarUrl04() throws MalformedURLException {
|
||||
File jarFile = new File("/patha/pathb" + separator + "/pathc");
|
||||
String result = UriUtil.buildJarUrl(jarFile).toString();
|
||||
|
||||
int index = result.indexOf("!/");
|
||||
Assert.assertEquals(result, result.length() - 2, index);
|
||||
|
||||
index = result.indexOf(separator + "/");
|
||||
Assert.assertEquals(result, -1, index);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWarToJar01() throws MalformedURLException {
|
||||
doTestWarToJar("^");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWarToJar02() throws MalformedURLException {
|
||||
doTestWarToJar("*");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWarToJar03() throws MalformedURLException {
|
||||
doTestWarToJar(separator);
|
||||
}
|
||||
|
||||
|
||||
private void doTestWarToJar(String separator) throws MalformedURLException {
|
||||
URL warUrl = new URL("war:file:/external/path" + separator + "/internal/path");
|
||||
URL jarUrl = UriUtil.warToJar(warUrl);
|
||||
Assert.assertEquals("jar:file:/external/path!/internal/path", jarUrl.toString());
|
||||
}
|
||||
|
||||
|
||||
// @Test /* Uncomment to test performance for different implementations. */
|
||||
public void performanceTestBuildJarUrl() throws MalformedURLException {
|
||||
File jarFile = new File("/patha/pathb^/pathc");
|
||||
|
||||
URL url = null;
|
||||
|
||||
int count = 1000000;
|
||||
|
||||
// Warm up
|
||||
for (int i = 0; i < count / 10; i++) {
|
||||
url = UriUtil.buildJarUrl(jarFile);
|
||||
}
|
||||
|
||||
// Test
|
||||
long start = System.nanoTime();
|
||||
for (int i = 0; i < count / 10; i++) {
|
||||
url = UriUtil.buildJarUrl(jarFile);
|
||||
}
|
||||
long duration = System.nanoTime() - start;
|
||||
|
||||
System.out.println("[" + count + "] iterations took [" +
|
||||
duration + "] ns for [" + url + "]");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user