Add checkstyle to utils.config
[netvirt.git] / utils / config / src / test / java / org / opendaylight / ovsdb / utils / config / ConfigPropertiesTest.java
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Authors : Flavio Fernandes
9 */
10 package org.opendaylight.ovsdb.utils.config;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16
17 import static org.mockito.Mockito.eq;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleContext;
23 import org.powermock.api.mockito.PowerMockito;
24 import org.osgi.framework.FrameworkUtil;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.junit4.PowerMockRunner;
27
28 @RunWith(PowerMockRunner.class)
29 @PrepareForTest( {System.class, FrameworkUtil.class} )
30  public class ConfigPropertiesTest {
31
32     private static final String TEST_PROPERTY_KEY1 = "foobar34465$3467";
33     private static final String TEST_PROPERTY_MOCK_VALUE1 = "xbarMock1";
34     private static final String TEST_PROPERTY_KEY2 = "foobar34465$3468";
35     private static final String TEST_PROPERTY_MOCK_VALUE2 = "xbarMock2";
36     private static final String TEST_PROPERTY_KEY_NOT_FOUND = "foobarKey2_12445^%346";
37     private static final String DEFAULT_PROPERTY_VALUE = "xbarDefaultValue";
38
39     @Test
40     public void testGetProperty() {
41         Bundle bundleWithoutBundleNoContext = mock(Bundle.class);
42         Bundle bundle = mock(Bundle.class);
43         BundleContext bundleContext = mock(BundleContext.class);
44
45         // mock #1
46         PowerMockito.mockStatic(FrameworkUtil.class);
47         PowerMockito.when(FrameworkUtil.getBundle(this.getClass())).thenReturn(bundle);
48         PowerMockito.when(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class))
49                 .thenReturn(bundleWithoutBundleNoContext);
50         // mock #2
51         when(bundle.getBundleContext()).thenReturn(bundleContext);
52         when(bundleWithoutBundleNoContext.getBundleContext()).thenReturn(null);
53         // mock #3
54         when(bundleContext.getProperty(eq(TEST_PROPERTY_KEY1))).thenReturn(TEST_PROPERTY_MOCK_VALUE1);
55         // mock #4
56         // PowerMockito.mockStatic(System.class);
57         // PowerMockito.when(System.getProperty(eq(TEST_PROPERTY_KEY2))).thenReturn(TEST_PROPERTY_MOCK_VALUE2);
58         // NOTE: The mock #4 above is not supported by PowerMockito. To work around this limitation,
59         // we will simply add the property explicitly into System (instead of the solution mentioned in link below).
60         // See: http://javax0.wordpress.com/2013/01/29/how-to-mock-the-system-class/
61         System.setProperty(TEST_PROPERTY_KEY2, TEST_PROPERTY_MOCK_VALUE2);
62
63         // test 1. bundle is null, returned from a mock
64         assertNull(FrameworkUtil.getBundle(ConfigPropertiesTestMocking.class));
65         assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTest.class), bundle);
66         assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class),
67                      bundleWithoutBundleNoContext);
68
69         // test 2. bundleContext is null
70         assertNull(bundleWithoutBundleNoContext.getBundleContext());
71         assertEquals(bundle.getBundleContext(), bundleContext);
72
73         // test 3. value returned from bundleContext.getProperty() is null.
74         // Then System.getProperty() is called and can return a valid value if key is found.
75         final String value31 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY2);
76         assertEquals(TEST_PROPERTY_MOCK_VALUE2, value31);
77
78         // test 4. value returned from ConfigProperties.getProperty is null
79         final String value41 = ConfigProperties.getProperty(ConfigPropertiesTestMocking.class, TEST_PROPERTY_KEY1);
80         assertNull(value41);  // class has no bundle
81         final String value42 = ConfigProperties.getProperty(ConfigPropertiesTestMockingBundleNoContext.class,
82                                                             TEST_PROPERTY_KEY1);
83         assertNull(value42);  // class has no bundleContext
84         final String value43 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY_NOT_FOUND);
85         assertNull(value43);  // bundleContext will not know about key provided
86
87         // test 5. value returned from ConfigProperties.getProperty is the default value provided
88         final String value5 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY_NOT_FOUND, DEFAULT_PROPERTY_VALUE);
89         assertEquals(DEFAULT_PROPERTY_VALUE, value5);
90
91         // test 6. value returned from ConfigProperties.getProperty is the mocked value
92         final String value61 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY1);
93         assertEquals(TEST_PROPERTY_MOCK_VALUE1, value61);
94         final String value62 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY1, DEFAULT_PROPERTY_VALUE);
95         assertEquals(TEST_PROPERTY_MOCK_VALUE1, value62);
96     }
97
98     // Helper classes used to de-mux mock behaviors
99     private class ConfigPropertiesTestMockingBundleNoContext {
100     }
101     private class ConfigPropertiesTestMocking {
102     }
103 }