• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.*;
19 
20 import com.android.tradefed.config.proto.ConfigurationDescription.Descriptor;
21 import com.android.tradefed.util.FileUtil;
22 import com.android.tradefed.util.SerializationUtil;
23 
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 import java.io.File;
30 
31 /** Unit Tests for {@link ConfigurationDescriptor} */
32 @RunWith(JUnit4.class)
33 public class ConfigurationDescriptorTest {
34 
35     private ConfigurationFactory mFactory;
36 
37     @Before
setUp()38     public void setUp() throws Exception {
39         mFactory =
40                 new ConfigurationFactory() {
41                     @Override
42                     protected String getConfigPrefix() {
43                         return "testconfigs/";
44                     }
45                 };
46     }
47 
48     /**
49      * Test that even if an option exists in a ConfigurationDescriptor, it cannot receive value via
50      * command line.
51      */
52     @Test
testRejectCommandLine()53     public void testRejectCommandLine() throws Exception {
54         IConfiguration config = mFactory.createConfigurationFromArgs(new String[] {"test-config"});
55         OptionSetter setter = new OptionSetter(config.getConfigurationDescription());
56         // Confirm that options exists for the object
57         setter.setOptionValue("test-suite-tag", "Test");
58         try {
59             // This will still throw since it cannot be set via command line.
60             mFactory.createConfigurationFromArgs(
61                     new String[] {"test-config", "--test-suite-tag", "TEST"});
62             fail("Should have thrown an exception.");
63         } catch (OptionNotAllowedException expected) {
64             assertEquals(
65                     "Option test-suite-tag cannot be specified via command line. "
66                             + "Only in the configuration xml.",
67                     expected.getMessage());
68         }
69     }
70 
71     /** Test to ensure ConfigurationDescriptor is serializable/deserializable properly. */
72     @Test
testSerialization()73     public void testSerialization() throws Exception {
74         IConfiguration config = mFactory.createConfigurationFromArgs(new String[] {"test-config"});
75         ConfigurationDescriptor descriptor = config.getConfigurationDescription();
76         descriptor.setModuleName("test");
77         assertEquals("test", descriptor.getModuleName());
78         File serialized = SerializationUtil.serialize(descriptor);
79         try {
80             ConfigurationDescriptor desc =
81                     (ConfigurationDescriptor) SerializationUtil.deserialize(serialized, true);
82             assertEquals("test", desc.getModuleName());
83         } finally {
84             FileUtil.deleteFile(serialized);
85         }
86     }
87 
88     /** Test that ConfigurationDescriptor can be serialized an deserialized in proto format. */
89     @Test
testProtoSerialization()90     public void testProtoSerialization() {
91         ConfigurationDescriptor descriptor = new ConfigurationDescriptor();
92         descriptor.setSandboxed(true);
93         Descriptor protoDescriptor = descriptor.toProto();
94         assertTrue(protoDescriptor.getUseSandboxing());
95         assertTrue(protoDescriptor.getShardable());
96         assertTrue(protoDescriptor.getStrictShardable());
97         // Check the deserialization
98         ConfigurationDescriptor deserialized = ConfigurationDescriptor.fromProto(protoDescriptor);
99         assertNull(deserialized.getAbi());
100         assertTrue(deserialized.shouldUseSandbox());
101         assertFalse(deserialized.isNotShardable());
102         assertFalse(deserialized.isNotStrictShardable());
103     }
104 }
105