001: /*
002: * FSTest.java
003: *
004: * Created on July 15, 2002, 3:59 PM
005: */
006:
007: package FileSystemTest;
008:
009: import java.beans.PropertyVetoException;
010: import java.io.*;
011: import java.util.Iterator;
012: import java.util.Set;
013:
014: import org.openide.nodes.Children;
015: import org.openide.nodes.Node;
016: import org.openide.windows.Mode;
017: import org.openide.windows.TopComponent;
018: import org.openide.windows.WindowManager;
019: import org.openide.windows.Workspace;
020: import org.openide.filesystems.*;
021: import org.openide.loaders.DataObject;
022: import org.openide.loaders.DataObjectNotFoundException;
023: import org.openide.loaders.DataFilter;
024: import org.openide.loaders.RepositoryNodeFactory;
025:
026: import junit.framework.*;
027: import org.netbeans.junit.*;
028: import org.openide.util.Lookup;
029:
030: /**
031: *
032: * @author pz97949
033: */
034: public class FSTest extends NbTestCase {
035: File testDir;
036: LocalFileSystem testFS;
037: String TEST_FILE_OBJECT_NAME = "testedfile.java";
038:
039: static class FailException extends Exception {
040: FailException() {
041: super ();
042: }
043:
044: FailException(String msg) {
045: super (msg);
046: }
047: }
048:
049: static class ErrorException extends Exception {
050: ErrorException() {
051: super ();
052: }
053:
054: ErrorException(String name) {
055: super (name);
056: }
057: }
058:
059: /** Creates a new instance of FSTest */
060: public FSTest(String testName) {
061: super (testName);
062: }
063:
064: /**
065: * @param args the command line arguments
066: */
067: public static void main(String[] args) {
068: junit.textui.TestRunner.run(suite());
069: }
070:
071: /**This suite*/
072: public static Test suite() {
073: NbTestSuite suite = new NbTestSuite(FSTest.class);
074: return suite;
075: }
076:
077: /** Test if filesystem is hiden or shown in repository
078: * (test is done through nodes of explorer)
079: */
080: public void testHide() throws IOException {
081: Node node = null;
082: FileObject fo = null;
083: FileSystem fs = null;
084: try {
085: try {
086: // TopManager tm = TopManager.getDefault();
087:
088: // get node of dataObject
089:
090: fo = getRefFO();
091: fs = fo.getFileSystem();
092: fs.setHidden(false);
093: System.out.println(fs);
094: failTest("file system is hidden",
095: fs.isHidden() == false);
096: DataObject dobj = DataObject.find(fo);
097: node = dobj.getNodeDelegate();
098: } catch (DataObjectNotFoundException donfe) {
099: failTest(donfe);
100: } catch (FileStateInvalidException fsie) {
101: failTest(fsie);
102: }
103: Node repNode = RepositoryNodeFactory.getDefault()
104: .repository(DataFilter.ALL);
105: fs.setHidden(false);
106: // System.out.println("status:" + ((fs.isHidden()) ? "hidden" : "shown"));
107: if (findNode(repNode, node, 2) == false) {
108: failTest("filesystem is hidden (must be shown)", false);
109: }
110: fs.setHidden(true);
111: if (findNode(repNode, node, 2) == true) {
112: failTest("filesystem is shown (must be hidden)", false);
113: }
114: fs.setHidden(false);
115: if (findNode(repNode, node, 2) == false) {
116: failTest("filesystem is hidden (must be shown)", false);
117: }
118: } catch (FailException fe) {
119: System.out.println("failed");
120: }
121: System.out.println("passed");
122: }
123:
124: /** @return unique fileobject in tested filesystem
125: */
126: protected FileObject getRefFO() throws java.io.IOException {
127: Repository rep = Repository.getDefault();
128: FileObject fo = rep.findResource(TEST_FILE_OBJECT_NAME);
129: if (fo == null) {
130: fo = testFS.getRoot().createData(TEST_FILE_OBJECT_NAME);
131: }
132: return fo;
133: }
134:
135: /**
136: *
137: */
138: private void failTest(Exception e) throws FailException {
139: e.printStackTrace();
140: throw new FailException();
141:
142: }
143:
144: private void failTest(String str, boolean status)
145: throws FailException {
146: if (status == false) {
147: System.out.println(str);
148: throw new FailException();
149: }
150:
151: }
152:
153: private boolean findNode(Node parent, Node child, int depth) {
154: if (parent.getName().equals(child.getName())) {
155: return true;
156: }
157: if (depth == 0) {
158: return false;
159: }
160: Children children = parent.getChildren();
161: Node childNodes[] = children.getNodes();
162: for (int i = 0; i < childNodes.length; i++) {
163: if (childNodes[i] == child
164: || findNode(childNodes[i], child, depth - 1)) {
165: return true;
166: }
167: // System.out.println(childNodes[i].getName());
168: }
169: return false;
170: }
171:
172: private void sleepCurThread(int milis) {
173: try {
174: Thread.currentThread().sleep(milis);
175: } catch (InterruptedException exception) {
176: exception.printStackTrace();
177: }
178: }
179:
180: /**
181: * test if filesystem is readonly
182: */
183: public void testReadOnLocalFS() throws IOException {
184: try {
185: FileObject fo = getRefFO();
186: FileSystem fs = null;
187: try {
188: fs = fo.getFileSystem();
189: } catch (FileStateInvalidException fse) {
190: fse.printStackTrace();
191: throw new FailException("fo.getFileSystem() failed");
192: }
193: if (!(fs instanceof LocalFileSystem)) {
194: throw new ErrorException(
195: "tested filesystem is not instance of LocalFileSystem");
196: }
197: LocalFileSystem lfs = (LocalFileSystem) fs;
198: boolean tmpROValue = lfs.isReadOnly();
199: // test read only
200: lfs.setReadOnly(true);
201: FileObject root = fs.getRoot();
202:
203: // test create file object
204: try {
205: root.createData("MyNewFile.txt");
206: throw new FailException(
207: "Create FileObject on read only filesystem failed.");
208: } catch (IOException ioe) {
209: System.out
210: .println("Create FileObject on read only filesystem file passed.");
211: }
212: //test create folder
213: try {
214: root.createFolder("MyNewFolder");
215: throw new FailException(
216: "Create folder on read only filesytem failed.");
217: } catch (IOException ioe) {
218: System.out
219: .println("Create folder on read only filesystem passed");
220: }
221:
222: // tet write data into file object
223: FileLock lock = null;
224: try {
225:
226: lock = fo.lock();
227: OutputStream os = fo.getOutputStream(lock);
228: PrintStream ps = new PrintStream(os);
229: ps.println("import ahoj;");
230: lock.releaseLock();
231: throw new FailException(
232: "write to file object on read only filesystem failed.");
233: } catch (IOException ioe) {
234: System.out
235: .println("write to file object on read only filesystem passed.");
236: }
237:
238: // test on readonly = false
239: //
240: lfs.setReadOnly(false);
241:
242: // test create FileObject
243: try {
244: FileObject fo2 = root.createData("MyNewFile.txt");
245: fo2.delete();
246: System.out
247: .println("Create FileObject on read/write filesystem passed.");
248: } catch (IOException ioe) {
249: ioe.printStackTrace();
250: throw new FSTest.FailException(
251: "Create FileObject on read/write filesystem file failed.");
252: }
253: //test create folder
254: try {
255: FileObject folder = root.createFolder("MyNewFolder");
256: folder.delete();
257: System.out
258: .println("Create folder on read only filesytem passed.");
259: } catch (IOException ioe) {
260: ioe.printStackTrace();
261: throw new FSTest.FailException(
262: "Create folder on read/write filesystem failed.");
263: }
264:
265: // tet write data into file object
266: lock = fo.lock();
267: try {
268: OutputStream os = fo.getOutputStream(lock);
269: PrintStream ps = new PrintStream(os);
270: ps.println("import ahoj;");
271: System.out
272: .println("write to file object on read/write filesystem passed.");
273: lock.releaseLock();
274: } catch (IOException ioe) {
275: ioe.printStackTrace();
276: lock.releaseLock();
277: throw new FSTest.FailException(
278: "write to file object on read/write filesystem failed.");
279: }
280: } catch (FailException fe) {
281: fe.printStackTrace();
282: assertTrue(fe.getMessage(), false);
283:
284: } catch (ErrorException ee) {
285: ee.printStackTrace();
286: assertTrue(ee.getMessage(), false);
287: }
288: }
289:
290: protected void setUp() throws IOException, PropertyVetoException {
291: testDir = new File(File.createTempFile("ssadfasdfsadf",
292: "6346436").getParentFile(), "fgsagkjasdhgksa");
293: // System.out.println("testDir = " + testDir );
294: if (testDir.mkdir() == false && testDir.isDirectory() == false) {
295: throw new IOException(
296: "Error, temporary directory is not created");
297: }
298: testFS = new LocalFileSystem();
299: testFS.setRootDirectory(testDir);
300: FileSystem[] fss = Repository.getDefault().toArray();
301: for (int i = 0; i < fss.length; i++) {
302: FileSystem fs = fss[i];
303: if (fs instanceof LocalFileSystem
304: && ((LocalFileSystem) fs).getRootDirectory()
305: .equals(testDir)) {
306: // filesystem is allready mounted
307: testFS = (LocalFileSystem) fs;
308: return;
309: }
310: }
311: FileObject root = testFS.getRoot();
312:
313: Repository.getDefault().addFileSystem(testFS);
314: }
315:
316: }
|