xref: /trunk/main/connectivity/qa/complex/sdbc/ToolsTest.java (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package complex.sdbc;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 import org.junit.Assume;
32 import org.junit.Test;
33 
34 import com.sun.star.comp.sdbc.Tools;
35 
36 /**
37  * Tests for the {@link Tools} helper class.
38  */
39 public final class ToolsTest {
40 
41     /** Runs each entry through addClassPathURL and returns what was accepted. */
collect(String... entries)42     private static List<URL> collect(String... entries) {
43         List<URL> urls = new ArrayList<URL>();
44         for (String entry : entries) {
45             Tools.addClassPathURL(urls, entry);
46         }
47         return urls;
48     }
49 
50     /** True if the running JRE has a URL stream handler for the given scheme. */
schemeSupported(String scheme)51     private static boolean schemeSupported(String scheme) {
52         try {
53             new URL(scheme + ":/probe");
54             return true;
55         } catch (MalformedURLException e) {
56             return false;
57         }
58     }
59 
60     @Test
testAddClassPathURLAddsLocalFileEntry()61     public void testAddClassPathURLAddsLocalFileEntry() {
62         List<URL> urls = collect("file:/opt/a.jar");
63         assertEquals(1, urls.size());
64         assertEquals("file", urls.get(0).getProtocol());
65     }
66 
67     @Test
testAddClassPathURLAddsJarWrappedLocalFile()68     public void testAddClassPathURLAddsJarWrappedLocalFile() {
69         assertEquals(1, collect("jar:file:/opt/a.jar!/").size());
70     }
71 
72     @Test
testAddClassPathURLTreatsJarInnerSchemeCaseInsensitively()73     public void testAddClassPathURLTreatsJarInnerSchemeCaseInsensitively() {
74         // URL.getPath() does not normalize the wrapped URL.
75         assertEquals(1, collect("jar:FILE:/opt/a.jar!/").size());
76     }
77 
78     @Test
testAddClassPathURLSkipsRemoteEntries()79     public void testAddClassPathURLSkipsRemoteEntries() {
80         assertTrue(collect(
81                 "http://host/a.jar",
82                 "https://host/b.jar",
83                 "ftp://host/c.jar").isEmpty());
84     }
85 
86     @Test
testAddClassPathURLSkipsJarWrappedRemoteEntry()87     public void testAddClassPathURLSkipsJarWrappedRemoteEntry() {
88         assertTrue(collect("jar:http://host/a.jar!/").isEmpty());
89     }
90 
91     @Test
testAddClassPathURLSkipsMalformedEntry()92     public void testAddClassPathURLSkipsMalformedEntry() {
93         assertTrue(collect("::not-a-url::").isEmpty());
94     }
95 
96     @Test
testAddClassPathURLSkipsUnknownScheme()97     public void testAddClassPathURLSkipsUnknownScheme() {
98         // A scheme with no registered URL handler cannot be constructed, so the
99         // entry is treated as malformed and skipped.
100         assertTrue(collect(
101                 "wibble:/opt/a.jar",
102                 "classpath:/opt/b.jar",
103                 "foo://host/c.jar").isEmpty());
104     }
105 
106     @Test
testAddClassPathURLSkipsJarWrappedUnknownScheme()107     public void testAddClassPathURLSkipsJarWrappedUnknownScheme() {
108         assertTrue(collect("jar:wibble:/opt/a.jar!/").isEmpty());
109     }
110 
111     @Test
testAddClassPathURLKeepsOnlyLocalEntriesInOrder()112     public void testAddClassPathURLKeepsOnlyLocalEntriesInOrder() {
113         List<URL> urls = collect(
114                 "http://host/a.jar",
115                 "file:/opt/b.jar",
116                 "jar:http://host/c.jar!/",
117                 "file:/opt/d.jar");
118         assertEquals(2, urls.size());
119         assertEquals("/opt/b.jar", urls.get(0).getPath());
120         assertEquals("/opt/d.jar", urls.get(1).getPath());
121     }
122 
123     @Test
testAddClassPathURLAddsJrtSchemeWhenSupported()124     public void testAddClassPathURLAddsJrtSchemeWhenSupported() {
125         Assume.assumeTrue(schemeSupported("jrt"));
126         assertEquals(1, collect("jrt:/java.base/module-info.class").size());
127     }
128 
129     @Test
testAddClassPathURLAddsJmodSchemeWhenSupported()130     public void testAddClassPathURLAddsJmodSchemeWhenSupported() {
131         Assume.assumeTrue(schemeSupported("jmod"));
132         assertEquals(1, collect("jmod:/x").size());
133     }
134 }
135