001: package com.ecyrd.jspwiki;
002:
003: import junit.framework.*;
004: import java.io.*;
005: import java.util.*;
006: import org.apache.log4j.*;
007:
008: public class FileUtilTest extends TestCase {
009: public FileUtilTest(String s) {
010: super (s);
011: Properties props = new Properties();
012: try {
013: props.load(TestEngine.findTestProperties());
014: PropertyConfigurator.configure(props);
015: } catch (IOException e) {
016: }
017: }
018:
019: public void setUp() throws Exception {
020: }
021:
022: public void tearDown() {
023: }
024:
025: /**
026: * This test actually checks if your JDK is misbehaving. On my own Debian
027: * machine, changing the system to use UTF-8 suddenly broke Java, and I put
028: * in this test to check for its brokenness. If your tests suddenly stop
029: * running, check if this one is failing too. If it is, your platform is
030: * broken. If it's not, seek for the bug in your code.
031: */
032: public void testJDKString() throws Exception {
033: String src = "abc\u00e4\u00e5\u00a6";
034:
035: String res = new String(src.getBytes("ISO-8859-1"),
036: "ISO-8859-1");
037:
038: assertEquals(src, res);
039: }
040:
041: public void testReadContentsLatin1() throws Exception {
042: String src = "abc\u00e4\u00e5\u00a6";
043:
044: String res = FileUtil.readContents(new ByteArrayInputStream(src
045: .getBytes("ISO-8859-1")), "ISO-8859-1");
046:
047: assertEquals(src, res);
048: }
049:
050: /**
051: * Check that fallbacks to ISO-Latin1 still work.
052: */
053: public void testReadContentsLatin1_2() throws Exception {
054: String src = "abc\u00e4\u00e5\u00a6def";
055:
056: String res = FileUtil.readContents(new ByteArrayInputStream(src
057: .getBytes("ISO-8859-1")), "UTF-8");
058:
059: assertEquals(src, res);
060: }
061:
062: /**
063: ISO Latin 1 from a pipe.
064:
065: FIXME: Works only on UNIX systems now.
066: */
067: public void testReadContentsFromPipe() throws Exception {
068: String src = "abc\n123456\n\nfoobar.\n";
069:
070: // Make a very long string.
071:
072: for (int i = 0; i < 10; i++) {
073: src += src;
074: }
075:
076: src += "\u00e4\u00e5\u00a6";
077:
078: File f = FileUtil.newTmpFile(src, "ISO-8859-1");
079:
080: String[] envp = {};
081:
082: try {
083: Process process = Runtime.getRuntime().exec(
084: "cat " + f.getAbsolutePath(), envp,
085: f.getParentFile());
086:
087: String result = FileUtil.readContents(process
088: .getInputStream(), "UTF-8");
089:
090: f.delete();
091:
092: assertEquals(src, result);
093: } catch (IOException e) {
094: }
095: }
096:
097: public void testReadContentsReader() throws IOException {
098: String data = "ABCDEF";
099:
100: String result = FileUtil.readContents(new StringReader(data));
101:
102: assertEquals(data, result);
103: }
104:
105: public static Test suite() {
106: return new TestSuite(FileUtilTest.class);
107: }
108: }
|