KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataFolderMoveTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import org.openide.filesystems.*;
23 import org.openide.filesystems.FileSystem; // override java.io.FileSystem
24
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 /** Test of folders move. Originally written for testing #8705.
33  *
34  * @author Petr Hamernik
35  */

36 public class DataFolderMoveTest extends LoggingTestCaseHid {
37     private org.openide.ErrorManager err;
38
39
40     /** Creates new DataFolderTest */
41     public DataFolderMoveTest(String JavaDoc name) {
42         super (name);
43     }
44
45     
46     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
58         
59         // create directory structur description
60
String JavaDoc[] fsstruct = new String JavaDoc[FS_WIDTH * (FS_DEPTH + 1)];
61         int index = 0;
62         StringBuffer JavaDoc nameBuf = new StringBuffer JavaDoc();
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         // clean and create new filesystems
77
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 JavaDoc[] {});
82
83         // create directory structure
84
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         // data folders - roots
98
final DataFolder[] roots = new DataFolder[] {
99             DataFolder.findFolder(fsA.findResource("")),
100             DataFolder.findFolder(fsB.findResource(""))
101         };
102
103         // node delegates of roots
104
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 moving whole directory structure from src to dest FS
119
Thread JavaDoc t = new Thread JavaDoc("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 JavaDoc e) {
128                             e.printStackTrace();
129                         }
130                         finally {
131                             working[0] = false;
132                         }
133                     }
134                 };
135
136                 
137                 // moving started
138
t.start();
139
140                 // during moving try to obtain children nodes
141
// child node is created for temporary DataFolder;
142
// When moving is complete, datafolder of this node is not valid.
143

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 JavaDoc ie) {
153                         ie.printStackTrace();
154                     }
155                 }
156                 
157                 testNodes(fsNodes[dest], true);
158             }
159         }
160         finally {
161             // clean
162
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 JavaDoc 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