1 package com.github.mikkoi.maven.plugins.enforcer.rule.propertyusage;
2
3 import org.apache.maven.plugin.logging.SystemStreamLog;
4 import org.junit.Test;
5
6 import java.io.FileNotFoundException;
7 import java.nio.charset.Charset;
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import static org.junit.Assert.assertEquals;
15
16 public class PropertyFilesTest {
17
18 private SystemStreamLog slog = new SystemStreamLog();
19
20 @Test
21 public void readPropertiesFromFiles_Normal() throws Exception {
22 Map<String, Integer> expected = new HashMap<>();
23 expected.put("my.property.value", 1);
24 expected.put("other.prop.val", 1);
25 expected.put("my-too.property.value", 1);
26 expected.put("other-too.prop.val", 1);
27 expected.put("also-prop.val", 1);
28 expected.put("my-first.property.value", 1);
29 expected.put("my-second.prop.val", 1);
30 expected.put("my-third-val", 1);
31 Collection<String> filenames = Arrays.asList(
32 "src/test/resources/app1.properties",
33 "src/test/resources/app2.properties",
34 "src/test/resources/app3.properties"
35 );
36 PropertyFiles propertyFiles = new PropertyFiles(slog, Charset.forName("UTF-8"));
37 Map<String, Integer> properties = propertyFiles.readPropertiesFromFilesWithoutCount(filenames);
38 assertEquals("Read properties are as expected.", expected, properties);
39 }
40
41 @Test(expected = FileNotFoundException.class)
42 public void readPropertiesFromFiles_ExceptionBecauseFileNotExists() throws Exception {
43 Collection<String> filenames = Collections.singletonList(
44 "src/test/resources/not-exists.properties"
45 );
46 PropertyFiles propertyFiles = new PropertyFiles(slog, Charset.forName("UTF-8"));
47 propertyFiles.readPropertiesFromFilesWithoutCount(filenames);
48 }
49
50 @Test
51 public void readPropertiesFromFiles_EmptyFilesAreOK() throws Exception {
52 Map<String, Integer> expected = new HashMap<>();
53 Collection<String> filenames = Collections.singletonList(
54 "src/test/resources/empty.properties"
55 );
56 PropertyFiles propertyFiles = new PropertyFiles(slog, Charset.forName("UTF-8"));
57 Map<String, Integer> properties = propertyFiles.readPropertiesFromFilesWithoutCount(filenames);
58 assertEquals("Read properties are as expected (empty)", expected, properties);
59 }
60
61 @Test
62 public void readPropertiesFromTestPropertyFile() throws Exception {
63 Map<String, Integer> expected = new HashMap<>();
64 expected.put("first.property.value", 2);
65 expected.put("second.property.value", 1);
66 expected.put("third.property.value", 4);
67 expected.put("fourth.property.value", 1);
68 Collection<String> filenames = Collections.singleton(
69 "src/test/resources/app1-double-def.properties"
70 );
71 PropertyFiles propertyFiles = new PropertyFiles(slog, Charset.forName("UTF-8"));
72 Map<String, Integer> properties = propertyFiles.readPropertiesFromFilesWithCount(filenames);
73 assertEquals("Read properties are as expected.", expected, properties);
74 }
75
76 }