blob: 87c128bfc17ae44f198d9ad95bcefa2af0299af8 [file] [log] [blame]
Brett Chabote278a5b2010-06-01 16:20:14 -07001/*
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
18import com.android.tradefed.targetsetup.StubBuildProvider;
19
20import java.io.ByteArrayOutputStream;
21import java.io.PrintStream;
22
23import junit.framework.TestCase;
24
25/**
26 * Unit tests for {@link ConfigurationDef}
27 */
28public class ConfigurationDefTest extends TestCase {
29
30 private static final String CONFIG_NAME = "name";
31 private static final String CONFIG_DESCRIPTION = "config description";
32 private static final String OPTION_VALUE = "val";
33 private static final String OPTION_NAME = "option";
34 private static final String OPTION_DESCRIPTION = "option description";
35
36 public static class OptionTest extends StubBuildProvider {
37 @Option(name=OPTION_NAME, description=OPTION_DESCRIPTION)
38 private String mOption = null;
39 }
40
41 private ConfigurationDef mConfigDef;
42
43 @Override
44 protected void setUp() throws Exception {
45 super.setUp();
46 mConfigDef = new ConfigurationDef(CONFIG_NAME);
47 mConfigDef.setDescription(CONFIG_DESCRIPTION);
48 mConfigDef.addConfigObjectDef(Configuration.BUILD_PROVIDER_NAME,
49 OptionTest.class.getName());
50 mConfigDef.addOptionDef(OPTION_NAME, OPTION_VALUE);
51 }
52
53 /**
54 * Test success case for {@link ConfigurationDef#createConfiguration()}.
55 */
56 public void testCreateConfiguration() throws ConfigurationException {
57 IConfiguration config = mConfigDef.createConfiguration();
58 OptionTest test = (OptionTest)config.getBuildProvider();
59 assertEquals(OPTION_VALUE, test.mOption);
60 }
61
62 /**
63 * Basic test for {@link ConfigurationDef#printCommandUsage(java.io.PrintStream)}.
64 */
65 public void testPrintCommandUsage() throws ConfigurationException {
66 // dump the print stream results to the ByteArrayOutputStream, so contents can be evaluated
67 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
68 PrintStream mockPrintStream = new PrintStream(outputStream);
69 mConfigDef.printCommandUsage(mockPrintStream);
70
71 // verifying exact contents would be prone to high-maintenance, so instead, just validate
72 // all expected names are present
73 final String usageString = outputStream.toString();
74 assertTrue("Usage text does not contain config name", usageString.contains(CONFIG_NAME));
75 assertTrue("Usage text does not contain config description", usageString.contains(
76 CONFIG_DESCRIPTION));
77 assertTrue("Usage text does not contain object name", usageString.contains(
78 Configuration.BUILD_PROVIDER_NAME));
79 assertTrue("Usage text does not contain option name", usageString.contains(OPTION_NAME));
80 assertTrue("Usage text does not contain option description",
81 usageString.contains(OPTION_DESCRIPTION));
82 }
83}