< prev index next >

test/jdk/java/net/URLConnection/ContentHandlers/ContentHandlersTest.java

Print this page

  1 /*
  2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 


 24 import java.io.BufferedReader;
 25 import java.io.BufferedWriter;
 26 import java.io.File;
 27 import java.io.FileReader;
 28 import java.io.FileWriter;
 29 import java.io.IOException;
 30 import java.io.InputStream;
 31 import java.io.InputStreamReader;
 32 import java.io.Reader;
 33 import java.io.SequenceInputStream;
 34 import java.io.StringWriter;
 35 import java.io.Writer;
 36 import java.nio.file.Files;
 37 import java.nio.file.Path;
 38 import java.nio.file.Paths;
 39 import java.util.ArrayList;
 40 import java.util.Arrays;
 41 import java.util.Collection;
 42 import java.util.Collections;
 43 import java.util.Iterator;
 44 import java.util.List;
 45 import java.util.Map;
 46 import java.util.function.Function;
 47 import java.util.stream.Collectors;
 48 import java.util.stream.Stream;
 49 
 50 import static java.lang.String.format;
 51 import static java.util.Arrays.asList;
 52 import static java.util.Collections.emptyMap;
 53 import static java.util.Collections.singleton;
 54 import static java.util.Collections.singletonMap;
 55 
 56 /*
 57  * @test
 58  * @bug 8064925

 59  * @summary Basic test for ContentHandler. Ensures discovery paths for content
 60  *          handlers follow a particular order.
 61  */
 62 public class ContentHandlersTest {
 63 
 64     public static void main(String[] args) throws Throwable {
 65         step1_ContentHandlerFactory();
 66         step2_ServiceLoader();
 67         step3_UserDefined();
 68         step4_BuiltIn();
 69     }
 70 
 71     private static void step1_ContentHandlerFactory() throws IOException {
 72         String factoryClassFqn = "net.java.openjdk.test.TestContentHandlerFactory";
 73 
 74         Path tmp = Files.createDirectory(Paths.get("ContentHandlersTest-1"));
 75 
 76         Path src = templatesHome().resolve("test.template");
 77         Path dst = tmp.resolve("Test.java");
 78         Files.copy(src, dst);

251     private static void javac(Path compilationOutput, Path... sourceFiles) {
252         String javac = getJDKTool("javac");
253         List<String> commands = new ArrayList<>();
254         commands.addAll(asList(javac, "-d", compilationOutput.toString()));
255         List<Path> paths = asList(sourceFiles);
256         commands.addAll(paths.stream()
257                 .map(Path::toString)
258                 .collect(Collectors.toList()));
259         quickFail(run(new ProcessBuilder(commands)));
260     }
261 
262     private static void quickFail(Result r) {
263         if (r.exitValue != 0)
264             throw new RuntimeException(r.output);
265     }
266 
267     private static Result java(Map<String, String> properties,
268                                Collection<Path> classpath,
269                                String classname, String... args) {
270 
271         String java = getJDKTool("java");
272 
273         List<String> commands = new ArrayList<>();
274         commands.add(java);
275         commands.addAll(properties.entrySet()
276                 .stream()
277                 .map(e -> "-D" + e.getKey() + "=" + e.getValue())
278                 .collect(Collectors.toList()));
279 
280         String cp = classpath.stream()
281                 .map(Path::toString)
282                 .collect(Collectors.joining(File.pathSeparator));
283         commands.add("-cp");
284         commands.add(cp);
285         commands.add(classname);
286         commands.addAll(Arrays.asList(args));
287 
288         return run(new ProcessBuilder(commands));
289     }
290 
291     private static Result run(ProcessBuilder b) {
292         Process p;
293         try {
294             p = b.start();
295         } catch (IOException e) {
296             throw new RuntimeException(
297                     format("Couldn't start process '%s'", b.command()), e);
298         }
299 
300         String output;
301         try {
302             output = toString(p.getInputStream(), p.getErrorStream());
303         } catch (IOException e) {
304             throw new RuntimeException(
305                     format("Couldn't read process output '%s'", b.command()), e);
306         }
307 
308         try {

  1 /*
  2  * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import jdk.test.lib.process.ProcessTools;
 25 
 26 import java.io.BufferedReader;
 27 import java.io.BufferedWriter;
 28 import java.io.File;
 29 import java.io.FileReader;
 30 import java.io.FileWriter;
 31 import java.io.IOException;
 32 import java.io.InputStream;
 33 import java.io.InputStreamReader;
 34 import java.io.Reader;
 35 import java.io.SequenceInputStream;
 36 import java.io.StringWriter;
 37 import java.io.Writer;
 38 import java.nio.file.Files;
 39 import java.nio.file.Path;
 40 import java.nio.file.Paths;
 41 import java.util.ArrayList;
 42 import java.util.Arrays;
 43 import java.util.Collection;
 44 import java.util.Collections;
 45 import java.util.Iterator;
 46 import java.util.List;
 47 import java.util.Map;
 48 import java.util.function.Function;
 49 import java.util.stream.Collectors;
 50 import java.util.stream.Stream;
 51 
 52 import static java.lang.String.format;
 53 import static java.util.Arrays.asList;
 54 import static java.util.Collections.emptyMap;
 55 import static java.util.Collections.singleton;
 56 import static java.util.Collections.singletonMap;
 57 
 58 /*
 59  * @test
 60  * @bug 8064925
 61  * @library /test/lib
 62  * @summary Basic test for ContentHandler. Ensures discovery paths for content
 63  *          handlers follow a particular order.
 64  */
 65 public class ContentHandlersTest {
 66 
 67     public static void main(String[] args) throws Throwable {
 68         step1_ContentHandlerFactory();
 69         step2_ServiceLoader();
 70         step3_UserDefined();
 71         step4_BuiltIn();
 72     }
 73 
 74     private static void step1_ContentHandlerFactory() throws IOException {
 75         String factoryClassFqn = "net.java.openjdk.test.TestContentHandlerFactory";
 76 
 77         Path tmp = Files.createDirectory(Paths.get("ContentHandlersTest-1"));
 78 
 79         Path src = templatesHome().resolve("test.template");
 80         Path dst = tmp.resolve("Test.java");
 81         Files.copy(src, dst);

254     private static void javac(Path compilationOutput, Path... sourceFiles) {
255         String javac = getJDKTool("javac");
256         List<String> commands = new ArrayList<>();
257         commands.addAll(asList(javac, "-d", compilationOutput.toString()));
258         List<Path> paths = asList(sourceFiles);
259         commands.addAll(paths.stream()
260                 .map(Path::toString)
261                 .collect(Collectors.toList()));
262         quickFail(run(new ProcessBuilder(commands)));
263     }
264 
265     private static void quickFail(Result r) {
266         if (r.exitValue != 0)
267             throw new RuntimeException(r.output);
268     }
269 
270     private static Result java(Map<String, String> properties,
271                                Collection<Path> classpath,
272                                String classname, String... args) {
273 
274         List<String> commands = properties.entrySet()




275                 .stream()
276                 .map(e -> "-D" + e.getKey() + "=" + e.getValue())
277                 .collect(Collectors.toList());
278 
279         String cp = classpath.stream()
280                 .map(Path::toString)
281                 .collect(Collectors.joining(File.pathSeparator));
282         commands.add("-cp");
283         commands.add(cp);
284         commands.add(classname);
285         commands.addAll(Arrays.asList(args));
286 
287         return run(ProcessTools.createTestJavaProcessBuilder(commands));
288     }
289 
290     private static Result run(ProcessBuilder b) {
291         Process p;
292         try {
293             p = b.start();
294         } catch (IOException e) {
295             throw new RuntimeException(
296                     format("Couldn't start process '%s'", b.command()), e);
297         }
298 
299         String output;
300         try {
301             output = toString(p.getInputStream(), p.getErrorStream());
302         } catch (IOException e) {
303             throw new RuntimeException(
304                     format("Couldn't read process output '%s'", b.command()), e);
305         }
306 
307         try {
< prev index next >