1   package de.fu_berlin.ties.io;
2   
3   import junit.framework.TestCase;
4   // JUnitDoclet begin import
5   import java.io.ByteArrayInputStream;
6   import java.io.ByteArrayOutputStream;
7   import java.io.File;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  import java.io.Reader;
11  import java.io.StringReader;
12  import java.io.StringWriter;
13  import java.io.Writer;
14  import java.net.URL;
15  // JUnitDoclet end import
16  
17  /***
18  * Generated by JUnitDoclet, a tool provided by
19  * ObjectFab GmbH under LGPL.
20  * Please see www.junitdoclet.org, www.gnu.org
21  * and www.objectfab.de for informations about
22  * the tool, the licence and the authors.
23  */
24  
25  
26  public class IOUtilsTest
27  // JUnitDoclet begin extends_implements
28  extends TestCase
29  // JUnitDoclet end extends_implements
30  {
31    // JUnitDoclet begin class
32    private static final String testString =
33      "This is just a simple test string without any real value";
34    // JUnitDoclet end class
35    
36    public IOUtilsTest(String name) {
37      // JUnitDoclet begin method IOUtilsTest
38      super(name);
39      // JUnitDoclet end method IOUtilsTest
40    }
41    
42    public de.fu_berlin.ties.io.IOUtils createInstance() throws Exception {
43      // JUnitDoclet begin method testcase.createInstance
44      return null; // singleton
45      // JUnitDoclet end method testcase.createInstance
46    }
47    
48    protected void setUp() throws Exception {
49      // JUnitDoclet begin method testcase.setUp
50      super.setUp();
51      // JUnitDoclet end method testcase.setUp
52    }
53    
54    protected void tearDown() throws Exception {
55      // JUnitDoclet begin method testcase.tearDown
56      super.tearDown();
57      // JUnitDoclet end method testcase.tearDown
58    }
59    
60    public void testCreateOutFile() throws Exception {
61      // JUnitDoclet begin method createOutFile
62      // JUnitDoclet end method createOutFile
63    }
64    
65    public void testDetermineCharset() throws Exception {
66      // JUnitDoclet begin method determineCharset
67      // JUnitDoclet end method determineCharset
68    }
69    
70    public void testDetermineCharsetName() throws Exception {
71      // JUnitDoclet begin method determineCharsetName
72      // JUnitDoclet end method determineCharsetName
73    }
74    
75    public void testDetermineOutputDirectory() throws Exception {
76      // JUnitDoclet begin method determineOutputDirectory
77      // JUnitDoclet end method determineOutputDirectory
78    }
79    
80    public void testGetBaseName() throws Exception {
81      // JUnitDoclet begin method getBaseName
82      // JUnitDoclet end method getBaseName
83    }
84    
85    public void testGetExtension() throws Exception {
86      // JUnitDoclet begin method getExtension
87      final String[] urls = new String[] {
88          "http://java.sun.com",
89          "http://java.sun.com/",
90          "http://java.sun.com/index.html",
91          "http://java.sun.com/j2ee/index.html",
92          "http://java.sun.com/j2ee/index.html?param1=bla&param2=blub",
93          "http://java.sun.com/j2ee/index.jsp;sessionXYZ",
94          "http://java.sun.com/j2ee/#section2",
95          "http://java.sun.com/j2ee/file.index.html#section2",
96      };
97      final String[] expectedURLExt = new String[] {
98          "",
99          "",
100         "html",
101         "html",
102         "html",
103         "jsp",
104         "",
105         "html"
106     };
107     URL currentURL;
108 
109     for (int i = 0; i < urls.length; i++) {
110         currentURL = new URL(urls[i]);
111         assertEquals(IOUtils.getExtension(currentURL), expectedURLExt[i]);
112     }
113     
114     final String[] filenames = new String[] {
115          "README",
116          "docs/README",
117          "/usr/local/share",
118          "README.txt",
119          "docs/readme.html",
120          "C://Windows",
121          "C://Windows//",
122          "C://autoexec.bat",
123          "C://Windows//logo.bmp",
124          "/dir/sub.directory/README",
125          "C://dir//sub.directory//README",
126          "/usr/local/j2sdk1.4.1_01",
127          "jdk1.4",
128      };
129      final String[] expectedFileExt = new String[] {
130          "",
131          "",
132          "",
133          "txt",
134          "html",
135          "",
136          "",
137          "bat",
138          "bmp",
139          "",
140          "",
141          "1_01",
142          "4"
143      };
144      File currentFile;
145 
146      for (int i = 0; i < filenames.length; i++) {
147          currentFile = new File(filenames[i]);
148          assertEquals(expectedFileExt[i], IOUtils.getExtension(currentFile));
149      }
150     // JUnitDoclet end method getExtension
151   }
152   
153   public void testGetLocalName() throws Exception {
154     // JUnitDoclet begin method getLocalName
155     // JUnitDoclet end method getLocalName
156   }
157   
158   public void testOpenCompressableInStream() throws Exception {
159     // JUnitDoclet begin method openCompressableInStream
160     // JUnitDoclet end method openCompressableInStream
161   }
162   
163   public void testOpenCompressableOutStream() throws Exception {
164     // JUnitDoclet begin method openCompressableOutStream
165     // JUnitDoclet end method openCompressableOutStream
166   }
167   
168   public void testOpenReader() throws Exception {
169     // JUnitDoclet begin method openReader
170     // JUnitDoclet end method openReader
171   }
172   
173   public void testOpenUnicodeReader() throws Exception {
174     // JUnitDoclet begin method openUnicodeReader
175     // JUnitDoclet end method openUnicodeReader
176   }
177   
178   public void testOpenUnicodeWriter() throws Exception {
179     // JUnitDoclet begin method openUnicodeWriter
180     // JUnitDoclet end method openUnicodeWriter
181   }
182   
183   public void testOpenWriter() throws Exception {
184     // JUnitDoclet begin method openWriter
185     // JUnitDoclet end method openWriter
186   }
187   
188   public void testReadToString() throws Exception {
189     // JUnitDoclet begin method readToString
190     final StringReader in = new StringReader(testString);
191     final String result = IOUtils.readToString(in);
192     in.close();
193     assertEquals(testString, result);
194     // JUnitDoclet end method readToString
195   }
196   
197   public void testReadToWriter() throws Exception {
198     // JUnitDoclet begin method readToWriter
199     final StringReader in = new StringReader(testString);
200     final Writer out = new StringWriter();
201     final int length = IOUtils.readToWriter(in, out);
202     assertEquals(testString, out.toString());
203     assertEquals(testString.length(), length);
204     in.close();
205     out.close();
206     // JUnitDoclet end method readToWriter
207   }
208   
209   public void testReadURIList() throws Exception {
210     // JUnitDoclet begin method readURIList
211     final String uriListContents = "# urn:isbn:0-201-08372-8\r\n"
212         + "http://www.huh.org/books/foo.html#section2\n"
213         + "http://www.huh.org/books/foo.pdf\r\n"
214         + "ftp://ftp.foo.org/books/foo.txt\n"
215         + "#The URI list format is defined in\r\n"
216         + " # RFC 2483: \r"
217         + "  http://www.rfc-editor.org/rfc/rfc2483.txt\n\r"
218         + " http://www.rfc-editor.org/ "
219         + "\r\n"
220         + "# It' stricter than this example but we should be \n"
221         + "\t## generous when reading.\r\n\r\n ";
222     final String[] expectedURIs = new String[] {
223         "http://www.huh.org/books/foo.html#section2",
224         "http://www.huh.org/books/foo.pdf",
225         "ftp://ftp.foo.org/books/foo.txt",
226         "http://www.rfc-editor.org/rfc/rfc2483.txt",
227         "http://www.rfc-editor.org/"
228     };
229 
230     final String[] uris = IOUtils.readURIList(uriListContents);
231     assertEquals(uris.length, expectedURIs.length);
232     for (int i = 0; i < uris.length; i++) {
233         assertEquals(uris[i], expectedURIs[i]);        
234     }
235     // JUnitDoclet end method readURIList
236   }
237   
238   public void testSetGetDefaultDirectory() throws Exception {
239     // JUnitDoclet begin method setDefaultDirectory getDefaultDirectory
240     java.io.File[] tests = {new java.io.File("."), null};
241     
242     for (int i = 0; i < tests.length; i++) {
243       IOUtils.setDefaultDirectory(tests[i]);
244       assertEquals(tests[i], IOUtils.getDefaultDirectory());
245     }
246     // JUnitDoclet end method setDefaultDirectory getDefaultDirectory
247   }
248   
249   public void testTryToClose() throws Exception {
250     // JUnitDoclet begin method tryToClose
251     Reader in = new StringReader(testString);
252     Writer out = new StringWriter();
253     InputStream inStream = new ByteArrayInputStream(new byte[1024]);
254     OutputStream outStream = new ByteArrayOutputStream(1024);
255     
256     // should be closeable
257     assertTrue(IOUtils.tryToClose(in));
258     assertTrue(IOUtils.tryToClose(out));
259     assertTrue(IOUtils.tryToClose(inStream));
260     assertTrue(IOUtils.tryToClose(outStream));
261     
262     // should return false for null objects
263     in = null;
264     out = null;
265     inStream = null;
266     outStream = null;
267 
268     assertFalse(IOUtils.tryToClose(in));
269     assertFalse(IOUtils.tryToClose(out));
270     assertFalse(IOUtils.tryToClose(inStream));
271     assertFalse(IOUtils.tryToClose(outStream));
272     // JUnitDoclet end method tryToClose
273   }
274   
275   public void testWriteToWriter() throws Exception {
276     // JUnitDoclet begin method writeToWriter
277     // JUnitDoclet end method writeToWriter
278   }
279   
280   
281   
282   /***
283   * JUnitDoclet moves marker to this method, if there is not match
284   * for them in the regenerated code and if the marker is not empty.
285   * This way, no test gets lost when regenerating after renaming.
286   * Method testVault is supposed to be empty.
287   */
288   public void testVault() throws Exception {
289     // JUnitDoclet begin method testcase.testVault
290     // JUnitDoclet end method testcase.testVault
291   }
292   
293   public static void main(String[] args) {
294     // JUnitDoclet begin method testcase.main
295     junit.textui.TestRunner.run(IOUtilsTest.class);
296     // JUnitDoclet end method testcase.main
297   }
298 }