2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.util;
8 import java.io.IOException;
9 import java.io.StringReader;
10 import java.util.List;
11 import java.util.Random;
14 import static org.junit.Assert.*;
17 * Tests {@link Config}.
19 public class ConfigTest
21 public static class Pair {
22 public final String key;
23 public final String value;
24 public Pair (String key, String value) {
30 public static final Pair[] SIMPLE_PAIRS = {
31 new Pair("one", "two"),
32 new Pair("three", "four"),
33 new Pair("five", "six"),
34 new Pair("seven", "eight"),
35 new Pair("nine", "ten"),
38 @Test public void testSimplePairs () throws IOException
40 List<String[]> pairs = Config.parsePairs(
41 toReader(SIMPLE_PAIRS), Config.createOpts(true));
42 for (int ii = 0; ii < SIMPLE_PAIRS.length; ii++) {
43 assertEquals(SIMPLE_PAIRS[ii].key, pairs.get(ii)[0]);
44 assertEquals(SIMPLE_PAIRS[ii].value, pairs.get(ii)[1]);
48 @Test public void testQualifiedPairs () throws IOException
50 Pair linux = new Pair("one", "[linux] two");
51 Pair mac = new Pair("three", "[mac os x] four");
52 Pair linuxAndMac = new Pair("five", "[linux, mac os x] six");
53 Pair linux64 = new Pair("seven", "[linux-x86_64] eight");
54 Pair linux64s = new Pair("nine", "[linux-x86_64, linux-amd64] ten");
55 Pair mac64 = new Pair("eleven", "[mac os x-x86_64] twelve");
56 Pair win64 = new Pair("thirteen", "[windows-x86_64] fourteen");
57 Pair notWin = new Pair("fifteen", "[!windows] sixteen");
58 Pair[] pairs = { linux, mac, linuxAndMac, linux64, linux64s, mac64, win64, notWin };
60 Config.ParseOpts opts = Config.createOpts(false);
61 opts.osname = "linux";
63 List<String[]> parsed = Config.parsePairs(toReader(pairs), opts);
64 assertTrue(exists(parsed, linux.key));
65 assertTrue(!exists(parsed, mac.key));
66 assertTrue(exists(parsed, linuxAndMac.key));
67 assertTrue(!exists(parsed, linux64.key));
68 assertTrue(!exists(parsed, linux64s.key));
69 assertTrue(!exists(parsed, mac64.key));
70 assertTrue(!exists(parsed, win64.key));
71 assertTrue(exists(parsed, notWin.key));
73 opts.osarch = "x86_64";
74 parsed = Config.parsePairs(toReader(pairs), opts);
75 assertTrue(exists(parsed, linux.key));
76 assertTrue(!exists(parsed, mac.key));
77 assertTrue(exists(parsed, linuxAndMac.key));
78 assertTrue(exists(parsed, linux64.key));
79 assertTrue(exists(parsed, linux64s.key));
80 assertTrue(!exists(parsed, mac64.key));
81 assertTrue(!exists(parsed, win64.key));
82 assertTrue(exists(parsed, notWin.key));
84 opts.osarch = "amd64";
85 parsed = Config.parsePairs(toReader(pairs), opts);
86 assertTrue(exists(parsed, linux.key));
87 assertTrue(!exists(parsed, mac.key));
88 assertTrue(exists(parsed, linuxAndMac.key));
89 assertTrue(!exists(parsed, linux64.key));
90 assertTrue(exists(parsed, linux64s.key));
91 assertTrue(!exists(parsed, mac64.key));
92 assertTrue(!exists(parsed, win64.key));
93 assertTrue(exists(parsed, notWin.key));
95 opts.osname = "mac os x";
96 opts.osarch = "x86_64";
97 parsed = Config.parsePairs(toReader(pairs), opts);
98 assertTrue(!exists(parsed, linux.key));
99 assertTrue(exists(parsed, mac.key));
100 assertTrue(exists(parsed, linuxAndMac.key));
101 assertTrue(!exists(parsed, linux64.key));
102 assertTrue(!exists(parsed, linux64s.key));
103 assertTrue(exists(parsed, mac64.key));
104 assertTrue(!exists(parsed, win64.key));
105 assertTrue(exists(parsed, notWin.key));
107 opts.osname = "windows";
108 opts.osarch = "i386";
109 parsed = Config.parsePairs(toReader(pairs), opts);
110 assertTrue(!exists(parsed, linux.key));
111 assertTrue(!exists(parsed, mac.key));
112 assertTrue(!exists(parsed, linuxAndMac.key));
113 assertTrue(!exists(parsed, linux64.key));
114 assertTrue(!exists(parsed, linux64s.key));
115 assertTrue(!exists(parsed, mac64.key));
116 assertTrue(!exists(parsed, win64.key));
117 assertTrue(!exists(parsed, notWin.key));
119 opts.osarch = "x86_64";
120 parsed = Config.parsePairs(toReader(pairs), opts);
121 assertTrue(!exists(parsed, linux.key));
122 assertTrue(!exists(parsed, mac.key));
123 assertTrue(!exists(parsed, linuxAndMac.key));
124 assertTrue(!exists(parsed, linux64.key));
125 assertTrue(!exists(parsed, linux64s.key));
126 assertTrue(!exists(parsed, mac64.key));
127 assertTrue(exists(parsed, win64.key));
128 assertTrue(!exists(parsed, notWin.key));
130 opts.osarch = "amd64";
131 parsed = Config.parsePairs(toReader(pairs), opts);
132 assertTrue(!exists(parsed, linux.key));
133 assertTrue(!exists(parsed, mac.key));
134 assertTrue(!exists(parsed, linuxAndMac.key));
135 assertTrue(!exists(parsed, linux64.key));
136 assertTrue(!exists(parsed, linux64s.key));
137 assertTrue(!exists(parsed, mac64.key));
138 assertTrue(!exists(parsed, win64.key));
139 assertTrue(!exists(parsed, notWin.key));
142 protected static boolean exists (List<String[]> pairs, String key)
144 for (String[] pair : pairs) {
145 if (pair[0].equals(key)) {
152 protected static StringReader toReader (Pair[] pairs)
154 StringBuilder builder = new StringBuilder();
155 for (Pair pair : pairs) {
156 // throw some whitespace in to ensure it's trimmed
157 builder.append(whitespace()).append(pair.key).
158 append(whitespace()).append("=").
159 append(whitespace()).append(pair.value).
160 append(whitespace()).append("\n");
162 return new StringReader(builder.toString());
165 protected static String whitespace ()
167 return _rando.nextBoolean() ? " " : "";
170 protected static Random _rando = new Random();