@@ -0,0 +1,72 @@ | |||
| 1 | + /*- | ||
| 2 | + * #%L | ||
| 3 | + * SciJava Common shared library for SciJava software. | ||
| 4 | + * %% | ||
| 5 | + * Copyright (C) 2009 - 2024 SciJava developers. | ||
| 6 | + * %% | ||
| 7 | + * Redistribution and use in source and binary forms, with or without | ||
| 8 | + * modification, are permitted provided that the following conditions are met: | ||
| 9 | + * | ||
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, | ||
| 11 | + * this list of conditions and the following disclaimer. | ||
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| 13 | + * this list of conditions and the following disclaimer in the documentation | ||
| 14 | + * and/or other materials provided with the distribution. | ||
| 15 | + * | ||
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | + * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | + * #L% | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | + package org.scijava.util; | ||
| 31 | + | ||
| 32 | + import java.io.*; | ||
| 33 | + import java.util.HashMap; | ||
| 34 | + import java.util.Map; | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * Simple utility for reading and writing a property map to/from plain text. | ||
| 38 | + */ | ||
| 39 | + public final class PropertiesHelper { | ||
| 40 | + | ||
| 41 | + public static Map<String, String> get(File filename) { | ||
| 42 | + Map<String, String> map = new HashMap<>(); | ||
| 43 | + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { | ||
| 44 | + String line; | ||
| 45 | + while ((line = reader.readLine()) != null) { | ||
| 46 | + String[] parts = line.split("=", 2); | ||
| 47 | + if (parts.length == 2) { | ||
| 48 | + map.put(parts[0], parts[1]); | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + catch (FileNotFoundException e) { | ||
| 53 | + throw new RuntimeException(e); | ||
| 54 | + } | ||
| 55 | + catch (IOException e) { | ||
| 56 | + throw new RuntimeException(e); | ||
| 57 | + } | ||
| 58 | + return map; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + public static void put(Map<String, String> properties, File filename) { | ||
| 62 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) { | ||
| 63 | + for (Map.Entry<String, String> entry : properties.entrySet()) { | ||
| 64 | + writer.write(entry.getKey() + "=" + entry.getValue()); | ||
| 65 | + writer.newLine(); | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + catch (IOException e) { | ||
| 69 | + throw new RuntimeException(e); | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + } | ||
@@ -0,0 +1,138 @@ | |||
| 1 | + /*- | ||
| 2 | + * #%L | ||
| 3 | + * SciJava Common shared library for SciJava software. | ||
| 4 | + * %% | ||
| 5 | + * Copyright (C) 2009 - 2024 SciJava developers. | ||
| 6 | + * %% | ||
| 7 | + * Redistribution and use in source and binary forms, with or without | ||
| 8 | + * modification, are permitted provided that the following conditions are met: | ||
| 9 | + * | ||
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, | ||
| 11 | + * this list of conditions and the following disclaimer. | ||
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| 13 | + * this list of conditions and the following disclaimer in the documentation | ||
| 14 | + * and/or other materials provided with the distribution. | ||
| 15 | + * | ||
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE | ||
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | + * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | + * #L% | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | + package org.scijava.util; | ||
| 31 | + | ||
| 32 | + import org.junit.After; | ||
| 33 | + import org.junit.Before; | ||
| 34 | + import org.junit.Test; | ||
| 35 | + | ||
| 36 | + import java.io.*; | ||
| 37 | + import java.util.HashMap; | ||
| 38 | + import java.util.Map; | ||
| 39 | + | ||
| 40 | + import static org.junit.Assert.assertEquals; | ||
| 41 | + import static org.junit.Assert.assertTrue; | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Tests for {@link PropertiesHelper} | ||
| 45 | + */ | ||
| 46 | + public class PropertiesHelperTest { | ||
| 47 | + | ||
| 48 | + private static final String EXPECTED_1 = "a=b"; | ||
| 49 | + private static final String EXPECTED_2 = "hello=goodbye"; | ||
| 50 | + | ||
| 51 | + private Map<String, String> props; | ||
| 52 | + private File temp; | ||
| 53 | + | ||
| 54 | + @Before | ||
| 55 | + public void setup() throws IOException { | ||
| 56 | + temp = File.createTempFile("PropertiesHelper", "txt"); | ||
| 57 | + props = new HashMap<>(); | ||
| 58 | + props.put("a", "b"); | ||
| 59 | + props.put("hello", "goodbye"); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + @After | ||
| 63 | + public void cleanup() { | ||
| 64 | + temp.delete(); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + @Test | ||
| 68 | + public void testWrite() throws IOException { | ||
| 69 | + PropertiesHelper.put(props, temp); | ||
| 70 | + | ||
| 71 | + int count = 0; | ||
| 72 | + boolean saw1 = false, saw2 = false; | ||
| 73 | + | ||
| 74 | + try (BufferedReader reader = new BufferedReader(new FileReader(temp))) { | ||
| 75 | + String line; | ||
| 76 | + while ((line = reader.readLine()) != null) { | ||
| 77 | + if (line.equals(EXPECTED_1)) { | ||
| 78 | + saw1 = true; | ||
| 79 | + } | ||
| 80 | + if (line.equals(EXPECTED_2)) { | ||
| 81 | + saw2 = true; | ||
| 82 | + } | ||
| 83 | + count++; | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + assertTrue(saw1); | ||
| 87 | + assertTrue(saw2); | ||
| 88 | + assertEquals(2, count); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + @Test | ||
| 92 | + public void testRead() throws IOException { | ||
| 93 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(temp))) { | ||
| 94 | + writer.write(EXPECTED_1); | ||
| 95 | + writer.newLine(); | ||
| 96 | + writer.write(EXPECTED_2); | ||
| 97 | + writer.newLine(); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + Map<String, String> propsMap = PropertiesHelper.get(temp); | ||
| 101 | + assertTrue(props.equals(propsMap)); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @Test | ||
| 105 | + public void testIO() throws IOException { | ||
| 106 | + PropertiesHelper.put(props, temp); | ||
| 107 | + Map<String, String> propsMap = PropertiesHelper.get(temp); | ||
| 108 | + assertTrue(props.equals(propsMap)); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Test | ||
| 112 | + public void testMultipleEquals() throws IOException { | ||
| 113 | + props.clear(); | ||
| 114 | + final String K = "hello", V = "world=true"; | ||
| 115 | + props.put(K, V); | ||
| 116 | + PropertiesHelper.put(props, temp); | ||
| 117 | + Map<String, String> propsMap = PropertiesHelper.get(temp); | ||
| 118 | + assertEquals(1, propsMap.size()); | ||
| 119 | + assertEquals(props.get(K), propsMap.get(K)); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Test | ||
| 123 | + public void testOverwrite() throws IOException { | ||
| 124 | + PropertiesHelper.put(props, temp); | ||
| 125 | + props.put("myname", "jonas"); | ||
| 126 | + PropertiesHelper.put(props, temp); | ||
| 127 | + Map<String, String> loadedProps = PropertiesHelper.get(temp); | ||
| 128 | + assertEquals(3, loadedProps.size()); | ||
| 129 | + int count = 0; | ||
| 130 | + try (BufferedReader reader = new BufferedReader(new FileReader(temp))) { | ||
| 131 | + String line; | ||
| 132 | + while ((line = reader.readLine()) != null) { | ||
| 133 | + count++; | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + assertEquals(3, count); | ||
| 137 | + } | ||
| 138 | + } | ||
0 commit comments