Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 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 | |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 18 | import com.android.ddmlib.Log.LogLevel; |
| 19 | import com.android.tradefed.log.ILeveledLogOutput; |
| 20 | |
| 21 | import java.io.ByteArrayOutputStream; |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 22 | import java.io.File; |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 23 | import java.io.PrintStream; |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 24 | |
| 25 | import junit.framework.TestCase; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for {@link ConfigurationFactory} |
| 29 | */ |
| 30 | public class ConfigurationFactoryTest extends TestCase { |
| 31 | |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 32 | private IConfigurationFactory mFactory; |
| 33 | |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 34 | /** |
| 35 | * {@inheritDoc} |
| 36 | */ |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 37 | @Override |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 38 | protected void setUp() throws Exception { |
| 39 | super.setUp(); |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 40 | mFactory = ConfigurationFactory.getInstance(); |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 44 | * Simple test method to ensure {@link ConfigurationFactory#getConfiguration(String)} return a |
| 45 | * valid configuration for all the default configs |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 46 | */ |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 47 | public void testGetConfiguration_defaults() throws ConfigurationException { |
| 48 | for (String config : ConfigurationFactory.sDefaultConfigs) { |
| 49 | assertConfigValid(config); |
| 50 | } |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 54 | * Test that a config xml defined in this test jar can be read |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 55 | */ |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 56 | public void testGetConfiguration_extension() throws ConfigurationException { |
| 57 | assertConfigValid("test-config"); |
Brett Chabot | a08f718 | 2010-03-08 18:55:38 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 61 | * Checks all config attributes are non-null |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 62 | */ |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 63 | private void assertConfigValid(String name) throws ConfigurationException { |
| 64 | IConfiguration config = mFactory.getConfiguration(name); |
| 65 | assertNotNull(config); |
| 66 | assertNotNull(config.getBuildProvider()); |
| 67 | assertNotNull(config.getDeviceRecovery()); |
| 68 | assertNotNull(config.getLogOutput()); |
| 69 | assertNotNull(config.getTargetPreparer()); |
| 70 | assertNotNull(config.getTest()); |
| 71 | assertNotNull(config.getTestInvocationListener()); |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 75 | * Test calling {@link ConfigurationFactory#getConfiguration(String)} with a name that does not |
| 76 | * exist. |
| 77 | */ |
| 78 | public void testGetConfiguration_missing() { |
| 79 | try { |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 80 | mFactory.getConfiguration("non existent"); |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 81 | fail("did not throw ConfigurationException"); |
| 82 | } catch (ConfigurationException e) { |
| 83 | // expected |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Placeholder Test method for {@link ConfigurationFactory#createConfigurationFromXML(File)}. |
| 89 | */ |
| 90 | public void testCreateConfigurationFromXML() throws ConfigurationException { |
| 91 | try { |
| 92 | // TODO: use a mock File |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 93 | mFactory.createConfigurationFromXML(new File("mockFile")); |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 94 | fail("did not throw UnsupportedOperationException"); |
| 95 | } catch (UnsupportedOperationException e) { |
| 96 | // expected |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Test passing empty arg list to |
| 102 | * {@link ConfigurationFactory#createConfigurationFromArgs(String[])}. |
| 103 | */ |
| 104 | public void testCreateConfigurationFromArgs_empty() { |
| 105 | try { |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 106 | mFactory.createConfigurationFromArgs(new String[] {}); |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 107 | fail("did not throw ConfigurationException"); |
| 108 | } catch (ConfigurationException e) { |
| 109 | // expected |
| 110 | } |
| 111 | } |
Brett Chabot | e278a5b | 2010-06-01 16:20:14 -0700 | [diff] [blame^] | 112 | |
| 113 | /** |
| 114 | * Test {@link ConfigurationFactory#createConfigurationFromArgs(String[])} using host |
| 115 | */ |
| 116 | public void testCreateConfigurationFromArgs() throws ConfigurationException { |
| 117 | // pick an arbitrary option to test to ensure it gets populated |
| 118 | IConfiguration config = mFactory.createConfigurationFromArgs(new String[] {"--log-level", |
| 119 | LogLevel.VERBOSE.getStringValue(), ConfigurationFactory.HOST_TEST_CONFIG}); |
| 120 | ILeveledLogOutput logger = config.getLogOutput(); |
| 121 | assertEquals(LogLevel.VERBOSE.getStringValue(), logger.getLogLevel()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test {@link ConfigurationFactory#printHelp(String[], PrintStream))} with no args specified |
| 126 | */ |
| 127 | public void testPrintHelp() { |
| 128 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 129 | PrintStream mockPrintStream = new PrintStream(outputStream); |
| 130 | mFactory.printHelp(new String[] {"--help"}, mockPrintStream); |
| 131 | // verify all the default configs names are present |
| 132 | final String usageString = outputStream.toString(); |
| 133 | for (String config : ConfigurationFactory.sDefaultConfigs) { |
| 134 | assertTrue(usageString.contains(config)); |
| 135 | } |
| 136 | } |
Brett Chabot | ccdf327 | 2010-02-27 18:59:03 -0800 | [diff] [blame] | 137 | } |