xref: /trunk/main/connectivity/qa/complex/sdbc/ToolsTest.java (revision bb3a5a855831b861943c55afcadf2a8cf42eff01)
145c5a001SPiotr P. Karwasz /**************************************************************
245c5a001SPiotr P. Karwasz  *
345c5a001SPiotr P. Karwasz  * Licensed to the Apache Software Foundation (ASF) under one
445c5a001SPiotr P. Karwasz  * or more contributor license agreements.  See the NOTICE file
545c5a001SPiotr P. Karwasz  * distributed with this work for additional information
645c5a001SPiotr P. Karwasz  * regarding copyright ownership.  The ASF licenses this file
745c5a001SPiotr P. Karwasz  * to you under the Apache License, Version 2.0 (the
845c5a001SPiotr P. Karwasz  * "License"); you may not use this file except in compliance
945c5a001SPiotr P. Karwasz  * with the License.  You may obtain a copy of the License at
1045c5a001SPiotr P. Karwasz  *
1145c5a001SPiotr P. Karwasz  *   http://www.apache.org/licenses/LICENSE-2.0
1245c5a001SPiotr P. Karwasz  *
1345c5a001SPiotr P. Karwasz  * Unless required by applicable law or agreed to in writing,
1445c5a001SPiotr P. Karwasz  * software distributed under the License is distributed on an
1545c5a001SPiotr P. Karwasz  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1645c5a001SPiotr P. Karwasz  * KIND, either express or implied.  See the License for the
1745c5a001SPiotr P. Karwasz  * specific language governing permissions and limitations
1845c5a001SPiotr P. Karwasz  * under the License.
1945c5a001SPiotr P. Karwasz  *
2045c5a001SPiotr P. Karwasz  *************************************************************/
2145c5a001SPiotr P. Karwasz package complex.sdbc;
2245c5a001SPiotr P. Karwasz 
2345c5a001SPiotr P. Karwasz import static org.junit.Assert.assertEquals;
2445c5a001SPiotr P. Karwasz import static org.junit.Assert.assertTrue;
2545c5a001SPiotr P. Karwasz 
2645c5a001SPiotr P. Karwasz import java.net.URL;
2745c5a001SPiotr P. Karwasz import java.util.ArrayList;
2845c5a001SPiotr P. Karwasz import java.util.List;
2945c5a001SPiotr P. Karwasz 
3045c5a001SPiotr P. Karwasz import org.junit.Test;
3145c5a001SPiotr P. Karwasz 
3245c5a001SPiotr P. Karwasz import com.sun.star.comp.sdbc.Tools;
3345c5a001SPiotr P. Karwasz 
3445c5a001SPiotr P. Karwasz /**
3545c5a001SPiotr P. Karwasz  * Tests for the {@link Tools} helper class.
3645c5a001SPiotr P. Karwasz  */
3745c5a001SPiotr P. Karwasz public final class ToolsTest {
3845c5a001SPiotr P. Karwasz 
3945c5a001SPiotr P. Karwasz     /** Runs each entry through addClassPathURL and returns what was accepted. */
collect(String... entries)4045c5a001SPiotr P. Karwasz     private static List<URL> collect(String... entries) {
4145c5a001SPiotr P. Karwasz         List<URL> urls = new ArrayList<URL>();
4245c5a001SPiotr P. Karwasz         for (String entry : entries) {
4345c5a001SPiotr P. Karwasz             Tools.addClassPathURL(urls, entry);
4445c5a001SPiotr P. Karwasz         }
4545c5a001SPiotr P. Karwasz         return urls;
4645c5a001SPiotr P. Karwasz     }
4745c5a001SPiotr P. Karwasz 
4845c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLAddsLocalFileEntry()4945c5a001SPiotr P. Karwasz     public void testAddClassPathURLAddsLocalFileEntry() {
5045c5a001SPiotr P. Karwasz         List<URL> urls = collect("file:/opt/a.jar");
5145c5a001SPiotr P. Karwasz         assertEquals(1, urls.size());
5245c5a001SPiotr P. Karwasz         assertEquals("file", urls.get(0).getProtocol());
5345c5a001SPiotr P. Karwasz     }
5445c5a001SPiotr P. Karwasz 
5545c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLTreatsSchemeCaseInsensitively()56*bb3a5a85SPiotr P. Karwasz     public void testAddClassPathURLTreatsSchemeCaseInsensitively() {
57*bb3a5a85SPiotr P. Karwasz         assertEquals(1, collect("FILE:/opt/a.jar").size());
5845c5a001SPiotr P. Karwasz     }
5945c5a001SPiotr P. Karwasz 
6045c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsJarWrappedLocalFile()61*bb3a5a85SPiotr P. Karwasz     public void testAddClassPathURLSkipsJarWrappedLocalFile() {
62*bb3a5a85SPiotr P. Karwasz         // A JAR file is added by its own file: URL; a jar: URL is never needed.
63*bb3a5a85SPiotr P. Karwasz         assertTrue(collect("jar:file:/opt/a.jar!/").isEmpty());
6445c5a001SPiotr P. Karwasz     }
6545c5a001SPiotr P. Karwasz 
6645c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsRemoteEntries()6745c5a001SPiotr P. Karwasz     public void testAddClassPathURLSkipsRemoteEntries() {
6845c5a001SPiotr P. Karwasz         assertTrue(collect(
6945c5a001SPiotr P. Karwasz                 "http://host/a.jar",
7045c5a001SPiotr P. Karwasz                 "https://host/b.jar",
7145c5a001SPiotr P. Karwasz                 "ftp://host/c.jar").isEmpty());
7245c5a001SPiotr P. Karwasz     }
7345c5a001SPiotr P. Karwasz 
7445c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsJarWrappedRemoteEntry()7545c5a001SPiotr P. Karwasz     public void testAddClassPathURLSkipsJarWrappedRemoteEntry() {
7645c5a001SPiotr P. Karwasz         assertTrue(collect("jar:http://host/a.jar!/").isEmpty());
7745c5a001SPiotr P. Karwasz     }
7845c5a001SPiotr P. Karwasz 
7945c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsMalformedEntry()8045c5a001SPiotr P. Karwasz     public void testAddClassPathURLSkipsMalformedEntry() {
8145c5a001SPiotr P. Karwasz         assertTrue(collect("::not-a-url::").isEmpty());
8245c5a001SPiotr P. Karwasz     }
8345c5a001SPiotr P. Karwasz 
8445c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsUnknownScheme()8545c5a001SPiotr P. Karwasz     public void testAddClassPathURLSkipsUnknownScheme() {
8645c5a001SPiotr P. Karwasz         // A scheme with no registered URL handler cannot be constructed, so the
8745c5a001SPiotr P. Karwasz         // entry is treated as malformed and skipped.
8845c5a001SPiotr P. Karwasz         assertTrue(collect(
8945c5a001SPiotr P. Karwasz                 "wibble:/opt/a.jar",
9045c5a001SPiotr P. Karwasz                 "classpath:/opt/b.jar",
9145c5a001SPiotr P. Karwasz                 "foo://host/c.jar").isEmpty());
9245c5a001SPiotr P. Karwasz     }
9345c5a001SPiotr P. Karwasz 
9445c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsJarWrappedUnknownScheme()9545c5a001SPiotr P. Karwasz     public void testAddClassPathURLSkipsJarWrappedUnknownScheme() {
9645c5a001SPiotr P. Karwasz         assertTrue(collect("jar:wibble:/opt/a.jar!/").isEmpty());
9745c5a001SPiotr P. Karwasz     }
9845c5a001SPiotr P. Karwasz 
9945c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLAddsLocalhostAndEscapedLocalFile()1007456e17cSPeter     public void testAddClassPathURLAddsLocalhostAndEscapedLocalFile() {
1017456e17cSPeter         assertEquals(1, collect("file://localhost/opt/a.jar").size());
1027456e17cSPeter         assertEquals(1, collect("file:///opt/Program%20Files/a.jar").size());
1037456e17cSPeter         assertEquals(1, collect("file:///opt/a%2Fb/c.jar").size());
1047456e17cSPeter         // One level of decoding only: this names a file called "%5C%5Chost".
1057456e17cSPeter         assertEquals(1, collect("file:///opt/%255C%255Chost/a.jar").size());
1067456e17cSPeter     }
1077456e17cSPeter 
1087456e17cSPeter     @Test
testAddClassPathURLSkipsFileOnAnotherHost()1097456e17cSPeter     public void testAddClassPathURLSkipsFileOnAnotherHost() {
1107456e17cSPeter         assertTrue(collect(
1117456e17cSPeter                 "file://host/share/a.jar",
1127456e17cSPeter                 "file://wsl.localhost/distro/a.jar",
1137456e17cSPeter                 "jar:file://host/share/b.jar!/").isEmpty());
1147456e17cSPeter     }
1157456e17cSPeter 
1167456e17cSPeter     @Test
testAddClassPathURLSkipsPathNamingAnotherHost()1177456e17cSPeter     public void testAddClassPathURLSkipsPathNamingAnotherHost() {
1187456e17cSPeter         assertTrue(collect(
1197456e17cSPeter                 "file:////host/share/a.jar",
1207456e17cSPeter                 "file://localhost//host/share/b.jar",
1217456e17cSPeter                 "jar:file:////host/share/c.jar!/").isEmpty());
1227456e17cSPeter     }
1237456e17cSPeter 
1247456e17cSPeter     @Test
testAddClassPathURLSkipsEscapedSeparatorNamingAnotherHost()1257456e17cSPeter     public void testAddClassPathURLSkipsEscapedSeparatorNamingAnotherHost() {
1267456e17cSPeter         // The file: handler decodes the path and, on Windows, turns slashes
1277456e17cSPeter         // into backslashes, so each of these would be opened as \\host\share.
1287456e17cSPeter         assertTrue(collect(
1297456e17cSPeter                 "file:///%5C%5Chost/share/a.jar",
1307456e17cSPeter                 "file:///%5c%5chost/share/b.jar",
1317456e17cSPeter                 "file:///%2F%2Fhost/share/c.jar",
1327456e17cSPeter                 "file:/%2Fhost/share/d.jar",
1337456e17cSPeter                 "file://localhost/%5C%5Chost/share/e.jar",
1347456e17cSPeter                 "jar:file:///%5C%5Chost/share/f.jar!/").isEmpty());
1357456e17cSPeter     }
1367456e17cSPeter 
1377456e17cSPeter     @Test
testAddClassPathURLSkipsBackslash()1387456e17cSPeter     public void testAddClassPathURLSkipsBackslash() {
1397456e17cSPeter         assertTrue(collect(
1407456e17cSPeter                 "file:\\\\host\\share\\a.jar",
1417456e17cSPeter                 "file:///C:%5C..%5Cb.jar").isEmpty());
1427456e17cSPeter     }
1437456e17cSPeter 
1447456e17cSPeter     @Test
testAddClassPathURLKeepsOnlyLocalEntriesInOrder()14545c5a001SPiotr P. Karwasz     public void testAddClassPathURLKeepsOnlyLocalEntriesInOrder() {
14645c5a001SPiotr P. Karwasz         List<URL> urls = collect(
14745c5a001SPiotr P. Karwasz                 "http://host/a.jar",
14845c5a001SPiotr P. Karwasz                 "file:/opt/b.jar",
14945c5a001SPiotr P. Karwasz                 "jar:http://host/c.jar!/",
15045c5a001SPiotr P. Karwasz                 "file:/opt/d.jar");
15145c5a001SPiotr P. Karwasz         assertEquals(2, urls.size());
15245c5a001SPiotr P. Karwasz         assertEquals("/opt/b.jar", urls.get(0).getPath());
15345c5a001SPiotr P. Karwasz         assertEquals("/opt/d.jar", urls.get(1).getPath());
15445c5a001SPiotr P. Karwasz     }
15545c5a001SPiotr P. Karwasz 
15645c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLAddsPathWithPlus()157*bb3a5a85SPiotr P. Karwasz     public void testAddClassPathURLAddsPathWithPlus() {
158*bb3a5a85SPiotr P. Karwasz         List<URL> urls = collect("file:/opt/c++/a.jar");
159*bb3a5a85SPiotr P. Karwasz         assertEquals(1, urls.size());
160*bb3a5a85SPiotr P. Karwasz         assertEquals("/opt/c++/a.jar", urls.get(0).getPath());
16145c5a001SPiotr P. Karwasz     }
16245c5a001SPiotr P. Karwasz 
16345c5a001SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsMalformedEscape()164*bb3a5a85SPiotr P. Karwasz     public void testAddClassPathURLSkipsMalformedEscape() {
165*bb3a5a85SPiotr P. Karwasz         assertTrue(collect(
166*bb3a5a85SPiotr P. Karwasz                 "file:/opt/%zz.jar",
167*bb3a5a85SPiotr P. Karwasz                 "file:/opt/a.jar%").isEmpty());
168*bb3a5a85SPiotr P. Karwasz     }
169*bb3a5a85SPiotr P. Karwasz 
170*bb3a5a85SPiotr P. Karwasz     @Test
testAddClassPathURLSkipsJvmImageSchemes()171*bb3a5a85SPiotr P. Karwasz     public void testAddClassPathURLSkipsJvmImageSchemes() {
172*bb3a5a85SPiotr P. Karwasz         // Unsupported schemes fail to parse and are skipped as well.
173*bb3a5a85SPiotr P. Karwasz         assertTrue(collect(
174*bb3a5a85SPiotr P. Karwasz                 "jrt:/java.base/module-info.class",
175*bb3a5a85SPiotr P. Karwasz                 "jmod:/x").isEmpty());
17645c5a001SPiotr P. Karwasz     }
17745c5a001SPiotr P. Karwasz }
178