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