Skip to content

Commit

Permalink
Improve logging of update hints; add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cube committed Dec 9, 2024
1 parent 4493505 commit a6bcfe9
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/oolite/starter/GithubVersionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public boolean maybeAnnounceUpdate(Component parentComponent) {
try {
Semver latest = getLatestVersion(getMyVersion());
if (latest != null) {
log.info("OoliteStarter {} found. MrGimlet to suggest update.", latest);
String message = getHtmlUserMessage(latest);
EventQueue.invokeLater(() -> MrGimlet.showMessage(parentComponent, message, 10000) );
return true;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/oolite/starter/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ protected void done() {
}

if (mf.configuration.getInstallations().isEmpty()) {
log.info("No installation. MrGimlet point to installations");

// point user to creating an active installation
mf.jTabbedPane1.setEnabledAt(0, false);
mf.jTabbedPane1.setEnabledAt(1, false);
Expand All @@ -146,6 +148,8 @@ protected void done() {

MrGimlet.showMessage(mf.getRootPane(), message.toString(), 0);
} else if (!mf.isInstallationsValid()) {
log.info("Fishy installation. MrGimlet point to installations");

mf.jTabbedPane1.setSelectedIndex(4);

StringBuilder message = new StringBuilder("<html>");
Expand All @@ -156,6 +160,8 @@ protected void done() {

MrGimlet.showMessage(mf.getRootPane(), message.toString(), 0);
} else if (mf.configuration.getActiveInstallation() == null) {
log.info("No active installation. MrGimlet point to installations");

// point user to creating an active installation
mf.jTabbedPane1.setEnabledAt(0, false);
mf.jTabbedPane1.setEnabledAt(1, false);
Expand Down Expand Up @@ -405,6 +411,7 @@ protected boolean isInstallationsValid() {
try {
String v = oolite.getVersionFromHomeDir(f);
if (!i.getVersion().equals(v)) {
log.warn("Oolite declared as {} but found {}", i.getVersion(), v);
return false;
}
} catch (IOException e) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/oolite/starter/Oolite.java
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,10 @@ public static NodeList parseExpansionSet(URL source) throws ParserConfigurationE
* @return a map with identifier:version -> downloadurl
*/
private TreeMap<String, String> prepareEnabledAddonsList(NodeList target) {
if (target == null) {
throw new IllegalArgumentException("target must not be null");
}

TreeMap<String, String> result = new TreeMap<>();
for (int i = 0; i < target.getLength(); i++) {
Element e = (Element)target.item(i);
Expand All @@ -1659,6 +1663,10 @@ private TreeMap<String, String> prepareEnabledAddonsList(NodeList target) {
* @return
*/
private List<Command> prepareDisableCommands(TreeMap<String, String> enabledAddons, List<Expansion> expansions) {
if (expansions == null) {
throw new IllegalArgumentException("expansions must not be null");
}

List<Command> result = new ArrayList<>();

for (Expansion expansion: expansions) {
Expand All @@ -1683,6 +1691,12 @@ private List<Command> prepareDisableCommands(TreeMap<String, String> enabledAddo
*/
public List<Command> buildCommandList(List<Expansion> expansions, NodeList target) {
log.debug("buildCommandList({}, {})", expansions, target);
if (expansions == null) {
throw new IllegalArgumentException("expansions must not be null");
}
if (target == null) {
throw new IllegalArgumentException("target must not be null");
}

List<Command> result = new ArrayList<>();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/oolite/starter/OoliteVersionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public ModuleDescriptor.Version getLatestVersion(ModuleDescriptor.Version curren
Collections.sort(versions);
log.debug("versions {}", versions);
ModuleDescriptor.Version latest = versions.get(versions.size()-1);
log.debug("version me={} latest={}", currentVersion, latest);
log.info("version me={} latest={}", currentVersion, latest);

if (latest.compareTo(currentVersion)>0) {
log.debug("latest is greater!");
Expand Down Expand Up @@ -222,6 +222,7 @@ public boolean maybeAnnounceUpdate(Component parentComponent, ModuleDescriptor.V
try {
ModuleDescriptor.Version latest = getLatestVersion(myVersion);
if (latest != null) {
log.info("Oolite {} found while we have {}. MrGimlet to suggest update.", latest);
String message = getHtmlUserMessage(latest);
EventQueue.invokeLater(() -> MrGimlet.showMessage(parentComponent, message, 10000, image) );
return true;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/oolite/starter/model/Expansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ public class Expansion implements Comparable<Expansion> {

private static final String EXPANSION_OOLITE_MUST_BE_SET = "oolite must be set before";

public static class Builder {

private String identifier;

/**
* Sets the identifier for the expansion.
*
* @param identifier the identifier
* @return the builder
*/
public Builder identifier(String identifier) {
this.identifier = identifier;
return this;
}

/**
* Builds the expansion according to current builder status.
*
* @return the expansion
*/
public Expansion build() {
Expansion result = new Expansion();
result.setIdentifier(identifier);
return result;
}

}

/**
* Expansion Manager status fields.
* See https://wiki.alioth.net/index.php/Expansions_Manager
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/oolite/starter/OoliteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1604,5 +1604,71 @@ public void testGetFlavorsList() throws Exception {
log.warn("Could not test loading flavors list - are we offline?", e);
}
}

@Test
public void testBuildCommandList() {
log.info("testBuildCommandList");

Oolite instance = new Oolite();

try {
instance.buildCommandList(null, null);
fail("exception expected");
} catch (IllegalArgumentException e) {
assertEquals("expansions must not be null", e.getMessage());
log.debug("caught expected exception");
}
}

@Test
public void testBuildCommandList2() throws ParserConfigurationException {
log.info("testBuildCommandList2");

Oolite instance = new Oolite();
List<Expansion> expansions = new ArrayList<>();

try {
instance.buildCommandList(expansions, null);
fail("exception expected");
} catch (IllegalArgumentException e) {
assertEquals("target must not be null", e.getMessage());
log.debug("caught expected exception");
}
}

@Test
public void testBuildCommandList3() throws ParserConfigurationException {
log.info("testBuildCommandList3");

List<Expansion> expansions = new ArrayList<>();

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.newDocument();
NodeList target = doc.getChildNodes();

Oolite instance = new Oolite();

List<Command> result = instance.buildCommandList(expansions, target);
assertNotNull(result);
assertEquals(0, result.size());
}

@Test
public void testBuildCommandList4() throws ParserConfigurationException {
log.info("testBuildCommandList4");

List<Expansion> expansions = new ArrayList<>();
expansions.add(new Expansion.Builder().identifier("meeh").build());

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.newDocument();
NodeList target = doc.getChildNodes();

Oolite instance = new Oolite();

List<Command> result = instance.buildCommandList(expansions, target);
assertNotNull(result);
assertEquals(0, result.size());
}

}
98 changes: 98 additions & 0 deletions src/test/java/oolite/starter/model/SaveGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package oolite.starter.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -178,4 +180,100 @@ public void testToString() {
assertEquals("SaveGame{file=null, playerName=null, credits=0, currentSystemName=null, ooliteVersion=null, shipKills=0, shipClassName=null, shipName=shipName}", sg.toString());
}

@Test
public void testHasMissingExpansions() {
log.info("testHasMissingExpansions");

SaveGame sg = new SaveGame();
assertFalse(sg.hasMissingExpansions());
}

@Test
public void testHasMissingExpansions2() {
log.info("testHasMissingExpansions2");

SaveGame sg = new SaveGame();
sg.setExpansions(new ArrayList<>());
assertFalse(sg.hasMissingExpansions());
}

@Test
public void testHasMissingExpansions3() {
log.info("testHasMissingExpansions3");

List<ExpansionReference> expansions = new ArrayList<>();
expansions.add(new ExpansionReference("nameOk", ExpansionReference.Status.OK));
expansions.add(new ExpansionReference("nameConflict", ExpansionReference.Status.CONFLICT));
expansions.add(new ExpansionReference("nameRequiredMissing", ExpansionReference.Status.REQUIRED_MISSING));
expansions.add(new ExpansionReference("nameSurplus", ExpansionReference.Status.SURPLUS));

SaveGame sg = new SaveGame();
sg.setExpansions(expansions);
assertFalse(sg.hasMissingExpansions());
}

@Test
public void testHasMissingExpansions4() {
log.info("testHasMissingExpansions4");

List<ExpansionReference> expansions = new ArrayList<>();
expansions.add(new ExpansionReference("nameOk", ExpansionReference.Status.OK));
expansions.add(new ExpansionReference("nameConflict", ExpansionReference.Status.CONFLICT));
expansions.add(new ExpansionReference("nameMissing", ExpansionReference.Status.MISSING));
expansions.add(new ExpansionReference("nameRequiredMissing", ExpansionReference.Status.REQUIRED_MISSING));
expansions.add(new ExpansionReference("nameSurplus", ExpansionReference.Status.SURPLUS));

SaveGame sg = new SaveGame();
sg.setExpansions(expansions);
assertTrue(sg.hasMissingExpansions());
}


@Test
public void testHasTooManyExpansions() {
log.info("testHasTooManyExpansions");

SaveGame sg = new SaveGame();
assertFalse(sg.hasTooManyExpansions());
}

@Test
public void testHasTooManyExpansions2() {
log.info("testHasTooManyExpansions2");

SaveGame sg = new SaveGame();
sg.setExpansions(new ArrayList<>());
assertFalse(sg.hasTooManyExpansions());
}

@Test
public void testHasTooManyExpansions3() {
log.info("testHasTooManyExpansions3");

List<ExpansionReference> expansions = new ArrayList<>();
expansions.add(new ExpansionReference("nameOk", ExpansionReference.Status.OK));
expansions.add(new ExpansionReference("nameConflict", ExpansionReference.Status.CONFLICT));
expansions.add(new ExpansionReference("nameMissing", ExpansionReference.Status.MISSING));
expansions.add(new ExpansionReference("nameRequiredMissing", ExpansionReference.Status.REQUIRED_MISSING));

SaveGame sg = new SaveGame();
sg.setExpansions(expansions);
assertFalse(sg.hasTooManyExpansions());
}

@Test
public void testHasTooManyExpansions4() {
log.info("testHasTooManyExpansions4");

List<ExpansionReference> expansions = new ArrayList<>();
expansions.add(new ExpansionReference("nameOk", ExpansionReference.Status.OK));
expansions.add(new ExpansionReference("nameConflict", ExpansionReference.Status.CONFLICT));
expansions.add(new ExpansionReference("nameMissing", ExpansionReference.Status.MISSING));
expansions.add(new ExpansionReference("nameRequiredMissing", ExpansionReference.Status.REQUIRED_MISSING));
expansions.add(new ExpansionReference("nameSurplus", ExpansionReference.Status.SURPLUS));

SaveGame sg = new SaveGame();
sg.setExpansions(expansions);
assertTrue(sg.hasTooManyExpansions());
}
}

0 comments on commit a6bcfe9

Please sign in to comment.