Add unit tests for welcome info

This commit is contained in:
SiboVG 2022-11-30 01:41:33 +01:00
parent faa7e43891
commit bc2a39c2e0
3 changed files with 48 additions and 6 deletions

View File

@ -1,7 +1,8 @@
<project name="OpenRocket-Core" basedir=".">
<property file="resources/build.properties" />
<property name="root.dir" value=".."/> <!-- Root directory -->
<property name="src.dir" value="${basedir}/src"/> <!-- Source directory -->
<property name="src-test.dir" value="${basedir}/test"/> <!-- Test directory -->
<property name="build.dir" value="${basedir}/build"/> <!-- Build directory -->
@ -34,6 +35,7 @@
<pathelement location="${build-test.dir}"/>
<pathelement location="${classes.dir}"/>
<pathelement location="${src-test.dir}"/>
<pathelement location="${root.dir}"/>
<fileset dir="${libtest.dir}/" includes="*.jar"/>
</path>

View File

@ -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());
}
}

View File

@ -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);
}
}