/* * Copyright (c) 2014, 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. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.xml.testutils; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; /** * Base class of jaxp test, also contains utility methods. */ public abstract class TestBase { public static final String ERROR_MSG_HEADER = "Unexcepted exception thrown!"; public static final String FILE_SEP = System.getProperty("file.separator"); public static final String USER_DIR = System.getProperty("user.dir", "."); public static final String SRC_DIR = System.getProperty("test.src", "."); public static final String TEMP_DIR = System.getProperty("java.io.tmpdir", "."); /** * Compare the contents of 2 files. If equal return true, otherwise, return * false. */ protected boolean compareWithGold(File goldfile, File outputfile) throws IOException { return Files.readAllLines(goldfile.toPath()).equals(Files.readAllLines(outputfile.toPath())); } /** * Print log before test starts */ @BeforeMethod protected void startTest(Method methodName) { System.out.println("\n\n****************************"); System.out.println("Starting test: " + methodName); } /** * Print log after test ends */ @AfterMethod protected void endTest(Method methodName) { System.out.println("Ending test: " + methodName); System.out.println("\n\n****************************"); } /** * This method is called by child tests to prepare xml files for testing. */ protected void setup(String srcDirName, String dstDirName, String... fileNames) throws IOException { for (String fileName : fileNames) { File src = new File(srcDirName, fileName); File dstDir = new File(dstDirName); if (!dstDir.exists()) { dstDir.mkdirs(); } File dst = new File(dstDirName, fileName); Files.copy(src.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING); } } /** * Assert test failed with customized message. * * @param message * @param e */ public static void fail(String message, Throwable e) { Assert.fail(message, e); } /** * Assert test failed with default message. * * @param e */ public static void fail(Throwable e) { fail(ERROR_MSG_HEADER, e); } }