001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.utils.file; 015 016import org.gbif.utils.file.properties.PropertiesUtil; 017 018import java.util.Properties; 019 020import org.junit.jupiter.api.Test; 021 022import static org.junit.jupiter.api.Assertions.assertEquals; 023import static org.junit.jupiter.api.Assertions.assertFalse; 024import static org.junit.jupiter.api.Assertions.assertNotNull; 025import static org.junit.jupiter.api.Assertions.assertThrows; 026import static org.junit.jupiter.api.Assertions.assertTrue; 027 028public class PropertiesUtilTest { 029 030 @Test 031 public void testFilterProperties() { 032 033 Properties properties = new Properties(); 034 properties.put("prefix.key1", "value1"); 035 properties.put("key2", "value2"); 036 037 Properties filteredProperties = PropertiesUtil.filterProperties(properties, "prefix."); 038 assertEquals(1, filteredProperties.size()); 039 assertTrue(filteredProperties.containsKey("key1"), "Prefix is removed from the original key"); 040 assertEquals( 041 properties.get("prefix.key1"), 042 filteredProperties.getProperty("key1"), 043 "Value remains the same"); 044 } 045 046 @Test 047 public void testSubsetProperties() { 048 049 Properties properties = new Properties(); 050 properties.put("prefix.key1", "value1"); 051 properties.put("key2", "value2"); 052 053 Properties newProperties = PropertiesUtil.subsetProperties(properties, "prefix."); 054 assertEquals(1, newProperties.size()); 055 assertEquals(2, properties.size()); 056 057 assertTrue(newProperties.containsKey("prefix.key1"), "Prefix is kept from the original key"); 058 assertTrue(properties.containsKey("prefix.key1"), "key1 is still in original Properties"); 059 assertTrue(properties.containsKey("key2"), "key2 is still in original Properties"); 060 } 061 062 @Test 063 public void testRemoveProperties() { 064 065 Properties properties = new Properties(); 066 properties.put("prefix.key1", "value1"); 067 properties.put("key2", "value2"); 068 069 Properties newProperties = PropertiesUtil.removeProperties(properties, "prefix."); 070 assertEquals(1, newProperties.size()); 071 assertEquals(1, properties.size()); 072 073 assertTrue(newProperties.containsKey("prefix.key1"), "Prefix is kept from the original key"); 074 assertTrue( 075 properties.containsKey("key2"), "Other element is still present in original Propeties"); 076 } 077 078 @Test 079 public void testEmptyProperties() { 080 Properties properties = new Properties(); 081 Properties newProperties = PropertiesUtil.removeProperties(properties, "prefix"); 082 assertNotNull(newProperties); 083 assertEquals(0, properties.size()); 084 } 085 086 @Test 087 public void testBooleanProperties() { 088 final String KEY = "key"; 089 Properties p = new Properties(); 090 p.put(KEY, "value1"); 091 assertFalse(PropertiesUtil.propertyAsBool(p, KEY, false)); 092 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, true)); 093 094 p.put(KEY, "false"); 095 assertFalse(PropertiesUtil.propertyAsBool(p, KEY, true)); 096 097 p.put(KEY, "f"); 098 assertFalse(PropertiesUtil.propertyAsBool(p, KEY, true)); 099 100 p.put(KEY, "csy"); 101 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, true)); 102 103 p.put(KEY, "no"); 104 assertFalse(PropertiesUtil.propertyAsBool(p, KEY, true)); 105 106 p.remove(KEY); 107 assertFalse(PropertiesUtil.propertyAsBool(p, KEY, false)); 108 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, true)); 109 110 p.put(KEY, "yes"); 111 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, false)); 112 113 p.put(KEY, "y"); 114 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, false)); 115 116 p.put(KEY, "Yes"); 117 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, false)); 118 119 p.put(KEY, "True"); 120 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, false)); 121 122 p.put(KEY, "t"); 123 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, false)); 124 125 p.put(KEY, "on"); 126 assertTrue(PropertiesUtil.propertyAsBool(p, KEY, false)); 127 } 128 129 @Test 130 public void testExceptionNoProperties() { 131 assertThrows(NullPointerException.class, () -> PropertiesUtil.removeProperties(null, "prefix")); 132 } 133 134 @Test 135 public void testExceptionNoPrefix() { 136 Properties properties = new Properties(); 137 properties.put("prefix.key1", "value1"); 138 assertThrows( 139 IllegalStateException.class, () -> PropertiesUtil.removeProperties(properties, null)); 140 } 141}