1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.AbstractVfsTestCase; 19 import org.apache.commons.vfs.Capability; 20 import org.apache.commons.vfs.FileContent; 21 import org.apache.commons.vfs.FileObject; 22 import org.apache.commons.vfs.FileSystemException; 23 import org.apache.commons.vfs.FileType; 24 import org.apache.commons.vfs.impl.DefaultFileSystemManager; 25 import org.apache.commons.vfs.provider.AbstractFileSystem; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.InputStream ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.net.URLConnection ; 32 import java.util.Arrays ; 33 34 43 public abstract class AbstractProviderTestCase 44 extends AbstractVfsTestCase 45 { 46 private FileObject baseFolder; 47 private FileObject readFolder; 48 private FileObject writeFolder; 49 private DefaultFileSystemManager manager; 50 private Method method; 51 52 public static final String FILE1_CONTENT = "This is a test file."; 54 55 public static final String TEST_FILE_CONTENT = "A test file."; 57 58 61 public void setMethod(final Method method) 62 { 63 this.method = method; 64 } 65 66 69 public void setConfig(final DefaultFileSystemManager manager, 70 final FileObject baseFolder, 71 final FileObject readFolder, 72 final FileObject writeFolder) 73 { 74 this.manager = manager; 75 this.baseFolder = baseFolder; 76 this.readFolder = readFolder; 77 this.writeFolder = writeFolder; 78 } 79 80 83 protected DefaultFileSystemManager getManager() 84 { 85 return manager; 86 } 87 88 92 public FileObject getBaseFolder() 93 { 94 return baseFolder; 95 } 96 97 100 protected FileObject getReadFolder() 101 { 102 return readFolder; 103 } 104 105 108 protected FileObject getWriteFolder() 109 { 110 return writeFolder; 111 } 112 113 121 protected Capability[] getRequiredCaps() 122 { 123 return null; 124 } 125 126 134 protected void runTest() throws Throwable 135 { 136 final Capability[] caps = getRequiredCaps(); 138 if (caps != null) 139 { 140 for (int i = 0; i < caps.length; i++) 141 { 142 final Capability cap = caps[i]; 143 if (!readFolder.getFileSystem().hasCapability(cap)) 144 { 145 System.out.println("skipping " + getName() + " because fs does not have cap " + cap); 146 return; 147 } 148 } 149 } 150 151 if (method != null) 153 { 154 try 155 { 156 method.invoke(this, null); 157 } 158 catch (final InvocationTargetException e) 159 { 160 throw e.getTargetException(); 161 } 162 } 163 else 164 { 165 super.runTest(); 166 } 167 168 if (((AbstractFileSystem) readFolder.getFileSystem()).isOpen()) 169 { 170 String name = "unknown"; 171 if (method != null) 172 { 173 name = method.getName(); 174 } 175 176 throw new IllegalStateException (getClass().getName() + ": filesystem has open streams after: " + name); 177 } 178 } 179 180 186 protected void assertSameURLContent(final String expected, 187 final URLConnection connection) 188 throws Exception 189 { 190 final byte[] expectedBin = expected.getBytes("utf-8"); 192 193 assertEquals("same content length", expectedBin.length, connection.getContentLength()); 195 196 final InputStream instr = connection.getInputStream(); 198 final ByteArrayOutputStream outstr; 199 try 200 { 201 outstr = new ByteArrayOutputStream (); 202 final byte[] buffer = new byte[256]; 203 int nread = 0; 204 while (nread >= 0) 205 { 206 outstr.write(buffer, 0, nread); 207 nread = instr.read(buffer); 208 } 209 } 210 finally 211 { 212 instr.close(); 213 } 214 215 assertTrue("same binary content", Arrays.equals(expectedBin, outstr.toByteArray())); 217 } 218 219 225 protected void assertSameContent(final String expected, 226 final FileObject file) 227 throws Exception 228 { 229 assertTrue(file.exists()); 231 assertSame(FileType.FILE, file.getType()); 232 233 final byte[] expectedBin = expected.getBytes("utf-8"); 235 236 final FileContent content = file.getContent(); 238 assertEquals("same content length", expectedBin.length, content.getSize()); 239 240 final InputStream instr = content.getInputStream(); 242 final ByteArrayOutputStream outstr; 243 try 244 { 245 outstr = new ByteArrayOutputStream (expectedBin.length); 246 final byte[] buffer = new byte[256]; 247 int nread = 0; 248 while (nread >= 0) 249 { 250 outstr.write(buffer, 0, nread); 251 nread = instr.read(buffer); 252 } 253 } 254 finally 255 { 256 instr.close(); 257 } 258 259 assertTrue("same binary content", Arrays.equals(expectedBin, outstr.toByteArray())); 261 } 262 263 266 protected FileInfo buildExpectedStructure() throws FileSystemException 267 { 268 final FileInfo base = new FileInfo(getReadFolder().getName().getBaseName(), FileType.FOLDER); 270 base.addFile("file1.txt", FILE1_CONTENT); 271 base.addFile("file%25.txt", FILE1_CONTENT); 273 274 284 base.addFile("file space.txt", FILE1_CONTENT); 285 286 base.addFile("empty.txt", ""); 287 base.addFolder("emptydir"); 288 289 final FileInfo dir = base.addFolder("dir1"); 290 dir.addFile("file1.txt", TEST_FILE_CONTENT); 291 dir.addFile("file2.txt", TEST_FILE_CONTENT); 292 dir.addFile("file3.txt", TEST_FILE_CONTENT); 293 294 final FileInfo subdir1 = dir.addFolder("subdir1"); 295 subdir1.addFile("file1.txt", TEST_FILE_CONTENT); 296 subdir1.addFile("file2.txt", TEST_FILE_CONTENT); 297 subdir1.addFile("file3.txt", TEST_FILE_CONTENT); 298 299 final FileInfo subdir2 = dir.addFolder("subdir2"); 300 subdir2.addFile("file1.txt", TEST_FILE_CONTENT); 301 subdir2.addFile("file2.txt", TEST_FILE_CONTENT); 302 subdir2.addFile("file3.txt", TEST_FILE_CONTENT); 303 304 final FileInfo subdir3 = dir.addFolder("subdir3"); 305 subdir3.addFile("file1.txt", TEST_FILE_CONTENT); 306 subdir3.addFile("file2.txt", TEST_FILE_CONTENT); 307 subdir3.addFile("file3.txt", TEST_FILE_CONTENT); 308 309 return base; 310 } 311 } 312 | Popular Tags |