1 /* 2 * Copyright 2012 ZXing authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.zxing.common; 18 19 import org.junit.Assert; 20 import org.junit.Test; 21 22 import java.nio.charset.Charset; 23 import java.nio.charset.StandardCharsets; 24 import java.util.Random; 25 26 /** 27 * Tests {@link StringUtils}. 28 */ 29 public final class StringUtilsTestCase extends Assert { 30 31 @Test testRandom()32 public void testRandom() { 33 Random r = new Random(1234L); 34 byte[] bytes = new byte[1000]; 35 r.nextBytes(bytes); 36 assertEquals(Charset.defaultCharset(), StringUtils.guessCharset(bytes, null)); 37 } 38 39 @Test testShortShiftJIS1()40 public void testShortShiftJIS1() { 41 // 金魚 42 doTest(new byte[] { (byte) 0x8b, (byte) 0xe0, (byte) 0x8b, (byte) 0x9b, }, StringUtils.SHIFT_JIS_CHARSET, "SJIS"); 43 } 44 45 @Test testShortISO885911()46 public void testShortISO885911() { 47 // båd 48 doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, StandardCharsets.ISO_8859_1, "ISO8859_1"); 49 } 50 51 @Test testShortUTF81()52 public void testShortUTF81() { 53 // Español 54 doTest(new byte[] { (byte) 0x45, (byte) 0x73, (byte) 0x70, (byte) 0x61, (byte) 0xc3, 55 (byte) 0xb1, (byte) 0x6f, (byte) 0x6c }, 56 StandardCharsets.UTF_8, "UTF8"); 57 } 58 59 @Test testMixedShiftJIS1()60 public void testMixedShiftJIS1() { 61 // Hello 金! 62 doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f, 63 (byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, }, 64 StringUtils.SHIFT_JIS_CHARSET, "SJIS"); 65 } 66 67 @Test testUTF16BE()68 public void testUTF16BE() { 69 // 调压柜 70 doTest(new byte[] { (byte) 0xFE, (byte) 0xFF, (byte) 0x8c, (byte) 0x03, (byte) 0x53, (byte) 0x8b, 71 (byte) 0x67, (byte) 0xdc, }, 72 StandardCharsets.UTF_16, 73 StandardCharsets.UTF_16.name()); 74 } 75 76 @Test testUTF16LE()77 public void testUTF16LE() { 78 // 调压柜 79 doTest(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x03, (byte) 0x8c, (byte) 0x8b, (byte) 0x53, 80 (byte) 0xdc, (byte) 0x67, }, 81 StandardCharsets.UTF_16, 82 StandardCharsets.UTF_16.name()); 83 } 84 doTest(byte[] bytes, Charset charset, String encoding)85 private static void doTest(byte[] bytes, Charset charset, String encoding) { 86 Charset guessedCharset = StringUtils.guessCharset(bytes, null); 87 String guessedEncoding = StringUtils.guessEncoding(bytes, null); 88 assertEquals(charset, guessedCharset); 89 assertEquals(encoding, guessedEncoding); 90 } 91 92 /** 93 * Utility for printing out a string in given encoding as a Java statement, since it's better 94 * to write that into the Java source file rather than risk character encoding issues in the 95 * source file itself. 96 * 97 * @param args command line arguments 98 */ main(String[] args)99 public static void main(String[] args) { 100 String text = args[0]; 101 Charset charset = Charset.forName(args[1]); 102 StringBuilder declaration = new StringBuilder(); 103 declaration.append("new byte[] { "); 104 for (byte b : text.getBytes(charset)) { 105 declaration.append("(byte) 0x"); 106 int value = b & 0xFF; 107 if (value < 0x10) { 108 declaration.append('0'); 109 } 110 declaration.append(Integer.toHexString(value)); 111 declaration.append(", "); 112 } 113 declaration.append('}'); 114 System.out.println(declaration); 115 } 116 117 } 118