001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.test;
021    
022    import com.google.common.base.Throwables;
023    import org.apache.commons.io.Charsets;
024    import org.apache.commons.io.FileUtils;
025    import org.apache.commons.io.IOUtils;
026    import org.apache.commons.lang.CharUtils;
027    import org.apache.commons.lang.StringUtils;
028    import org.custommonkey.xmlunit.Diff;
029    import org.custommonkey.xmlunit.XMLUnit;
030    import org.sonar.api.utils.SonarException;
031    
032    import java.io.File;
033    import java.io.IOException;
034    import java.net.URL;
035    
036    import static org.junit.Assert.assertTrue;
037    
038    /**
039     * Utilities for unit tests
040     *
041     * @since 2.2
042     */
043    public final class TestUtils {
044    
045      private TestUtils() {
046      }
047    
048      /**
049       * Search for a test resource in the classpath. For example getResource("org/sonar/MyClass/foo.txt");
050       *
051       * @param path the starting slash is optional
052       * @return the resource. Null if resource not found
053       */
054      public static File getResource(String path) {
055        String resourcePath = path;
056        if (!resourcePath.startsWith("/")) {
057          resourcePath = "/" + resourcePath;
058        }
059        URL url = TestUtils.class.getResource(resourcePath);
060        if (url != null) {
061          return FileUtils.toFile(url);
062        }
063        return null;
064      }
065    
066      public static String getResourceContent(String path) {
067        URL url = TestUtils.class.getResource(path);
068        if (url == null) {
069          return null;
070        }
071    
072        try {
073          return IOUtils.toString(url, Charsets.UTF_8);
074        } catch (IOException e) {
075          throw new SonarException("Can not load the resource: " + path, e);
076        }
077      }
078    
079      /**
080       * Search for a resource in the classpath. For example calling the method getResource(getClass(), "myTestName/foo.txt") from
081       * the class org.sonar.Foo loads the file $basedir/src/test/resources/org/sonar/Foo/myTestName/foo.txt
082       *
083       * @return the resource. Null if resource not found
084       */
085      public static File getResource(Class baseClass, String path) {
086        String resourcePath = StringUtils.replaceChars(baseClass.getCanonicalName(), '.', '/');
087        if (!path.startsWith("/")) {
088          resourcePath += "/";
089        }
090        resourcePath += path;
091        return getResource(resourcePath);
092      }
093    
094      /**
095       * Shortcut for getTestTempDir(baseClass, testName, true) : cleans the unit test directory
096       */
097      public static File getTestTempDir(Class baseClass, String testName) {
098        return getTestTempDir(baseClass, testName, true);
099      }
100    
101      /**
102       * Create a temporary directory for unit tests.
103       *
104       * @param baseClass the unit test class
105       * @param testName  the test name
106       * @param clean     remove all the sub-directories and files ?
107       */
108      public static File getTestTempDir(Class baseClass, String testName, boolean clean) {
109        File dir = new File("target/test-tmp/" + baseClass.getCanonicalName() + "/" + testName);
110        if (clean && dir.exists()) {
111          try {
112            FileUtils.deleteDirectory(dir);
113          } catch (IOException e) {
114            throw new SonarException("Can not delete the directory " + dir, e);
115          }
116        }
117        try {
118          FileUtils.forceMkdir(dir);
119        } catch (IOException e) {
120          throw new SonarException("Can not create the directory " + dir, e);
121        }
122        return dir;
123      }
124    
125      public static void assertSimilarXml(String expectedXml, String xml) {
126        Diff diff = isSimilarXml(expectedXml, xml);
127        String message = "Diff: " + diff.toString() + CharUtils.LF + "XML: " + xml;
128        assertTrue(message, diff.similar());
129      }
130    
131      static Diff isSimilarXml(String expectedXml, String xml) {
132        XMLUnit.setIgnoreWhitespace(true);
133        try {
134          return XMLUnit.compareXML(xml, expectedXml);
135        } catch (Exception e) {
136          throw Throwables.propagate(e);
137        }
138      }
139    }