diff --git a/README.md b/README.md index e3736f891bbe88d2628c1a2fa5f305adf71250c0..5c04e73667ad15a7ca1cfb51a903d76f93290c9e 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,13 @@ Les commandes gradle les plus utiles : - `gradle jar` pour construire un `jar` dans `build/libs`. - `gradle checkStyleMain` pour vérifier le style du code principal avec l'outil [checkstyle](https://checkstyle.sourceforge.io/) (rapports dans `build/reports/checkstyle/`). - `gradle checkStyleTest` pour vérifier le style du code de test avec l'outil [checkstyle](https://checkstyle.sourceforge.io/) (rapports dans `build/reports/checkstyle/`). -- `gradle jacocoTestReport` pour lancer la couverture de code via l'outil [Jacoco](https://www.eclemma.org/jacoco/) (rapports dans `build/reports/jacoco/`). +- `gradle jacocoTestReport` pour lancer la couverture de code via l'outil [Jacoco](https://www.eclemma.org/jacoco/) (rapports dans `build/reports/jacoco/`). +- `gradle spotbugsMain` pour vérifier la présence de bugs dans le code principal avec l'outil [SpotBugs](https://spotbugs.github.io/) (rapports dans `reports/spotbugs/main/spotbugs.html`). +- `gradle spotbugsTest` pour vérifier la présence de bugs dans le code de test avec l'outil [SpotBugs](https://spotbugs.github.io/) (rapports dans `reports/spotbugs/test/spotbugs.html`). +- `gradle pmdMain` pour faire l'analyse statique du code principal avec l'outil [PMD](https://pmd.github.io/) (rapports dans `reports/pmd`). +- `gradle pmdTest` pour faire l'analyse statique du code de test avec l'outil [PMD](https://pmd.github.io/) (rapports dans `reports/pmd`). -Le fichier `build.gradle` contient la configuration du projet avec notamment la classe contenant la méthode `main` à exécuter. +Le fichier `build.gradle` contient la configuration du projet avec notamment la définition de la classe contenant la méthode `main` à exécuter pour l'application. ## Copyrights diff --git a/build.gradle b/build.gradle index db58c7129dfe09aebf3e0eb5f212b169fabb3724..3becc77af52a39f0a4cfafd424648f396a43cb0e 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,8 @@ plugins { id 'application' id 'checkstyle' id 'jacoco' + id 'pmd' + id "com.github.spotbugs" version "5.0.12" } group 'fr.univ_amu' @@ -19,7 +21,10 @@ repositories { dependencies { testImplementation('org.junit.jupiter:junit-jupiter-api:5.9.0', 'org.assertj:assertj-core:3.23.1') + spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0' + implementation 'org.apache.logging.log4j:log4j-api:2.19.0' + implementation 'org.apache.logging.log4j:log4j-core:2.19.0' } test { @@ -34,7 +39,7 @@ test { } application { - mainClass = 'sample.MyJFrame' + mainClass = 'sample.MyJavaFrame' } tasks.named('jar') { @@ -45,6 +50,34 @@ tasks.named('jar') { } } +spotbugs { + reportLevel = 'high' + effort = 'max' +} + +spotbugsMain { + reports { + html { + required = true + outputLocation = file("$buildDir/reports/spotbugs/main/spotbugs.html") + stylesheet = 'fancy-hist.xsl' + } + } +} +spotbugsTest { + reports { + html { + required = true + outputLocation = file("$buildDir/reports/spotbugs/test/spotbugs.html") + stylesheet = 'fancy-hist.xsl' + } + } +} +pmd { + consoleOutput = true + sourceSets = [sourceSets.main] + ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"] +} \ No newline at end of file diff --git a/src/main/java/sample/MyJavaFrame.java b/src/main/java/sample/MyJavaFrame.java index 4396df4a61c9624884edac2d44ebd85e51218741..d3aaf0758226fae936382e4585590baa016436f2 100644 --- a/src/main/java/sample/MyJavaFrame.java +++ b/src/main/java/sample/MyJavaFrame.java @@ -1,14 +1,20 @@ package sample; +import java.io.Serial; import javax.swing.JFrame; + /** * An extended version of javax.swing.JFrame containing a panel to draw images. */ public class MyJavaFrame extends JFrame { + + @Serial + private static final long serialVersionUID = 42L; /** * Constructs a new visible frame. */ + public MyJavaFrame() { setTitle("Main window"); setSize(400, 400); diff --git a/src/main/java/sample/MyJavaPanel.java b/src/main/java/sample/MyJavaPanel.java index c2e8fb2a7223fb8c4052909af9b530e9568a686a..d8239fcb1355144be38de2a3eaaed4dc3108bcdb 100644 --- a/src/main/java/sample/MyJavaPanel.java +++ b/src/main/java/sample/MyJavaPanel.java @@ -2,27 +2,40 @@ package sample; import java.awt.Graphics; import java.awt.image.BufferedImage; -import java.io.IOException; +import java.io.Serial; +import java.text.MessageFormat; import java.util.Objects; import javax.imageio.ImageIO; import javax.swing.JPanel; - +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * An extension of javax.swing.JFrame that can draw images. */ public class MyJavaPanel extends JPanel { + @Serial + private static final long serialVersionUID = 4242L; + private transient BufferedImage image; - private BufferedImage image; /** * Constructs a new panel that draw an image. */ public MyJavaPanel() { + Logger logger = LogManager.getLogger(this.getClass()); + logger.debug("Construct a MyJavaPanel"); + String path = "image.png"; + if (logger.isDebugEnabled()) { + String message = MessageFormat.format("Loading image at path {0}", path); + logger.debug(message); + + } try { - image = ImageIO.read(Objects.requireNonNull(getClass().getResource("image.png"))); - } catch (IOException ex) { - System.out.println("problem! image can't be loaded!"); + image = ImageIO.read(Objects.requireNonNull(getClass().getResource(path))); + } catch (Exception ex) { + String message = MessageFormat.format("Error: Cannot load image at path: {0}", path); + logger.error(message, ex); } } diff --git a/src/test/java/TestMyClass.java b/src/test/java/TestMyClass.java index 61b1a9802b29822dc545f96ee0414fb90e05889f..0dd1628239e520e8eb575f4d492c957a587c44b5 100644 --- a/src/test/java/TestMyClass.java +++ b/src/test/java/TestMyClass.java @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test; /** * A template class for testing with assertJ. */ -public class TestMyClass { +class TestMyClass { @Test void testTrue() {