| 1 | |
package org.webslinger.commons.vfs.tests; |
| 2 | |
|
| 3 | |
import java.util.Collection; |
| 4 | |
|
| 5 | |
import org.apache.commons.vfs.FileObject; |
| 6 | |
import org.apache.commons.vfs.FileSystem; |
| 7 | |
|
| 8 | |
import org.webslinger.commons.vfs.operations.COWStateOperation; |
| 9 | |
import org.webslinger.commons.vfs.operations.SymlinkOperation; |
| 10 | |
|
| 11 | |
public class COWTester extends OperationTester<COWStateOperation> { |
| 12 | |
protected final String fsName; |
| 13 | |
protected final boolean isDirectory; |
| 14 | |
protected final COWFileMapping m1; |
| 15 | |
protected final COWFileMapping m2; |
| 16 | |
protected final COWFileMapping m3; |
| 17 | |
|
| 18 | |
public COWTester(FileObject root, String fsName, boolean isDirectory, COWFileMapping m1, COWFileMapping m2, COWFileMapping m3) throws Exception { |
| 19 | 4 | super(root, COWStateOperation.class); |
| 20 | 4 | this.fsName = fsName; |
| 21 | 4 | this.isDirectory = isDirectory; |
| 22 | 4 | this.m1 = m1; |
| 23 | 4 | this.m2 = m2; |
| 24 | 4 | this.m3 = m3; |
| 25 | 4 | TestUtil.writeString(root.resolveFile("layer1/" + m1.name), m1.name); |
| 26 | 4 | TestUtil.writeString(root.resolveFile("layer2/" + m2.name), m2.name); |
| 27 | 4 | TestUtil.writeString(root.resolveFile("layer3/" + m3.name), m3.name); |
| 28 | 4 | } |
| 29 | |
|
| 30 | |
protected void checkContent(FileObject base, boolean first, boolean second, boolean third) throws Exception { |
| 31 | 76 | TestUtil.checkContent("layer1", base.resolveFile(m1.name), m1.name, m1.name.toUpperCase(), first); |
| 32 | 76 | TestUtil.checkContent("layer2", base.resolveFile(m2.name), m2.name, m2.name.toUpperCase(), second); |
| 33 | 76 | TestUtil.checkContent("layer3", base.resolveFile(m3.name), m3.name, m3.name.toUpperCase(), third); |
| 34 | 76 | } |
| 35 | |
|
| 36 | |
protected void checkFiles(FileObject base, boolean first, boolean second, boolean third) throws Exception { |
| 37 | 76 | assertEquals("layer1 file existance", first, base.resolveFile(m1.name).exists()); |
| 38 | 76 | assertEquals("layer2 file existance", second, base.resolveFile(m2.name).exists()); |
| 39 | 76 | assertEquals("layer3 file existance", third, base.resolveFile(m3.name).exists()); |
| 40 | 76 | FileObject[] children = base.getChildren(); |
| 41 | 76 | assertNotNull("getChildren is not null", children); |
| 42 | 188 | for (FileObject child: children) { |
| 43 | 112 | String baseName = child.getName().getBaseName(); |
| 44 | 112 | if (baseName.equals(m1.name)) { |
| 45 | 40 | if (!first) fail("layer1 file in child list"); |
| 46 | 72 | } else if (baseName.equals(m2.name)) { |
| 47 | 32 | if (!second) fail("layer2 file in child list"); |
| 48 | 40 | } else if (baseName.equals(m3.name)) { |
| 49 | 40 | if (!third) fail("layer3 file in child list"); |
| 50 | |
} else { |
| 51 | 0 | fail("Unknown child in list(" + baseName + ")"); |
| 52 | |
} |
| 53 | |
} |
| 54 | 76 | assertEquals("layer1 getChild", first, base.getChild(m1.name) != null); |
| 55 | 76 | assertEquals("layer2 getChild", second, base.getChild(m2.name) != null); |
| 56 | 76 | assertEquals("layer3 getChild", third, base.getChild(m3.name) != null); |
| 57 | 76 | assertNotNull(m1.name + " url", base.resolveFile(m1.name).getURL()); |
| 58 | 76 | assertNotNull(m2.name + " url", base.resolveFile(m2.name).getURL()); |
| 59 | 76 | assertNotNull(m3.name + " url", base.resolveFile(m3.name).getURL()); |
| 60 | 76 | } |
| 61 | |
|
| 62 | |
protected void checkBases(FileSystem fs, boolean first, boolean second, boolean third) throws Exception { |
| 63 | 24 | if (isDirectory) { |
| 64 | 12 | Collection<String> bases = getOperation(fs, m1.viewPath).getBases(); |
| 65 | 12 | int requiredCount = 0; |
| 66 | 12 | requiredCount += checkBases("layer1", bases, m1.cowPath, first); |
| 67 | 12 | requiredCount += checkBases("layer2", bases, m2.cowPath, second); |
| 68 | 12 | requiredCount += checkBases("layer3", bases, m3.cowPath, third); |
| 69 | 12 | assertEquals("bases count", requiredCount, bases.size()); |
| 70 | 12 | } else { |
| 71 | 12 | checkBases("layer1", getOperation(fs, m1.viewPath).getBases(), m1.cowPath, first); |
| 72 | 12 | checkBases("layer2", getOperation(fs, m2.viewPath).getBases(), m2.cowPath, second); |
| 73 | 12 | checkBases("layer3", getOperation(fs, m3.viewPath).getBases(), m3.cowPath, third); |
| 74 | |
} |
| 75 | 24 | } |
| 76 | |
|
| 77 | |
protected int checkBases(String label, Collection<String> bases, String allowedBase, boolean shouldHaveIt) { |
| 78 | 72 | if (shouldHaveIt) { |
| 79 | 12 | assertTrue(label + " bases contains", bases.contains(allowedBase)); |
| 80 | 12 | if (isDirectory) return 1; |
| 81 | 6 | assertEquals(label + " base count", 1, bases.size()); |
| 82 | |
} else { |
| 83 | 60 | assertFalse(label + " bases contains", bases.contains(allowedBase)); |
| 84 | 60 | if (!isDirectory) assertEquals(label + " base count", 0, bases.size()); |
| 85 | |
} |
| 86 | 66 | return 0; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public void doTest() throws Exception { |
| 90 | 4 | FileSystem cfs = root.getFileSystem().getFileSystemManager().createFileSystem(fsName, root).getFileSystem(); |
| 91 | 4 | FileObject view = cfs.getRoot().resolveFile("view"); |
| 92 | 4 | checkFiles(view, false, false, false); |
| 93 | 4 | checkContent(view, false, false, false); |
| 94 | |
|
| 95 | 4 | getOperation(cfs, m1.viewPath).addBase(m1.cowPath); |
| 96 | 4 | tree(view); |
| 97 | 4 | checkBases(cfs, true, false, false); |
| 98 | 4 | checkFiles(view, true, false, false); |
| 99 | 4 | checkContent(view, false, false, false); |
| 100 | 4 | getOperation(cfs, m1.viewPath).removeBase(m1.cowPath); |
| 101 | 4 | checkBases(cfs, false, false, false); |
| 102 | 4 | checkFiles(view, false, false, false); |
| 103 | 4 | checkContent(view, false, false, false); |
| 104 | |
|
| 105 | 4 | getOperation(cfs, m2.viewPath).addBase(m2.cowPath); |
| 106 | 4 | checkBases(cfs, false, true, false); |
| 107 | 4 | checkFiles(view, false, true, false); |
| 108 | 4 | checkContent(view, false, false, false); |
| 109 | 4 | getOperation(cfs, m2.viewPath).removeBase(m2.cowPath); |
| 110 | 4 | checkBases(cfs, false, false, false); |
| 111 | 4 | checkFiles(view, false, false, false); |
| 112 | 4 | checkContent(view, false, false, false); |
| 113 | |
|
| 114 | 4 | getOperation(cfs, m3.viewPath).addBase(m3.cowPath); |
| 115 | 4 | checkBases(cfs, false, false, true); |
| 116 | 4 | checkFiles(view, false, false, true); |
| 117 | 4 | checkContent(view, false, false, false); |
| 118 | 4 | getOperation(cfs, m3.viewPath).removeBase(m3.cowPath); |
| 119 | 4 | checkBases(cfs, false, false, false); |
| 120 | 4 | checkFiles(view, false, false, false); |
| 121 | 4 | checkContent(view, false, false, false); |
| 122 | |
|
| 123 | 4 | getOperation(cfs, m1.viewPath).addBase(m1.cowPath); |
| 124 | 4 | getOperation(cfs, m2.viewPath).addBase(m2.cowPath); |
| 125 | 4 | getOperation(cfs, m3.viewPath).addBase(m3.cowPath); |
| 126 | 4 | checkFiles(view, true, true, true); |
| 127 | 4 | checkContent(view, false, false, false); |
| 128 | 4 | TestUtil.writeString(view.resolveFile(m1.name), m1.name.toUpperCase()); |
| 129 | 4 | TestUtil.writeString(view.resolveFile(m2.name), m2.name.toUpperCase()); |
| 130 | 4 | TestUtil.writeString(view.resolveFile(m3.name), m3.name.toUpperCase()); |
| 131 | 4 | checkFiles(view, true, true, true); |
| 132 | 4 | checkContent(view, true, true, true); |
| 133 | |
|
| 134 | 4 | view.resolveFile(m2.name).delete(); |
| 135 | 4 | checkFiles(view, true, false, true); |
| 136 | 4 | checkContent(view, true, false, true); |
| 137 | |
|
| 138 | 4 | view.resolveFile(m3.name).delete(); |
| 139 | 4 | checkFiles(view, true, false, false); |
| 140 | 4 | checkContent(view, true, false, false); |
| 141 | |
|
| 142 | 4 | TestUtil.writeString(view.resolveFile(m2.name), m2.name.toUpperCase()); |
| 143 | 4 | checkFiles(view, true, true, false); |
| 144 | 4 | checkContent(view, true, true, false); |
| 145 | |
|
| 146 | 4 | view.resolveFile(m1.name).delete(); |
| 147 | 4 | checkFiles(view, false, true, false); |
| 148 | 4 | checkContent(view, false, true, false); |
| 149 | |
|
| 150 | 4 | TestUtil.writeString(view.resolveFile(m3.name), m3.name.toUpperCase()); |
| 151 | 4 | checkFiles(view, false, true, true); |
| 152 | 4 | checkContent(view, false, true, true); |
| 153 | |
|
| 154 | 4 | view.resolveFile(m2.name).delete(); |
| 155 | 4 | checkFiles(view, false, false, true); |
| 156 | 4 | checkContent(view, false, false, true); |
| 157 | |
|
| 158 | 4 | TestUtil.writeString(view.resolveFile(m1.name), m1.name.toUpperCase()); |
| 159 | 4 | checkFiles(view, true, false, true); |
| 160 | 4 | checkContent(view, true, false, true); |
| 161 | |
|
| 162 | 4 | TestUtil.writeString(view.resolveFile(m2.name), m2.name.toUpperCase()); |
| 163 | 4 | checkFiles(view, true, true, true); |
| 164 | 4 | checkContent(view, true, true, true); |
| 165 | |
|
| 166 | 4 | getOperation(cfs, m1.viewPath).clear(); |
| 167 | 4 | getOperation(cfs, m2.viewPath).clear(); |
| 168 | 4 | getOperation(cfs, m3.viewPath).clear(); |
| 169 | 4 | checkFiles(view, true, true, true); |
| 170 | 4 | checkContent(view, true, true, true); |
| 171 | |
|
| 172 | 4 | root.resolveFile("view/" + m2.name).delete(); |
| 173 | 4 | view.refresh(); |
| 174 | 4 | view.resolveFile(m2.name).refresh(); |
| 175 | 4 | checkFiles(view, true, false, true); |
| 176 | 4 | checkContent(view, true, false, true); |
| 177 | |
|
| 178 | 4 | SymlinkOperation symOp = getOperation(cfs, "view/links/" + m1.name, SymlinkOperation.class); |
| 179 | 4 | if (symOp != null) { |
| 180 | 2 | symOp.setSymlink("../" + m1.name); |
| 181 | 2 | assertEquals("symink content(" + m1.name + ")", m1.name.toUpperCase(), TestUtil.readString(view.resolveFile("links/" + m1.name))); |
| 182 | |
} |
| 183 | |
|
| 184 | 4 | tree(view); |
| 185 | 4 | } |
| 186 | |
} |
| 187 | |
|