1 /* 2 * Copyright (C) 2010 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 package com.android.tradefed.config; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assert.fail; 22 23 import com.android.tradefed.build.StubBuildProvider; 24 import com.android.tradefed.device.metric.BaseDeviceMetricCollector; 25 import com.android.tradefed.device.metric.IMetricCollector; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 import java.util.ArrayList; 33 import java.util.Collection; 34 import java.util.HashMap; 35 import java.util.Map; 36 37 /** Unit tests for {@link ConfigurationDef}. */ 38 @RunWith(JUnit4.class) 39 public class ConfigurationDefTest { 40 41 private static final String CONFIG_NAME = "name"; 42 private static final String CONFIG_DESCRIPTION = "config description"; 43 private static final String OPTION_KEY = "key"; 44 private static final String OPTION_KEY2 = "key2"; 45 private static final String OPTION_VALUE = "val"; 46 private static final String OPTION_VALUE2 = "val2"; 47 private static final String OPTION_NAME = "option"; 48 private static final String COLLECTION_OPTION_NAME = "collection"; 49 private static final String MAP_OPTION_NAME = "map"; 50 private static final String OPTION_DESCRIPTION = "option description"; 51 52 public static class OptionTest extends StubBuildProvider { 53 @Option(name=COLLECTION_OPTION_NAME, description=OPTION_DESCRIPTION) 54 private Collection<String> mCollectionOption = new ArrayList<String>(); 55 56 @Option(name=MAP_OPTION_NAME, description=OPTION_DESCRIPTION) 57 private Map<String, String> mMapOption = new HashMap<String, String>(); 58 59 @Option(name=OPTION_NAME, description=OPTION_DESCRIPTION) 60 private String mOption; 61 } 62 63 private ConfigurationDef mConfigDef; 64 65 @Before setUp()66 public void setUp() throws Exception { 67 mConfigDef = new ConfigurationDef(CONFIG_NAME); 68 mConfigDef.setDescription(CONFIG_DESCRIPTION); 69 mConfigDef.addConfigObjectDef(Configuration.BUILD_PROVIDER_TYPE_NAME, 70 OptionTest.class.getName()); 71 } 72 73 /** Test {@link ConfigurationDef#createConfiguration()} for a map option. */ 74 @Test testCreateConfiguration_optionMap()75 public void testCreateConfiguration_optionMap() throws ConfigurationException { 76 mConfigDef.addOptionDef(MAP_OPTION_NAME, OPTION_KEY, OPTION_VALUE, CONFIG_NAME); 77 mConfigDef.addOptionDef(MAP_OPTION_NAME, OPTION_KEY2, OPTION_VALUE2, CONFIG_NAME); 78 IConfiguration config = mConfigDef.createConfiguration(); 79 OptionTest test = (OptionTest)config.getBuildProvider(); 80 assertNotNull(test.mMapOption); 81 assertEquals(2, test.mMapOption.size()); 82 assertEquals(OPTION_VALUE, test.mMapOption.get(OPTION_KEY)); 83 assertEquals(OPTION_VALUE2, test.mMapOption.get(OPTION_KEY2)); 84 } 85 86 /** Test {@link ConfigurationDef#createConfiguration()} for a collection option. */ 87 @Test testCreateConfiguration_optionCollection()88 public void testCreateConfiguration_optionCollection() throws ConfigurationException { 89 mConfigDef.addOptionDef(COLLECTION_OPTION_NAME, null, OPTION_VALUE, CONFIG_NAME); 90 mConfigDef.addOptionDef(COLLECTION_OPTION_NAME, null, OPTION_VALUE2, CONFIG_NAME); 91 IConfiguration config = mConfigDef.createConfiguration(); 92 OptionTest test = (OptionTest)config.getBuildProvider(); 93 assertTrue(test.mCollectionOption.contains(OPTION_VALUE)); 94 assertTrue(test.mCollectionOption.contains(OPTION_VALUE2)); 95 } 96 97 /** Test {@link ConfigurationDef#createConfiguration()} for a String field. */ 98 @Test testCreateConfiguration()99 public void testCreateConfiguration() throws ConfigurationException { 100 mConfigDef.addOptionDef(OPTION_NAME, null, OPTION_VALUE, CONFIG_NAME); 101 IConfiguration config = mConfigDef.createConfiguration(); 102 OptionTest test = (OptionTest)config.getBuildProvider(); 103 assertEquals(OPTION_VALUE, test.mOption); 104 } 105 106 /** Test {@link ConfigurationDef#createConfiguration()} for a String field. */ 107 @Test testCreateConfiguration_withDeviceHolder()108 public void testCreateConfiguration_withDeviceHolder() throws ConfigurationException { 109 mConfigDef = new ConfigurationDef(CONFIG_NAME); 110 mConfigDef.addExpectedDevice("device1", false); 111 mConfigDef.setMultiDeviceMode(true); 112 mConfigDef.setDescription(CONFIG_DESCRIPTION); 113 mConfigDef.addConfigObjectDef("device1:" + Configuration.BUILD_PROVIDER_TYPE_NAME, 114 OptionTest.class.getName()); 115 mConfigDef.addOptionDef(OPTION_NAME, null, OPTION_VALUE, CONFIG_NAME); 116 IConfiguration config = mConfigDef.createConfiguration(); 117 OptionTest test = (OptionTest)config.getDeviceConfigByName("device1") 118 .getBuildProvider(); 119 assertEquals(OPTION_VALUE, test.mOption); 120 } 121 122 /** 123 * Test {@link ConfigurationDef#createConfiguration()} for a {@link IMetricCollector} declared 124 * as a metric collector. 125 */ 126 @Test testCreateConfiguration_withCollectors()127 public void testCreateConfiguration_withCollectors() throws ConfigurationException { 128 mConfigDef = new ConfigurationDef(CONFIG_NAME); 129 mConfigDef.addConfigObjectDef( 130 Configuration.DEVICE_METRICS_COLLECTOR_TYPE_NAME, 131 BaseDeviceMetricCollector.class.getName()); 132 IConfiguration config = mConfigDef.createConfiguration(); 133 assertEquals(1, config.getMetricCollectors().size()); 134 } 135 136 /** 137 * Test {@link ConfigurationDef#createConfiguration()} for a {@link IMetricCollector} declared 138 * as a result reporter should be rejected. 139 */ 140 @Test testCreateConfiguration_withCollectors_asReporter()141 public void testCreateConfiguration_withCollectors_asReporter() { 142 mConfigDef = new ConfigurationDef(CONFIG_NAME); 143 mConfigDef.addConfigObjectDef( 144 Configuration.RESULT_REPORTER_TYPE_NAME, BaseDeviceMetricCollector.class.getName()); 145 try { 146 mConfigDef.createConfiguration(); 147 fail("Should have thrown an exception."); 148 } catch (ConfigurationException expected) { 149 // expected 150 } 151 } 152 } 153