001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.test;
021
022 import org.apache.commons.io.FileUtils;
023 import org.apache.commons.lang.StringUtils;
024 import org.custommonkey.xmlunit.Diff;
025 import org.custommonkey.xmlunit.XMLUnit;
026 import org.xml.sax.SAXException;
027
028 import java.io.File;
029 import java.io.IOException;
030 import java.net.URL;
031
032 import static org.hamcrest.core.Is.is;
033 import static org.junit.Assert.*;
034
035 /**
036 * Utilities for unit tests
037 *
038 * @since 2.2
039 */
040 public class TestUtils {
041
042 /**
043 * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt");
044 *
045 * @param path the starting slash is optional
046 * @return the resource. Null if resource not found
047 */
048 public static File getResource(String path) {
049 String resourcePath = path;
050 if ( !resourcePath.startsWith("/")) {
051 resourcePath = "/" + resourcePath;
052 }
053 URL url = TestUtils.class.getResource(resourcePath);
054 if (url != null) {
055 return FileUtils.toFile(url);
056 }
057 return null;
058 }
059
060 /**
061 * Search for a resource in the classpath. For example calling the method getResource(getClass(), "myTestName/foo.txt") from
062 * the class org.sonar.Foo loads the file $basedir/src/test/resources/org/sonar/Foo/myTestName/foo.txt
063 *
064 * @return the resource. Null if resource not found
065 */
066 public static File getResource(Class baseClass, String path) {
067 String resourcePath = StringUtils.replaceChars(baseClass.getCanonicalName(), '.', '/');
068 if ( !path.startsWith("/")) {
069 resourcePath += "/";
070 }
071 resourcePath += path;
072 return getResource(resourcePath);
073 }
074
075 /**
076 * Shortcut for getTestTempDir(baseClass, testName, true) : cleans the unit test directory
077 */
078 public static File getTestTempDir(Class baseClass, String testName) {
079 return getTestTempDir(baseClass, testName, true);
080 }
081
082 /**
083 * Create a temporary directory for unit tests.
084 * @param baseClass the unit test class
085 * @param testName the test name
086 * @param clean remove all the sub-directories and files ?
087 */
088 public static File getTestTempDir(Class baseClass, String testName, boolean clean) {
089 File dir = new File("target/test-tmp/" + baseClass.getCanonicalName() + "/" + testName);
090 if (clean && dir.exists()) {
091 try {
092 FileUtils.deleteDirectory(dir);
093 } catch (IOException e) {
094 throw new RuntimeException("Can not delete the directory " + dir);
095 }
096 }
097 try {
098 FileUtils.forceMkdir(dir);
099 } catch (IOException e) {
100 throw new RuntimeException("Can not create the directory " + dir);
101 }
102 return dir;
103 }
104
105 /**
106 * Checks that a file or a directory is not null and exists.
107 */
108 public static void assertExists(File file) {
109 assertNotNull(file);
110 assertThat(file.exists(), is(true));
111 }
112
113 public static void assertSimilarXml(String expectedXml, String xml) throws IOException, SAXException {
114 Diff diff = isSimilarXml(expectedXml, xml);
115 assertTrue(diff.toString(), diff.similar());
116 }
117
118 static Diff isSimilarXml(String expectedXml, String xml) throws IOException, SAXException {
119 XMLUnit.setIgnoreWhitespace(true);
120 Diff diff = XMLUnit.compareXML(xml, expectedXml);
121 return diff;
122 }
123 }