01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.resource;
19:
20: import java.io.BufferedReader;
21: import java.io.File;
22: import java.io.FileWriter;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.InputStreamReader;
26: import java.net.URL;
27: import java.net.URLClassLoader;
28:
29: import org.junit.After;
30: import org.junit.Assert;
31: import org.junit.Before;
32: import org.junit.Test;
33:
34: public class ClassLoaderResolverTest extends Assert {
35: private static final String RESOURCE_DATA = "this is the resource data";
36:
37: private String resourceName;
38: private ClassLoaderResolver clr;
39:
40: @Before
41: public void setUp() throws IOException {
42: File resource = File.createTempFile("test", "resource");
43: resource.deleteOnExit();
44: resourceName = resource.getName();
45:
46: FileWriter writer = new FileWriter(resource);
47: writer.write(RESOURCE_DATA);
48: writer.write("\n");
49: writer.close();
50:
51: URL[] urls = { resource.getParentFile().toURI().toURL() };
52: ClassLoader loader = new URLClassLoader(urls);
53: assertNotNull(loader.getResourceAsStream(resourceName));
54: assertNull(ClassLoader.getSystemResourceAsStream(resourceName));
55: clr = new ClassLoaderResolver(loader);
56: }
57:
58: @After
59: public void tearDown() {
60: clr = null;
61: resourceName = null;
62: }
63:
64: @Test
65: public void testResolve() {
66: assertNull(clr.resolve(resourceName, null));
67: assertNotNull(clr.resolve(resourceName, URL.class));
68: }
69:
70: @Test
71: public void testGetAsStream() throws IOException {
72: InputStream in = clr.getAsStream(resourceName);
73: assertNotNull(in);
74:
75: BufferedReader reader = new BufferedReader(
76: new InputStreamReader(in));
77: String content = reader.readLine();
78:
79: assertEquals("resource content incorrect", RESOURCE_DATA,
80: content);
81: }
82:
83: }
|