001: /*
002: * AttrTest.java
003: *
004: * Created on August 15, 2002, 4:09 PM
005: */
006:
007: package FileSystemTest;
008:
009: import java.beans.PropertyVetoException;
010: import java.io.*;
011: import java.util.Enumeration;
012: import junit.framework.Test;
013: import org.netbeans.junit.NbTestCase;
014: import org.netbeans.junit.NbTestSuite;
015: import org.openide.filesystems.*;
016:
017: /**
018: *
019: * @author pz97949
020: */
021: public class AttrTest extends NbTestCase {
022: // File fileSystemFile;
023: File fileSystemDir;
024: LocalFileSystem fileSystem;
025:
026: public AttrTest(String testName) {
027: super (testName);
028: }
029:
030: /** tests set/get attribute to fileobject special named:
031: * "\"
032: * see to bug http://installer.netbeans.org/issues/show_bug.cgi?id=8976
033: */
034: public void testSpecialNamedAttr() throws IOException,
035: PropertyVetoException {
036: preprocess();
037: FileObject fo = getAnyFileObject();
038: setAttribute(fo, "\"", "1");
039: setAttribute(fo, "h&", "2");
040: setAttribute(fo, "<", "3");
041: setAttribute(fo, ">", "4");
042: setAttribute(fo, "-", "5");
043: setAttribute(fo, "*", "6");
044: System.gc();
045:
046: getAttribute(fo, "\"", "1");
047: getAttribute(fo, "h&", "2");
048: getAttribute(fo, "<", "3");
049: getAttribute(fo, ">", "4");
050: getAttribute(fo, "-", "5");
051: getAttribute(fo, "*", "6");
052: postprocess();
053: }
054:
055: /** set attribute to FileObject
056: */
057: private void setAttribute(FileObject fo, String name, String value) {
058: try {
059: fo.setAttribute(name, value);
060: log("attribute (name = " + name + ", value = " + value
061: + ") setted");
062: } catch (Exception e) {
063: String msg = "failed on set attribute name = " + name
064: + " , value = " + value;
065: log(msg);
066: assertTrue(msg, false);
067: }
068: }
069:
070: /** read attribude from fileobject and tests if is correct
071: */
072: private String getAttribute(FileObject fo, String name,
073: String refValue) {
074: String value = (String) fo.getAttribute(name);
075: if (value == null) {
076: assertTrue("File object doesn't contain attribute (name = "
077: + name + ", value = " + value + " ", false);
078: } else {
079: if (!value.equals(refValue)) {
080: assertTrue("FileObject read wrong attr value ( name = "
081: + name + ",correct value = " + refValue
082: + " , read value = " + value, false);
083: }
084: }
085: return value;
086: }
087:
088: /** it mounts LocalFileSystem in temorary directory
089: */
090: private void preprocess() throws IOException, PropertyVetoException {
091: // fileSystemFile.mkdir();
092: fileSystemDir = new File(getWorkDir(), "testAtt123rDir");
093: if (fileSystemDir.mkdir() == false
094: || fileSystemDir.isDirectory() == false) {
095: throw new IOException(fileSystemDir.toString()
096: + " is not directory");
097: }
098: Enumeration en = Repository.getDefault().fileSystems();
099: while (en.hasMoreElements()) {
100: FileSystem fs = (FileSystem) en.nextElement();
101: if (fs instanceof LocalFileSystem) {
102: LocalFileSystem lfs = (LocalFileSystem) fs;
103: if (lfs.getRootDirectory().equals(fileSystemDir)) {
104: fileSystem = lfs;
105: break;
106: }
107: }
108: }
109: if (fileSystem == null) {
110: fileSystem = new LocalFileSystem();
111: fileSystem.setRootDirectory(fileSystemDir);
112: Repository.getDefault().addFileSystem(fileSystem);
113: }
114:
115: }
116:
117: /** unmount temporary filesystem
118: **/
119: private void postprocess() {
120: Repository.getDefault().getDefault().removeFileSystem(
121: fileSystem);
122: fileSystemDir.deleteOnExit();
123: // fileSystemFile.deleteOnExit();
124: }
125:
126: private FileObject getAnyFileObject() {
127: return fileSystem.getRoot();
128: }
129:
130: /**This suite*/
131: public static Test suite() {
132: NbTestSuite suite = new NbTestSuite(AttrTest.class);
133: return suite;
134: }
135:
136: /** test set "\\" attr value, see to : 8977 in Issuezila
137: */
138: public void testSetBackslashValue() throws IOException,
139: PropertyVetoException {
140: preprocess();
141: FileObject fo = getAnyFileObject();
142: try {
143: setAttribute(fo, "\\", "2");
144: getAttribute(fo, "\\", "2");
145: } catch (Exception e) {
146: assertTrue(" failed:no attribute setted " + e, false);
147: }
148:
149: postprocess();
150: }
151:
152: /**
153: * @param args the command line arguments
154: */
155: public static void main(String[] args) {
156: junit.textui.TestRunner.run(suite());
157: }
158:
159: }
|