/** * Parses the specified source as a properties file. * <p> * This parses the specified character source expecting a properties file format. * The resulting instance can be queried for each key and value. * <p> * Properties files sometimes contain a Unicode Byte Order Mark. * Callers are responsible for handling this, such as by using {@link UnicodeBom}. * * @param source the properties file resource * @return the properties file * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed */ public static PropertiesFile of(CharSource source) { ArgChecker.notNull(source, "source"); ImmutableList<String> lines = Unchecked.wrap(() -> source.readLines()); PropertySet keyValues = parse(lines); return new PropertiesFile(keyValues); }
public void test_equalsHashCode() { PropertiesFile a1 = PropertiesFile.of(CharSource.wrap(FILE1)); PropertiesFile a2 = PropertiesFile.of(CharSource.wrap(FILE1)); PropertiesFile b = PropertiesFile.of(CharSource.wrap(FILE2)); assertEquals(a1.equals(a1), true); assertEquals(a1.equals(a2), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(null), false); assertEquals(a1.equals(ANOTHER_TYPE), false); assertEquals(a1.hashCode(), a2.hashCode()); }
public void test_of_noLists() { PropertiesFile test = PropertiesFile.of(CharSource.wrap(FILE1)); Multimap<String, String> keyValues = ImmutableListMultimap.of("a", "x", "c", "z", "b", "y"); assertEquals(test.getProperties(), PropertySet.of(keyValues)); assertEquals(test.toString(), "{a=[x], c=[z], b=[y]}"); }
public void test_of_escaping() { PropertiesFile test = PropertiesFile.of(CharSource.wrap(FILE3)); Multimap<String, String> keyValues1 = ImmutableListMultimap.of("a=d=", "x"); assertEquals(test.getProperties(), PropertySet.of(keyValues1)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void test_of_invalid_emptyKey() { String invalid = "= y\n"; PropertiesFile.of(CharSource.wrap(invalid)); }
/** * Obtains an instance from a key-value property set. * * @param keyValueMap the key-value property set * @return the properties file */ public static PropertiesFile of(PropertySet keyValueMap) { ArgChecker.notNull(keyValueMap, "keyValueMap"); return new PropertiesFile(keyValueMap); }
public void test_of_list() { PropertiesFile test = PropertiesFile.of(CharSource.wrap(FILE2)); Multimap<String, String> keyValues = ImmutableListMultimap.of("a", "x", "a", "y"); assertEquals(test.getProperties(), PropertySet.of(keyValues)); assertEquals(test.toString(), "{a=[x, y]}"); }
public void test_of_ioException() { assertThrows( () -> PropertiesFile.of(Files.asCharSource(new File("src/test/resources"), StandardCharsets.UTF_8)), UncheckedIOException.class); }
public void test_of_propertyNoEquals() { PropertiesFile test = PropertiesFile.of(CharSource.wrap("b\n")); Multimap<String, String> keyValues = ImmutableListMultimap.of("b", ""); assertEquals(test.getProperties(), PropertySet.of(keyValues)); assertEquals(test.toString(), "{b=[]}"); }
public void test_of_set() { Multimap<String, String> keyValues = ImmutableListMultimap.of("a", "x", "b", "y"); PropertiesFile test = PropertiesFile.of(PropertySet.of(keyValues)); assertEquals(test.getProperties(), PropertySet.of(keyValues)); assertEquals(test.toString(), "{a=[x], b=[y]}"); }