< prev index next >

test/javax/xml/jaxp/libs/jaxp/library/JAXPTestUtilities.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -20,10 +20,12 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 package jaxp.library;
 
+import static org.testng.Assert.fail;
+
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;

@@ -32,25 +34,30 @@
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.security.Permission;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
-import static org.testng.Assert.fail;
+
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
 
 /**

@@ -87,11 +94,11 @@
                 = new ConcurrentHashMap<>();
 
     /**
      * BOM table for storing BOM header.
      */
-    private final static Map<String, byte[]> bom = new HashMap<>();
+    private final static Map<String, byte[]> bom = new HashMap();
 
     /**
      * Initialize all BOM headers.
      */
     static {

@@ -128,12 +135,19 @@
      * @throws IOException if an I/O error occurs reading from the file or a
      *         malformed or unmappable byte sequence is read.
      */
     public static boolean compareWithGold(String goldfile, String outputfile,
              Charset cs) throws IOException {
-        return Files.readAllLines(Paths.get(goldfile)).
+        boolean isSame = Files.readAllLines(Paths.get(goldfile)).
                 equals(Files.readAllLines(Paths.get(outputfile), cs));
+        if (!isSame) {
+            System.err.println("Golden file " + goldfile + " :");
+            Files.readAllLines(Paths.get(goldfile)).forEach(System.err::println);
+            System.err.println("Output file " + outputfile + " :");
+            Files.readAllLines(Paths.get(outputfile), cs).forEach(System.err::println);
+        }
+        return isSame;
     }
 
     /**
      * Compare contents of golden file with test output list line by line.
      * return true if they're identical.

@@ -306,14 +320,14 @@
      */
     public static String getNextFile(Class clazz) {
         int nextNumber = currentFileNumber.contains(clazz)
                 ? currentFileNumber.get(clazz) + 1 : 1;
         Integer i = currentFileNumber.putIfAbsent(clazz, nextNumber);
-        if (i != null && i != nextNumber) {
+        if (i != null) {
             do {
                 nextNumber = currentFileNumber.get(clazz) + 1;
-            } while (currentFileNumber.replace(clazz, nextNumber -1, nextNumber));
+            } while (!currentFileNumber.replace(clazz, nextNumber - 1, nextNumber));
         }
         return USER_DIR + clazz.getName() + nextNumber + ".out";
     }
 
     /**

@@ -330,6 +344,96 @@
                 + packageName + FILE_SEP;
         String normalizedPath = Paths.get(javaSourcePath, relativeDir).normalize().
                 toAbsolutePath().toString();
         return normalizedPath.replace("\\", FILE_SEP) + FILE_SEP;
     }
+
+
+    /**
+     * Run the RunnableWithException with creating a JAXPPolicyManager and
+     * assigning temporary permissions. It's not thread-safe to use this
+     * function.
+     * 
+     * @param r
+     *            RunnableWithException to execute
+     * @param ps
+     *            assigning permissions to add.
+     */
+    public static void tryRunWithPolicyManager(RunnableWithException r, Permission... ps) throws Exception {
+        JAXPPolicyManager policyManager = JAXPPolicyManager.getJAXPPolicyManager(true);
+        if (policyManager != null)
+            Stream.of(ps).forEach(p -> policyManager.addTmpPermission(p));
+        try {
+            r.run();
+        } finally {
+            JAXPPolicyManager.teardownPolicyManager();
+        }
+    }
+
+    /**
+     * Run the runnable with assigning temporary permissions. This won't impact
+     * global policy.
+     * 
+     * @param r
+     *            Runnable to run
+     * @param ps
+     *            assigning permissions to add.
+     */
+    public static void runWithTmpPermission(Runnable r, Permission... ps) {
+        JAXPPolicyManager policyManager = JAXPPolicyManager.getJAXPPolicyManager(false);
+        List<Integer> tmpPermissionIndexes = new ArrayList();
+        if (policyManager != null)
+            Stream.of(ps).forEach(p -> tmpPermissionIndexes.add(policyManager.addTmpPermission(p)));
+        try {
+            r.run();
+        } finally {
+            tmpPermissionIndexes.forEach(index -> policyManager.removeTmpPermission(index));
+        }
+    }
+
+    /**
+     * Run the supplier with assigning temporary permissions. This won't impact
+     * global policy.
+     * 
+     * @param s
+     *            Supplier to run
+     * @param ps
+     *            assigning permissions to add.
+     */
+    public static <T> T runWithTmpPermission(Supplier<T> s, Permission... ps) {
+        JAXPPolicyManager policyManager = JAXPPolicyManager.getJAXPPolicyManager(false);
+        List<Integer> tmpPermissionIndexes = new ArrayList();
+        if (policyManager != null)
+            Stream.of(ps).forEach(p -> tmpPermissionIndexes.add(policyManager.addTmpPermission(p)));
+        try {
+            return s.get();
+        } finally {
+            tmpPermissionIndexes.forEach(index -> policyManager.removeTmpPermission(index));
+        }
+    }
+
+    /**
+     * Run the RunnableWithException with assigning temporary permissions. This won't impact
+     * global policy.
+     * 
+     * @param r
+     *            RunnableWithException to execute
+     * @param ps
+     *            assigning permissions to add.
+     */
+    public static void tryRunWithTmpPermission(RunnableWithException r, Permission... ps) throws Exception {
+        JAXPPolicyManager policyManager = JAXPPolicyManager.getJAXPPolicyManager(false);
+        List<Integer> tmpPermissionIndexes = new ArrayList();
+        if (policyManager != null)
+            Stream.of(ps).forEach(p -> tmpPermissionIndexes.add(policyManager.addTmpPermission(p)));
+        try {
+            r.run();
+        } finally {
+            tmpPermissionIndexes.forEach(index -> policyManager.removeTmpPermission(index));
+        }
+    }
+
+    @FunctionalInterface
+    public interface RunnableWithException {
+        void run() throws Exception;
+    }
 }
< prev index next >