1 19 20 package org.openide.loaders; 21 22 import org.openide.filesystems.*; 23 import org.openide.filesystems.FileSystem; import org.openide.nodes.Node; 25 26 import java.beans.*; 27 import java.io.*; 28 import java.util.*; 29 30 import org.netbeans.junit.*; 31 32 36 public class DataFolderMoveTest extends LoggingTestCaseHid { 37 private org.openide.ErrorManager err; 38 39 40 41 public DataFolderMoveTest(String name) { 42 super (name); 43 } 44 45 46 protected void setUp() throws Exception { 47 clearWorkDir(); 48 err = org.openide.ErrorManager.getDefault().getInstance("TEST-" + getName()); 49 } 50 51 52 private static final int FS_WIDTH = 5; 53 private static final int FS_DEPTH = 4; 54 private static final int TXT_SIZE = 20000; 55 private static final int CYCLE = 4; 56 57 public void testMoveFolders() throws Exception { 58 59 String [] fsstruct = new String [FS_WIDTH * (FS_DEPTH + 1)]; 61 int index = 0; 62 StringBuffer nameBuf = new StringBuffer (); 63 for (int i = 0; i < FS_WIDTH; i++) { 64 nameBuf.setLength(0); 65 for (int j = 0; j < FS_DEPTH; j++) { 66 nameBuf.append("pack"); 67 nameBuf.append(i); 68 nameBuf.append(j); 69 nameBuf.append("/"); 70 fsstruct[index++] = nameBuf.toString(); 71 } 72 nameBuf.append("test.txt"); 73 fsstruct[index++] = nameBuf.toString(); 74 } 75 76 TestUtilHid.destroyLocalFileSystem(getName() + "A"); 78 TestUtilHid.destroyLocalFileSystem(getName() + "B"); 79 80 FileSystem fsA = TestUtilHid.createLocalFileSystem(new File (getWorkDir (), "A"), fsstruct); 81 FileSystem fsB = TestUtilHid.createLocalFileSystem(new File (getWorkDir (), "B"), new String [] {}); 82 83 for (int i = 0; i < fsstruct.length; i++) { 85 if (fsstruct[i].endsWith("test.txt")) { 86 FileObject obj = fsA.findResource(fsstruct[i]); 87 FileLock lock = obj.lock(); 88 OutputStream out = obj.getOutputStream(lock); 89 for (int j = 0; j < TXT_SIZE; j++) { 90 out.write('a'); 91 } 92 out.close(); 93 lock.releaseLock(); 94 } 95 } 96 97 final DataFolder[] roots = new DataFolder[] { 99 DataFolder.findFolder(fsA.findResource("")), 100 DataFolder.findFolder(fsB.findResource("")) 101 }; 102 103 final Node[] fsNodes = new Node[] { 105 roots[0].getNodeDelegate(), 106 roots[1].getNodeDelegate() 107 }; 108 109 try { 110 for (int k = 0; k < CYCLE; k++) { 111 final int src = (k % 2 == 0) ? 0 : 1; 112 final int dest = (src == 0) ? 1 : 0; 113 114 err.log("Copy cycle "+k+" (from "+src+" to "+dest+")"); 115 116 final boolean[] working = new boolean[] { true }; 117 118 Thread t = new Thread ("moving thread") { 120 public void run() { 121 try { 122 DataObject[] objects = roots[src].getChildren(); 123 for (int i = 0; i < objects.length; i++) { 124 objects[i].move(roots[dest]); 125 } 126 } 127 catch (Exception e) { 128 e.printStackTrace(); 129 } 130 finally { 131 working[0] = false; 132 } 133 } 134 }; 135 136 137 t.start(); 139 140 144 boolean failed = false; 145 while (working[0]) { 146 failed = testNodes(fsNodes[dest], false); 147 } 148 if (failed) { 149 try { 150 Thread.sleep(3000); 151 } 152 catch (InterruptedException ie) { 153 ie.printStackTrace(); 154 } 155 } 156 157 testNodes(fsNodes[dest], true); 158 } 159 } 160 finally { 161 TestUtilHid.destroyLocalFileSystem(getName() + "A"); 163 TestUtilHid.destroyLocalFileSystem(getName() + "B"); 164 } 165 } 166 167 private boolean testNodes(Node n, boolean callFail) { 168 boolean failed = false; 169 Node[] nodes = n.getChildren().getNodes(); 170 for (int j = 0; j < nodes.length; j++) { 171 DataObject dobj = (DataObject) nodes[j].getCookie(DataObject.class); 172 if (!dobj.isValid()) { 173 failed = true; 174 try { 175 Thread.sleep(500); 176 } 177 catch (InterruptedException ie) { 178 ie.printStackTrace(); 179 } 180 if (callFail) { 181 fail("Found Invalid Object: ["+j+"/"+nodes.length+"]: "+dobj+" / Node:"+nodes[j]); 182 } 183 } 184 } 185 return failed; 186 } 187 } 188 | Popular Tags |