X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=getdown%2Fsrc%2Fgetdown%2Fcore%2Fsrc%2Ftest%2Fjava%2Fcom%2Fthreerings%2Fgetdown%2Fdata%2FEnvConfigTest.java;fp=getdown%2Fsrc%2Fgetdown%2Fcore%2Fsrc%2Ftest%2Fjava%2Fcom%2Fthreerings%2Fgetdown%2Fdata%2FEnvConfigTest.java;h=61786518ce830559bbd7bd569e3236452d34d3a8;hb=aace9d05c0870c703bfdfb28c1608213cee019bf;hp=0000000000000000000000000000000000000000;hpb=2a3bac30ae8290e912eb7ffe7ff7ec700b6cfaac;p=jalview.git diff --git a/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java b/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java new file mode 100644 index 0000000..6178651 --- /dev/null +++ b/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java @@ -0,0 +1,142 @@ +// +// Getdown - application installer, patcher and launcher +// Copyright (C) 2004-2018 Getdown authors +// https://github.com/threerings/getdown/blob/master/LICENSE + +package com.threerings.getdown.data; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.io.File; + +import org.junit.*; +import static org.junit.Assert.*; + +public class EnvConfigTest { + + static String CWD = System.getProperty("user.dir"); + static String TESTID = "testid"; + static String TESTBASE = "https://test.com/test"; + + private void debugNotes(List notes) { + for (EnvConfig.Note note : notes) { + System.out.println(note.message); + } + } + + private void checkNoNotes (List notes) { + StringBuilder msg = new StringBuilder(); + for (EnvConfig.Note note : notes) { + if (note.level != EnvConfig.Note.Level.INFO) { + msg.append("\n").append(note.message); + } + } + if (msg.length() > 0) { + fail("Unexpected notes:" + msg.toString()); + } + } + private void checkDir (EnvConfig cfg) { + assertTrue(cfg != null); + assertEquals(new File(CWD), cfg.appDir); + } + private void checkNoAppId (EnvConfig cfg) { + assertNull(cfg.appId); + } + private void checkAppId (EnvConfig cfg, String appId) { + assertEquals(appId, cfg.appId); + } + private void checkAppBase (EnvConfig cfg, String appBase) { + assertEquals(appBase, cfg.appBase); + } + private void checkNoAppBase (EnvConfig cfg) { + assertNull(cfg.appBase); + } + private void checkNoAppArgs (EnvConfig cfg) { + assertTrue(cfg.appArgs.isEmpty()); + } + private void checkAppArgs (EnvConfig cfg, String... args) { + assertEquals(Arrays.asList(args), cfg.appArgs); + } + + @Test public void testArgvDir () { + List notes = new ArrayList<>(); + String[] args = { CWD }; + EnvConfig cfg = EnvConfig.create(args, notes); + // debugNotes(notes); + checkNoNotes(notes); + checkDir(cfg); + checkNoAppId(cfg); + checkNoAppBase(cfg); + checkNoAppArgs(cfg); + } + + @Test public void testArgvDirId () { + List notes = new ArrayList<>(); + String[] args = { CWD, TESTID }; + EnvConfig cfg = EnvConfig.create(args, notes); + // debugNotes(notes); + checkNoNotes(notes); + checkDir(cfg); + checkAppId(cfg, TESTID); + checkNoAppBase(cfg); + checkNoAppArgs(cfg); + } + + @Test public void testArgvDirArgs () { + List notes = new ArrayList<>(); + String[] args = { CWD, "", "one", "two" }; + EnvConfig cfg = EnvConfig.create(args, notes); + // debugNotes(notes); + checkNoNotes(notes); + checkDir(cfg); + checkNoAppId(cfg); + checkNoAppBase(cfg); + checkAppArgs(cfg, "one", "two"); + } + + @Test public void testArgvDirIdArgs () { + List notes = new ArrayList<>(); + String[] args = { CWD, TESTID, "one", "two" }; + EnvConfig cfg = EnvConfig.create(args, notes); + // debugNotes(notes); + checkNoNotes(notes); + checkDir(cfg); + checkAppId(cfg, TESTID); + checkNoAppBase(cfg); + checkAppArgs(cfg, "one", "two"); + } + + private EnvConfig sysPropsConfig (List notes, String... keyVals) { + for (int ii = 0; ii < keyVals.length; ii += 2) { + System.setProperty(keyVals[ii], keyVals[ii+1]); + } + EnvConfig cfg = EnvConfig.create(new String[0], notes); + for (int ii = 0; ii < keyVals.length; ii += 2) { + System.clearProperty(keyVals[ii]); + } + return cfg; + } + + @Test public void testSysPropsDir () { + List notes = new ArrayList<>(); + EnvConfig cfg = sysPropsConfig(notes, "appdir", CWD); + // debugNotes(notes); + checkNoNotes(notes); + checkDir(cfg); + checkNoAppId(cfg); + checkNoAppBase(cfg); + checkNoAppArgs(cfg); + } + + @Test public void testSysPropsDirIdBase () { + List notes = new ArrayList<>(); + EnvConfig cfg = sysPropsConfig(notes, "appdir", CWD, "appid", TESTID, "appbase", TESTBASE); + // debugNotes(notes); + checkNoNotes(notes); + checkDir(cfg); + checkAppId(cfg, TESTID); + checkAppBase(cfg, TESTBASE); + checkNoAppArgs(cfg); + } +}