| 1 | |
package org.webslinger.commons.vfs.tests; |
| 2 | |
|
| 3 | |
import java.io.EOFException; |
| 4 | |
import java.io.InputStream; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
|
| 7 | |
import junit.framework.Assert; |
| 8 | |
|
| 9 | |
import org.apache.commons.vfs.FileContent; |
| 10 | |
import org.apache.commons.vfs.FileObject; |
| 11 | |
import org.apache.commons.vfs.RandomAccessContent; |
| 12 | |
import org.apache.commons.vfs.util.RandomAccessMode; |
| 13 | |
|
| 14 | |
import org.webslinger.io.IOUtil; |
| 15 | |
|
| 16 | 7 | public class TestUtil extends Assert { |
| 17 | |
public static void checkContent(String name, FileObject file, String oldContent, String newContent, boolean which) throws Exception { |
| 18 | 233 | if (file.exists()) { |
| 19 | 117 | String contentToCheck = which ? newContent : oldContent; |
| 20 | 117 | assertEquals(name + " in content", contentToCheck, readString(file)); |
| 21 | |
if (false) { |
| 22 | |
RandomAccessContent rac = file.getContent().getRandomAccessContent(RandomAccessMode.READ); |
| 23 | |
StringBuilder sb = new StringBuilder(); |
| 24 | |
while (true) { |
| 25 | |
try { |
| 26 | |
sb.append(rac.readChar()); |
| 27 | |
} catch (EOFException e) { |
| 28 | |
break; |
| 29 | |
} |
| 30 | |
} |
| 31 | |
assertEquals(name + " rac content", contentToCheck, sb.toString()); |
| 32 | |
} |
| 33 | 117 | } else if (which) { |
| 34 | 0 | fail(name + " file doesn't exist, when content was written"); |
| 35 | |
} |
| 36 | 233 | } |
| 37 | |
|
| 38 | |
public static void writeString(FileObject file, String value) throws Exception { |
| 39 | 42 | FileContent content = file.getContent(); |
| 40 | 42 | assertFalse(file.getName() + " is not open", content.isOpen()); |
| 41 | 42 | OutputStream out = content.getOutputStream(); |
| 42 | 42 | assertTrue(file.getName() + " is open", content.isOpen()); |
| 43 | 42 | IOUtil.writeString(out, value); |
| 44 | 42 | assertFalse(file.getName() + " is not open", content.isOpen()); |
| 45 | 42 | } |
| 46 | |
|
| 47 | |
public static String readString(FileObject file) throws Exception { |
| 48 | 123 | FileContent content = file.getContent(); |
| 49 | 123 | assertFalse(file.getName() + " is not open", content.isOpen()); |
| 50 | 123 | InputStream in = content.getInputStream(); |
| 51 | 123 | assertTrue(file.getName() + " is open", content.isOpen()); |
| 52 | 123 | String value = IOUtil.readString(in); |
| 53 | 123 | assertFalse(file.getName() + " is not open", content.isOpen()); |
| 54 | 123 | return value; |
| 55 | |
} |
| 56 | |
} |