blob: 317d1e541bd1d262c8b878bbea5c493570daf4c2 [file] [log] [blame]
Paul Duffine2363012015-11-30 16:20:41 +00001/*
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
17package com.google.caliper.config;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.when;
21
22import com.google.caliper.options.CaliperOptions;
23import com.google.common.collect.ImmutableMap;
24
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.mockito.Mock;
30import org.mockito.runners.MockitoJUnitRunner;
31
32import java.io.File;
33import java.io.FileOutputStream;
34import java.io.IOException;
35import java.util.Properties;
36
37/**
38 * Tests {@link CaliperConfigLoader}.
39 */
40@RunWith(MockitoJUnitRunner.class)
41
42public class CaliperConfigLoaderTest {
43 @Mock CaliperOptions optionsMock;
44
45 private File tempConfigFile;
46
47 @Before public void createTempUserProperties() throws IOException {
48 tempConfigFile = File.createTempFile("caliper-config-test", "properties");
49 tempConfigFile.deleteOnExit();
50 Properties userProperties = new Properties();
51 userProperties.put("some.property", "franklin");
52 FileOutputStream fs = new FileOutputStream(tempConfigFile);
53 userProperties.store(fs, null);
54 fs.close();
55 }
56
57 @After public void deleteTempUserProperties() {
58 tempConfigFile.delete();
59 }
60
61 @Test public void loadOrCreate_configFileExistsNoOverride() throws Exception {
62 when(optionsMock.caliperConfigFile()).thenReturn(tempConfigFile);
63 when(optionsMock.configProperties()).thenReturn(ImmutableMap.<String, String>of());
64 CaliperConfigLoader loader = new CaliperConfigLoader(optionsMock);
65 CaliperConfig config = loader.loadOrCreate();
66 assertEquals("franklin", config.properties.get("some.property"));
67 }
68
69 @Test public void loadOrCreate_configFileExistsWithOverride() throws Exception {
70 when(optionsMock.caliperConfigFile()).thenReturn(tempConfigFile);
71 when(optionsMock.configProperties()).thenReturn(ImmutableMap.of(
72 "some.property", "tacos"));
73 CaliperConfigLoader loader = new CaliperConfigLoader(optionsMock);
74 CaliperConfig config = loader.loadOrCreate();
75 assertEquals("tacos", config.properties.get("some.property"));
76 }
77}