< prev index next >

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

Print this page




   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 package jaxp.library;
  24 
  25 
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.Paths;



  29 import static org.testng.Assert.fail;


  30 
  31 /**
  32  * This is an interface provide basic support for JAXP functional test.
  33  */
  34 public class JAXPTestUtilities {
  35     /**
  36      * Prefix for error message.
  37      */
  38     public static final String ERROR_MSG_HEADER = "Unexcepted exception thrown:";
  39 
  40     /**
  41      * Prefix for error message on clean up block.
  42      */
  43     public static final String ERROR_MSG_CLEANUP = "Clean up failed on %s";
  44 
  45     /**
  46      * Force using slash as File separator as we always use cygwin to test in
  47      * Windows platform.
  48      */
  49     public static final String FILE_SEP = "/";


  58      */
  59     public static final String TEMP_DIR = System.getProperty("java.io.tmpdir", ".");
  60 
  61     /**
  62      * Compare contents of golden file with test output file line by line.
  63      * return true if they're identical.
  64      * @param goldfile Golden output file name
  65      * @param outputfile Test output file name
  66      * @return true if two files are identical.
  67      *         false if two files are not identical.
  68      * @throws IOException if an I/O error occurs reading from the file or a
  69      *         malformed or unmappable byte sequence is read
  70      */
  71     public static boolean compareWithGold(String goldfile, String outputfile)
  72             throws IOException {
  73         return Files.readAllLines(Paths.get(goldfile)).
  74                 equals(Files.readAllLines(Paths.get(outputfile)));
  75     }
  76 
  77     /**































  78      * Prints error message if an exception is thrown
  79      * @param ex The exception is thrown by test.
  80      */
  81     public static void failUnexpected(Throwable ex) {
  82         fail(ERROR_MSG_HEADER, ex);
  83     }
  84 
  85     /**
  86      * Prints error message if an exception is thrown when clean up a file.
  87      * @param ex The exception is thrown in cleaning up a file.
  88      * @param name Cleaning up file name.
  89      */
  90     public static void failCleanup(IOException ex, String name) {
  91         fail(String.format(ERROR_MSG_CLEANUP, name), ex);
  92     }
  93 }


   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 package jaxp.library;
  24 

  25 import java.io.IOException;
  26 import java.nio.file.Files;
  27 import java.nio.file.Paths;
  28 import javax.xml.parsers.DocumentBuilder;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.parsers.ParserConfigurationException;
  31 import static org.testng.Assert.fail;
  32 import org.w3c.dom.Document;
  33 import org.xml.sax.SAXException;
  34 
  35 /**
  36  * This is an interface provide basic support for JAXP functional test.
  37  */
  38 public class JAXPTestUtilities {
  39     /**
  40      * Prefix for error message.
  41      */
  42     public static final String ERROR_MSG_HEADER = "Unexcepted exception thrown:";
  43 
  44     /**
  45      * Prefix for error message on clean up block.
  46      */
  47     public static final String ERROR_MSG_CLEANUP = "Clean up failed on %s";
  48 
  49     /**
  50      * Force using slash as File separator as we always use cygwin to test in
  51      * Windows platform.
  52      */
  53     public static final String FILE_SEP = "/";


  62      */
  63     public static final String TEMP_DIR = System.getProperty("java.io.tmpdir", ".");
  64 
  65     /**
  66      * Compare contents of golden file with test output file line by line.
  67      * return true if they're identical. 
  68      * @param goldfile Golden output file name
  69      * @param outputfile Test output file name
  70      * @return true if two files are identical.
  71      *         false if two files are not identical.
  72      * @throws IOException if an I/O error occurs reading from the file or a 
  73      *         malformed or unmappable byte sequence is read  
  74      */
  75     public static boolean compareWithGold(String goldfile, String outputfile)
  76             throws IOException {
  77         return Files.readAllLines(Paths.get(goldfile)).
  78                 equals(Files.readAllLines(Paths.get(outputfile)));
  79     }
  80 
  81     /**
  82      * Compare contents of golden file with test output file by their document
  83      * representation. 
  84      * Here we ignore the white space and comments. return true if they're  
  85      * lexical identical. 
  86      * @param goldfile Golden output file name.
  87      * @param resultFile Test output file name.
  88      * @return true if two file's document representation are identical.
  89      *         false if two file's document representation are not identical.
  90      * @throws FactoryConfigurationError in case of service configuration error.
  91      * or if the implementation is not available or cannot be instantiated.
  92      * @throws SAXException If any parse errors occur.
  93      * @throws IOException if an I/O error occurs reading from the file or a 
  94      *         malformed or unmappable byte sequence is read . 
  95      */
  96     public static boolean compareDocumentWithGold(String goldfile, String resultFile) 
  97             throws ParserConfigurationException, SAXException, IOException {
  98         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  99         factory.setNamespaceAware(true);
 100         factory.setCoalescing(true);
 101         factory.setIgnoringElementContentWhitespace(true);
 102         factory.setIgnoringComments(true);
 103         DocumentBuilder db = factory.newDocumentBuilder();
 104 
 105         Document goldD = db.parse(Paths.get(goldfile).toFile());
 106         goldD.normalizeDocument();
 107         Document resultD = db.parse(Paths.get(resultFile).toFile());
 108         resultD.normalizeDocument();
 109         return goldD.isEqualNode(resultD);
 110     }
 111     
 112     /**
 113      * Prints error message if an exception is thrown
 114      * @param ex The exception is thrown by test. 
 115      */
 116     public static void failUnexpected(Throwable ex) {
 117         fail(ERROR_MSG_HEADER, ex);
 118     }
 119 
 120     /**
 121      * Prints error message if an exception is thrown when clean up a file.
 122      * @param ex The exception is thrown in cleaning up a file. 
 123      * @param name Cleaning up file name. 
 124      */
 125     public static void failCleanup(IOException ex, String name) {
 126         fail(String.format(ERROR_MSG_CLEANUP, name), ex);
 127     }   
 128 }
< prev index next >