1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.sdkmanager;
import com.android.SdkConstants;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.SdkManager;
import com.android.sdklib.SdkManagerTestCase;
import com.android.sdklib.internal.avd.AvdInfo;
import com.android.sdklib.internal.repository.CanceledByUserException;
import com.android.sdklib.internal.repository.DownloadCache;
import com.android.sdklib.internal.repository.DownloadCache.Strategy;
import com.android.sdklib.internal.repository.NullTaskMonitor;
import com.android.sdklib.repository.SdkAddonConstants;
import com.android.sdklib.repository.SdkRepoConstants;
import com.android.utils.Pair;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
public class MainTest extends SdkManagerTestCase {
private IAndroidTarget mTarget;
private File mAvdFolder;
@Override
public void setUp() throws Exception {
super.setUp();
mTarget = getSdkManager().getTargets()[0];
mAvdFolder = AvdInfo.getDefaultAvdFolder(getAvdManager(), getName());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
public void testDisplayEmptyAvdList() {
Main main = new Main();
main.setLogger(getLog());
getLog().clear();
main.displayAvdList(getAvdManager());
assertEquals("[P Available Android Virtual Devices:\n]", getLog().toString());
}
public void testDisplayAvdListOfOneNonSnapshot() {
Main main = new Main();
main.setLogger(getLog());
getAvdManager().createAvd(
mAvdFolder,
this.getName(),
mTarget,
SdkConstants.ABI_ARMEABI,
null, // skinName
null, // sdName
null, // properties
false, // createSnapshot
false, // removePrevious
false, // editExisting
getLog());
getLog().clear();
main.displayAvdList(getAvdManager());
assertEquals(
"[P Available Android Virtual Devices:\n"
+ ", P Name: " + this.getName() + "\n"
+ ", P Path: " + mAvdFolder + "\n"
+ ", P Target: Android 0.0 (API level 0)\n"
+ ", P ABI: armeabi\n"
+ ", P Skin: HVGA\n"
+ "]",
getLog().toString());
}
public void testDisplayAvdListOfOneSnapshot() {
Main main = new Main();
main.setLogger(getLog());
getAvdManager().createAvd(
mAvdFolder,
this.getName(),
mTarget,
SdkConstants.ABI_ARMEABI,
null, // skinName
null, // sdName
null, // properties
true, // createSnapshot
false, // removePrevious
false, // editExisting
getLog());
getLog().clear();
main.displayAvdList(getAvdManager());
assertEquals(
"[P Available Android Virtual Devices:\n"
+ ", P Name: " + this.getName() + "\n"
+ ", P Path: " + mAvdFolder + "\n"
+ ", P Target: Android 0.0 (API level 0)\n"
+ ", P ABI: armeabi\n"
+ ", P Skin: HVGA\n"
+ ", P Snapshot: true\n"
+ "]",
getLog().toString());
}
public void testDisplayTargetList() {
Main main = new Main();
main.setLogger(getLog());
main.setSdkManager(getSdkManager());
getLog().clear();
main.displayTargetList();
assertEquals(
"[P Available Android targets:\n" +
", P ----------\n" +
", P id: 1 or \"android-0\"\n" +
", P Name: Android 0.0\n" +
", P Type: Platform\n" +
", P API level: 0\n" +
", P Revision: 1\n" +
", P Skins: , P \n" +
", P ABIs : , P armeabi, P \n" +
"]",
getLog().toString());
}
public void testDisplayAbiList() {
Main main = new Main();
main.setLogger(getLog());
main.setSdkManager(getSdkManager());
getLog().clear();
main.displayAbiList(mTarget, "message");
assertEquals(
"[P message, P armeabi, P \n" +
"]",
getLog().toString());
}
public void testDisplaySkinList() {
Main main = new Main();
main.setLogger(getLog());
main.setSdkManager(getSdkManager());
getLog().clear();
main.displaySkinList(mTarget, "message");
assertEquals(
"[P message, P \n" +
"]",
getLog().toString());
}
public void testSdkManagerHasChanged() throws IOException {
Main main = new Main();
main.setLogger(getLog());
SdkManager sdkman = getSdkManager();
main.setSdkManager(sdkman);
getLog().clear();
assertFalse(sdkman.hasChanged());
File addonsDir = new File(sdkman.getLocation(), SdkConstants.FD_ADDONS);
assertTrue(addonsDir.isDirectory());
FileWriter readme = new FileWriter(new File(addonsDir, "android.txt"));
readme.write("test\n");
readme.close();
// Adding a file doesn't alter sdk.hasChanged
assertFalse(sdkman.hasChanged());
sdkman.reloadSdk(getLog());
assertFalse(sdkman.hasChanged());
File fakeAddon = new File(addonsDir, "google-addon");
fakeAddon.mkdirs();
File sourceProps = new File(fakeAddon, SdkConstants.FN_SOURCE_PROP);
FileWriter propsWriter = new FileWriter(sourceProps);
propsWriter.write("revision=7\n");
propsWriter.close();
// Adding a directory does alter sdk.hasChanged even if not a real add-on
assertTrue(sdkman.hasChanged());
// Once reloaded, sdk.hasChanged will be reset
sdkman.reloadSdk(getLog());
assertFalse(sdkman.hasChanged());
// Changing the source.properties file alters sdk.hasChanged
propsWriter = new FileWriter(sourceProps);
propsWriter.write("revision=8\n");
propsWriter.close();
assertTrue(sdkman.hasChanged());
// Once reloaded, sdk.hasChanged will be reset
sdkman.reloadSdk(getLog());
assertFalse(sdkman.hasChanged());
}
public void testCheckFilterValues() {
// These are the values we expect checkFilterValues() to match.
String[] expectedValues = {
"platform",
"system-image",
"tool",
"platform-tool",
"doc",
"sample",
"add-on",
"extra",
"source"
};
Set<String> expectedSet = new TreeSet<String>(Arrays.asList(expectedValues));
// First check the values are actually defined in the proper arrays
// in the Sdk*Constants.NODES
for (String node : SdkRepoConstants.NODES) {
assertTrue(
String.format(
"Error: value '%1$s' from SdkRepoConstants.NODES should be used in unit-test",
node),
expectedSet.contains(node));
}
for (String node : SdkAddonConstants.NODES) {
assertTrue(
String.format(
"Error: value '%1$s' from SdkAddonConstants.NODES should be used in unit-test",
node),
expectedSet.contains(node));
}
// Now check none of these values are NOT present in the NODES arrays
for (String node : SdkRepoConstants.NODES) {
expectedSet.remove(node);
}
for (String node : SdkAddonConstants.NODES) {
expectedSet.remove(node);
}
assertTrue(
String.format(
"Error: values %1$s are missing from Sdk[Repo|Addons]Constants.NODES",
Arrays.toString(expectedSet.toArray())),
expectedSet.isEmpty());
// We're done with expectedSet now
expectedSet = null;
// Finally check that checkFilterValues accepts all these values, one by one.
Main main = new Main();
main.setLogger(getLog());
for (int step = 0; step < 3; step++) {
for (String value : expectedValues) {
switch(step) {
// step 0: use value as-is
case 1:
// add some whitespace before and after
value = " " + value + " ";
break;
case 2:
// same with some empty arguments that should get ignored
value = " ," + value + " , ";
break;
}
Pair<String, ArrayList<String>> result = main.checkFilterValues(value);
assertNull(
String.format("Expected error to be null for value '%1$s', got: %2$s",
value, result.getFirst()),
result.getFirst());
assertEquals(
String.format("[%1$s]", value.replace(',', ' ').trim()),
Arrays.toString(result.getSecond().toArray()));
}
}
}
public void testLocalFileDownload() throws IOException, CanceledByUserException {
Main main = new Main();
main.setLogger(getLog());
SdkManager sdkman = getSdkManager();
main.setSdkManager(sdkman);
getLog().clear();
IAndroidTarget target = sdkman.getTargets()[0];
File sourceProps = new File(target.getLocation(), SdkConstants.FN_SOURCE_PROP);
assertTrue(sourceProps.isFile());
String urlStr = getFileUrl(sourceProps);
assertTrue(urlStr.startsWith("file:///"));
DownloadCache cache = new DownloadCache(Strategy.DIRECT);
NullTaskMonitor monitor = new NullTaskMonitor(getLog());
Pair<InputStream, Integer> result = cache.openDirectUrl(urlStr, monitor);
assertNotNull(result);
assertEquals(200, result.getSecond().intValue());
int len = (int) sourceProps.length();
byte[] buf = new byte[len];
FileInputStream is = new FileInputStream(sourceProps);
is.read(buf);
is.close();
String expected = new String(buf, "UTF-8");
buf = new byte[len];
result.getFirst().read(buf);
result.getFirst().close();
String actual = new String(buf, "UTF-8");
assertEquals(expected, actual);
}
private String getFileUrl(File file) throws IOException {
// Note: to create a file:// URL, one would typically use something like
// f.toURI().toURL().toString(). However this generates a broken path on
// Windows, namely "C:\\foo" is converted to "file:/C:/foo" instead of
// "file:///C:/foo" (i.e. there should be 3 / after "file:"). So we'll
// do the correct thing manually.
String path = file.getCanonicalPath();
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
// A file:// should start with 3 // (2 for file:// and 1 to make it an absolute
// path. On Windows that should look like file:///C:/. Linux/Mac will already
// have that leading / in their path so we need to compensate for windows.
if (!path.startsWith("/")) {
path = "/" + path;
}
// For some reason the URL class doesn't add the mandatory "//" after
// the "file:" protocol name, so it has to be hacked into the path.
URL url = new URL("file", null, "//" + path); //$NON-NLS-1$ //$NON-NLS-2$
String result = url.toString();
return result;
}
}
|