BAEL-5676 Implement ini file parsing and unit tests

This commit is contained in:
Ashley Frieze 2022-08-19 09:08:39 +01:00
parent 520c7d7083
commit c987c8caf7
8 changed files with 334 additions and 0 deletions

48
libraries-files/pom.xml Normal file
View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-files</artifactId>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>${org.ini4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>${commons-configuration2}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<properties>
<org.ini4j.version>0.5.4</org.ini4j.version>
<commons-configuration2>2.8.0</commons-configuration2>
<jackson.version>2.13.1</jackson.version>
</properties>
</project>

View File

@ -0,0 +1,45 @@
package com.baeldung.ini;
import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.SubnodeConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class CommonsParser {
public static Map<String, Map<String, String>> parseIniFile(File fileToParse) throws IOException, ConfigurationException {
Map<String, Map<String, String>> iniFileContents = new HashMap<>();
INIConfiguration iniConfiguration = new INIConfiguration();
try (FileReader fileReader = new FileReader(fileToParse)) {
iniConfiguration.read(fileReader);
}
for (String section : iniConfiguration.getSections()) {
Map<String, String> subSectionMap = new HashMap<>();
SubnodeConfiguration confSection = iniConfiguration.getSection(section);
Iterator<String> keyIterator = confSection.getKeys();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
String value = confSection.getProperty(key)
.toString();
subSectionMap.put(key, value);
}
iniFileContents.put(section, subSectionMap);
}
return iniFileContents;
}
public static String readIniFileValue(File fileToParse, String section, String value) throws IOException, ConfigurationException {
INIConfiguration iniConfiguration = new INIConfiguration();
try (FileReader fileReader = new FileReader(fileToParse)) {
iniConfiguration.read(fileReader);
}
return iniConfiguration.getSection(section).getProperty(value).toString();
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.ini;
import org.ini4j.Ini;
import java.io.File;
import java.io.IOException;
import java.util.*;
import static java.util.stream.Collectors.toMap;
public class Ini4JParser {
public static Map<String, Map<String, String>> parseIniFile(File fileToParse) throws IOException {
Ini ini = new Ini(fileToParse);
return ini.entrySet().stream()
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public static String readIniFileValue(File fileToParse, String section, String key) throws IOException {
Ini ini = new Ini(fileToParse);
return ini.get(section, key);
}
}

View File

@ -0,0 +1,109 @@
package com.baeldung.ini;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
public class MyConfiguration {
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
public static class Fonts {
private String letter;
private int textSize;
public String getLetter() {
return letter;
}
public void setLetter(String letter) {
this.letter = letter;
}
public int getTextSize() {
return textSize;
}
public void setTextSize(int textSize) {
this.textSize = textSize;
}
}
public static class Background {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public static class RequestResult {
private int requestCode;
public int getRequestCode() {
return requestCode;
}
public void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}
}
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public static class ResponseResult {
private int resultCode;
public int getResultCode() {
return resultCode;
}
public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}
}
private Fonts fonts;
private Background background;
@JsonProperty("RequestResult")
private RequestResult requestResult;
@JsonProperty("ResponseResult")
private ResponseResult responseResult;
public Fonts getFonts() {
return fonts;
}
public void setFonts(Fonts fonts) {
this.fonts = fonts;
}
public Background getBackground() {
return background;
}
public void setBackground(Background background) {
this.background = background;
}
public RequestResult getRequestResult() {
return requestResult;
}
public void setRequestResult(RequestResult requestResult) {
this.requestResult = requestResult;
}
public ResponseResult getResponseResult() {
return responseResult;
}
public void setResponseResult(ResponseResult responseResult) {
this.responseResult = responseResult;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.ini;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.nio.file.Paths;
import java.util.Map;
import static com.baeldung.ini.CommonsParser.parseIniFile;
import static com.baeldung.ini.CommonsParser.readIniFileValue;
import static org.assertj.core.api.Assertions.assertThat;
class CommonsParserUnitTest {
private static final File TEST_FILE = Paths.get("src", "test", "resources", "sample.ini").toFile();
@Test
void givenIniFileThenCanParseWithIni4j() throws Exception {
Map<String, Map<String, String>> result =
parseIniFile(TEST_FILE);
assertThat(result.get("fonts"))
.containsEntry("letter", "bold")
.containsEntry("text-size", "28");
}
@Test
void givenIniFileThenCanReadKeyWithIni4j() throws Exception {
assertThat(readIniFileValue(TEST_FILE, "fonts", "letter"))
.isEqualTo("bold");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.ini;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.nio.file.Paths;
import java.util.Map;
import static com.baeldung.ini.Ini4JParser.readIniFileValue;
import static com.baeldung.ini.Ini4JParser.parseIniFile;
import static org.assertj.core.api.Assertions.assertThat;
class Ini4JParserUnitTest {
private static final File TEST_FILE = Paths.get("src", "test", "resources", "sample.ini").toFile();
@Test
void givenIniFileThenCanParseWithIni4j() throws Exception {
Map<String, Map<String, String>> result =
parseIniFile(TEST_FILE);
assertThat(result.get("fonts"))
.containsEntry("letter", "bold")
.containsEntry("text-size", "28");
}
@Test
void givenIniFileThenCanReadKeyFromIt() throws Exception {
assertThat(readIniFileValue(TEST_FILE, "fonts", "letter"))
.isEqualTo("bold");
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.ini;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.nio.file.Paths;
import java.util.Map;
import static com.baeldung.ini.Ini4JParser.parseIniFile;
import static org.assertj.core.api.Assertions.assertThat;
class SerializeIntoPojoUnitTest {
private static final File TEST_FILE = Paths.get("src", "test", "resources", "sample.ini").toFile();
@Test
void givenAnIniFileThenCanLoadAsPojo() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Map<String, String>> iniKeys = parseIniFile(TEST_FILE);
MyConfiguration config = objectMapper.convertValue(iniKeys, MyConfiguration.class);
assertThat(config.getFonts().getLetter()).isEqualTo("bold");
assertThat(config.getFonts().getTextSize()).isEqualTo(28);
assertThat(config.getBackground().getColor()).isEqualTo("white");
assertThat(config.getRequestResult().getRequestCode()).isEqualTo(1);
assertThat(config.getResponseResult().getResultCode()).isZero();
}
}

View File

@ -0,0 +1,14 @@
; for 16-bit app support
[fonts]
letter=bold
text-size=28
[background]
color=white
[RequestResult]
RequestCode=1
[ResponseResult]
ResultCode=0