StringUtilsTestCase.java (zxing-zxing-3.4.1) | : | StringUtilsTestCase.java (zxing-zxing-3.5.0) | ||
---|---|---|---|---|
skipping to change at line 23 | skipping to change at line 23 | |||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package com.google.zxing.common; | package com.google.zxing.common; | |||
import org.junit.Assert; | import org.junit.Assert; | |||
import org.junit.Test; | import org.junit.Test; | |||
import java.nio.charset.Charset; | import java.nio.charset.Charset; | |||
import java.nio.charset.StandardCharsets; | ||||
import java.util.Random; | ||||
/** | /** | |||
* Tests {@link StringUtils}. | * Tests {@link StringUtils}. | |||
*/ | */ | |||
public final class StringUtilsTestCase extends Assert { | public final class StringUtilsTestCase extends Assert { | |||
@Test | @Test | |||
public void testRandom() { | ||||
Random r = new Random(1234L); | ||||
byte[] bytes = new byte[1000]; | ||||
r.nextBytes(bytes); | ||||
assertEquals(Charset.defaultCharset(), StringUtils.guessCharset(bytes, null) | ||||
); | ||||
} | ||||
@Test | ||||
public void testShortShiftJIS1() { | public void testShortShiftJIS1() { | |||
// ÈáëÈ≠ö | // 金魚 | |||
doTest(new byte[] { (byte) 0x8b, (byte) 0xe0, (byte) 0x8b, (byte) 0x9b, }, " | doTest(new byte[] { (byte) 0x8b, (byte) 0xe0, (byte) 0x8b, (byte) 0x9b, }, S | |||
SJIS"); | tringUtils.SHIFT_JIS_CHARSET, "SJIS"); | |||
} | } | |||
@Test | @Test | |||
public void testShortISO885911() { | public void testShortISO885911() { | |||
// b√•d | // båd | |||
doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, "ISO-8859-1"); | doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, StandardCharse | |||
ts.ISO_8859_1, "ISO8859_1"); | ||||
} | ||||
@Test | ||||
public void testShortUTF81() { | ||||
// Español | ||||
doTest(new byte[] { (byte) 0x45, (byte) 0x73, (byte) 0x70, (byte) 0x61, (byt | ||||
e) 0xc3, | ||||
(byte) 0xb1, (byte) 0x6f, (byte) 0x6c }, | ||||
StandardCharsets.UTF_8, "UTF8"); | ||||
} | } | |||
@Test | @Test | |||
public void testMixedShiftJIS1() { | public void testMixedShiftJIS1() { | |||
// Hello Èáë! | // Hello 金! | |||
doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byt e) 0x6f, | doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byt e) 0x6f, | |||
(byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, }, | (byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, }, | |||
"SJIS"); | StringUtils.SHIFT_JIS_CHARSET, "SJIS"); | |||
} | ||||
@Test | ||||
public void testUTF16BE() { | ||||
// 调压柜 | ||||
doTest(new byte[] { (byte) 0xFE, (byte) 0xFF, (byte) 0x8c, (byte) 0x03, (byt | ||||
e) 0x53, (byte) 0x8b, | ||||
(byte) 0x67, (byte) 0xdc, }, | ||||
StandardCharsets.UTF_16, | ||||
StandardCharsets.UTF_16.name()); | ||||
} | ||||
@Test | ||||
public void testUTF16LE() { | ||||
// 调压柜 | ||||
doTest(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x03, (byte) 0x8c, (byt | ||||
e) 0x8b, (byte) 0x53, | ||||
(byte) 0xdc, (byte) 0x67, }, | ||||
StandardCharsets.UTF_16, | ||||
StandardCharsets.UTF_16.name()); | ||||
} | } | |||
private static void doTest(byte[] bytes, String charsetName) { | private static void doTest(byte[] bytes, Charset charset, String encoding) { | |||
Charset charset = Charset.forName(charsetName); | Charset guessedCharset = StringUtils.guessCharset(bytes, null); | |||
String guessedName = StringUtils.guessEncoding(bytes, null); | String guessedEncoding = StringUtils.guessEncoding(bytes, null); | |||
Charset guessedEncoding = Charset.forName(guessedName); | assertEquals(charset, guessedCharset); | |||
assertEquals(charset, guessedEncoding); | assertEquals(encoding, guessedEncoding); | |||
} | } | |||
/** | /** | |||
* Utility for printing out a string in given encoding as a Java statement, si nce it's better | * Utility for printing out a string in given encoding as a Java statement, si nce it's better | |||
* to write that into the Java source file rather than risk character encoding issues in the | * to write that into the Java source file rather than risk character encoding issues in the | |||
* source file itself. | * source file itself. | |||
* | * | |||
* @param args command line arguments | * @param args command line arguments | |||
*/ | */ | |||
public static void main(String[] args) { | public static void main(String[] args) { | |||
String text = args[0]; | String text = args[0]; | |||
Charset charset = Charset.forName(args[1]); | Charset charset = Charset.forName(args[1]); | |||
StringBuilder declaration = new StringBuilder(); | StringBuilder declaration = new StringBuilder(); | |||
declaration.append("new byte[] { "); | declaration.append("new byte[] { "); | |||
for (byte b : text.getBytes(charset)) { | for (byte b : text.getBytes(charset)) { | |||
declaration.append("(byte) 0x"); | declaration.append("(byte) 0x"); | |||
declaration.append(Integer.toHexString(b & 0xFF)); | int value = b & 0xFF; | |||
if (value < 0x10) { | ||||
declaration.append('0'); | ||||
} | ||||
declaration.append(Integer.toHexString(value)); | ||||
declaration.append(", "); | declaration.append(", "); | |||
} | } | |||
declaration.append('}'); | declaration.append('}'); | |||
System.out.println(declaration); | System.out.println(declaration); | |||
} | } | |||
} | } | |||
End of changes. 8 change blocks. | ||||
13 lines changed or deleted | 58 lines changed or added |