1   package org.microemu.app.util;
2   
3   import java.io.File;
4   import java.io.InputStream;
5   import java.net.URL;
6   import java.net.URLClassLoader;
7   
8   import org.microemu.app.classloader.MIDletClassLoaderTest;
9   
10  import junit.framework.TestCase;
11  
12  public class IOUtilsTest extends TestCase {
13  
14  	private static final String TEST_JAR = MIDletClassLoaderTest.TEST_APP_JAR; 
15  
16  	public void testCanonicalFileURL() throws Exception {
17  		
18  		ClassLoader parent = IOUtilsTest.class.getClassLoader();
19  		
20  		URL jarURL = parent.getResource(TEST_JAR);
21  		assertNotNull("Can't find jar", jarURL);
22  		
23  		File file = new File(jarURL.getPath());
24  		
25  		assertTrue("is real file",  file.canRead());
26  		
27  		String urlString = IOUtils.getCanonicalFileURL(file);
28  		System.out.println("local file url [" + urlString + "]");
29  		URL testURL = new URL(urlString);
30  		
31  		InputStream is = testURL.openStream();
32  		assertNotNull("Can't openStream jar", is);
33  		IOUtils.closeQuietly(is);
34  		
35  		URLClassLoader ucl = new URLClassLoader(new URL[]{testURL});
36  		
37  		final String testFile = "META-INF/MANIFEST.MF";
38  		
39  		is = null;
40  		try {
41  			is = ucl.getResourceAsStream(testFile);
42  			assertNotNull("URLClassLoader", is);
43  		} finally {
44  			IOUtils.closeQuietly(is);
45  		}
46  	}
47  	
48  	
49  	/**
50  	 * This manual tests. Just remove x_ to test on your system
51  	 * @throws Exception
52  	 */
53  	public void x_testCanonicalNetworkFileURL() throws Exception {
54  	
55  		File file;
56  		
57  		/*  tested on Windows */
58  		//file = new File("//vladsdesk/test/dir/" + TEST_JAR);
59  		/*  tested on Windows */
60  		//file = new File("//linux/vlads/test/dir/" + TEST_JAR);
61  		
62  		// Can't make this work on Linux connectin to XP!
63  		//file = new File("//vladsdesk/test/dir/" + TEST_JAR);
64  		//file = new File("//linux/vlads/test/dir/" + TEST_JAR);
65  		file = new File("//localhost/home/vlads/test/dir/" + TEST_JAR);
66  		
67  		assertTrue("is real file",  file.canRead());
68  		
69  		String urlString = IOUtils.getCanonicalFileURL(file);
70  		
71  		System.out.println("network URL [" + urlString + "]");
72  		
73  		URL testURL = new URL(urlString);
74  		
75  		InputStream is = testURL.openStream();
76  		assertNotNull("Can't openStream jar", is);
77  		IOUtils.closeQuietly(is);
78  		
79  		URLClassLoader ucl = new URLClassLoader(new URL[]{testURL});
80  		
81  		final String testFile = "META-INF/MANIFEST.MF";
82  		
83  		is = null;
84  		try {
85  			is = ucl.getResourceAsStream(testFile);
86  			assertNotNull("URLClassLoader", is);
87  		} finally {
88  			IOUtils.closeQuietly(is);
89  		}
90  	}
91  }