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