blob: 2d9bed8726fa5b16d9075157b3d7c542f1a3f724 [file] [log] [blame]
Brett Chabotccdf3272010-02-27 18:59:03 -08001/*
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 */
16package com.android.tradefed.config;
17
Brett Chabote278a5b2010-06-01 16:20:14 -070018import com.android.ddmlib.Log.LogLevel;
19import com.android.tradefed.log.ILeveledLogOutput;
20
21import java.io.ByteArrayOutputStream;
Brett Chabotccdf3272010-02-27 18:59:03 -080022import java.io.File;
Brett Chabote278a5b2010-06-01 16:20:14 -070023import java.io.PrintStream;
Brett Chabotccdf3272010-02-27 18:59:03 -080024
25import junit.framework.TestCase;
26
27/**
28 * Unit tests for {@link ConfigurationFactory}
29 */
30public class ConfigurationFactoryTest extends TestCase {
31
Brett Chabote278a5b2010-06-01 16:20:14 -070032 private IConfigurationFactory mFactory;
33
Brett Chabotccdf3272010-02-27 18:59:03 -080034 /**
35 * {@inheritDoc}
36 */
Brett Chabota08f7182010-03-08 18:55:38 -080037 @Override
Brett Chabotccdf3272010-02-27 18:59:03 -080038 protected void setUp() throws Exception {
39 super.setUp();
Brett Chabote278a5b2010-06-01 16:20:14 -070040 mFactory = ConfigurationFactory.getInstance();
Brett Chabotccdf3272010-02-27 18:59:03 -080041 }
42
43 /**
Brett Chabote278a5b2010-06-01 16:20:14 -070044 * Simple test method to ensure {@link ConfigurationFactory#getConfiguration(String)} return a
45 * valid configuration for all the default configs
Brett Chabotccdf3272010-02-27 18:59:03 -080046 */
Brett Chabote278a5b2010-06-01 16:20:14 -070047 public void testGetConfiguration_defaults() throws ConfigurationException {
48 for (String config : ConfigurationFactory.sDefaultConfigs) {
49 assertConfigValid(config);
50 }
Brett Chabotccdf3272010-02-27 18:59:03 -080051 }
52
53 /**
Brett Chabote278a5b2010-06-01 16:20:14 -070054 * Test that a config xml defined in this test jar can be read
Brett Chabota08f7182010-03-08 18:55:38 -080055 */
Brett Chabote278a5b2010-06-01 16:20:14 -070056 public void testGetConfiguration_extension() throws ConfigurationException {
57 assertConfigValid("test-config");
Brett Chabota08f7182010-03-08 18:55:38 -080058 }
59
60 /**
Brett Chabote278a5b2010-06-01 16:20:14 -070061 * Checks all config attributes are non-null
Brett Chabotfeeb2142010-04-16 16:22:52 -070062 */
Brett Chabote278a5b2010-06-01 16:20:14 -070063 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 Chabotfeeb2142010-04-16 16:22:52 -070072 }
73
74 /**
Brett Chabotccdf3272010-02-27 18:59:03 -080075 * Test calling {@link ConfigurationFactory#getConfiguration(String)} with a name that does not
76 * exist.
77 */
78 public void testGetConfiguration_missing() {
79 try {
Brett Chabote278a5b2010-06-01 16:20:14 -070080 mFactory.getConfiguration("non existent");
Brett Chabotccdf3272010-02-27 18:59:03 -080081 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 Chabote278a5b2010-06-01 16:20:14 -070093 mFactory.createConfigurationFromXML(new File("mockFile"));
Brett Chabotccdf3272010-02-27 18:59:03 -080094 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 Chabote278a5b2010-06-01 16:20:14 -0700106 mFactory.createConfigurationFromArgs(new String[] {});
Brett Chabotccdf3272010-02-27 18:59:03 -0800107 fail("did not throw ConfigurationException");
108 } catch (ConfigurationException e) {
109 // expected
110 }
111 }
Brett Chabote278a5b2010-06-01 16:20:14 -0700112
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 Chabotccdf3272010-02-27 18:59:03 -0800137}