JAXPTestUtilities.java revision 653:c4cb73fc93db
1/*
2 * Copyright (c) 2014, 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 */
23package jaxp.library;
24
25
26import java.io.IOException;
27import java.nio.file.Files;
28import java.nio.file.Paths;
29import static org.testng.Assert.fail;
30
31/**
32 * This is an interface provide basic support for JAXP functional test.
33 */
34public 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 = "/";
50
51    /**
52     * User home.
53     */
54    public static final String USER_DIR = System.getProperty("user.dir", ".");
55
56    /**
57     * TEMP file directory.
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}
94