|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.filesystems; import org.xml.sax.*; import org.openide.util.*; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.util.*; import java.net.*; /** * * @author rm111737 */ public class FileObjectTestHid extends TestBaseHid { private final static String FOLDER_CHILD_NAME= "testFolder"; private final static String FOLDER_CHILD= "/"+FOLDER_CHILD_NAME; private final static String FILE_CHILD_NAME= "test"; private final static String FILE_CHILD_EXT= "txt"; private final static String FILE_CHILD= "/"+FILE_CHILD_NAME+"." +FILE_CHILD_EXT; /**Should be deleted and testedFS renamed to fs*/ private FileSystem fs; private FileObject root; private static Set res = null; /** Here add necessary resources. But prefered to use: * root, getFile1, getFile2, getFolder1, getFolder2, */ private static String[] resources = new String [] { }; public FileObjectTestHid(java.lang.String testName) { super(testName); } protected void setUp() throws java.lang.Exception { super.setUp(); /**Should be deleted and testedFS renamed to fs*/ fs = this.testedFS; root = fs.findResource(getResourcePrefix()); } protected void tearDown() throws Exception { super.tearDown(); fs = this.testedFS = null; allTestedFS = null; } /** Test of copy method, of class org.openide.filesystems.FileObject. */ public void testCopy() { checkSetUp(); FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); try { fo1.copy(fold,fo2.getName(),fo2.getExt()); } catch (IOException iex) { /** Test passed*/ return; } fsFail ("copy should fire exception if file already exists"); } /** Test of copy method, of class org.openide.filesystems.FileObject. */ public void testCopy1() throws IOException { checkSetUp(); FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); FileObject fo3 = null; String testStr = "text..."; String attrName = "attrName"; String value = "value"; registerDefaultListener(fold); try { writeStr(fo1, testStr); fo1.setAttribute(attrName,value); fo3 = fo1.copy(fold,fo2.getExt(),fo2.getName()); } catch (IOException iex) { fsAssert("expected copy will success on writable FS", fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); return; } fsAssert("no exception fired but copy returned null",fo3 != null); fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); fsAssert("attributes should be copied too", value.equals((String)fo3.getAttribute(attrName)) ); fileDataCreatedAssert("parent should fire fileDataCreated",1); } /** Test of copy method, of class org.openide.filesystems.FileObject. */ public void testCopy1_FS() throws IOException { checkSetUp(); FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); FileObject fo3 = null; String testStr = "text..."; String attrName = "attrName"; String value = "value"; registerDefaultListener(testedFS); try { writeStr(fo1, testStr); fo1.setAttribute(attrName,value); fo3 = fo1.copy(fold,fo2.getExt(),fo2.getName()); } catch (IOException iex) { fsAssert("expected copy will success on writable FS", fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); return; } fsAssert("no exception fired but copy returned null",fo3 != null); fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); fsAssert("attributes should be copied too", value.equals((String)fo3.getAttribute(attrName)) ); fileDataCreatedAssert("parent should fire fileDataCreated",1); } /** Test of move method, of class org.openide.filesystems.FileObject. */ public void testMove() { checkSetUp(); FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); FileLock lock = null; try { lock = fo1.lock(); fo1.move(lock, fold,fo2.getName(),fo2.getExt()); } catch (IOException iex) { /** Test passed*/ return; } finally { if (lock != null) lock.releaseLock(); } fsFail ("move should fire exception if file already exists"); } /** Test of move method, of class org.openide.filesystems.FileObject. */ public void testMove1() throws IOException { checkSetUp(); FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); FileObject fo3 = null; String testStr = "text..."; String attrName = "attrName"; String value = "value"; registerDefaultListener(fold); try { writeStr(fo1, testStr); fo1.setAttribute(attrName,value); FileLock lock = fo1.lock(); fo3 = fo1.move(lock,fold,fo2.getExt(),fo2.getName()); lock.releaseLock(); } catch (IOException iex) { fsAssert("expected move will success on writable FS", fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); return; } fsAssert("no exception fired but copy returned null",fo3 != null); fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); fsAssert("attributes should be copied too", value.equals((String)fo3.getAttribute(attrName)) ); fsAssert ("",fold.getFileObject(fo3.getName(),fo3.getExt()) != null); if (fo1.equals(fo3)) this.fileRenamedAssert("File was actually renamed.",1); else { fileDeletedAssert("parent should fire fileDeleted",1); fileDataCreatedAssert("parent should fire fileDataCreated",1); } } /** Test of move method, of class org.openide.filesystems.FileObject. */ public void testMove1_Fs() throws IOException { checkSetUp(); FileObject fold = getTestFolder1(root); FileObject fo1 = getTestFile1(fold); FileObject fo2 = getTestFile2(fold); FileObject fo3 = null; String testStr = "text..."; String attrName = "attrName"; String value = "value"; registerDefaultListener(testedFS); try { writeStr(fo1, testStr); fo1.setAttribute(attrName,value); FileLock lock = fo1.lock(); fo3 = fo1.move(lock,fold,fo2.getExt(),fo2.getName()); lock.releaseLock(); } catch (IOException iex) { fsAssert("expected move will success on writable FS", fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); return; } fsAssert("no exception fired but copy returned null",fo3 != null); fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); fsAssert("attributes should be copied too", value.equals((String)fo3.getAttribute(attrName)) ); fsAssert ("",fold.getFileObject(fo3.getName(),fo3.getExt()) != null); if (fo1.equals(fo3)) this.fileRenamedAssert("File was actually renamed.",1); else { fileDeletedAssert("parent should fire fileDeleted",1); fileDataCreatedAssert("parent should fire fileDataCreated",1); } } /** Test whether the read is forbiden while somebody is writing */ public void testWriteReadExclusion() throws Exception { testWriteReadExclusion(false); } public void testWriteReadExclusionDeadlock() throws Exception { testWriteReadExclusion(true); } private void testWriteReadExclusion(final boolean deadlockInWrite) throws Exception { checkSetUp(); FileObject fold = getTestFolder1(root); final FileObject fo1 = getTestFile1(fold); try { firstThreadImpl1(fo1, deadlockInWrite); firstThreadImpl2(fo1, deadlockInWrite); } catch (IOException iex) { fsAssert( "expected move will success on writable FS", fs.isReadOnly() || fo1.isReadOnly() ); } } private static void firstThreadImpl1(final FileObject fo1, final boolean deadlockInWrite) throws IOException, InterruptedException { RequestProcessor.Task secondThread; writeStr(fo1, "text"); secondThread = startSecondThreadAndWait(fo1, deadlockInWrite); InputStream is = null; try { is = fo1.getInputStream(); byte[] arr = new byte[200]; int len = is.read(arr); assertEquals("Read all four bytes", 4, len); for (int i = 1; i <= 4; i++) { assertEquals(i + " th byte is " + i, i, arr[i - 1]); } } catch (IOException ex) { // FileAlreadyLockedException is fine, means we could not read the stream assertTrue("FileAlreadyLockedException is fine here but just for dedlock in write", deadlockInWrite); } finally { try { if (is != null) is.close(); } catch (IOException e) { } synchronized (FileObjectTestHid.class) { // let the writer thread finish FileObjectTestHid.class.notifyAll(); } secondThread.waitFinished(); } } private static void firstThreadImpl2(final FileObject fo1, final boolean deadlockInWrite) throws IOException ,InterruptedException { RequestProcessor.Task secondThread = null; writeStr(fo1, "text"); InputStream is = null; try { is = fo1.getInputStream(); secondThread = startSecondThreadAndWait(fo1, deadlockInWrite); byte[] arr = new byte[200]; int len = is.read(arr); assertEquals("Initial data only" , "text".length(), len); } finally { try { if (is != null) is.close(); } catch (IOException e) { } synchronized (FileObjectTestHid.class) { // let the writer thread finish FileObjectTestHid.class.notifyAll(); } if (secondThread != null) secondThread.waitFinished(); } } private static void secondThreadImpl(final FileObject fo1, final boolean deadlockInWrite) { OutputStream os = null; FileLock lock = null; try { try { lock = fo1.lock(); os = fo1.getOutputStream(lock); os.write(1); os.write(2); os.flush(); synchronized (FileObjectTestHid.class) { FileObjectTestHid.class.notify(); // block for a while so the other thread // can do the partial read FileObjectTestHid.class.wait(deadlockInWrite ? 0 : 1000); } os.write(3); os.write(4); } finally { if (lock != null) lock.releaseLock(); if (os != null) os.close(); synchronized (FileObjectTestHid.class) { FileObjectTestHid.class.notify(); } } } catch (Exception e) { } } private static RequestProcessor.Task startSecondThreadAndWait(final FileObject fo1, final boolean deadlockInWrite) throws InterruptedException { RequestProcessor.Task secondThread; synchronized (FileObjectTestHid.class) { secondThread = new RequestProcessor("Writes with delay").post(new Runnable() { public void run() { secondThreadImpl(fo1, deadlockInWrite); } }); FileObjectTestHid.class.wait(); } return secondThread; } public void testGetPath1() { checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = getTestFile1(fold2); FileObject result = fs.findResource(fo1.getPath()); fsAssert("findResource problem", result != null); fsAssert("findResource problem",fo1.equals(result)); } public void testGetPath2() { checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = null; try { fo1 = fold2.createFolder("a.b.c"); } catch (IOException iex) { fsAssert("There is not possible to create folder a.b.c", fs.isReadOnly() || fold2.isReadOnly()); return; } FileObject result = fs.findResource(fo1.getPath()); fsAssert("findResource problem", result != null); fsAssert("findResource problem",fo1.equals(result)); } public void testGetPath3() throws IOException{ /** There is no possible to create filenames ending in dots on Win platforms*/ if (Utilities.isWindows()) return; checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = null; try { fo1 = fold2.createFolder("a.b.c."); } catch (IOException iex) { fsAssert("There is not possible to create folder a.b.c", fs.isReadOnly() || fold2.isReadOnly()); return; } FileObject result = fs.findResource(fo1.getPath()); fsAssert("findResource problem", result != null); fsAssert("findResource problem",fo1.equals(result)); } public void testGetPath4() throws IOException{ checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = null; try { fo1 = fold2.createData("a.b.c.java"); } catch (IOException iex) { fsAssert("There is not possible to create folder a.b.c", fs.isReadOnly() || fold2.isReadOnly()); return; } FileObject result = fs.findResource(fo1.getPath()); fsAssert("findResource problem", result != null); fsAssert("findResource problem",fo1.equals(result)); } public void testGetPath5() throws IOException{ checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = null; try { fo1 = fold2.createData("a.b.c","java"); } catch (IOException iex) { fsAssert("There is not possible to create folder a.b.c", fs.isReadOnly() || fold2.isReadOnly()); return; } FileObject result = fs.findResource(fo1.getPath()); fsAssert("findResource problem", result != null); fsAssert("findResource problem",fo1.equals(result)); } /** Test of getPackageName method, of class org.openide.filesystems.FileObject. * / public void testGetPackageName() { checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = getTestFile1(fold2); String getPackageNameExt = fo1.getPackageName('/')+"."+fo1.getExt(); FileObject result = fs.findResource(getPackageNameExt); fsAssert("getPackageName or getPackageNameExt problem: " + "!fo1.getPath("+fo1.getPath()+").equals("+getPackageNameExt+")",fo1.getPath().equals(getPackageNameExt)); fsAssert("getPackageName or findResource problem",result != null); fsAssert("getPackageName or findResource problem",fo1.equals(result)); } */ /** Test of getPackageName method, of class org.openide.filesystems.FileObject. * / public void testGetPackageName2() { checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = null; try { fo1 = fold2.createFolder("a.b.c"); } catch (IOException iex) { fsAssert("There is not possible to create folder a.b.c", fs.isReadOnly() || fold2.isReadOnly()); return; } String getPackageNameExt = fo1.getPackageName('/')+"."+fo1.getExt(); FileObject result = fs.findResource(getPackageNameExt); fsAssert("getPackageName or getPackageNameExt problem: " + "!fo1.getPath("+fo1.getPath()+").equals("+getPackageNameExt+")",fo1.getPath().equals(getPackageNameExt)); fsAssert("getPackageName or findResource problem",result != null); fsAssert("getPackageName or findResource problem",fo1.equals(result)); } */ /** Test of getNameExt method, of class org.openide.filesystems.FileObject. */ public void testGetNameExt() { checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder1(fold1); FileObject fo1 = getTestFile1(fold2); fsAssert("getNameExt problem",fo1.getNameExt().equals(fo1.getName() + "." +fo1.getExt())); } /** Test of existsExt method, of class org.openide.filesystems.FileObject. */ public void testExistsExt() { checkSetUp(); FileObject fo1 = getTestFile1(root); FileObject fo2 = getTestFile2(root); fsAssert("Expected both file`s names are equal. But extension differ.",fo1.existsExt(fo2.getExt())); } /** Test of hasExt method, of class org.openide.filesystems.FileObject. */ public void testHasExt() { checkSetUp(); FileObject fo1 = getTestFile1(root); FileObject fo2 = getTestFile2(root); FileObject fo11 = getTestFile1(getTestFolder1(root)); fsTestFrameworkErrorAssert("",fo1.getExt().equals(fo11.getExt()) && !fo1.getExt().equals(fo2.getExt())); fsAssert("Unexpected ",fo1.hasExt(fo11.getExt()) && !fo1.hasExt(fo2.getExt())); } /** Test of fireFileDataCreatedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileDataCreatedEvent() throws IOException { checkSetUp(); registerDefaultListener(root); root.getChildren(); FileObject fo; try { fo = root.createData("name","ext"); } catch (IOException iex) { fsAssert("createData fired IOException. So there was expected fs or fo are read-only: " + iex.toString() , fs.isReadOnly() || root.isReadOnly()); fileDataCreatedAssert("fs or fo is read-only. So no event should be fired",0); return; } fileDataCreatedAssert("createData should fire event fileDataCreated",1); fsAssert("createData returned null",fo != null); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); fileChangedAssert("fireFileChangedEvent should not be fired ",0); } /** Test of fireFileDataCreatedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileDataCreatedEvent_FS() throws IOException { checkSetUp(); registerDefaultListener(testedFS); root.getChildren(); FileObject fo; try { fo = root.createData("name","ext"); } catch (IOException iex) { fsAssert("createData fired IOException. So there was expected fs or fo are read-only: " + iex.toString() , fs.isReadOnly() || root.isReadOnly()); fileDataCreatedAssert("fs or fo is read-only. So no event should be fired",0); return; } fileDataCreatedAssert("createData should fire event fileDataCreated",1); fsAssert("createData returned null",fo != null); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); fileChangedAssert("fireFileChangedEvent should not be fired ",0); } public void testFireFileDataCreatedEvent_FS2() throws IOException { checkSetUp(); registerDefaultListener(testedFS); root.getChildren(); File rootFile = FileUtil.toFile(root); if (rootFile == null) return; (new File (rootFile, "testfile.test")).createNewFile(); root.refresh(); fileDataCreatedAssert("createData should fire event fileDataCreated",1); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); fileChangedAssert("fireFileChangedEvent should not be fired ",0); } /** Test of fireFileFolderCreatedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileFolderCreatedEvent() { checkSetUp(); root.getChildren(); registerDefaultListener(root); FileObject fo; try { fo = root.createFolder("name"); } catch (IOException iex) { fsAssert("createFolder fired IOException. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileFolderCreatedAssert("fs or fo is read-only. So no event should be fired",0); return; } fileFolderCreatedAssert("createFolder should fire event fileFolderCreated",1); fsAssert("createFolder returned null",fo != null); fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); fileChangedAssert("fireFileChangedEvent should not be fired ",0); } /** Test of fireFileFolderCreatedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileFolderCreatedEvent_FS() { checkSetUp(); root.getChildren(); registerDefaultListener(testedFS); FileObject fo; try { fo = root.createFolder("name"); } catch (IOException iex) { fsAssert("createFolder fired IOException. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileFolderCreatedAssert("fs or fo is read-only. So no event should be fired",0); return; } fileFolderCreatedAssert("createFolder should fire event fileFolderCreated",1); fsAssert("createFolder returned null",fo != null); fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); fileChangedAssert("fireFileChangedEvent should not be fired ",0); } /** Test of fireFileChangedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileChangedEvent() throws IOException{ checkSetUp(); FileObject fo = getTestFile1(root); registerDefaultListener(fo); try { writeStr(fo,"Text ..."); } catch (IOException iex) { fsAssert("FileObject could not be modified. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileChangedAssert("fs or fo is read-only. So no event should be fired",0); return; } fileChangedAssert("FileObject was modified. fireFileChangedEvent should be fired",1); fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); } /** Test of fireFileChangedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileChangedEvent_FS() throws IOException{ checkSetUp(); FileObject fo = getTestFile1(root); registerDefaultListener(testedFS); System.setProperty("hoho","on"); try { writeStr(fo,"Text ..."); System.setProperty("hoho","off"); } catch (IOException iex) { System.setProperty("hoho","off"); fsAssert("FileObject could not be modified. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileChangedAssert("fs or fo is read-only. So no event should be fired",0); return; } fileChangedAssert("FileObject was modified. fireFileChangedEvent should be fired",1); fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); } /** Test of fireFileDeletedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileDeletedEvent() throws IOException { checkSetUp(); FileObject fo1 = getTestFile1(root); FileObject fo2 = getTestFile2(root); registerDefaultListener(root); registerDefaultListener(fo1); registerDefaultListener(fo2); FileLock lock1 = null; FileLock lock2 = null; try { lock1 = fo1.lock(); lock2 = fo2.lock(); fo1.delete(lock1); fileDeletedAssert("FileObject was deleted. fireFileDeletedEvent should be fired",2); fo2.delete(lock2); fileDeletedAssert("FileObject was deleted. fireFileDeletedEvent should be fired",4); } catch (IOException iex) { fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileDeletedAssert("fs or fo is read-only. So no event should be fired",0); return; } finally { if (lock1 != null) lock1.releaseLock(); if (lock2 != null) lock2.releaseLock(); } fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); fileChangedAssert("fireFileChangedEvent should not be fired ",0); fsAssert("FileObject should be invalid after delete",!fo1.isValid()); fsAssert("FileObject should be invalid after delete",!fo2.isValid()); } public void testFireFileDeletedEvent2() throws IOException { checkSetUp(); FileObject fo1 = getTestFolder1(root); FileObject fo2 = getTestFolder1(fo1); FileObject fo3 = getTestFile1(fo2); registerDefaultListener(fo3); FileLock lock1 = null; try { lock1 = fo1.lock(); fo1.delete(lock1); fileDeletedAssert("FileObject was deleted. fireFileDeletedEvent should be fired",1); fsAssert("FileObject should be invalid after delete",!fo1.isValid()); fsAssert("FileObject should be invalid after delete",!fo2.isValid()); fsAssert("FileObject should be invalid after delete",!fo3.isValid()); } catch (IOException iex) { fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileDeletedAssert("fs or fo is read-only. So no event should be fired",0); } finally { if (lock1 != null) lock1.releaseLock(); } } /** Test of fireFileAttributeChangedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileAttributeChangedEvent() { checkSetUp(); FileObject fo1 = getTestFile1(root); String value = "value"; registerDefaultListener(fo1); try { fo1.setAttribute("attrName",value); fileAttributeChangedAssert("",1); fsAssert("",((String)fo1.getAttribute("attrName")).equals(value)); } catch (IOException iex) { fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); return; } } /** Test of fireFileAttributeChangedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileAttributeChangedEvent_FS() { checkSetUp(); FileObject fo1 = getTestFile1(root); String value = "value"; registerDefaultListener(testedFS); try { fo1.setAttribute("attrName",value); fileAttributeChangedAssert("",1); fsAssert("",((String)fo1.getAttribute("attrName")).equals(value)); } catch (IOException iex) { fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); return; } } /** Test of fireFileRenamedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileRenamedEvent() { checkSetUp(); FileObject fo = getTestFile1(root); registerDefaultListener(fo); FileLock lock = null; try { lock = fo.lock(); fo.rename(lock,fo.getName()+"X",fo.getExt()+"X"); } catch (IOException iex) { fsAssert("FileObject could not be renamed. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileRenamedAssert("fs or fo is read-only. So no event should be fired",0); return; } finally { if (lock != null) lock.releaseLock(); } fileRenamedAssert("",1); fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); } /** Test of fireFileRenamedEvent method, of class org.openide.filesystems.FileObject. */ public void testFireFileRenamedEvent_FS() { checkSetUp(); FileObject fo = getTestFile1(root); registerDefaultListener(testedFS); FileLock lock = null; try { lock = fo.lock(); fo.rename(lock,fo.getName()+"X",fo.getExt()+"X"); } catch (IOException iex) { fsAssert("FileObject could not be renamed. So there was expected fs or fo are read-only", fs.isReadOnly() || root.isReadOnly()); fileRenamedAssert("fs or fo is read-only. So no event should be fired",0); return; } finally { if (lock != null) lock.releaseLock(); } fileRenamedAssert("",1); fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); } /** Test of getMIMEType method, of class org.openide.filesystems.FileObject. */ public void testGetMIMEType() { checkSetUp(); FileObject fo = getTestFile1(root); String mimeType = "mimeType"; FileUtil.setMIMEType(fo.getExt(),mimeType); String actualMT = fo.getMIMEType(); fsAssert("mimeType for this fo was registered; was really " + actualMT, actualMT.equals(mimeType)); } /** Default mime type for files that do not contain * strange chacracters is text/plain * public void testDefaultMimeTypeForTextFiles () throws Exception { checkSetUp(); FileObject fo; try { fo = FileUtil.createData(root, "file.jarda"); // file with completely strange extension } catch (IOException iex) { fsAssert( "Does not seem to be writeable. So there was expected.", !root.canWrite () && fs.isReadOnly() ); return; } FileLock lock = fo.lock(); PrintWriter w = new PrintWriter ( fo.getOutputStream (lock) ); w.println ("Hi Jarda,"); w.println ("how are you?"); w.close(); lock.releaseLock(); assertEquals ("File is recognized as text type", "text/plain", fo.getMIMEType()); } /** /etc/hosts was broken * public void testDefaultMimeTypeFilesWithoutExtensionsIsComputed () throws Exception { checkSetUp(); FileObject fo; try { fo = FileUtil.createData(root, "file"); // file without extension } catch (IOException iex) { fsAssert( "Does not seem to be writeable. So there was expected.", !root.canWrite () && fs.isReadOnly() ); return; } FileLock lock = fo.lock(); InputStream is = getClass ().getResourceAsStream("FileObjectHosts.txt"); OutputStream os = fo.getOutputStream (lock); FileUtil.copy (is, os); is.close (); os.close (); lock.releaseLock (); assertEquals ("File is recognized as text plain", "text/plain", fo.getMIMEType()); } /** Default mime type for files with strange chacracters is content/unknown * public void testDefaultMimeTypeForHostsAllow () throws Exception { checkSetUp(); FileObject fo; try { fo = FileUtil.createData(root, "file.jarda"); // file with completely strange extension } catch (IOException iex) { fsAssert( "Does not seem to be writeable. So there was expected.", !root.canWrite () && fs.isReadOnly() ); return; } FileLock lock = fo.lock(); InputStream is = getClass ().getResourceAsStream("FileObjecttextplain.txt"); OutputStream os = fo.getOutputStream (lock); FileUtil.copy (is, os); is.close (); os.close (); lock.releaseLock (); assertEquals ("File is recognized as text plain", "text/plain", fo.getMIMEType()); } /** Default mime type for files with strange chacracters is content/unknown */ public void testDefaultMimeTypeForBinaryFiles () throws Exception { checkSetUp(); FileObject fo; try { fo = FileUtil.createData(root, "file.jarda"); // file with completely strange extension } catch (IOException iex) { fsAssert( "Does not seem to be writeable. So there was expected.", !root.canWrite () && fs.isReadOnly() ); return; } FileLock lock = fo.lock(); InputStream is = getClass ().getResourceAsStream("FileObjectTestHid.class"); OutputStream os = fo.getOutputStream (lock); FileUtil.copy (is, os); is.close (); os.close (); lock.releaseLock (); assertEquals ("File is recognized as unknown", "content/unknown", fo.getMIMEType()); } /** Mime type for folder fallbacks to content/unknown, issue 42965 */ public void testFolderMimeTypeIsUnknown () throws Exception { checkSetUp(); FileObject fo; try { fo = FileUtil.createFolder (root, "SomeStrangeFolder"); } catch (IOException iex) { fsAssert( "Does not seem to be writeable. So there was expected.", !root.canWrite () && fs.isReadOnly() ); return; } assertEquals ("File is recognized as unknown", "content/unknown", fo.getMIMEType()); } /** Regardless of the default file content the registered extensions * take precedence. * public void testRegisteredMimeTypeIsMoreImportant () throws Exception { checkSetUp(); FileObject fo; try { fo = FileUtil.createData(root, "file.mov"); // mov is registered } catch (IOException iex) { fsAssert( "Does not seem to be writeable. So there was expected.", !root.canWrite () && fs.isReadOnly() ); return; } FileLock lock = fo.lock(); PrintWriter w = new PrintWriter ( fo.getOutputStream (lock) ); w.println ("Hi Jarda,"); w.println ("how are you?"); w.close(); lock.releaseLock(); assertEquals ("File is recognized as movie", "video/quicktime", fo.getMIMEType()); } */ /** Test of getChildren method, of class org.openide.filesystems.FileObject. */ public void testGetChildren() { checkSetUp(); FileObject[] childs = root.getChildren(); FileObject fo1 = getTestFile1(root); FileObject fo2 = getTestFile2(root); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder2(root); fsAssert("Expected 2 folders and 2 files under root",childs.length >= 4); fsAssert(fo1.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fo1) ); fsAssert(fo2.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fo2) ); fsAssert(fold1.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fold1)); fsAssert(fold2.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fold2)); } /** Test of getFolders method, of class org.openide.filesystems.FileObject. */ public void testGetFolders() { checkSetUp(); FileObject fold1 = getTestFolder1(root); FileObject fold2 = getTestFolder2(root); List list= makeList(root.getFolders(false)); fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(fold1)); fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(fold2)); } /** Test of getFolders method, of class org.openide.filesystems.FileObject. */ public void testGetFolders1() { checkSetUp(); List childs = makeList(root.getChildren(false)); List folders = makeList(root.getFolders(false)); List datas = makeList(root.getData(false)); fsAssert("Expected that numbre of children equals number of folders + number of datas ", childs.size() == (folders.size() + datas.size())); } /** Test of getFolders method, of class org.openide.filesystems.FileObject. */ public void testGetFolders2() { checkSetUp(); List childs = makeList(root.getChildren(true)); List folders = makeList(root.getFolders(true)); List datas = makeList(root.getData(true)); fsAssert("Expected that numbre of children equals number of folders + number of datas ", childs.size() == (folders.size() + datas.size())); } /** Test of getFolders method, of class org.openide.filesystems.FileObject. */ public void testGetFolders3() { checkSetUp(); List folders = makeList(root.getFolders(true)); Iterator it = folders.iterator(); while (it.hasNext()) { fsAssert("getData should return FileObjects that return isFolder () == true and isData () == false", ((FileObject)it.next()).isFolder() && !((FileObject)it.next()).isData()); } } /** Test of getData method, of class org.openide.filesystems.FileObject. */ public void testGetData() { checkSetUp(); FileObject file1 = getTestFile1(root); FileObject file2 = getTestFile2(root); List list= makeList(root.getData(false)); fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(file1)); fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(file2)); } /** Test of getData method, of class org.openide.filesystems.FileObject. */ public void testGetData1() { checkSetUp(); List folders = makeList(root.getData(true)); Iterator it = folders.iterator(); while (it.hasNext()) { fsAssert("getData should return FileObjects that return isFolder () == false and isData () == true", !((FileObject)it.next()).isFolder() && ((FileObject)it.next()).isData()); } } public void testNbfsTransformation () throws FileStateInvalidException{ checkSetUp(); // additional check String sysName = fs.getSystemName(); if (sysName == null || sysName.length() == 0) return; if (Repository.getDefault().findFileSystem(fs.getSystemName()) == null) Repository.getDefault().addFileSystem(fs); try { FileObject file1 = getTestFile1(root); URL u = FileURL.encodeFileObject(file1); FileObject file2 = FileURL.decodeURL(u); fsAssert("Nbfs check: both files should be equal: " + file1 + " | " + file2 + " url: " + u + " fs: " + fs.getSystemName(), file1 == file2); } finally { Repository.getDefault().removeFileSystem(fs); } } public void testNbfsTransformation2() throws FileStateInvalidException, IOException, SAXException, ParserConfigurationException { checkSetUp(); // additional check if (fs.isReadOnly() || root.isReadOnly()) return; String sysName = fs.getSystemName(); if (sysName == null || sysName.length() == 0) return; if (Repository.getDefault().findFileSystem(fs.getSystemName()) == null) Repository.getDefault().addFileSystem(fs); InputStream inputStream = null; try { FileObject test = getTestFolder1(root); FileObject f = test.createData("layer.xml"); createSimpleXML(f); SAXParserFactory pFactory = SAXParserFactory.newInstance(); pFactory.setValidating (false); Parser p = pFactory.newSAXParser().getParser(); p.setDocumentHandler(new HandlerBase()); URL u = f.getURL(); p.parse(u.toExternalForm()); // byte[] b = new byte[10]; inputStream = u.openConnection().getInputStream(); inputStream.read(b); fsAssert("Nbfs check: unexpected content ", (new String (b)).startsWith(""); sb.append(" |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.