Skip to content

Commit

Permalink
Add test coverage with jacoco. Use Junit 5 instead of assertj.
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Feb 23, 2024
1 parent 87cb1f4 commit 98a2cfa
Show file tree
Hide file tree
Showing 27 changed files with 754 additions and 629 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
run: mvn -Pcoverage -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<sonar.language>java</sonar.language>
<sonar.java.source>${java-version}</sonar.java.source>
<sonar.test.exclusions>src/test/**/*</sonar.test.exclusions>
<!--suppress UnresolvedMavenProperty -->
<sonar.coverage.jacoco.xmlReportPaths>${maven.multiModuleProjectDirectory}/tests/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<modules>
<module>weasis-parent</module>
Expand All @@ -36,5 +38,6 @@
<module>weasis-dicom</module>
<module>weasis-acquire</module>
<module>weasis-launcher</module>
<module>tests</module>
</modules>
</project>
72 changes: 72 additions & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.weasis</groupId>
<artifactId>weasis-framework</artifactId>
<!--suppress MavenPropertyInParent -->
<version>${revision}${changelist}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>tests</artifactId>
<name>Tests</name>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>
<include>org/weasis/core/**/*.*</include>
<include>org/weasis/acquire/**/*.*</include>
<include>org/weasis/dicom/**/*.*</include>
</includes>
<excludes>
<exclude>**/package-info.java</exclude>
<!-- Exclude weasis-dicom-tools library-->
<exclude>org/weasis/dicom/mf/*.*</exclude>
<exclude>org/weasis/dicom/op/*.*</exclude>
<exclude>org/weasis/dicom/param/*.*</exclude>
<exclude>org/weasis/dicom/tool/*.*</exclude>
<exclude>org/weasis/dicom/util/*.*</exclude>
<exclude>org/weasis/dicom/web/*.*</exclude>
<!-- Exclude weasis-core-img library-->
<exclude>org/weasis/core/util/*.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>report</id>
<goals>
<goal>report-aggregate</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.weasis.acquire</groupId>
<artifactId>weasis-acquire-explorer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.weasis.dicom</groupId>
<artifactId>weasis-dicom-codec</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.weasis.core</groupId>
<artifactId>weasis-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
25 changes: 25 additions & 0 deletions tests/src/test/java/org/weasis/tests/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.weasis.tests;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.LocalDate;
import org.junit.jupiter.api.Test;
import org.weasis.dicom.codec.TagD;

public class IntegrationTest {

@Test
public void integrationTest1() {
LocalDate date1 = TagD.getDicomDate("19930822");
assertThat(date1).isEqualTo(LocalDate.of(1993, 8, 22));
}
}
10 changes: 1 addition & 9 deletions weasis-acquire/weasis-acquire-explorer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,10 @@
<bnd>
-includeresource.all: lib/=target/dependency/;lib:=true
Export-Package: org.weasis.acquire.explorer.*
-exportcontents:net.samuelcampos.usbdrivedetector.*
</bnd>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Xshare:off
</argLine>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
package org.weasis.acquire.explorer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.weasis.core.api.image.util.Unit;
Expand All @@ -20,16 +20,16 @@ class AcquireImageValuesTest {
void testConstructor() {
AcquireImageValues a1 = new AcquireImageValues();

assertThat(a1).isNotNull();
assertThat(a1.getBrightness()).isZero();
assertThat(a1.getCalibrationRatio()).isEqualTo(1.0);
assertThat(a1.getCalibrationUnit()).isEqualTo(Unit.PIXEL);
assertThat(a1.getContrast()).isEqualTo(100);
assertThat(a1.getCropZone()).isNull();
assertThat(a1.getFullRotation()).isZero();
assertThat(a1.getLayerOffset()).isNull();
assertThat(a1.getOrientation()).isZero();
assertThat(a1.getRotation()).isZero();
assertThat(a1.isAutoLevel()).isFalse();
assertNotNull(a1);
assertEquals(0, a1.getBrightness());
assertEquals(1.0, a1.getCalibrationRatio());
assertEquals(Unit.PIXEL, a1.getCalibrationUnit());
assertEquals(100, a1.getContrast());
assertNull(a1.getCropZone());
assertEquals(0, a1.getFullRotation());
assertNull(a1.getLayerOffset());
assertEquals(0, a1.getOrientation());
assertEquals(0, a1.getRotation());
assertFalse(a1.isAutoLevel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
*/
package org.weasis.acquire.explorer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.assertj.core.data.Percentage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -30,26 +29,26 @@ class PublishDicomTaskTest {
void testCalculateRatio() {
Mockito.when(imgInfo.getImage()).thenReturn(imgElt);

assertThat(PublishDicomTask.calculateRatio(null, null)).isNull();
assertThat(PublishDicomTask.calculateRatio(imgInfo, null)).isNull();
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.ORIGINAL)).isNull();
assertNull(PublishDicomTask.calculateRatio(null, null));
assertNull(PublishDicomTask.calculateRatio(imgInfo, null));
assertNull(PublishDicomTask.calculateRatio(imgInfo, Resolution.ORIGINAL));

Mockito.when(imgElt.getTagValue(TagW.ImageWidth)).thenReturn(4000);
Mockito.when(imgElt.getTagValue(TagW.ImageHeight)).thenReturn(2000);
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.ULTRA_HD))
.isCloseTo(0.96, Percentage.withPercentage(1));
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.FULL_HD))
.isCloseTo(0.48, Percentage.withPercentage(1));
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.HD_DVD))
.isCloseTo(0.32, Percentage.withPercentage(1));
assertTrue(isCloseTo(PublishDicomTask.calculateRatio(imgInfo, Resolution.ULTRA_HD), 0.96, 1));
assertTrue(isCloseTo(PublishDicomTask.calculateRatio(imgInfo, Resolution.FULL_HD), 0.48, 1));
assertTrue(isCloseTo(PublishDicomTask.calculateRatio(imgInfo, Resolution.HD_DVD), 0.32, 1));

