From 72afb8a3154d4525d9a956b755d2871a4a5c3d9f Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 14 Jun 2012 14:15:47 +0200 Subject: [PATCH] Remove some violations on unit tests Took the opportunity to migrate some tests to FestAssert. Also removed some duplication --- .../timemachine/TendencyAnalyserTest.java | 104 +++++++++--------- .../sonar/java/bytecode/asm/AsmClassTest.java | 27 +++-- sonar-duplications/pom.xml | 5 + .../utils/FastStringComparatorTest.java | 41 +++---- sonar-graph/pom.xml | 5 + .../test/java/org/sonar/graph/CycleTest.java | 46 ++++---- .../api/qualitymodel/CharacteristicTest.java | 45 ++++---- .../configuration/ProfilesBackupTest.java | 16 ++- .../server/configuration/RulesBackupTest.java | 5 +- sonar-squid/pom.xml | 5 + .../org/sonar/squid/api/SourceCodeTest.java | 13 ++- 11 files changed, 166 insertions(+), 146 deletions(-) diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyAnalyserTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyAnalyserTest.java index 5a285805a9a..9d8011f5e7f 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyAnalyserTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TendencyAnalyserTest.java @@ -22,111 +22,107 @@ package org.sonar.plugins.core.timemachine; import org.junit.Test; import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.*; +import static org.fest.assertions.Assertions.assertThat; public class TendencyAnalyserTest { - private TendencyAnalyser analyser = new TendencyAnalyser(); - - private List getValues(Double[] array) { - return Arrays.asList(array); + static TendencyAnalyser.SlopeData analyse(Double... values) { + return new TendencyAnalyser().analyse(Arrays.asList(values)); } - - protected void assertBetween(String typeLabel, Double value, Double min, Double max) { - assertTrue(typeLabel + " " + value + "<" + min, value >= min); - assertTrue(typeLabel + "=" + value + ">" + max, value <= max); + static Integer analyseLevel(Double... values) { + return new TendencyAnalyser().analyseLevel(Arrays.asList(values)); } @Test public void testNoData() { - assertThat(analyser.analyse(Collections.emptyList()), nullValue()); + TendencyAnalyser.SlopeData slopeData = analyse(); + + assertThat(slopeData).isNull(); } @Test public void testNotEnoughData() { - assertThat(analyser.analyseLevel(Arrays.asList(10.0)), nullValue()); + assertThat(analyseLevel(10.0)).isNull(); } @Test public void testTendencyOnThreeDays() { - Double[] doubles = new Double[]{10.0, null, 9.9}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), -0.5, 0.5); - assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(10.0, null, 9.9); + + assertThat(slopeData.getSlope()).isGreaterThan(-0.5).isLessThan(0.5); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_NEUTRAL); } @Test public void testTendencyOnTwoZeroDays() { - Double[] doubles = new Double[]{0.0, 0.0}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), -0.0, 0.0); - assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(0.0, 0.0); + + assertThat(slopeData.getSlope()).isZero(); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_NEUTRAL); } @Test public void testTendencyOnThreeZeroDays() { - Double[] doubles = new Double[]{0.0, 0.0, 0.0}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), -0.0, 0.0); - assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(0.0, 0.0, 0.0); + + assertThat(slopeData.getSlope()).isZero(); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_NEUTRAL); } @Test public void testBigDownOnThreeDays() { - Double[] doubles = new Double[]{90.0, 91.0, 50.0}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertTrue("slope", slopeData.getSlope() < -2.0); - assertEquals(TendencyAnalyser.TENDENCY_BIG_DOWN, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(90.0, 91.0, 50.0); + + assertThat(slopeData.getSlope()).isLessThan(-2.0); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_BIG_DOWN); } @Test public void testFlatTendency() { - Double[] doubles = new Double[]{10.0, 10.2, 9.9}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), -0.5, 0.5); - assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(10.0, 10.2, 9.9); + + assertThat(slopeData.getSlope()).isGreaterThan(-0.5).isLessThan(0.5); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_NEUTRAL); } @Test public void testFlatTendencyWithPeak() { - Double[] doubles = new Double[]{10.0, 15.0, 10.0}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), -0.5, 0.5); - assertEquals(TendencyAnalyser.TENDENCY_NEUTRAL, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(10.0, 15.0, 10.0); + + assertThat(slopeData.getSlope()).isGreaterThan(-0.5).isLessThan(0.5); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_NEUTRAL); } @Test public void testBigUpTendencyOnThreeValues() { - Double[] doubles = new Double[]{10.0, 12.0, 15.5}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), 2.5, 3.0); - assertEquals(TendencyAnalyser.TENDENCY_BIG_UP, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(10.0, 12.0, 15.5); + + assertThat(slopeData.getSlope()).isGreaterThan(2.5).isLessThan(3.0); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_BIG_UP); } @Test public void testBigUpTendencyOnTenValues() { - Double[] doubles = new Double[]{45.0, 60.0, 57.0, 65.0, 58.0, 68.0, 59.0, 66.0, 76.0, 80.0}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), 2.5, 3.0); - assertEquals(TendencyAnalyser.TENDENCY_BIG_UP, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(45.0, 60.0, 57.0, 65.0, 58.0, 68.0, 59.0, 66.0, 76.0, 80.0); + + assertThat(slopeData.getSlope()).isGreaterThan(2.5).isLessThan(3.0); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_BIG_UP); } @Test public void testMediumUpTendency() { - Double[] doubles = new Double[]{5.0, 4.5, 5.1, 5.5, 5.3, 6.4, 6.3, 6.6, 6.8, 6.5}; - TendencyAnalyser.SlopeData slopeData = analyser.analyse(getValues(doubles)); - assertBetween("slope", slopeData.getSlope(), 0.0, 1.0); - assertEquals(TendencyAnalyser.TENDENCY_UP, slopeData.getLevel()); + TendencyAnalyser.SlopeData slopeData = analyse(5.0, 4.5, 5.1, 5.5, 5.3, 6.4, 6.3, 6.6, 6.8, 6.5); + + assertThat(slopeData.getSlope()).isGreaterThan(0.0).isLessThan(1.0); + assertThat(slopeData.getLevel()).isEqualTo(TendencyAnalyser.TENDENCY_UP); } @Test public void testAsymetricAlgorithm() { - TendencyAnalyser.SlopeData slopeData1 = analyser.analyse(getValues(new Double[]{45.0, 47.0, 95.0})); - TendencyAnalyser.SlopeData slopeData2 = analyser.analyse(getValues(new Double[]{95.0, 45.0, 47.0})); - assertTrue(slopeData1.getSlope() != slopeData2.getSlope()); + TendencyAnalyser.SlopeData slopeData1 = analyse(45.0, 47.0, 95.0); + TendencyAnalyser.SlopeData slopeData2 = analyse(95.0, 45.0, 47.0); + + assertThat(slopeData1.getSlope()).isNotEqualTo(slopeData2.getSlope()); } -} \ No newline at end of file +} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmClassTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmClassTest.java index 6de6bd92ecb..4c6599e4ea6 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmClassTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmClassTest.java @@ -21,34 +21,33 @@ package org.sonar.java.bytecode.asm; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.fest.assertions.Assertions.assertThat; public class AsmClassTest { @Test public void testGetFieldOrCreateIt() { AsmClass asmClass = new AsmClass("java/lang/String"); - assertNull(asmClass.getField("internalString")); - assertNotNull(asmClass.getFieldOrCreateIt("internalString")); - assertNotNull(asmClass.getField("internalString")); + + assertThat(asmClass.getField("internalString")).isNull(); + assertThat(asmClass.getFieldOrCreateIt("internalString")).isNotNull(); + assertThat(asmClass.getField("internalString")).isNotNull(); } @Test public void testGetMethoddOrCreateIt() { AsmClass asmClass = new AsmClass("java/lang/String"); - assertNull(asmClass.getMethod("toString()Ljava/lang/String;")); - assertNotNull(asmClass.getMethodOrCreateIt("toString()Ljava/lang/String;")); - assertNotNull(asmClass.getMethod("toString()Ljava/lang/String;")); + + assertThat(asmClass.getMethod("toString()Ljava/lang/String;")).isNull(); + assertThat(asmClass.getMethodOrCreateIt("toString()Ljava/lang/String;")).isNotNull(); + assertThat(asmClass.getMethod("toString()Ljava/lang/String;")).isNotNull(); } @Test public void testEqualsAndHashcode() { - assertEquals(new AsmClass("java/lang/String"), new AsmClass("java/lang/String")); - assertEquals(new AsmClass("java/lang/String").hashCode(), new AsmClass("java/lang/String").hashCode()); - assertFalse(new AsmClass("java/lang/String").equals(new AsmClass("java/lang/Number"))); - assertFalse(new AsmClass("java/lang/String").hashCode() == new AsmClass("java/lang/Number").hashCode()); + assertThat(new AsmClass("java/lang/String")).isEqualTo(new AsmClass("java/lang/String")); + assertThat(new AsmClass("java/lang/String").hashCode()).isEqualTo(new AsmClass("java/lang/String").hashCode()); + assertThat(new AsmClass("java/lang/String")).isNotEqualTo(new AsmClass("java/lang/Number")); + assertThat(new AsmClass("java/lang/String").hashCode()).isNotEqualTo(new AsmClass("java/lang/Number").hashCode()); } } diff --git a/sonar-duplications/pom.xml b/sonar-duplications/pom.xml index 1b822ab9df4..c5cd45c6742 100644 --- a/sonar-duplications/pom.xml +++ b/sonar-duplications/pom.xml @@ -52,5 +52,10 @@ logback-classic test + + org.easytesting + fest-assert + test + diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/utils/FastStringComparatorTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/utils/FastStringComparatorTest.java index 6da568e4b4e..bc79187bec1 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/utils/FastStringComparatorTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/utils/FastStringComparatorTest.java @@ -19,52 +19,53 @@ */ package org.sonar.duplications.utils; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThan; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - import org.junit.Test; -import org.sonar.duplications.utils.FastStringComparator; + +import static org.fest.assertions.Assertions.assertThat; public class FastStringComparatorTest { + static int compare(String left, String right) { + return FastStringComparator.INSTANCE.compare(left, right); + } @Test public void sameHashCode() { // Next two Strings have same hash code in Java - see http://www.drmaciver.com/2008/07/javalangstringhashcode/ String s1 = "Od"; String s2 = "PE"; - assertTrue("same hash code", s1.hashCode() == s2.hashCode()); - assertThat("s1 < s2", FastStringComparator.INSTANCE.compare(s1, s2), lessThan(0)); - assertThat("s2 > s1", FastStringComparator.INSTANCE.compare(s2, s1), greaterThan(0)); + + assertThat(s1.hashCode()).isEqualTo(s2.hashCode()); + assertThat(compare(s1, s2)).isLessThan(0); + assertThat(compare(s2, s1)).isGreaterThan(0); } @Test public void differentHashCode() { String s1 = "a"; String s2 = "c"; - assertTrue("different hash code", s1.hashCode() != s2.hashCode()); - assertThat("s1 < s2", FastStringComparator.INSTANCE.compare(s1, s2), is(-1)); - assertThat("s2 > s1", FastStringComparator.INSTANCE.compare(s2, s1), is(1)); + + assertThat(s1.hashCode()).isNotEqualTo(s2.hashCode()); + assertThat(compare(s1, s2)).isEqualTo(-1); + assertThat(compare(s2, s1)).isEqualTo(1); } @Test public void sameObject() { String s1 = "a"; String s2 = s1; - assertTrue("same objects", s1 == s2); - assertThat("s1 = s2", FastStringComparator.INSTANCE.compare(s1, s2), is(0)); - assertThat("s2 = s1", FastStringComparator.INSTANCE.compare(s2, s1), is(0)); + + assertThat(s1).isSameAs(s2); + assertThat(compare(s1, s2)).isZero(); + assertThat(compare(s1, s2)).isZero(); } @Test public void sameString() { String s1 = new String("a"); String s2 = new String("a"); - assertTrue("different objects", s1 != s2); - assertThat("s1 = s2", FastStringComparator.INSTANCE.compare(s1, s2), is(0)); - assertThat("s2 = s1", FastStringComparator.INSTANCE.compare(s2, s1), is(0)); - } + assertThat(s1).isNotSameAs(s2); + assertThat(compare(s1, s2)).isZero(); + assertThat(compare(s1, s2)).isZero(); + } } diff --git a/sonar-graph/pom.xml b/sonar-graph/pom.xml index 48a57076b77..0b64f3ed9af 100644 --- a/sonar-graph/pom.xml +++ b/sonar-graph/pom.xml @@ -31,5 +31,10 @@ hamcrest-all test + + org.easytesting + fest-assert + test + \ No newline at end of file diff --git a/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java b/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java index edf9d2905bf..bc823e9d8f8 100644 --- a/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java +++ b/sonar-graph/src/test/java/org/sonar/graph/CycleTest.java @@ -19,40 +19,46 @@ */ package org.sonar.graph; -import java.util.Arrays; - import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import java.util.List; -public class CycleTest { +import static org.fest.assertions.Assertions.assertThat; - private Edge[] AB_Cycle = { new StringEdge("A", "B"), new StringEdge("B", "A") }; - private Edge[] BA_Cycle = { new StringEdge("B", "A"), new StringEdge("A", "B") }; - private Edge[] ABC_Cycle = { new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "A") }; - private Edge[] HIJ_Cycle = { new StringEdge("H", "I"), new StringEdge("I", "J"), new StringEdge("J", "H") }; - private Edge[] ABCD_Cycle = { new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A") }; - private Edge[] BCDA_Cycle = { new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A"), new StringEdge("A", "B"), }; +public class CycleTest { + static List AB_BA = list(new StringEdge("A", "B"), new StringEdge("B", "A")); + static List BA_AB = list(new StringEdge("B", "A"), new StringEdge("A", "B")); + static List AB_BC_CA = list(new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "A")); + static List HI_IJ_JH = list(new StringEdge("H", "I"), new StringEdge("I", "J"), new StringEdge("J", "H")); + static List AB_BC_CD_DA = list(new StringEdge("A", "B"), new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A")); + static List BC_CD_DA_AB = list(new StringEdge("B", "C"), new StringEdge("C", "D"), new StringEdge("D", "A"), new StringEdge("A", "B")); @Test public void testHashCode() { - assertTrue(new Cycle(Arrays.asList(AB_Cycle)).hashCode() == new Cycle(Arrays.asList(BA_Cycle)).hashCode()); - assertTrue(new Cycle(Arrays.asList(BCDA_Cycle)).hashCode() == new Cycle(Arrays.asList(ABCD_Cycle)).hashCode()); - assertFalse(new Cycle(Arrays.asList(AB_Cycle)).hashCode() == new Cycle(Arrays.asList(ABC_Cycle)).hashCode()); + assertThat(new Cycle(AB_BA).hashCode()).isEqualTo(new Cycle(BA_AB).hashCode()); + assertThat(new Cycle(BC_CD_DA_AB).hashCode()).isEqualTo(new Cycle(AB_BC_CD_DA).hashCode()); + assertThat(new Cycle(AB_BA).hashCode()).isNotEqualTo(new Cycle(AB_BC_CA).hashCode()); } @Test public void testContains() { - assertTrue(new Cycle(Arrays.asList(ABCD_Cycle)).contains(new StringEdge("B", "C"))); + assertThat(new Cycle(AB_BC_CD_DA).contains(new StringEdge("B", "C"))).isTrue(); } @Test public void testEqualsObject() { - assertEquals(new Cycle(Arrays.asList(AB_Cycle)), new Cycle(Arrays.asList(BA_Cycle))); - assertEquals(new Cycle(Arrays.asList(BCDA_Cycle)), new Cycle(Arrays.asList(ABCD_Cycle))); - assertFalse(new Cycle(Arrays.asList(BCDA_Cycle)).equals(new Cycle(Arrays.asList(AB_Cycle)))); - assertFalse(new Cycle(Arrays.asList(ABC_Cycle)).equals(new Cycle(Arrays.asList(HIJ_Cycle)))); + assertThat(new Cycle(AB_BA)).isEqualTo(new Cycle(BA_AB)); + assertThat(new Cycle(BC_CD_DA_AB)).isEqualTo(new Cycle(AB_BC_CD_DA)); + } + + @Test + public void testNotEqualsObject() { + assertThat(new Cycle(BC_CD_DA_AB)).isNotEqualTo(new Cycle(AB_BA)); + assertThat(new Cycle(AB_BC_CA)).isNotEqualTo(new Cycle(HI_IJ_JH)); + } + + static List list(StringEdge... edges) { + return Arrays. asList(edges); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java index 5e9c0446dc9..b58d0bdd18a 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java @@ -21,11 +21,7 @@ package org.sonar.api.qualitymodel; import org.junit.Test; -import static junit.framework.Assert.assertTrue; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsNull.nullValue; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; public class CharacteristicTest { @@ -34,12 +30,12 @@ public class CharacteristicTest { Characteristic characteristic = Characteristic.create(); characteristic.setProperty("foo", "bar"); - assertThat(characteristic.getProperty("foo"), notNullValue()); - assertThat(characteristic.getPropertyTextValue("foo", null), is("bar")); - assertThat(characteristic.getPropertyValue("foo", null), nullValue()); + assertThat(characteristic.getProperty("foo")).isNotNull(); + assertThat(characteristic.getPropertyTextValue("foo", null)).isEqualTo("bar"); + assertThat(characteristic.getPropertyValue("foo", null)).isNull(); - assertThat(characteristic.getProperty("unknown"), nullValue()); - assertThat(characteristic.getPropertyTextValue("unknown", null), nullValue()); + assertThat(characteristic.getProperty("unknown")).isNull(); + assertThat(characteristic.getPropertyTextValue("unknown", null)).isNull(); } @Test @@ -47,9 +43,9 @@ public class CharacteristicTest { Characteristic characteristic = Characteristic.create(); characteristic.setProperty("foo", 3.1); - assertThat(characteristic.getProperty("foo"), notNullValue()); - assertThat(characteristic.getPropertyValue("foo", null), is(3.1)); - assertThat(characteristic.getPropertyTextValue("foo", null), nullValue()); + assertThat(characteristic.getProperty("foo")).isNotNull(); + assertThat(characteristic.getPropertyValue("foo", null)).isEqualTo(3.1); + assertThat(characteristic.getPropertyTextValue("foo", null)).isNull(); } @Test @@ -58,28 +54,27 @@ public class CharacteristicTest { characteristic.addProperty(CharacteristicProperty.create("foo")); CharacteristicProperty property = characteristic.getProperty("foo"); - assertThat(property, notNullValue()); - assertTrue(property.getCharacteristic()==characteristic); + assertThat(property).isNotNull(); + assertThat(property.getCharacteristic()).isSameAs(characteristic); } @Test public void shouldCreateByName() { Characteristic characteristic = Characteristic.createByName("Foo"); - assertThat(characteristic.getKey(), is("FOO")); - assertThat(characteristic.getName(), is("Foo")); + + assertThat(characteristic.getKey()).isEqualTo("FOO"); + assertThat(characteristic.getName()).isEqualTo("Foo"); } @Test public void shouldReturnDefaultValues() { Characteristic characteristic = Characteristic.create(); - characteristic.setProperty("foo", (String)null); - characteristic.setProperty("bar", (Double)null); + characteristic.setProperty("foo", (String) null); + characteristic.setProperty("bar", (Double) null); - assertThat(characteristic.getPropertyTextValue("foo", "foodef"), is("foodef")); - assertThat(characteristic.getPropertyTextValue("other", "otherdef"), is("otherdef")); - assertThat(characteristic.getPropertyValue("bar", 3.14), is(3.14)); - assertThat(characteristic.getPropertyValue("other", 3.14), is(3.14)); + assertThat(characteristic.getPropertyTextValue("foo", "foodef")).isEqualTo("foodef"); + assertThat(characteristic.getPropertyTextValue("other", "otherdef")).isEqualTo("otherdef"); + assertThat(characteristic.getPropertyValue("bar", 3.14)).isEqualTo(3.14); + assertThat(characteristic.getPropertyValue("other", 3.14)).isEqualTo(3.14); } - - } diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java index 8533b26b7b3..5cd0cb6b63d 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java @@ -25,17 +25,25 @@ import org.junit.Test; import org.sonar.api.measures.Metric; import org.sonar.api.profiles.Alert; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.*; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.ActiveRuleParam; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleParam; +import org.sonar.api.rules.RulePriority; import org.sonar.jpa.test.AbstractDbUnitTestCase; import org.sonar.test.TestUtils; import java.io.IOException; import java.util.Arrays; +import static org.fest.assertions.Assertions.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.collection.IsCollectionContaining.hasItem; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; public class ProfilesBackupTest extends AbstractDbUnitTestCase { @@ -57,8 +65,8 @@ public class ProfilesBackupTest extends AbstractDbUnitTestCase { ProfilesBackup profilesBackup = new ProfilesBackup(Arrays.asList(profileProvided)); profilesBackup.exportXml(sonarConfig); - assertFalse(sonarConfig.getProfiles().iterator().next() == profileProvided); - assertEquals(sonarConfig.getProfiles().iterator().next().getName(), "provided"); + assertThat(sonarConfig.getProfiles().iterator().next()).isNotSameAs(profileProvided); + assertThat(sonarConfig.getProfiles().iterator().next().getName()).isEqualTo("provided"); } @Test diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java index 5e3f3bada92..49fca44c771 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/RulesBackupTest.java @@ -29,11 +29,11 @@ import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.util.Arrays; +import static org.fest.assertions.Assertions.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; public class RulesBackupTest extends AbstractDbUnitTestCase { @@ -65,8 +65,7 @@ public class RulesBackupTest extends AbstractDbUnitTestCase { RulesBackup rulesBackup = new RulesBackup(Arrays.asList(userRule)); rulesBackup.exportXml(sonarConfig); - assertThat(sonarConfig.getRules().size(), is(1)); - assertTrue(sonarConfig.getRules().iterator().next() == userRule); + assertThat(sonarConfig.getRules()).containsOnly(userRule); } @Test diff --git a/sonar-squid/pom.xml b/sonar-squid/pom.xml index 133ecbd2988..43223a8c387 100644 --- a/sonar-squid/pom.xml +++ b/sonar-squid/pom.xml @@ -57,6 +57,11 @@ hamcrest-all test + + org.easytesting + fest-assert + test + diff --git a/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java b/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java index ea4e916a8ae..6e15fb84e80 100644 --- a/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java +++ b/sonar-squid/src/test/java/org/sonar/squid/api/SourceCodeTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import org.sonar.squid.measures.Measurable; import org.sonar.squid.measures.Metric; +import static org.fest.assertions.Assertions.assertThat; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -62,12 +63,13 @@ public class SourceCodeTest { @Test public void testEqualsAndHashCode() { - assertFalse((prj.equals(pac))); - assertFalse(prj.hashCode() == pac.hashCode()); - assertFalse(prj.equals(new Object())); + assertThat(prj).isNotEqualTo(pac); + assertThat(prj.hashCode()).isNotEqualTo(pac.hashCode()); + assertThat(prj).isNotEqualTo(new Object()); + SourceCode samePac = new SourcePackage("org.sonar"); - assertEquals(pac, samePac); - assertEquals(pac.hashCode(), samePac.hashCode()); + assertThat(pac).isEqualTo(samePac); + assertThat(pac.hashCode()).isEqualTo(samePac.hashCode()); } @Test @@ -111,7 +113,6 @@ public class SourceCodeTest { assertEquals(file, method.getAncestor(SourceFile.class)); } - @Test public void testHasAmongParents() { assertTrue(cla.hasAmongParents(prj)); -- 2.39.5