|
package org.bukkit.encoding; |
|
|
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.PrintWriter; |
|
import java.nio.charset.Charset; |
|
import java.util.Arrays; |
|
import java.util.List; |
|
|
|
import org.bukkit.configuration.file.YamlConfiguration; |
|
import org.junit.After; |
|
import org.junit.BeforeClass; |
|
import org.junit.Test; |
|
import org.junit.runner.RunWith; |
|
import org.junit.runners.Parameterized; |
|
import org.junit.runners.Parameterized.Parameter; |
|
import org.junit.runners.Parameterized.Parameters; |
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; |
|
|
|
import com.google.common.io.ByteStreams; |
|
import com.google.common.io.Files; |
|
|
|
@RunWith(Parameterized.class) |
|
public class UTF8Test { |
|
private static final String COMMON_DIR = "target/encoding"; |
|
private static final String DEFAULT_DIR = COMMON_DIR + "/default-" + Charset.defaultCharset().name(); |
|
private static final String DEFAULT_2_DIR = COMMON_DIR + "/default-2-" + Charset.defaultCharset().name(); |
|
private static final String UTF8_DIR = COMMON_DIR + "/UTF8"; |
|
private static final String UTF8_2_DIR = COMMON_DIR + "/UTF8-2"; |
|
private static final String EXT_64 = ".b64"; |
|
private static final String EXT_LOG = ".log"; |
|
private static final String UTF8 = "UTF8"; |
|
private static final String[] DIRS = new String[] { DEFAULT_DIR, DEFAULT_2_DIR, UTF8_DIR, UTF8_2_DIR }; |
|
private static final char[] chars = new char[16]; |
|
static { |
|
int i = 0; |
|
for (char c = '0'; c <= '9'; c++) { |
|
chars[i++] = c; |
|
} |
|
for (char c = 'A'; c <= 'F'; c++) { |
|
chars[i++] = c; |
|
} |
|
} |
|
|
|
@Parameters |
|
public static List<Object[]> data() { |
|
return Arrays.asList( |
|
new Object[] {"file1.yml"}, |
|
new Object[] {"file2.yml"} |
|
// TODO - add your own filenames here |
|
); |
|
} |
|
|
|
@BeforeClass |
|
public static void initFolder() { |
|
for (String dir : DIRS) { |
|
new File(dir).mkdirs(); |
|
} |
|
} |
|
|
|
private String readEncodedFileToNormalString() throws Throwable { |
|
InputStream stream = null; |
|
try { |
|
stream = this.getClass().getResourceAsStream(fileName + EXT_64); |
|
if (stream == null) { |
|
stream = this.getClass().getResourceAsStream(fileName); |
|
if (stream == null) { |
|
throw new AssertionError(fileName + " not found"); |
|
} |
|
return new String(ByteStreams.toByteArray(stream), UTF8); |
|
} else { |
|
String base64 = new String(ByteStreams.toByteArray(stream), UTF8); |
|
return new String(Base64Coder.decodeLines(base64), UTF8); |
|
} |
|
} finally { |
|
if (stream != null) { |
|
try { |
|
stream.close(); |
|
} catch (IOException ex) { |
|
// NOOP |
|
} |
|
} |
|
} |
|
} |
|
|
|
@Parameter |
|
public String fileName; |
|
|
|
@Test |
|
public void testFile() throws Throwable { |
|
String file = readEncodedFileToNormalString(); |
|
Files.write(Base64Coder.encodeLines(file.getBytes(UTF8)), new File(COMMON_DIR + "/" + fileName + EXT_64), Charset.forName(UTF8)); |
|
|
|
YamlConfiguration yaml = new YamlConfiguration(); |
|
yaml.loadFromString(file); |
|
|
|
yaml.save(DEFAULT_DIR + "/" + fileName); |
|
Files.write(yaml.saveToString().getBytes(UTF8), new File(UTF8_DIR + "/" + fileName)); |
|
|
|
yaml = new YamlConfiguration(); |
|
yaml.load(new File(DEFAULT_DIR + "/" + fileName)); |
|
|
|
yaml.save(new File(DEFAULT_2_DIR + "/" + fileName)); |
|
Files.write(yaml.saveToString().getBytes(UTF8), new File(UTF8_2_DIR + "/" + fileName)); |
|
} |
|
|
|
@After |
|
public void compare() throws Throwable { |
|
PrintWriter out = new PrintWriter(new File(COMMON_DIR + "/" + fileName + EXT_LOG), UTF8); |
|
out.println(fileName); |
|
StringBuilder hexString = new StringBuilder(); |
|
|
|
for (String dir : DIRS) { |
|
hexString.delete(0, Integer.MAX_VALUE); |
|
for (byte b : Files.toByteArray(new File(dir + "/" + fileName))) { |
|
hexString.append(chars[(0xF0 & b) >> 4]).append(chars[0xF & b]).append(' '); |
|
} |
|
out.println(dir); |
|
out.println(hexString); |
|
} |
|
out.close(); |
|
} |
|
} |