jdesprez | 7509fca | 2018-04-02 16:36:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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.guice; |
| 17 | |
| 18 | import static org.junit.Assert.*; |
| 19 | |
| 20 | import com.android.tradefed.config.Configuration; |
| 21 | import com.android.tradefed.config.IConfiguration; |
| 22 | import com.android.tradefed.device.DeviceNotAvailableException; |
| 23 | import com.android.tradefed.result.ITestInvocationListener; |
| 24 | import com.android.tradefed.targetprep.multi.StubMultiTargetPreparer; |
| 25 | import com.android.tradefed.testtype.IRemoteTest; |
| 26 | |
| 27 | import com.google.inject.Inject; |
| 28 | import com.google.inject.Injector; |
| 29 | |
| 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.util.Arrays; |
| 36 | |
| 37 | /** Unit tests for {@link InvocationScope}. */ |
| 38 | @RunWith(JUnit4.class) |
| 39 | public class InvocationScopeTest { |
| 40 | |
| 41 | /** Test class to check that object are properly seeded. */ |
| 42 | public static class InjectedClass implements IRemoteTest { |
| 43 | |
| 44 | public Injector mInjector; |
| 45 | public IConfiguration mConfiguration; |
| 46 | |
| 47 | @Inject |
| 48 | public void setInjector(Injector injector) { |
| 49 | mInjector = injector; |
| 50 | } |
| 51 | |
| 52 | @Inject |
| 53 | public void setConfiguration(IConfiguration configuration) { |
| 54 | mConfiguration = configuration; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { |
| 59 | // Do nothing |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private IConfiguration mConfiguration; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | mConfiguration = new Configuration("test", "test"); |
| 68 | } |
| 69 | |
| 70 | /** Test that the injection and seed object are available in the scope. */ |
| 71 | @Test |
| 72 | public void testInjection() { |
| 73 | InjectedClass test = new InjectedClass(); |
| 74 | mConfiguration.setTest(test); |
| 75 | mConfiguration.setMultiTargetPreparers(Arrays.asList(new StubMultiTargetPreparer())); |
| 76 | InvocationScope scope = new InvocationScope(); |
| 77 | scope.enter(); |
| 78 | try { |
| 79 | scope.seedConfiguration(mConfiguration); |
| 80 | assertNotNull(test.mInjector); |
| 81 | assertNotNull(test.mConfiguration); |
| 82 | assertEquals(mConfiguration, test.mConfiguration); |
| 83 | } finally { |
| 84 | scope.exit(); |
| 85 | } |
| 86 | } |
| 87 | } |