Skip to content

Commit f287f91

Browse files
committed
CheckDuplicationProperties and PropsUtils added
1 parent d5e056f commit f287f91

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- CheckDuplicationProperties and PropsUtils
13+
1014
## [8.5.9] - 2024-05-05
1115

1216
### Fixed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.fugerit.java.core.util;
2+
3+
import lombok.Getter;
4+
5+
import java.util.*;
6+
7+
/**
8+
* Properties collecting values which where previously set.
9+
*
10+
* Especially useful after a <code>load()</code> or <code>loadFromXml()</code> methods to check duplications.
11+
*
12+
* NOTE: this object has some limitations, especially it depends on current implementation of <code>load()</code> and <code>put()</code> methods.
13+
*
14+
*/
15+
public class CheckDuplicationProperties extends Properties {
16+
17+
private static final long serialVersionUID = 1432652578L;
18+
19+
@Getter
20+
private Collection<Map.Entry<String, String>> duplications;
21+
22+
public CheckDuplicationProperties() {
23+
this.duplications = new ArrayList<>();
24+
}
25+
26+
@Override
27+
public synchronized Object put(Object key, Object value) {
28+
if ( this.containsKey( key ) ) {
29+
duplications.add( new AbstractMap.SimpleEntry( key.toString(), this.getProperty( key.toString() ) ) );
30+
}
31+
return super.put( key, value );
32+
}
33+
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.fugerit.java.core.util;
2+
3+
import org.fugerit.java.core.io.helper.StreamHelper;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
public class PropsUtils {
8+
9+
private PropsUtils() {}
10+
11+
public static CheckDuplicationProperties findDuplicatedKeys(String path) throws IOException {
12+
try ( InputStream in = StreamHelper.resolveStream( path ) ) {
13+
return findDuplicatedKeys( in );
14+
}
15+
}
16+
17+
public static CheckDuplicationProperties findDuplicatedKeysFromXml(String path) throws IOException {
18+
try ( InputStream in = StreamHelper.resolveStream( path ) ) {
19+
return findDuplicatedKeysFromXml( in );
20+
}
21+
}
22+
23+
public static CheckDuplicationProperties findDuplicatedKeys(InputStream in) throws IOException {
24+
final CheckDuplicationProperties props = new CheckDuplicationProperties();
25+
props.load( in );
26+
return props;
27+
}
28+
29+
public static CheckDuplicationProperties findDuplicatedKeysFromXml(InputStream in) throws IOException {
30+
final CheckDuplicationProperties props = new CheckDuplicationProperties();
31+
props.loadFromXML( in );
32+
return props;
33+
}
34+
35+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
key1=one
2+
key2=two
3+
key3=three
4+
key4=four
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3+
<properties>
4+
<entry key="key1">one</entry>
5+
<entry key="key2">two</entry>
6+
<entry key="key3">three</entry>
7+
<entry key="key4">four</entry>
8+
</properties>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
key1=one
2+
key2=two
3+
key3=three
4+
key2=four
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3+
<properties>
4+
<entry key="key1">one</entry>
5+
<entry key="key2">two</entry>
6+
<entry key="key3">three</entry>
7+
<entry key="key2">four</entry>
8+
</properties>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package test.org.fugerit.java.core.util;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.fugerit.java.core.util.PropsUtils;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
import java.util.Collection;
10+
import java.util.Map;
11+
12+
@Slf4j
13+
public class TestPropsUtils {
14+
15+
@Test
16+
public void testDuplicated1File() throws IOException {
17+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeys( "file://src/main/resources/core/util/props_utils/duplicated_1.properties" ).getDuplications();
18+
log.info( "test duplicated on property file : {}", duplicated );
19+
Assert.assertEquals( 1, duplicated.size() );
20+
}
21+
22+
@Test
23+
public void testDuplicated1Col() throws IOException {
24+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeys( "cl://core/util/props_utils/duplicated_1.properties" ).getDuplications();
25+
log.info( "test duplicated on property from class loader : {}", duplicated );
26+
Assert.assertEquals( 1, duplicated.size() );
27+
}
28+
29+
@Test
30+
public void testDuplicated0File() throws IOException {
31+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeys( "file://src/main/resources/core/util/props_utils/duplicated_0.properties" ).getDuplications();
32+
log.info( "test no duplicated on property file : {}", duplicated );
33+
Assert.assertEquals( 0, duplicated.size() );
34+
}
35+
36+
@Test
37+
public void testDuplicated0Col() throws IOException {
38+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeys( "cl://core/util/props_utils/duplicated_0.properties" ).getDuplications();
39+
log.info( "test no duplicated on property from class loader : {}", duplicated );
40+
Assert.assertEquals( 0, duplicated.size() );
41+
}
42+
43+
@Test
44+
public void testDuplicated1FileXml() throws IOException {
45+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeysFromXml( "file://src/main/resources/core/util/props_utils/duplicated_1.xml" ).getDuplications();
46+
log.info( "test xml duplicated on property file : {}", duplicated );
47+
Assert.assertEquals( 1, duplicated.size() );
48+
}
49+
50+
@Test
51+
public void testDuplicated1ColXml() throws IOException {
52+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeysFromXml( "cl://core/util/props_utils/duplicated_1.xml" ).getDuplications();
53+
log.info( "test xml duplicated on property from class loader : {}", duplicated );
54+
Assert.assertEquals( 1, duplicated.size() );
55+
}
56+
57+
@Test
58+
public void testDuplicated0FileXml() throws IOException {
59+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeysFromXml( "file://src/main/resources/core/util/props_utils/duplicated_0.xml" ).getDuplications();
60+
log.info( "test xml no duplicated on property file : {}", duplicated );
61+
Assert.assertEquals( 0, duplicated.size() );
62+
}
63+
64+
@Test
65+
public void testDuplicated0ColXml() throws IOException {
66+
Collection<Map.Entry<String,String>> duplicated = PropsUtils.findDuplicatedKeysFromXml( "cl://core/util/props_utils/duplicated_0.xml" ).getDuplications();
67+
log.info( "test xml no duplicated on property from class loader : {}", duplicated );
68+
Assert.assertEquals( 0, duplicated.size() );
69+
}
70+
71+
}

0 commit comments

Comments
 (0)