/* * Created on Jun 2, 2165 */ package textfile; import java.io.IOException; import MDUtils.TestFiles; import junit.framework.TestCase; /** * @author dickey */ public class FileOrURLInputStreamTest extends TestCase { /** * Constructor for FileOrULRInputStreamTest. * @param name */ public FileOrURLInputStreamTest(String name) { super(name); } /** To run this test, make sure first there IS a testdir directory. * Make sure there is no "bad" file anywhere, and make sure there * is the "good" file in the testdir. * * */ public static void testFindInCurrDir() { System.out.println("Start current dir test"); String currDirProp = "user.dir"; String testDir = "C:\\jtestdir"; String currDir; currDir = System.getProperty(currDirProp); assert currDir != null; currDir = System.setProperty(currDirProp, testDir); assert currDir != null; String checkDir = System.getProperty(currDirProp); assert checkDir.equals(testDir): currDir + " " + checkDir; String bad_tfilename = "nosuchx.txt"; System.out.println("For testing, CANCEL the next file choice."); { FileOrURLInputStream tf = new FileOrURLInputStream(bad_tfilename); checkNonExistentFile(tf); } String good_tfilename = "README.txt"; FileOrURLInputStream tf2 = new FileOrURLInputStream(good_tfilename); checkGoodNonEmptyFile(tf2); String emptyFile = "validButEmptyFile.txt"; FileOrURLInputStream tf3 = new FileOrURLInputStream(emptyFile); checkValidButEmptyFile(tf3); System.setProperty(currDirProp, currDir); System.out.println("End current dir test " + currDir + " " + testDir); } public static void testBadFiles() { tryBadFileList(new String[] { TestFiles.badGIFfileTwoSlashURL2 }); try { new FileOrURLInputStream(null); assert false: "missed param exception"; } catch (IllegalArgumentException e) { System.out.println("Caught expected exception"); } try { new FileOrURLInputStream(""); assert false: "missed param exception"; } catch (IllegalArgumentException e) { System.out.println("Caught expected exception"); } } public static void testGoodFileORURLInputStream() { String [] testArray = TestFiles.goodLocalPicfileNames; tryFileList(testArray); tryFileList(TestFiles.goodImageFileURLs); tryFileList(TestFiles.goodLocalHostURLs); tryFileList(TestFiles.goodFileUrls); tryFileList(TestFiles.goodLocalPicURLs); tryFileList(TestFiles.goodPaths); tryFileList(TestFiles.miscHardImageURLS); tryFileList(TestFiles.mixedGoodUrls); tryFileList(TestFiles.remoteGoodURLs); } public static void testEmptyFileORURLInputStream() { //tryFileList(TestFiles.zeroBytesGraphics); } public static void testBadWithPrompt() { String fname = "xxxxx"; System.out.println("Reply to file chooser box with CANCEL"); FileOrURLInputStream fs; fs = new FileOrURLInputStream(fname, FileFinderOption.PROMPT_FOR_FILE); checkNonExistentFile(fs); System.out.println("Reply to file chooser box by choosing a good file"); fs = new FileOrURLInputStream(fname, FileFinderOption.PROMPT_FOR_FILE); checkGoodNonEmptyFile(fs); } private static void tryFileList(String[] testArray) { for (int f = 0; f < testArray.length; f++) { String fname = testArray[f]; FileOrURLInputStream fs = new FileOrURLInputStream(fname); String id = fs.getFileID(); assert id != null: fname; System.out.println(fs); checkGoodNonEmptyFile(fs); } } private static void tryBadFileList(String[] testArray) { for (int f = 0; f < testArray.length; f++) { String fname = testArray[f]; FileOrURLInputStream fs = new FileOrURLInputStream(fname); String id = fs.getFileID(); assert id == null: fname; System.out.println(fs); checkNonExistentFile(fs); } } private static void checkGoodNonEmptyFile(FileOrURLInputStream tf2) { try { int aByte = tf2.read(); assert aByte != -1; System.out.println("Found: " + tf2); assert tf2.available() > 0; //unsure about this one if (tf2.markSupported()) { tf2.mark(1); //have to mark before trying reset tf2.reset(); } assert tf2.skip(0) == 0; assert tf2.skip(1) == 1; assert tf2.read(new byte[10]) == 10; assert tf2.skip(1000000) > 0; long skipCount; skipCount = tf2.skip(-71); //Bug in FileInputStream? Wrong answer when at end of file. //assert skipCount == 0: skipCount; assert tf2.equals(tf2); tf2.close(); } catch (IOException e) { assert false: e; } } private static void checkValidButEmptyFile(FileOrURLInputStream tf2) { try { assert tf2.getFileID() != null; int aByte = tf2.read(); assert aByte == -1; System.out.println("Found: " + tf2.getFileID()); assert tf2.available() == 0; if (tf2.markSupported()) { tf2.reset(); } assert tf2.skip(0) == 0; long sCount = tf2.skip(1); assert sCount == 1: sCount; //WHY??????????? long rCount = tf2.read(new byte[10]); assert rCount == -1: rCount; sCount = tf2.skip(1); assert sCount == 1: sCount; //WHY??????????? sCount = tf2.skip(2); assert sCount == 2: sCount; //WHY??????????? assert tf2.equals(tf2); tf2.close(); } catch (IOException e) { assert false: e; } } private static void checkNonExistentFile(FileOrURLInputStream tf) { int nextByte = 999; try { assert tf.getFileID() == null; nextByte = tf.read(); assert nextByte == -1; //at end of file assert tf.available() == 0; assert tf.markSupported() == false; tf.reset(); assert tf.skip(1) == 0; assert tf.read(new byte[100]) == -1; assert tf.equals(tf); tf.close(); } catch (IOException e) { assert false: e; } } }