|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.utils.server; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNull; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.mockStatic; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.util.Properties; |
| 30 | + |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.MockedStatic; |
| 35 | +import org.mockito.junit.MockitoJUnitRunner; |
| 36 | + |
| 37 | +import com.cloud.utils.PropertiesUtil; |
| 38 | + |
| 39 | +@RunWith(MockitoJUnitRunner.class) |
| 40 | +public class ServerPropertiesUtilTest { |
| 41 | + |
| 42 | + @After |
| 43 | + public void clearCache() { |
| 44 | + ServerPropertiesUtil.propertiesRef.set(null); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void returnsPropertyValueWhenPropertiesAreLoaded() { |
| 49 | + Properties mockProperties = mock(Properties.class); |
| 50 | + when(mockProperties.getProperty("key")).thenReturn("value"); |
| 51 | + ServerPropertiesUtil.propertiesRef.set(mockProperties); |
| 52 | + String result = ServerPropertiesUtil.getProperty("key"); |
| 53 | + assertEquals("value", result); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void returnsNullWhenPropertyDoesNotExist() { |
| 58 | + Properties mockProperties = mock(Properties.class); |
| 59 | + ServerPropertiesUtil.propertiesRef.set(mockProperties); |
| 60 | + assertNull(ServerPropertiesUtil.getProperty("nonexistentKey")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void loadsPropertiesFromFileWhenNotCached() throws Exception { |
| 65 | + File tempFile = Files.createTempFile("server", ".properties").toFile(); |
| 66 | + tempFile.deleteOnExit(); |
| 67 | + Files.writeString(tempFile.toPath(), "key=value\n"); |
| 68 | + try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) { |
| 69 | + mocked.when(() -> PropertiesUtil.findConfigFile(ServerPropertiesUtil.PROPERTIES_FILE)) |
| 70 | + .thenReturn(tempFile); |
| 71 | + assertEquals("value", ServerPropertiesUtil.getProperty("key")); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void returnsNullWhenPropertiesFileNotFound() { |
| 77 | + try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) { |
| 78 | + mocked.when(() -> PropertiesUtil.findConfigFile(ServerPropertiesUtil.PROPERTIES_FILE)) |
| 79 | + .thenReturn(null); |
| 80 | + assertNull(ServerPropertiesUtil.getProperty("key")); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void returnsNullWhenIOExceptionOccurs() throws IOException { |
| 86 | + File tempFile = Files.createTempFile("bad", ".properties").toFile(); |
| 87 | + tempFile.deleteOnExit(); |
| 88 | + Files.writeString(tempFile.toPath(), "\u0000\u0000\u0000"); |
| 89 | + try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) { |
| 90 | + mocked.when(() -> PropertiesUtil.findConfigFile(ServerPropertiesUtil.PROPERTIES_FILE)) |
| 91 | + .thenReturn(tempFile); |
| 92 | + assertNull(ServerPropertiesUtil.getProperty("key")); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments