<artifactId>elasticsearch-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>xmlunit</groupId>
+ <artifactId>xmlunit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
package org.sonar.server.debt;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.SystemUtils;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assert.assertTrue;
import static org.sonar.server.debt.DebtModelXMLExporter.DebtModel;
import static org.sonar.server.debt.DebtModelXMLExporter.RuleDebt;
.setSubCharacteristicKey("MEMORY_USE").setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setCoefficient("3d").setOffset("15min")
);
- TestUtils.assertSimilarXml(getFileContent("export_xml.xml"), xmlExporter.export(debtModel, rules));
+ assertSimilarXml(getFileContent("export_xml.xml"), xmlExporter.export(debtModel, rules));
+ }
+
+ public static void assertSimilarXml(String expectedXml, String xml) throws Exception {
+ XMLUnit.setIgnoreWhitespace(true);
+ Diff diff = XMLUnit.compareXML(xml, expectedXml);
+ String message = "Diff: " + diff.toString() + CharUtils.LF + "XML: " + xml;
+ assertTrue(message, diff.similar());
}
@Test
<version>${project.version}</version>
<type>test-jar</type>
</dependency>
- <dependency>
- <groupId>xmlunit</groupId>
- <artifactId>xmlunit</artifactId>
- </dependency>
</dependencies>
</project>
*/
package org.sonar.test;
-import com.google.common.base.Throwables;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
import org.sonar.api.utils.SonarException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
-import static org.junit.Assert.assertTrue;
-
/**
* Utilities for unit tests
*
resourcePath += path;
return getResource(resourcePath);
}
-
- /**
- * Shortcut for getTestTempDir(baseClass, testName, true) : cleans the unit test directory
- */
- public static File getTestTempDir(Class baseClass, String testName) {
- return getTestTempDir(baseClass, testName, true);
- }
-
- /**
- * Create a temporary directory for unit tests.
- *
- * @param baseClass the unit test class
- * @param testName the test name
- * @param clean remove all the sub-directories and files ?
- */
- public static File getTestTempDir(Class baseClass, String testName, boolean clean) {
- File dir = new File("target/test-tmp/" + baseClass.getCanonicalName() + "/" + testName);
- if (clean && dir.exists()) {
- try {
- FileUtils.deleteDirectory(dir);
- } catch (IOException e) {
- throw new SonarException("Can not delete the directory " + dir, e);
- }
- }
- try {
- FileUtils.forceMkdir(dir);
- } catch (IOException e) {
- throw new SonarException("Can not create the directory " + dir, e);
- }
- return dir;
- }
-
- public static void assertSimilarXml(String expectedXml, String xml) {
- Diff diff = isSimilarXml(expectedXml, xml);
- String message = "Diff: " + diff.toString() + CharUtils.LF + "XML: " + xml;
- assertTrue(message, diff.similar());
- }
-
- static Diff isSimilarXml(String expectedXml, String xml) {
- XMLUnit.setIgnoreWhitespace(true);
- try {
- return XMLUnit.compareXML(xml, expectedXml);
- } catch (Exception e) {
- throw Throwables.propagate(e);
- }
- }
}
*/
package org.sonar.test;
-import static org.fest.assertions.Assertions.assertThat;
-import static org.sonar.test.TestUtils.assertSimilarXml;
-import static org.sonar.test.TestUtils.getResource;
-import static org.sonar.test.TestUtils.getTestTempDir;
-import static org.sonar.test.TestUtils.isSimilarXml;
+import org.junit.Test;
import java.io.File;
-import org.apache.commons.io.FileUtils;
-import org.junit.Test;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.sonar.test.TestUtils.getResource;
public class TestUtilsTest {
File file = getResource("org/sonar/test/TestUtilsTest/unknown.txt");
assertThat(file).isNull();
}
-
- @Test
- public void testTempDir() throws Exception {
- File dir = getTestTempDir(getClass(), "testTempDir");
- assertThat(dir).exists().isDirectory();
- assertThat(dir.listFiles()).isEmpty();
-
- FileUtils.writeStringToFile(new File(dir, "bar.txt"), "some text");
- assertThat(dir.listFiles()).hasSize(1);
-
- // the directory is cleaned
- dir = getTestTempDir(getClass(), "testTempDir");
- assertThat(dir).exists().isDirectory();
- assertThat(dir.listFiles()).isEmpty();
- }
-
- @Test
- public void testAssertSimilarXml() throws Exception {
- assertSimilarXml("<foo></foo>", "<foo />");
-
- // order of attributes
- assertSimilarXml("<foo><bar id='1' key='one' /></foo>", "<foo><bar key='one' id='1' /></foo>");
-
- // whitespaces are ignored
- assertSimilarXml("<foo> <bar /> </foo>", "<foo><bar/></foo>");
-
- // attribute values are checked
- assertThat(isSimilarXml("<foo id='1' />", "<foo id='2'/>").similar()).isFalse();
-
- // different xml
- assertThat(isSimilarXml("<foo id='1' />", "<foo id='2'/>").similar()).isFalse();
-
- // order of nodes is important
- assertThat(isSimilarXml("<foo><bar id='1' /><bar id='2' /></foo>", "<foo><bar id='2' /><bar id='1' /></foo>").similar()).isFalse();
- }
}