Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/us/kolmafia/multitool/Multitool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,25 @@
import java.util.Locale;

public class Multitool {

private static String cwd;
/*
The code setting ROOT_LOCATION was lifted from KoLConstants.java and the dependency on
UtilityConstants.java eliminated by copying code from there.
*/
public static final File BASE_LOCATION =
new File(System.getProperty("user.dir")).getAbsoluteFile();
public static final File HOME_LOCATION =
new File(System.getProperty("user.home")).getAbsoluteFile();
public static final boolean USE_OSX_STYLE_DIRECTORIES =
System.getProperty("os.name").startsWith("Mac");
public static final boolean USE_LINUX_STYLE_DIRECTORIES =
USE_OSX_STYLE_DIRECTORIES && !System.getProperty("os.name").startsWith("Win");
public static final File ROOT_LOCATION =
Boolean.getBoolean("useCWDasROOT")
? BASE_LOCATION
: USE_OSX_STYLE_DIRECTORIES
? new File(HOME_LOCATION, "Library/Application Support/KoLmafia")
: USE_LINUX_STYLE_DIRECTORIES ? new File(HOME_LOCATION, ".kolmafia") : BASE_LOCATION;
static String cwd;
private static String localJava;
private static int localJavaVersion;
private static PrintWriter logWriter;
Expand Down Expand Up @@ -74,7 +91,7 @@ public static void main(String[] args) {
System.exit(0);
}

private static void processLocalInformation() {
static void processLocalInformation() {
String separator = FileSystems.getDefault().getSeparator();
cwd = cleanPath(Paths.get("").toAbsolutePath().toString());
localJava = cleanPath(System.getProperty("java.home") + separator + "bin" + separator + "java");
Expand Down Expand Up @@ -230,7 +247,7 @@ private static int getLatestReleaseVersion(String tool) {
return Integer.parseInt(retVal);
}

private static List<String> processDirectory(String nameRoot) {
static List<String> processDirectory(String nameRoot) {
// Returns a list of file names in the current directory that match
List<String> retVal = new ArrayList<>();
String lcRoot = nameRoot.toLowerCase();
Expand Down Expand Up @@ -261,7 +278,7 @@ private static List<String> processDirectory(String nameRoot) {
return retVal;
}

private static String cleanPath(String path) {
static String cleanPath(String path) {
String retVal = path;
String fs = "/";
String bs = "\\\\";
Expand Down
2 changes: 1 addition & 1 deletion src/us/kolmafia/multitool/ToolData.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setCurrentVersion(int currentVersion) {
}

public List<String> getLocalJars() {
return localJars;
return new ArrayList<>(localJars);
}

public void setLocalJars(List<String> localJars) {
Expand Down
Empty file added test/resources/fakeResource.txt
Empty file.
Empty file.
Empty file.
Empty file.
89 changes: 89 additions & 0 deletions test/us/kolmafia/multitool/MultitoolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package us.kolmafia.multitool;

import static java.nio.file.Files.copy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static us.kolmafia.multitool.Multitool.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class MultitoolTest {
@BeforeAll
static void beforeAll() {
processLocalInformation();
}

@Test
public void itShouldBeRunningInRoot() {
String cp = cleanPath(String.valueOf(ROOT_LOCATION.toPath()));
assertEquals(cp, cwd);
}

@Test
public void itShouldFindNoFiles() {
List<String> locals = processDirectory("KoLMafia");
assertTrue(locals.isEmpty());
}

private boolean validateDestination(Path destination) {
File dFile = destination.toFile();
boolean retVal = dFile.exists();
if (retVal) {
retVal = dFile.isDirectory();
if (retVal) {
retVal = dFile.canWrite();
if (retVal) {
retVal = (Objects.requireNonNull(dFile.list()).length == 1);
}
}
}
return retVal;
}

@Test
public void itShouldFindFilesThatWerePutThere() {
Path source = new File(ROOT_LOCATION.toString() + "/FileResources").toPath();
List<File> deleteWhenDone = new ArrayList<>();
List<String> inDir = processDirectory("Kolmafia");
assertTrue(inDir.isEmpty());
Path destination = ROOT_LOCATION.toPath();
assertTrue(validateDestination(destination));
String[] files = source.toFile().list();
assertNotNull(files);
for (String file : files) {
File sFile = new File(source + "/" + file);
Path sPath = sFile.toPath();
File dFile = new File(destination + "/" + file);
deleteWhenDone.add(dFile);
Path dPath = dFile.toPath();
try {
copy(sPath, dPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
inDir = processDirectory("Kolmafia");
assertEquals(2, inDir.size());
assertTrue(inDir.contains("KolMafia-1066.jar"));
assertTrue(inDir.contains("KolMafia-1066-M.jar"));
assertFalse(inDir.contains("KolMafia-Latest.jar"));
for (File f : deleteWhenDone) {
boolean whoCares = f.delete();
if (!whoCares) {
System.out.println("Failed to delete " + f);
}
}
inDir = processDirectory("Kolmafia");
assertTrue(inDir.isEmpty());
}
}
104 changes: 104 additions & 0 deletions test/us/kolmafia/multitool/ToolDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package us.kolmafia.multitool;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class ToolDataTest {
private static ToolData td;
private static final String toolName = "ToolName";

@BeforeAll
static void beforeAll() {
td = new ToolData(toolName);
}

@Test
public void itShouldHaveName() {
assertEquals(toolName, td.getToolName());
}

@Test
public void itShouldPreserveCurrentVersionNumber() {
int ver = 123;
td.setCurrentVersion(ver);
assertEquals(ver, td.getCurrentVersion());
}

@Test
public void itShouldPreserveLatestVersionNumber() {
int ver = 666;
td.setLatestVersion(ver);
assertEquals(ver, td.getLatestVersion());
}

@Test
public void itShouldPreserveNeedToDownload() {
boolean need = false;
td.setNeedToDownload(need);
assertEquals(need, td.isNeedToDownload());
}

@Test
public void itShouldPreserveLocalModification() {
boolean need = false;
td.setLocalModificationFound(need);
assertEquals(need, td.isLocalModificationFound());
}

@Test
public void itShouldPreserveDownloadURL() {
String fauxURL = "https://api.github.com/repos/kolmafia/" + toolName + "/releases/latest";
td.setDownloadURL(fauxURL);
assertEquals(fauxURL, td.getDownloadURL());
}

@Test
public void itShouldPreserveLatestJarFile() {
/*
Need to understand where this will be. May need to establish place for tests to create files
*/
File fauxFile = new File("NoName");
td.setLatestJarFile(fauxFile);
assertEquals(fauxFile, td.getLatestJarFile());
}

@Test
public void itShouldPreserveLatestJars() {
/*
Need to understand where this will be. May need to establish place for tests to create files
*/
File fauxFile = new File("NoName");
td.setLatestJarFile(fauxFile);
assertEquals(fauxFile, td.getLatestJarFile());
}

@Test
public void itShouldPreserveLocalJars() {
List<String> local = new ArrayList<>();
local.add("a");
local.add("b");
td.setLocalJars(local);
assertEquals(local, td.getLocalJars());
}

@Test
public void itShouldOverrideToString() {
ToolData newTd = new ToolData(toolName);
String expected =
"ToolData for ToolName\n"
+ "currentVersion=0\n"
+ "latestVersion=0\n"
+ "downloadURL=null\n"
+ "needToDownload=false\n"
+ "localJars=[]\n"
+ "latestJarFile=null\n"
+ "localModificationFound=false";
String got = newTd.toString();
assertEquals(expected, got);
}
}