| 1 | |
package org.webslinger.commons.vfs.flat.tests; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.util.Arrays; |
| 5 | |
import java.util.HashSet; |
| 6 | |
|
| 7 | |
import org.apache.commons.vfs.FileContent; |
| 8 | |
import org.apache.commons.vfs.FileObject; |
| 9 | |
|
| 10 | |
import org.webslinger.commons.vfs.tests.CreationDeletionHelper; |
| 11 | |
import org.webslinger.commons.vfs.tests.Tester; |
| 12 | |
import org.webslinger.io.IOUtil; |
| 13 | |
|
| 14 | |
public class CreationDeletionTest extends Tester { |
| 15 | |
protected final File base; |
| 16 | |
|
| 17 | |
public CreationDeletionTest(FileObject root, File base) { |
| 18 | 2 | super(root); |
| 19 | 2 | this.base = base; |
| 20 | 2 | } |
| 21 | |
|
| 22 | |
public void doTest(CreationDeletionHelper helper) throws Exception { |
| 23 | |
|
| 24 | 2 | FileObject realVFS = root.resolveFile("real"); |
| 25 | 2 | FileContent realContent = realVFS.getContent(); |
| 26 | 2 | File realReal = new File(base, "real"); |
| 27 | 2 | assertFalse("real(vfs) does not exist", realVFS.exists()); |
| 28 | 2 | assertFalse("real(real) does not exist", realReal.exists()); |
| 29 | 2 | helper.create(realReal); |
| 30 | 2 | realVFS.close(); |
| 31 | 2 | assertTrue("real(vfs) exists", realVFS.exists()); |
| 32 | |
|
| 33 | 2 | IOUtil.touch(realReal, 1000); |
| 34 | 2 | assertEquals("real(vfs) last modified time", 1000, realVFS.getContent().getLastModifiedTime()); |
| 35 | 2 | IOUtil.touch(realReal, 10000); |
| 36 | 2 | assertEquals("real(vfs) last modified time", 10000, realVFS.getContent().getLastModifiedTime()); |
| 37 | |
|
| 38 | |
|
| 39 | 2 | assertTrue("real(vfs) exists", realVFS.exists()); |
| 40 | 2 | assertTrue("real(real) exists", realReal.exists()); |
| 41 | 2 | IOUtil.delete(realReal); |
| 42 | 2 | realVFS.close(); |
| 43 | 2 | assertFalse("real(vfs) does not exist", realVFS.exists()); |
| 44 | 2 | assertFalse("real(real) does not exist", realReal.exists()); |
| 45 | |
|
| 46 | |
|
| 47 | 2 | FileObject vfsVFS = root.resolveFile("vfs"); |
| 48 | 2 | FileContent vfsContent = vfsVFS.getContent(); |
| 49 | 2 | File vfsReal = new File(base, "vfs"); |
| 50 | 2 | assertFalse("vfs(vfs) does not exist", vfsVFS.exists()); |
| 51 | 2 | assertFalse("vfs(real) does not exist", vfsReal.exists()); |
| 52 | 2 | helper.create(vfsVFS); |
| 53 | 2 | assertTrue("one(real) exists", vfsReal.exists()); |
| 54 | |
|
| 55 | 2 | vfsContent.setLastModifiedTime(1000); |
| 56 | 2 | assertEquals("vfs(vfs) last modified time", 1000, vfsReal.lastModified()); |
| 57 | 2 | vfsContent.setLastModifiedTime(10000); |
| 58 | 2 | assertEquals("vfs(vfs) last modified time", 10000, vfsReal.lastModified()); |
| 59 | |
|
| 60 | 2 | assertFalse("vfs(vfs)@first does not exist", vfsContent.attributeExists("first")); |
| 61 | 2 | assertFalse("vfs(vfs)@second does not exist", vfsContent.attributeExists("second")); |
| 62 | 2 | assertFalse("vfs(vfs)@third does not exist", vfsContent.attributeExists("third")); |
| 63 | 2 | HashSet<String> attributeNames = new HashSet<String>(Arrays.asList(vfsContent.getAttributeNames())); |
| 64 | 2 | assertEquals("vfs no attribute names", 0, attributeNames.size()); |
| 65 | 2 | vfsContent.setAttribute("first", "First's value"); |
| 66 | 2 | vfsContent.setAttribute("second", "The value for second"); |
| 67 | 2 | vfsContent.setAttribute("third", "Value for the third"); |
| 68 | 2 | assertTrue("vfs(vfs)@first file exists", TestUtil.getAttributeFile(vfsReal, "first").exists()); |
| 69 | 2 | assertTrue("vfs(vfs)@second file exists", TestUtil.getAttributeFile(vfsReal, "second").exists()); |
| 70 | 2 | assertTrue("vfs(vfs)@third file exists", TestUtil.getAttributeFile(vfsReal, "third").exists()); |
| 71 | 2 | assertTrue("vfs(vfs)@first exists", vfsContent.attributeExists("first")); |
| 72 | 2 | assertTrue("vfs(vfs)@second exists", vfsContent.attributeExists("second")); |
| 73 | 2 | assertTrue("vfs(vfs)@third exists", vfsContent.attributeExists("third")); |
| 74 | 2 | attributeNames.clear(); |
| 75 | 2 | attributeNames.addAll(Arrays.asList(vfsContent.getAttributeNames())); |
| 76 | 2 | assertEquals("vfs 3 attribute names", 3, attributeNames.size()); |
| 77 | 2 | assertTrue("vfs attribute name first", attributeNames.contains("first")); |
| 78 | 2 | assertTrue("vfs attribute name second", attributeNames.contains("second")); |
| 79 | 2 | assertTrue("vfs attribute name third", attributeNames.contains("third")); |
| 80 | |
|
| 81 | |
|
| 82 | 2 | assertTrue("vfs(vfs) exists", vfsVFS.exists()); |
| 83 | 2 | assertTrue("vfs(real) exists", vfsReal.exists()); |
| 84 | 2 | vfsVFS.delete(); |
| 85 | 2 | assertFalse("vfs(vfs) does not exist", vfsVFS.exists()); |
| 86 | 2 | assertFalse("vfs(real) does not exist", vfsReal.exists()); |
| 87 | 2 | assertFalse("vfs(vfs)@first does not exists", TestUtil.getAttributeFile(vfsReal, "first").exists()); |
| 88 | 2 | assertFalse("vfs(vfs)@second does not exists", TestUtil.getAttributeFile(vfsReal, "second").exists()); |
| 89 | 2 | assertFalse("vfs(vfs)@third does not exists", TestUtil.getAttributeFile(vfsReal, "third").exists()); |
| 90 | |
|
| 91 | 2 | helper.create(vfsVFS); |
| 92 | 2 | vfsContent.setAttribute("first", "First's value"); |
| 93 | 2 | vfsContent.setAttribute("second", "The value for second"); |
| 94 | 2 | vfsContent.setAttribute("third", "Value for the third"); |
| 95 | 2 | vfsVFS.moveTo(root.resolveFile("vfs-renamed")); |
| 96 | 2 | assertFalse("vfs(real)@ does not exist", TestUtil.getAttributeFile(vfsReal).exists()); |
| 97 | 2 | assertFalse("vfs(real) does not exist", vfsReal.exists()); |
| 98 | 2 | assertTrue("vfs-renamed exists", new File(base, "vfs-renamed").exists()); |
| 99 | 2 | assertTrue("vfs-renamed@ exists", TestUtil.getAttributeFile(new File(base, "vfs-renamed")).exists()); |
| 100 | |
|
| 101 | 2 | vfsVFS.delete(); |
| 102 | 2 | assertFalse("vfs(real) does not exist", vfsReal.exists()); |
| 103 | 2 | assertFalse("vfs(vfs)@first does not exists", TestUtil.getAttributeFile(vfsReal, "first").exists()); |
| 104 | 2 | assertFalse("vfs(vfs)@second does not exists", TestUtil.getAttributeFile(vfsReal, "second").exists()); |
| 105 | 2 | assertFalse("vfs(vfs)@third does not exists", TestUtil.getAttributeFile(vfsReal, "third").exists()); |
| 106 | 2 | helper.create(vfsVFS); |
| 107 | 2 | vfsContent.setAttribute("first", "First's value"); |
| 108 | 2 | vfsContent.setAttribute("second", "The value for second"); |
| 109 | 2 | vfsContent.setAttribute("third", "Value for the third"); |
| 110 | 2 | assertTrue("vfs(vfs)@first exists", vfsContent.attributeExists("first")); |
| 111 | 2 | assertTrue("vfs(vfs)@first file exists", TestUtil.getAttributeFile(vfsReal, "first").exists()); |
| 112 | 2 | assertTrue("vfs(vfs)@second exists", vfsContent.attributeExists("second")); |
| 113 | 2 | assertTrue("vfs(vfs)@second file exists", TestUtil.getAttributeFile(vfsReal, "second").exists()); |
| 114 | 2 | assertTrue("vfs(vfs)@third exists", vfsContent.attributeExists("third")); |
| 115 | 2 | assertTrue("vfs(vfs)@third file exists", TestUtil.getAttributeFile(vfsReal, "third").exists()); |
| 116 | 2 | vfsContent.removeAttribute("second"); |
| 117 | 2 | assertTrue("vfs(vfs)@first exists", vfsContent.attributeExists("first")); |
| 118 | 2 | assertTrue("vfs(vfs)@first file exists", TestUtil.getAttributeFile(vfsReal, "first").exists()); |
| 119 | 2 | assertFalse("vfs(vfs)@second does not exist", vfsContent.attributeExists("second")); |
| 120 | 2 | assertFalse("vfs(vfs)@second file does not exist", TestUtil.getAttributeFile(vfsReal, "second").exists()); |
| 121 | 2 | assertTrue("vfs(vfs)@third exists", vfsContent.attributeExists("third")); |
| 122 | 2 | assertTrue("vfs(vfs)@third file exists", TestUtil.getAttributeFile(vfsReal, "third").exists()); |
| 123 | 2 | attributeNames.clear(); |
| 124 | 2 | attributeNames.addAll(Arrays.asList(vfsContent.getAttributeNames())); |
| 125 | 2 | assertEquals("vfs 2 attribute names", 2, attributeNames.size()); |
| 126 | 2 | assertTrue("vfs attribute name first", attributeNames.contains("first")); |
| 127 | 2 | assertFalse("vfs attribute name second", attributeNames.contains("second")); |
| 128 | 2 | assertTrue("vfs attribute name third", attributeNames.contains("third")); |
| 129 | 2 | vfsContent.setAttribute("third", null); |
| 130 | 2 | assertTrue("vfs(vfs)@first exists", vfsContent.attributeExists("first")); |
| 131 | 2 | assertTrue("vfs(vfs)@first file exists", TestUtil.getAttributeFile(vfsReal, "first").exists()); |
| 132 | 2 | assertFalse("vfs(vfs)@second does not exist", vfsContent.attributeExists("second")); |
| 133 | 2 | assertFalse("vfs(vfs)@second file does not exist", TestUtil.getAttributeFile(vfsReal, "second").exists()); |
| 134 | 2 | assertFalse("vfs(vfs)@third does not exist", vfsContent.attributeExists("third")); |
| 135 | 2 | assertFalse("vfs(vfs)@third file does not exist", TestUtil.getAttributeFile(vfsReal, "third").exists()); |
| 136 | 2 | attributeNames.clear(); |
| 137 | 2 | attributeNames.addAll(Arrays.asList(vfsContent.getAttributeNames())); |
| 138 | 2 | assertEquals("vfs 1 attribute names", 1, attributeNames.size()); |
| 139 | 2 | assertTrue("vfs attribute name first", attributeNames.contains("first")); |
| 140 | 2 | assertFalse("vfs attribute name second", attributeNames.contains("second")); |
| 141 | 2 | assertFalse("vfs attribute name third", attributeNames.contains("third")); |
| 142 | 2 | } |
| 143 | |
} |