JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / test / java / com / threerings / getdown / data / EnvConfigTest.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.data;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11 import java.io.File;
12
13 import org.junit.*;
14 import static org.junit.Assert.*;
15
16 public class EnvConfigTest {
17
18     static String CWD = System.getProperty("user.dir");
19     static String TESTID = "testid";
20     static String TESTBASE = "https://test.com/test";
21
22     private void debugNotes(List<EnvConfig.Note> notes) {
23         for (EnvConfig.Note note : notes) {
24             System.out.println(note.message);
25         }
26     }
27
28     private void checkNoNotes (List<EnvConfig.Note> notes) {
29         StringBuilder msg = new StringBuilder();
30         for (EnvConfig.Note note : notes) {
31             if (note.level != EnvConfig.Note.Level.INFO) {
32                 msg.append("\n").append(note.message);
33             }
34         }
35         if (msg.length() > 0) {
36             fail("Unexpected notes:" + msg.toString());
37         }
38     }
39     private void checkDir (EnvConfig cfg) {
40         assertTrue(cfg != null);
41         assertEquals(new File(CWD), cfg.appDir);
42     }
43     private void checkNoAppId (EnvConfig cfg) {
44         assertNull(cfg.appId);
45     }
46     private void checkAppId (EnvConfig cfg, String appId) {
47         assertEquals(appId, cfg.appId);
48     }
49     private void checkAppBase (EnvConfig cfg, String appBase) {
50         assertEquals(appBase, cfg.appBase);
51     }
52     private void checkNoAppBase (EnvConfig cfg) {
53         assertNull(cfg.appBase);
54     }
55     private void checkNoAppArgs (EnvConfig cfg) {
56         assertTrue(cfg.appArgs.isEmpty());
57     }
58     private void checkAppArgs (EnvConfig cfg, String... args) {
59         assertEquals(Arrays.asList(args), cfg.appArgs);
60     }
61
62     @Test public void testArgvDir () {
63         List<EnvConfig.Note> notes = new ArrayList<>();
64         String[] args = { CWD };
65         EnvConfig cfg = EnvConfig.create(args, notes);
66         // debugNotes(notes);
67         checkNoNotes(notes);
68         checkDir(cfg);
69         checkNoAppId(cfg);
70         checkNoAppBase(cfg);
71         checkNoAppArgs(cfg);
72     }
73
74     @Test public void testArgvDirId () {
75         List<EnvConfig.Note> notes = new ArrayList<>();
76         String[] args = { CWD, TESTID };
77         EnvConfig cfg = EnvConfig.create(args, notes);
78         // debugNotes(notes);
79         checkNoNotes(notes);
80         checkDir(cfg);
81         checkAppId(cfg, TESTID);
82         checkNoAppBase(cfg);
83         checkNoAppArgs(cfg);
84     }
85
86     @Test public void testArgvDirArgs () {
87         List<EnvConfig.Note> notes = new ArrayList<>();
88         String[] args = { CWD, "", "one", "two" };
89         EnvConfig cfg = EnvConfig.create(args, notes);
90         // debugNotes(notes);
91         checkNoNotes(notes);
92         checkDir(cfg);
93         checkNoAppId(cfg);
94         checkNoAppBase(cfg);
95         checkAppArgs(cfg, "one", "two");
96     }
97
98     @Test public void testArgvDirIdArgs () {
99         List<EnvConfig.Note> notes = new ArrayList<>();
100         String[] args = { CWD, TESTID, "one", "two" };
101         EnvConfig cfg = EnvConfig.create(args, notes);
102         // debugNotes(notes);
103         checkNoNotes(notes);
104         checkDir(cfg);
105         checkAppId(cfg, TESTID);
106         checkNoAppBase(cfg);
107         checkAppArgs(cfg, "one", "two");
108     }
109
110     private EnvConfig sysPropsConfig (List<EnvConfig.Note> notes, String... keyVals) {
111         for (int ii = 0; ii < keyVals.length; ii += 2) {
112             System.setProperty(keyVals[ii], keyVals[ii+1]);
113         }
114         EnvConfig cfg = EnvConfig.create(new String[0], notes);
115         for (int ii = 0; ii < keyVals.length; ii += 2) {
116             System.clearProperty(keyVals[ii]);
117         }
118         return cfg;
119     }
120
121     @Test public void testSysPropsDir () {
122         List<EnvConfig.Note> notes = new ArrayList<>();
123         EnvConfig cfg = sysPropsConfig(notes, "appdir", CWD);
124         // debugNotes(notes);
125         checkNoNotes(notes);
126         checkDir(cfg);
127         checkNoAppId(cfg);
128         checkNoAppBase(cfg);
129         checkNoAppArgs(cfg);
130     }
131
132     @Test public void testSysPropsDirIdBase () {
133         List<EnvConfig.Note> notes = new ArrayList<>();
134         EnvConfig cfg = sysPropsConfig(notes, "appdir", CWD, "appid", TESTID, "appbase", TESTBASE);
135         // debugNotes(notes);
136         checkNoNotes(notes);
137         checkDir(cfg);
138         checkAppId(cfg, TESTID);
139         checkAppBase(cfg, TESTBASE);
140         checkNoAppArgs(cfg);
141     }
142 }