From bc2a39c2e05326105a4a99b6ca2fd235cf37ae5d Mon Sep 17 00:00:00 2001 From: SiboVG Date: Wed, 30 Nov 2022 01:41:33 +0100 Subject: [PATCH] Add unit tests for welcome info --- core/build.xml | 4 ++- .../communication/WelcomeInfoRetriever.java | 23 ++++++++++++---- .../communication/WelcomeInfoTest.java | 27 +++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 core/test/net/sf/openrocket/communication/WelcomeInfoTest.java diff --git a/core/build.xml b/core/build.xml index 007c10508..01893b763 100644 --- a/core/build.xml +++ b/core/build.xml @@ -1,7 +1,8 @@ - + + @@ -34,6 +35,7 @@ + diff --git a/core/src/net/sf/openrocket/communication/WelcomeInfoRetriever.java b/core/src/net/sf/openrocket/communication/WelcomeInfoRetriever.java index 0a6d1a5d0..6b4c579d6 100644 --- a/core/src/net/sf/openrocket/communication/WelcomeInfoRetriever.java +++ b/core/src/net/sf/openrocket/communication/WelcomeInfoRetriever.java @@ -6,22 +6,26 @@ import net.sf.openrocket.util.BuildProperties; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public abstract class WelcomeInfoRetriever { - /** * Retrieves the release notes of the current build version from the ReleaseNotes.md file. + * @param version the build version to search the release notes for (e.g. "22.02") * @return the release notes of the current build version * @throws IOException if the file could not be read */ - public static String retrieveWelcomeInfo() throws IOException { - InputStream in = new FileInputStream("ReleaseNotes.md"); + public static String retrieveWelcomeInfo(String version) throws IOException { + ClassLoader cl = WelcomeInfoRetriever.class.getClassLoader(); + InputStream in = cl.getResourceAsStream("ReleaseNotes.md"); + if (in == null) { + throw new FileNotFoundException("ReleaseNotes.md not found"); + } InputSource source = new InputSource(new InputStreamReader(in)); - ReleaseNotesHandler handler = new ReleaseNotesHandler(BuildProperties.getVersion()); + ReleaseNotesHandler handler = new ReleaseNotesHandler(version); WarningSet warnings = new WarningSet(); try { @@ -31,4 +35,13 @@ public abstract class WelcomeInfoRetriever { throw new IOException(e.getMessage(), e); } } + + /** + * Retrieves the release notes of the current build version from the ReleaseNotes.md file. + * @return the release notes of the current build version + * @throws IOException if the file could not be read + */ + public static String retrieveWelcomeInfo() throws IOException { + return retrieveWelcomeInfo(BuildProperties.getVersion()); + } } diff --git a/core/test/net/sf/openrocket/communication/WelcomeInfoTest.java b/core/test/net/sf/openrocket/communication/WelcomeInfoTest.java new file mode 100644 index 000000000..529c7ee23 --- /dev/null +++ b/core/test/net/sf/openrocket/communication/WelcomeInfoTest.java @@ -0,0 +1,27 @@ +package net.sf.openrocket.communication; + +import net.sf.openrocket.util.BaseTestCase.BaseTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class WelcomeInfoTest extends BaseTestCase { + + /** + * Note: this unit test will fail if you don't run it using 'ant unittest', because otherwise + * it can't load the ReleaseNotes.md file. + */ + @Test + public void testWelcomeInfo() throws Exception { + // Test the welcome info for the current build version + String welcomeInfo = WelcomeInfoRetriever.retrieveWelcomeInfo(); + assertNotNull(welcomeInfo); + assertTrue(welcomeInfo.length() > 0); + + // Test the release info for a bogus release version + welcomeInfo = WelcomeInfoRetriever.retrieveWelcomeInfo("bogus release"); + assertNull(welcomeInfo); + } +}