1 /*
2  * Copyright (C) 2012 Google Inc.
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 com.google.caliper.config;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21 
22 import com.google.caliper.api.ResultProcessor;
23 import com.google.caliper.model.Trial;
24 import com.google.caliper.platform.Platform;
25 import com.google.caliper.platform.jvm.JvmPlatform;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.collect.ImmutableSet;
29 
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.JUnit4;
35 
36 import java.io.File;
37 import java.lang.management.ManagementFactory;
38 
39 /**
40  * Tests {@link CaliperConfig}.
41  *
42  * @author gak@google.com (Gregory Kick)
43  */
44 @RunWith(JUnit4.class)
45 public class CaliperConfigTest {
46   @Rule public TemporaryFolder folder = new TemporaryFolder();
47 
48   private Platform platform = new JvmPlatform();
49 
getDefaultVmConfig()50   @Test public void getDefaultVmConfig() throws Exception {
51     CaliperConfig configuration = new CaliperConfig(
52         ImmutableMap.of("vm.args", "-very -special=args"));
53     VmConfig defaultVmConfig = configuration.getDefaultVmConfig(platform);
54     assertEquals(new File(System.getProperty("java.home")), defaultVmConfig.vmHome());
55     ImmutableList<String> expectedArgs = new ImmutableList.Builder<String>()
56         .addAll(ManagementFactory.getRuntimeMXBean().getInputArguments())
57         .add("-very")
58         .add("-special=args")
59         .build();
60     assertEquals(expectedArgs, defaultVmConfig.options());
61   }
62 
getVmConfig_baseDirectoryAndName()63   @Test public void getVmConfig_baseDirectoryAndName() throws Exception {
64     File tempBaseDir = folder.newFolder();
65     File jdkHome = new File(tempBaseDir, "test");
66     jdkHome.mkdir();
67     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
68         "vm.baseDirectory", tempBaseDir.getAbsolutePath()));
69     assertEquals(new VmConfig.Builder(platform, jdkHome).build(),
70         configuration.getVmConfig(platform, "test"));
71   }
72 
getVmConfig_baseDirectoryAndHome()73   @Test public void getVmConfig_baseDirectoryAndHome() throws Exception {
74     File tempBaseDir = folder.newFolder();
75     File jdkHome = new File(tempBaseDir, "test-home");
76     jdkHome.mkdir();
77     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
78         "vm.baseDirectory", tempBaseDir.getAbsolutePath(),
79         "vm.test.home", "test-home"));
80     assertEquals(new VmConfig.Builder(platform, jdkHome).build(),
81         configuration.getVmConfig(platform, "test"));
82   }
83 
getVmConfig()84   @Test public void getVmConfig() throws Exception {
85     File jdkHome = folder.newFolder();
86     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
87         "vm.args", "-a -b   -c",
88         "vm.test.home", jdkHome.getAbsolutePath(),
89         "vm.test.args", " -d     -e     "));
90     assertEquals(
91         new VmConfig.Builder(platform, jdkHome)
92             .addOption("-a")
93             .addOption("-b")
94             .addOption("-c")
95             .addOption("-d")
96             .addOption("-e")
97             .build(),
98         configuration.getVmConfig(platform, "test"));
99   }
100 
getVmConfig_escapedSpacesInArgs()101   @Test public void getVmConfig_escapedSpacesInArgs() throws Exception {
102     File jdkHome = folder.newFolder();
103     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
104         "vm.args", "-a=string\\ with\\ spa\\ces -b -c",
105         "vm.test.home", jdkHome.getAbsolutePath()));
106     assertEquals(
107         new VmConfig.Builder(platform, jdkHome)
108         .addOption("-a=string with spaces")
109         .addOption("-b")
110         .addOption("-c")
111         .build(),
112         configuration.getVmConfig(platform, "test"));
113   }
114 
getInstrumentConfig()115   @Test public void getInstrumentConfig() throws Exception {
116     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
117         "instrument.test.class", "test.ClassName",
118         "instrument.test.options.a", "1",
119         "instrument.test.options.b", "excited b b excited"));
120     assertEquals(
121         new InstrumentConfig.Builder()
122             .className("test.ClassName")
123             .addOption("a", "1")
124             .addOption("b", "excited b b excited")
125             .build(),
126         configuration.getInstrumentConfig("test"));
127   }
128 
getInstrumentConfig_notConfigured()129   @Test public void getInstrumentConfig_notConfigured() throws Exception {
130     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
131         "instrument.test.options.a", "1",
132         "instrument.test.options.b", "excited b b excited"));
133     try {
134       configuration.getInstrumentConfig("test");
135       fail();
136     } catch (IllegalArgumentException expected) {}
137   }
138 
getConfiguredInstruments()139   @Test public void getConfiguredInstruments() throws Exception {
140     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
141         "instrument.test.class", "test.ClassName",
142         "instrument.test2.class", "test.ClassName",
143         "instrument.test3.options.a", "1",
144         "instrument.test4.class", "test.ClassName",
145         "instrument.test4.options.b", "excited b b excited"));
146     assertEquals(ImmutableSet.of("test", "test2", "test4"),
147         configuration.getConfiguredInstruments());
148   }
149 
getConfiguredResultProcessors()150   @Test public void getConfiguredResultProcessors() throws Exception {
151     assertEquals(ImmutableSet.of(),
152         new CaliperConfig(ImmutableMap.<String, String>of()).getConfiguredResultProcessors());
153     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
154         "results.test.class", TestResultProcessor.class.getName()));
155     assertEquals(ImmutableSet.of(TestResultProcessor.class),
156         configuration.getConfiguredResultProcessors());
157   }
158 
getResultProcessorConfig()159   @Test public void getResultProcessorConfig() throws Exception {
160     CaliperConfig configuration = new CaliperConfig(ImmutableMap.of(
161         "results.test.class", TestResultProcessor.class.getName(),
162         "results.test.options.g", "ak",
163         "results.test.options.c", "aliper"));
164     assertEquals(
165         new ResultProcessorConfig.Builder()
166             .className(TestResultProcessor.class.getName())
167             .addOption("g", "ak")
168             .addOption("c", "aliper")
169             .build(),
170         configuration.getResultProcessorConfig(TestResultProcessor.class));
171   }
172 
173   private static final class TestResultProcessor implements ResultProcessor {
close()174     @Override public void close() {}
175 
processTrial(Trial trial)176     @Override public void processTrial(Trial trial) {}
177   }
178 }
179