• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package vogar.android;
18 
19 import com.google.common.base.Function;
20 import com.google.common.collect.Lists;
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.runners.MockitoJUnitRunner;
31 import vogar.LocalTarget;
32 import vogar.Mode;
33 import vogar.ModeId;
34 import vogar.Target;
35 import vogar.Variant;
36 import vogar.commands.Command;
37 import vogar.commands.VmCommandBuilder;
38 
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotNull;
41 
42 /**
43  * Test the behaviour of the {@link HostRuntime} class when run with {@link LocalTarget}.
44  */
45 @RunWith(MockitoJUnitRunner.class)
46 public class HostRuntimeLocalTargetTest extends AbstractModeTest {
47 
48     public static final String ANDROID_BUILD_TOP_REFERENCE =
49             Matcher.quoteReplacement("${ANDROID_BUILD_TOP}/");
50 
51     @Override
createTarget()52     protected Target createTarget() {
53         return new LocalTarget(console, mkdir, rm);
54     }
55 
56     @Test
57     @VogarArgs({"action"})
testLocalTarget()58     public void testLocalTarget()
59             throws IOException {
60 
61         Mode hostRuntime = new HostRuntime(run, ModeId.HOST, Variant.X32);
62 
63         VmCommandBuilder builder = newVmCommandBuilder(hostRuntime)
64                 .classpath(classpath)
65                 .mainClass("mainclass")
66                 .args("-x", "a b");
67         Command command = builder.build(run.target);
68         List<String> args = command.getArgs();
69         args = replaceEnvironmentVariables(args);
70 
71         assertEquals(Arrays.asList(
72                 "sh", "-c", ""
73                         + "ANDROID_PRINTF_LOG=tag"
74                         + " ANDROID_LOG_TAGS=*:i"
75                         + " ANDROID_DATA=" + run.localFile("android-data")
76                         + " ANDROID_ROOT=${ANDROID_BUILD_TOP}/out/host/linux-x86"
77                         + " LD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib"
78                         + " DYLD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib"
79                         + " LD_USE_LOAD_BIAS=1"
80                         + " ${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/dalvikvm32"
81                         + " -classpath classes"
82                         + " -Xbootclasspath"
83                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-libart-hostdex.jar"
84                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-oj-hostdex.jar"
85                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/conscrypt-hostdex.jar"
86                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/okhttp-hostdex.jar"
87                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/bouncycastle-hostdex.jar"
88                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/apache-xml-hostdex.jar"
89                         + " -Duser.language=en"
90                         + " -Duser.region=US"
91                         + " -Xcheck:jni"
92                         + " -Xjnigreflimit:2000"
93                         + " mainclass"
94                         + " -x a\\ b"), args);
95     }
96 
replaceEnvironmentVariables(List<String> args)97     public List<String> replaceEnvironmentVariables(List<String> args) {
98         String androidBuildTop = System.getenv("ANDROID_BUILD_TOP");
99         assertNotNull("ANDROID_BUILD_TOP not set", androidBuildTop);
100         Path path = Paths.get(androidBuildTop).normalize();
101         final String prefix = Pattern.quote(path.toString() + "/");
102         args = Lists.transform(args, new Function<String, String>() {
103             @Override
104             public String apply(String input) {
105                 return input.replaceAll(prefix, ANDROID_BUILD_TOP_REFERENCE);
106             }
107         });
108         return args;
109     }
110 
111     @Test
112     @VogarArgs({"--benchmark", "action"})
testLocalTarget_Benchmark()113     public void testLocalTarget_Benchmark()
114             throws IOException {
115 
116         Mode hostRuntime = new HostRuntime(run, ModeId.HOST, Variant.X32);
117 
118         VmCommandBuilder builder = newVmCommandBuilder(hostRuntime)
119                 .classpath(classpath)
120                 .mainClass("mainclass")
121                 .args("-x", "a b");
122         Command command = builder.build(run.target);
123         List<String> args = command.getArgs();
124         args = replaceEnvironmentVariables(args);
125 
126         assertEquals(Arrays.asList(
127                 "sh", "-c", ""
128                         + "ANDROID_PRINTF_LOG=tag"
129                         + " ANDROID_LOG_TAGS=*:i"
130                         + " ANDROID_DATA=" + run.localFile("android-data")
131                         + " ANDROID_ROOT=${ANDROID_BUILD_TOP}/out/host/linux-x86"
132                         + " LD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib"
133                         + " DYLD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib"
134                         + " LD_USE_LOAD_BIAS=1"
135                         + " ${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/dalvikvm32"
136                         + " -classpath classes"
137                         + " -Xbootclasspath"
138                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-libart-hostdex.jar"
139                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-oj-hostdex.jar"
140                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/conscrypt-hostdex.jar"
141                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/okhttp-hostdex.jar"
142                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/bouncycastle-hostdex.jar"
143                         + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/apache-xml-hostdex.jar"
144                         + " -Duser.language=en"
145                         + " -Duser.region=US"
146                         + " -Xjnigreflimit:2000"
147                         + " mainclass"
148                         + " -x a\\ b"), args);
149     }
150 }
151