Mockito.when(imgElt.getTagValue(TagW.ImageWidth)).thenReturn(2000);
Mockito.when(imgElt.getTagValue(TagW.ImageHeight)).thenReturn(4000);
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.ULTRA_HD))
.isCloseTo(0.96, Percentage.withPercentage(1));
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.FULL_HD))
.isCloseTo(0.48, Percentage.withPercentage(1));
assertThat(PublishDicomTask.calculateRatio(imgInfo, Resolution.HD_DVD))
.isCloseTo(0.32, Percentage.withPercentage(1));
assertTrue(isCloseTo(PublishDicomTask.calculateRatio(imgInfo, Resolution.ULTRA_HD), 0.96, 1));
assertTrue(isCloseTo(PublishDicomTask.calculateRatio(imgInfo, Resolution.FULL_HD), 0.48, 1));
assertTrue(isCloseTo(PublishDicomTask.calculateRatio(imgInfo, Resolution.HD_DVD), 0.32, 1));
}

private boolean isCloseTo(Double actual, double expected, double percentage) {
double diff = Math.abs(actual - expected);
double tolerance = expected * percentage / 100.0;
return diff <= tolerance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
*/
package org.weasis.acquire.explorer.core.bean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collections;
import org.assertj.core.api.Assertions;
import org.assertj.core.data.MapEntry;
import java.util.Map;
import org.dcm4che3.data.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -25,15 +26,13 @@ class GlobalTest extends GlobalHelper {
@Test
void testInitWithEmptyTaggable() {
Global global = new Global();

Mockito.when(taggable.getTagEntrySetIterator())
.thenReturn(Collections.EMPTY_MAP.entrySet().iterator());
Mockito.when(taggable.getTagEntrySetIterator()).thenReturn(Collections.emptyIterator());
// Method to test
global.init(taggable);

// Tests
assertThat(global.containTagKey(TagD.get(Tag.StudyInstanceUID))).isTrue();
assertThat(global.getTagValue(TagD.get(Tag.StudyInstanceUID))).isNotNull();
assertTrue(global.containTagKey(TagD.get(Tag.StudyInstanceUID)));
assertNotNull(global.getTagValue(TagD.get(Tag.StudyInstanceUID)));
}

@Test
Expand All @@ -52,21 +51,28 @@ void testInit() {

// Tests
TagW studyTag = TagD.get(Tag.StudyInstanceUID);
assertThat(global.containTagKey(studyTag)).isTrue();
assertTrue(global.containTagKey(studyTag));
global.setTag(studyTag, studyInstanceUIDValue);
assertThat(global.getTagEntrySet())
.containsExactlyInAnyOrder(
Assertions.entry(studyTag, studyInstanceUIDValue),
entry(GlobalTag.patientId),
entry(GlobalTag.patientName),
entry(GlobalTag.issuerOfPatientId),
entry(GlobalTag.patientBirthDate),
entry(GlobalTag.patientSex),
entry(GlobalTag.studyDate),
entry(GlobalTag.modality));
assertTrue(
global
.getTagEntrySet()
.containsAll(
Arrays.asList(
entry(studyTag, studyInstanceUIDValue),
entry(GlobalTag.patientId),
entry(GlobalTag.patientName),
entry(GlobalTag.issuerOfPatientId),
entry(GlobalTag.patientBirthDate),
entry(GlobalTag.patientSex),
entry(GlobalTag.studyDate),
entry(GlobalTag.modality))));
}

private static Map.Entry<TagW, Object> entry(GlobalTag tag) {
return entry(tag.tagW, tag.value);
}

private static MapEntry<TagW, Object> entry(GlobalTag tag) {
return Assertions.entry(tag.tagW, tag.value);
private static Map.Entry<TagW, Object> entry(TagW tag, Object value) {
return new AbstractMap.SimpleEntry<>(tag, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
package org.weasis.acquire.explorer.core.bean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -34,17 +34,17 @@ static void setUp() {

@Test
void testToString() {
assertThat(s1.toString()).hasToString("Other"); // NON-NLS
assertThat(s2.toString())
.hasToString(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(today));
assertThat(s3.toString()).hasToString(TEST_SERIES);
assertEquals("Other", s1.toString());
assertEquals(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(today), s2.toString());
assertEquals(TEST_SERIES, s3.toString());
}

@Test
void testGetters() {
assertThat(s1.getType()).isEqualTo(SeriesGroup.Type.NONE);
assertThat(s2.getType()).isEqualTo(SeriesGroup.Type.DATE);
assertThat(s3.getType()).isEqualTo(SeriesGroup.Type.NAME);
assertEquals(SeriesGroup.Type.NONE, s1.getType());
assertEquals(SeriesGroup.Type.DATE, s2.getType());
assertEquals(SeriesGroup.Type.NAME, s3.getType());
}

@Test
Expand All @@ -67,6 +67,6 @@ void testSort() {
}

private void assetSorted(SeriesGroup[] input, SeriesGroup[] expected) {
assertThat(Arrays.stream(input).sorted().toArray(SeriesGroup[]::new)).isEqualTo(expected);
assertArrayEquals(expected, Arrays.stream(input).sorted().toArray(SeriesGroup[]::new));
}
}
10 changes: 0 additions & 10 deletions weasis-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@
</bnd>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Xshare:off
--add-opens java.desktop/java.beans=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Loading

0 comments on commit 98a2cfa

Please sign in to comment.