Quantcast

[sonar-dev] Comparing and asserting measures

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[sonar-dev] Comparing and asserting measures

Sebastian Marek-2
Guys,

I have a question,

I am writing unit tests for checking whether class complexity distribution metric is saved properly. But I got into a problem that when asserting the Measure being saved it actually ignored the String data inside the measure. Here's an example:

Code:

RangeDistributionBuilder classComplexityDistribution = new RangeDistributionBuilder(
        CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION,
        CLASSES_DISTRIB_BOTTOM_LIMITS
    );
classComplexityDistribution.add(totalClassComplexity);

org.sonar.api.measures.Measure measure = classComplexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY);
      getContext().saveMeasure(
          sonarFile,
          measure
      );

Test:

  @Test
  public void testShouldGenerateClassComplexityDistribution() {
    metric = CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION;
    init(SUMMARY_RESULT);

   verify(context).saveMeasure(new File("Math3.php"), new Measure(metric, "anydata"));
  }

Initially I looked at custom argument matchers, but then I thought equals method on Measure should actually tell me that. So I looked at Measure class and its test and found this:

    // value doesn't matter
    measure1.setValue(1.0);
    measure2.setValue(2.0);
    assertThat(measure1.equals(measure2), is(true));
    assertThat(measure2.equals(measure1), is(true));
    assertThat(measure1.hashCode(), equalTo(measure2.hashCode()));

Which got me thinking that it probably doesn't take value into account for a reason - no matter whether we talk about double or String value. If that's the case what approach would you suggest for testing this?

Regards 
+------
| Sebastian Marek               Broadband Solutions for
| Software Architect            Home & Business @
| Plusnet                       www.plus.net
|
| Registered Office: The Balance, 2 Pinfold Street, Sheffield, S1 2GU
| Registered in England no: 3279013
|
+ ----- PlusNet - The smarter way to broadband ---------|

Loading...