Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 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.mockito.Mockito.when; |
| 21 | |
| 22 | import com.google.caliper.options.CaliperOptions; |
| 23 | import com.google.common.collect.ImmutableMap; |
| 24 | |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.mockito.Mock; |
| 30 | import org.mockito.runners.MockitoJUnitRunner; |
| 31 | |
| 32 | import java.io.File; |
| 33 | import java.io.FileOutputStream; |
| 34 | import java.io.IOException; |
| 35 | import java.util.Properties; |
| 36 | |
| 37 | /** |
| 38 | * Tests {@link CaliperConfigLoader}. |
| 39 | */ |
| 40 | @RunWith(MockitoJUnitRunner.class) |
| 41 | |
| 42 | public 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 | } |