KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > LoaderTest


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.util;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import net.sf.dozer.util.mapping.DozerTestBase;
24
25 /**
26  * @author tierney.matt
27  */

28 public class LoaderTest extends DozerTestBase {
29   private Loader loader = new Loader();
30
31   public void testResourceNotFound() throws Exception JavaDoc {
32     assertNull("file URL should not have been found", loader.getResource(String.valueOf(System.currentTimeMillis())));
33   }
34   
35   public void testGetResource_FileOutsideOfClasspath() throws Exception JavaDoc {
36     // Create temp file.
37
File JavaDoc temp = File.createTempFile("dozerfiletest", ".txt");
38   
39     // Delete temp file when program exits.
40
temp.deleteOnExit();
41
42     String JavaDoc resourceName = "file:" + temp.getAbsolutePath();
43     URL JavaDoc url = loader.getResource(resourceName);
44     assertNotNull("URL should not be null", url);
45     
46     InputStream JavaDoc is = url.openStream();
47     assertNotNull("input stream should not be null", is);
48   }
49   
50   public void testGetResource_FileOutsideOfClasspath_NotFound() throws Exception JavaDoc {
51     URL JavaDoc url = loader.getResource("file:" + System.currentTimeMillis());
52     assertNotNull("URL should not be null", url);
53     
54     try {
55       url.openStream();
56       fail("should have thrown a file not found exception");
57     } catch (IOException JavaDoc e) {
58       //expected
59
}
60   }
61   
62   public void testGetResource_FileOutsideOfClasspath_InvalidFormat() throws Exception JavaDoc {
63     //when using a file outside of classpath the file name must be prepended with "file:"
64
URL JavaDoc url = loader.getResource(String.valueOf(System.currentTimeMillis()));
65     assertNull("URL should be null", url);
66   }
67 }
68
Popular Tags