2 * Copyright (C) 2014 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.utils.config;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.mockStatic;
14 import static org.mockito.Mockito.when;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.osgi.framework.Bundle;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.FrameworkUtil;
23 @RunWith(MockitoJUnitRunner.StrictStubs.class)
24 public class ConfigPropertiesTest {
26 private static final String TEST_PROPERTY_KEY1 = "foobar34465$3467";
27 private static final String TEST_PROPERTY_MOCK_VALUE1 = "xbarMock1";
28 private static final String TEST_PROPERTY_KEY2 = "foobar34465$3468";
29 private static final String TEST_PROPERTY_MOCK_VALUE2 = "xbarMock2";
30 private static final String TEST_PROPERTY_KEY_NOT_FOUND = "foobarKey2_12445^%346";
31 private static final String DEFAULT_PROPERTY_VALUE = "xbarDefaultValue";
34 public void testGetProperty() {
35 try (var frameworkUtil = mockStatic(FrameworkUtil.class)) {
36 final var bundleWithoutBundleNoContext = mock(Bundle.class);
37 final var bundle = mock(Bundle.class);
40 frameworkUtil.when(() -> FrameworkUtil.getBundle(getClass()))
42 frameworkUtil.when(() -> FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class))
43 .thenReturn(bundleWithoutBundleNoContext);
46 BundleContext bundleContext = mock(BundleContext.class);
47 when(bundle.getBundleContext()).thenReturn(bundleContext);
48 when(bundleWithoutBundleNoContext.getBundleContext()).thenReturn(null);
50 when(bundleContext.getProperty(TEST_PROPERTY_KEY1)).thenReturn(TEST_PROPERTY_MOCK_VALUE1);
51 when(bundleContext.getProperty(TEST_PROPERTY_KEY2)).thenReturn(null);
52 when(bundleContext.getProperty(TEST_PROPERTY_KEY_NOT_FOUND)).thenReturn(null);
54 // mock #4, Mockito says:
55 // It is not possible to mock static methods of java.lang.System to avoid interfering with class loading
56 // what leads to infinite loops
58 // To work around this limitation, we will simply add the property explicitly into System
59 System.setProperty(TEST_PROPERTY_KEY2, TEST_PROPERTY_MOCK_VALUE2);
61 // test 1. bundle is null, returned from a mock
62 assertNull(FrameworkUtil.getBundle(ConfigPropertiesTestMocking.class));
63 assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTest.class), bundle);
64 assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class),
65 bundleWithoutBundleNoContext);
67 // test 2. bundleContext is null
68 assertNull(bundleWithoutBundleNoContext.getBundleContext());
69 assertEquals(bundle.getBundleContext(), bundleContext);
71 // test 3. value returned from bundleContext.getProperty() is null.
72 // Then System.getProperty() is called and can return a valid value if key is found.
73 assertEquals(TEST_PROPERTY_MOCK_VALUE2, ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY2));
75 // test 4. value returned from ConfigProperties.getProperty is null
76 final String value41 = ConfigProperties.getProperty(ConfigPropertiesTestMocking.class, TEST_PROPERTY_KEY1);
77 assertNull(value41); // class has no bundle
78 final String value42 = ConfigProperties.getProperty(ConfigPropertiesTestMockingBundleNoContext.class,
80 assertNull(value42); // class has no bundleContext
81 final String value43 = ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY_NOT_FOUND);
82 assertNull(value43); // bundleContext will not know about key provided
84 // test 5. value returned from ConfigProperties.getProperty is the default value provided
85 assertEquals(DEFAULT_PROPERTY_VALUE,
86 ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY_NOT_FOUND, DEFAULT_PROPERTY_VALUE));
88 // test 6. value returned from ConfigProperties.getProperty is the mocked value
89 assertEquals(TEST_PROPERTY_MOCK_VALUE1, ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY1));
90 assertEquals(TEST_PROPERTY_MOCK_VALUE1,
91 ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY1, DEFAULT_PROPERTY_VALUE));
95 // Helper classes used to de-mux mock behaviors
96 private static class ConfigPropertiesTestMockingBundleNoContext {
99 private static class ConfigPropertiesTestMocking {