1 /*
2  * Copyright (C) 2017 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.invoker;
17 
18 import static org.junit.Assert.*;
19 
20 import com.android.tradefed.build.BuildInfo;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.config.ConfigurationDescriptor;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.util.FileUtil;
25 import com.android.tradefed.util.MultiMap;
26 import com.android.tradefed.util.SerializationUtil;
27 import com.android.tradefed.util.UniqueMultiMap;
28 
29 import org.easymock.EasyMock;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 import java.io.File;
36 import java.util.Arrays;
37 
38 /** Unit tests for {@link InvocationContext} */
39 @RunWith(JUnit4.class)
40 public class InvocationContextTest {
41 
42     private InvocationContext mContext;
43 
44     @Before
setUp()45     public void setUp() {
46         mContext = new InvocationContext();
47     }
48 
49     /** Test the reverse look up of the device name in the configuration for an ITestDevice */
50     @Test
testGetDeviceName()51     public void testGetDeviceName() {
52         ITestDevice device1 = EasyMock.createMock(ITestDevice.class);
53         ITestDevice device2 = EasyMock.createMock(ITestDevice.class);
54         // assert that at init, map is empty.
55         assertNull(mContext.getDeviceName(device1));
56         mContext.addAllocatedDevice("test1", device1);
57         assertEquals("test1", mContext.getDeviceName(device1));
58         assertNull(mContext.getDeviceName(device2));
59     }
60 
61     /**
62      * Test adding attributes and querying them. The map returned is always a copy and does not
63      * affect the actual invocation attributes.
64      */
65     @Test
testGetAttributes()66     public void testGetAttributes() {
67         mContext.addInvocationAttribute("TEST_KEY", "TEST_VALUE");
68         assertEquals(Arrays.asList("TEST_VALUE"), mContext.getAttributes().get("TEST_KEY"));
69         MultiMap<String, String> map = mContext.getAttributes();
70         map.remove("TEST_KEY");
71         // assert that the key is still there in the map from the context
72         assertEquals(Arrays.asList("TEST_VALUE"), mContext.getAttributes().get("TEST_KEY"));
73     }
74 
75     /** Test that once locked the invocation context does not accept more invocation attributes. */
76     @Test
testLockedContext()77     public void testLockedContext() {
78         mContext.lockAttributes();
79         try {
80             mContext.addInvocationAttribute("test", "Test");
81             fail("Should have thrown an exception.");
82         } catch (IllegalStateException expected) {
83             // expected
84         }
85         try {
86             mContext.addInvocationAttributes(new UniqueMultiMap<>());
87             fail("Should have thrown an exception.");
88         } catch (IllegalStateException expected) {
89             // expected
90         }
91     }
92 
93     /** Test that serializing and deserializing an {@link InvocationContext}. */
94     @Test
testSerialize()95     public void testSerialize() throws Exception {
96         assertNotNull(mContext.getDeviceBuildMap());
97         ITestDevice device = EasyMock.createMock(ITestDevice.class);
98         IBuildInfo info = new BuildInfo("1234", "test-target");
99         mContext.addAllocatedDevice("test-device", device);
100         mContext.addDeviceBuildInfo("test-device", info);
101         mContext.setConfigurationDescriptor(new ConfigurationDescriptor());
102         assertEquals(info, mContext.getBuildInfo(device));
103         File ser = SerializationUtil.serialize(mContext);
104         try {
105             InvocationContext deserialized =
106                     (InvocationContext) SerializationUtil.deserialize(ser, true);
107             // One consequence is that transient attribute will become null but our custom
108             // deserialization should fix that.
109             assertNotNull(deserialized.getDeviceBuildMap());
110             assertNotNull(deserialized.getConfigurationDescriptor());
111             assertEquals(info, deserialized.getBuildInfo("test-device"));
112 
113             // The device are not carried
114             assertTrue(deserialized.getDevices().isEmpty());
115             // Re-assigning a device, recreate the previous relationships
116             deserialized.addAllocatedDevice("test-device", device);
117             assertEquals(info, mContext.getBuildInfo(device));
118         } finally {
119             FileUtil.deleteFile(ser);
120         }
121     }
122 }
123