diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..e61fafd
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,22 @@
+name: build
+
+on:
+ push:
+ branches: [ "master", "main" ]
+ pull_request:
+ branches: [ "master", "main" ]
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v3
+ - name: Set up JDK 11
+ uses: actions/setup-java@v3
+ with:
+ java-version: '11'
+ distribution: 'adopt'
+ cache: maven
+ - name: Build with Maven
+ run: mvn clean package jacoco:report
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 15f68f9..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,10 +0,0 @@
-language: java
-jdk:
- - openjdk11
-
-script: mvn clean test
-
-sudo: false
-after_success:
- - mvn clean compile test jacoco:report coveralls:report package
-# - mvn install
diff --git a/README.md b/README.md
index 91922ed..1cccf00 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# 🗝️ dotenv-java
-![](https://travis-ci.org/cdimascio/dotenv-java.svg?branch=master) [![Coverage Status](https://coveralls.io/repos/github/cdimascio/dotenv-java/badge.svg?branch=fixes)](https://coveralls.io/github/cdimascio/dotenv-java?branch=fixes) [![Maven Central](https://img.shields.io/maven-central/v/io.github.cdimascio/dotenv-java.svg?label=Maven%20Central)](https://search.maven.org/artifact/io.github.cdimascio/dotenv-java) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/66b8195f0da544f1ad9ed1352c0ea66f)](https://app.codacy.com/app/cdimascio/dotenv-java?utm_source=github.com&utm_medium=referral&utm_content=cdimascio/dotenv-java&utm_campaign=Badge_Grade_Dashboard) [![](https://img.shields.io/gitter/room/cdimascio-oss/community?color=%23eb205a)](https://gitter.im/cdimascio-oss/community) [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) [![](https://img.shields.io/badge/doc-javadoc-blue)](https://cdimascio.github.io/dotenv-java/docs/javadoc/index.html) ![](https://img.shields.io/badge/license-Apache%202.0-blue.svg)
+![Build Status](/~https://github.com/cloudsimplus/cloudsimplus/actions/workflows/build.yml/badge.svg) [![Maven Central](https://img.shields.io/maven-central/v/io.github.cdimascio/dotenv-java.svg?label=Maven%20Central)](https://search.maven.org/artifact/io.github.cdimascio/dotenv-java) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/66b8195f0da544f1ad9ed1352c0ea66f)](https://app.codacy.com/app/cdimascio/dotenv-java?utm_source=github.com&utm_medium=referral&utm_content=cdimascio/dotenv-java&utm_campaign=Badge_Grade_Dashboard) [![](https://img.shields.io/gitter/room/cdimascio-oss/community?color=%23eb205a)](https://gitter.im/cdimascio-oss/community) [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) [![](https://img.shields.io/badge/doc-javadoc-blue)](https://cdimascio.github.io/dotenv-java/docs/javadoc/index.html) ![](https://img.shields.io/badge/license-Apache%202.0-blue.svg)
A no-dependency, pure Java port of the Ruby dotenv project. Load environment variables from a `.env` file.
diff --git a/pom.xml b/pom.xml
index b491a92..3ba764a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
io.github.cdimascio
dotenv-java
- 2.3.2
+ 2.4.0
@@ -55,23 +55,20 @@
io.github.cdimascio.dotenv.java
UTF-8
- 1.8
- 11
- 8
+ 11
docs/javadoc
- 4.13.2
+ 5.9.2
- 3.1.0
- 3.5.1
- 3.0.1
- 3.2.0
- 2.22.0
- 0.8.6
- 4.3.0
+ 3.3.0
+ 3.11.0
+ 3.2.1
+ 3.5.0
+ 3.0.0
+ 0.8.10
1.0.1
- 1.0.0.RC2
+ 1.0.0.RC3
cdimascio
maven
@@ -88,8 +85,14 @@
- junit
- junit
+ org.junit.jupiter
+ junit-jupiter-api
+ ${junit.version}
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
${junit.version}
test
@@ -127,17 +130,10 @@
-
org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.plugin}
-
-
- ${compile.source}
- ${compile.test.source}
- ${compile.test.source}
-
@@ -194,13 +190,6 @@
org.apache.maven.plugins
maven-surefire-plugin
${maven.surefire.plugin}
-
-
- org.apache.maven.surefire
- surefire-junit4
- 2.22.0
-
-
**/*.java
@@ -257,23 +246,6 @@
-
-
- org.eluder.coveralls
- coveralls-maven-plugin
- ${maven.coveralls.plugin}
-
- r92OLg0S3kCJakLAMW6HlLsDgNSMaolGS
-
-
-
- javax.xml.bind
- jaxb-api
- 2.2.3
-
-
-
-
com.coderplus.maven.plugins
@@ -290,7 +262,7 @@
${project.basedir}/pom.xml
- ${project.build.directory}/dotenv-java-2.2.4.pom
+ ${project.build.directory}/${project.artifactId}-${project.version}.pom
@@ -315,7 +287,7 @@
org.sonatype.plugins
nexus-staging-maven-plugin
- 1.6.7
+ 1.6.13
true
ossrh
@@ -355,7 +327,7 @@
org.apache.maven.plugins
maven-gpg-plugin
- 1.5
+ 3.0.1
sign-artifacts
diff --git a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
index 6d6a60b..237370b 100644
--- a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
+++ b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
@@ -23,7 +23,7 @@ public class DotenvBuilder {
* @param path the directory containing the .env file
* @return this {@link DotenvBuilder}
*/
- public DotenvBuilder directory(String path) {
+ public DotenvBuilder directory(final String path) {
this.directoryPath = path;
return this;
}
@@ -32,7 +32,7 @@ public DotenvBuilder directory(String path) {
* @param name the filename
* @return this {@link DotenvBuilder}
*/
- public DotenvBuilder filename(String name) {
+ public DotenvBuilder filename(final String name) {
filename = name;
return this;
}
@@ -70,14 +70,14 @@ public DotenvBuilder systemProperties() {
* @throws DotenvException when an error occurs
*/
public Dotenv load() throws DotenvException {
- DotenvParser reader = new DotenvParser(
- new DotenvReader(directoryPath, filename),
- throwIfMissing,
- throwIfMalformed);
- List env = reader.parse();
+ final var reader = new DotenvParser(
+ new DotenvReader(directoryPath, filename),
+ throwIfMissing, throwIfMalformed);
+ final List env = reader.parse();
if (systemProperties) {
env.forEach(it -> System.setProperty(it.getKey(), it.getValue()));
}
+
return new DotenvImpl(env);
}
@@ -85,19 +85,25 @@ static class DotenvImpl implements Dotenv {
private final Map envVars;
private final Set set;
private final Set setInFile;
- public DotenvImpl(List envVars) {
- Map envVarsInFile = envVars.stream()
- .collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
+ public DotenvImpl(final List envVars) {
+ final Map envVarsInFile =
+ envVars.stream()
+ .collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
+
this.envVars = new HashMap<>(envVarsInFile);
this.envVars.putAll(System.getenv());
- this.set = this.envVars.entrySet().stream()
- .map(it -> new DotenvEntry(it.getKey(), it.getValue()))
- .collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
-
- this.setInFile = envVarsInFile.entrySet().stream()
- .map(it -> new DotenvEntry(it.getKey(), it.getValue()))
- .collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
+ this.set =
+ this.envVars.entrySet()
+ .stream()
+ .map(it -> new DotenvEntry(it.getKey(), it.getValue()))
+ .collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
+
+ this.setInFile =
+ envVarsInFile.entrySet()
+ .stream()
+ .map(it -> new DotenvEntry(it.getKey(), it.getValue()))
+ .collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
}
@Override
@@ -106,21 +112,21 @@ public Set entries() {
}
@Override
- public Set entries(Dotenv.Filter filter) {
- if (filter != null) return setInFile;
- return entries();
+ public Set entries(final Dotenv.Filter filter) {
+ return filter == null ? entries() : setInFile;
+
}
@Override
- public String get(String key) {
- String value = System.getenv(key);
- return value != null ? value : envVars.get(key);
+ public String get(final String key) {
+ final String value = System.getenv(key);
+ return value == null ? envVars.get(key) : value;
}
@Override
public String get(String key, String defaultValue) {
- String value = this.get(key);
- return value != null ? value : defaultValue;
+ final String value = this.get(key);
+ return value == null ? defaultValue : value;
}
}
}
diff --git a/src/main/java/io/github/cdimascio/dotenv/internal/ClasspathHelper.java b/src/main/java/io/github/cdimascio/dotenv/internal/ClasspathHelper.java
index a7fc8f8..94082b2 100644
--- a/src/main/java/io/github/cdimascio/dotenv/internal/ClasspathHelper.java
+++ b/src/main/java/io/github/cdimascio/dotenv/internal/ClasspathHelper.java
@@ -2,9 +2,8 @@
import io.github.cdimascio.dotenv.DotenvException;
-import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
-import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
@@ -13,8 +12,8 @@
*/
public class ClasspathHelper {
static Stream loadFileFromClasspath(String location) {
- Class loader = ClasspathHelper.class;
- InputStream inputStream = loader.getResourceAsStream(location);
+ final var loader = ClasspathHelper.class;
+ var inputStream = loader.getResourceAsStream(location);
if (inputStream == null) {
inputStream = loader.getResourceAsStream(location);
}
@@ -25,11 +24,13 @@ static Stream loadFileFromClasspath(String location) {
if (inputStream == null) {
throw new DotenvException("Could not find "+location+" on the classpath");
}
- Scanner scanner = new Scanner(inputStream, "utf-8");
- List lines = new ArrayList<>();
+
+ final var scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
+ final var lines = new ArrayList();
while (scanner.hasNext()) {
lines.add(scanner.nextLine());
}
+
return lines.stream();
}
}
diff --git a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java
index 97b8d00..38ad017 100644
--- a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java
+++ b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java
@@ -8,7 +8,6 @@
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Collections.emptyList;
@@ -46,7 +45,7 @@ public class DotenvParser {
* @param throwIfMissing if true, throws when the .env file is missing
* @param throwIfMalformed if true, throws when the .env file is malformed
*/
- public DotenvParser(DotenvReader reader, boolean throwIfMissing, boolean throwIfMalformed) {
+ public DotenvParser(final DotenvReader reader, final boolean throwIfMissing, final boolean throwIfMalformed) {
this.reader = reader;
this.throwIfMissing = throwIfMissing;
this.throwIfMalformed = throwIfMalformed;
@@ -58,50 +57,57 @@ public DotenvParser(DotenvReader reader, boolean throwIfMissing, boolean throwIf
* @throws DotenvException if an error is encountered during the parse
*/
public List parse() throws DotenvException {
- List entries = new ArrayList<>();
- for (String line : lines()) {
- String l = line.trim();
- if (isWhiteSpace.test(l) || isComment.test(l) || isBlank(l)) continue;
-
- DotenvEntry entry = parseLine.apply(l);
- if (entry == null) {
- if (throwIfMalformed) throw new DotenvException("Malformed entry " + l);
- continue;
- }
- String key = entry.getKey();
- String value = normalizeValue(entry.getValue());
- entries.add(new DotenvEntry(key, value));
+ final var lines = lines();
+ final var entries = new ArrayList(lines.size());
+ for (final var line : lines) {
+ addNewEntry(entries, line.trim());
}
+
return entries;
}
+ private void addNewEntry(final List entries, final String line) {
+ if (isWhiteSpace.test(line) || isComment.test(line) || isBlank(line))
+ return;
+
+ final var entry = parseLine.apply(line);
+ if (entry == null) {
+ if (throwIfMalformed)
+ throw new DotenvException("Malformed entry " + line);
+ return;
+ }
+
+ final var key = entry.getKey();
+ final var value = normalizeValue(entry.getValue());
+ entries.add(new DotenvEntry(key, value));
+ }
+
private List lines() throws DotenvException {
try {
return reader.read();
} catch (DotenvException e) {
- if (throwIfMissing) throw e;
+ if (throwIfMissing)
+ throw e;
return emptyList();
} catch (IOException e) {
throw new DotenvException(e);
}
}
- private String normalizeValue(String value) {
- String tr = value.trim();
- return isQuoted.test(tr)
- ? tr.substring(1, value.length() - 1)
- : tr;
+ private String normalizeValue(final String value) {
+ final var tr = value.trim();
+ return isQuoted.test(tr) ? tr.substring(1, value.length() - 1) : tr;
}
- private static boolean matches(Pattern regex, String text) {
- Matcher matcher = regex.matcher(text);
- return matcher.matches();
+ private static boolean matches(final Pattern regex, final String text) {
+ return regex.matcher(text).matches();
}
- private static DotenvEntry matchEntry(Pattern regex, String text) {
- Matcher matcher = regex.matcher(text);
- boolean result = matcher.matches();
- if (!result || matcher.groupCount() < 3) return null;
+ private static DotenvEntry matchEntry(final Pattern regex, final String text) {
+ final var matcher = regex.matcher(text);
+ if (!matcher.matches() || matcher.groupCount() < 3)
+ return null;
+
return new DotenvEntry(matcher.group(1), matcher.group(3));
}
diff --git a/src/test/java/tests/BasicTests.java b/src/test/java/tests/BasicTests.java
index 502e6ed..d0284ed 100644
--- a/src/test/java/tests/BasicTests.java
+++ b/src/test/java/tests/BasicTests.java
@@ -1,16 +1,16 @@
package tests;
-import io.github.cdimascio.dotenv.DotenvException;
import io.github.cdimascio.dotenv.Dotenv;
-import org.junit.Test;
+import io.github.cdimascio.dotenv.DotenvException;
+import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
-public class BasicTests {
- private Map envVars = new HashMap<>() {{
+class BasicTests {
+ private final Map envVars = new HashMap<>() {{
put("MY_TEST_EV1", "my test ev 1");
put("MY_TEST_EV2", "my test ev 2");
put("WITHOUT_VALUE", "");
@@ -19,98 +19,88 @@ public class BasicTests {
put("QUOTED_VALUE", "iH4>hb_d0#_GN8d]6");
}};
- @Test(expected = DotenvException.class)
- public void dotenvMalformed() {
- Dotenv.configure()
- .directory("./src/test/resources")
- .load();
+ @Test
+ void dotenvMalformed() {
+ assertThrows(DotenvException.class, () -> Dotenv.configure().directory("./src/test/resources").load());
}
@Test
- public void dotenvIgnoreMalformed() {
- var dotenv = Dotenv.configure()
+ void dotenvIgnoreMalformed() {
+ final var dotenv = Dotenv.configure()
.directory("./src/test/resources")
.ignoreIfMalformed()
.load();
- envVars.forEach((key, expected) -> {
- var actual = dotenv.get(key);
- assertEquals(expected, actual);
- });
-
+ envVars.forEach((key, expected) -> assertEquals(expected, dotenv.get(key)));
assertHostEnvVar(dotenv);
}
@Test
- public void dotenvFilename() {
- var dotenv = Dotenv.configure()
+ void dotenvFilename() {
+ final var dotenv = Dotenv.configure()
.directory("./src/test/resources")
.filename("env")
.ignoreIfMalformed()
.load();
- envVars.forEach((key, expected) -> {
- var actual = dotenv.get(key);
- assertEquals(expected, actual);
- });
-
+ envVars.forEach((key, expected) -> assertEquals(expected, dotenv.get(key)));
assertHostEnvVar(dotenv);
}
@Test
- public void resourceRelative() {
- var dotenv = Dotenv.configure()
+ void resourceRelative() {
+ final var dotenv = Dotenv.configure()
.directory("./")
.ignoreIfMalformed()
.load();
- assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
+ assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertHostEnvVar(dotenv);
}
@Test
- public void resourceAbsoluteDir() {
+ void resourceAbsoluteDir() {
assertDirectory("/envdir","Simple Subdirectory");
}
@Test
- public void resourceRelativeDir() {
+ void resourceRelativeDir() {
assertDirectory("./envdir", "Simple Subdirectory");
}
@Test
- public void resourceUnanchoredDir() {
+ void resourceUnanchoredDir() {
assertDirectory("envdir", "Simple Subdirectory");
}
@Test
- public void resourceAbsoluteTrailingDotDir() {
+ void resourceAbsoluteTrailingDotDir() {
assertDirectory("/trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}
@Test
- public void resourceRelativeTrailingDotDir() {
+ void resourceRelativeTrailingDotDir() {
assertDirectory("./trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}
@Test
- public void resourceUnanchoredTrailingDotDir() {
+ void resourceUnanchoredTrailingDotDir() {
assertDirectory("trailingdot./envdir", "Trailing Dot Directory With Subdirectory");
}
@Test
- public void resourceCurrent() {
- var dotenv = Dotenv.configure()
+ void resourceCurrent() {
+ final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
- assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
+ assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertHostEnvVar(dotenv);
}
@Test
- public void systemProperties() {
- var dotenv = Dotenv.configure()
+ void systemProperties() {
+ final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.systemProperties()
.load();
@@ -122,8 +112,8 @@ public void systemProperties() {
}
@Test
- public void noSystemProperties() {
- var dotenv = Dotenv.configure()
+ void noSystemProperties() {
+ final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
@@ -133,50 +123,47 @@ public void noSystemProperties() {
}
@Test
- public void iterateOverDotenv() {
- var dotenv = Dotenv.configure()
+ void iterateOverDotenv() {
+ final var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
- for (var e : dotenv.entries()) {
+ for (final var e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
- @Test(expected = DotenvException.class)
- public void dotenvMissing() {
- Dotenv.configure()
- .directory("/missing/.env")
- .load();
+ @Test
+ void dotenvMissing() {
+ assertThrows(DotenvException.class, () -> Dotenv.configure().directory("/missing/.env").load());
}
@Test
- public void dotenvIgnoreMissing() {
- var dotenv = Dotenv.configure()
+ void dotenvIgnoreMissing() {
+ final var dotenv = Dotenv.configure()
.directory("/missing/.env")
.ignoreIfMissing()
.load();
assertHostEnvVar(dotenv);
-
assertNull(dotenv.get("MY_TEST_EV1"));
}
- private void assertDirectory(String directory, String expected) {
- var dotenv = Dotenv.configure()
+ private void assertDirectory(final String directory, final String expected) {
+ final var dotenv = Dotenv.configure()
.directory(directory)
.load();
+
assertEquals(expected, dotenv.get("MY_TEST_EV1"));
}
- private void assertHostEnvVar(Dotenv env) {
- var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
- if (isWindows) {
- var path = env.get("PATH");
- assertNotNull(path);
- } else {
- var expectedHome = System.getProperty("user.home");
- var actualHome = env.get("HOME");
+ private void assertHostEnvVar(final Dotenv env) {
+ final var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
+ if (isWindows)
+ assertNotNull(env.get("PATH"));
+ else {
+ final var expectedHome = System.getProperty("user.home");
+ final var actualHome = env.get("HOME");
assertEquals(expectedHome, actualHome);
}
}
diff --git a/src/test/java/tests/DotenvTests.java b/src/test/java/tests/DotenvTests.java
index 28988bd..b6f6314 100644
--- a/src/test/java/tests/DotenvTests.java
+++ b/src/test/java/tests/DotenvTests.java
@@ -1,112 +1,99 @@
package tests;
-import io.github.cdimascio.dotenv.DotenvException;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;
-import org.junit.Before;
-import org.junit.Test;
+import io.github.cdimascio.dotenv.DotenvException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Map;
-import java.util.Set;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.*;
-public class DotenvTests {
+class DotenvTests {
private Map envVars;
- @Before
- public void setUp() {
+ @BeforeEach
+ void setUp() {
envVars = new HashMap<>();
envVars.put("MY_TEST_EV1", "my test ev 1");
envVars.put("MY_TEST_EV2", "my test ev 2");
envVars.put("WITHOUT_VALUE", "");
}
- @Test(expected = DotenvException.class)
- public void throwIfMalconfigured() {
- Dotenv.configure().load();
+ @Test
+ void throwIfMalconfigured() {
+ assertThrows(DotenvException.class, () -> Dotenv.configure().load());
}
- @Test(expected = DotenvException.class)
- public void load() {
- Dotenv dotenv = Dotenv.load();
-
- for (String envName : envVars.keySet()) {
- assertEquals(envVars.get(envName), dotenv.get(envName));
- }
+ @Test
+ void load() {
+ assertThrows(DotenvException.class, () -> {
+ final var dotenv = Dotenv.load();
+ envVars.keySet().forEach(envName -> assertEquals(envVars.get(envName), dotenv.get(envName)));
+ });
}
@Test
- public void iteratorOverDotenv() {
- Dotenv dotenv = Dotenv.configure()
- .ignoreIfMalformed()
- .load();
+ void iteratorOverDotenv() {
+ final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
dotenv
.entries()
.forEach(e -> assertEquals(dotenv.get(e.getKey()), e.getValue()));
- for (DotenvEntry e : dotenv.entries()) {
+ for (final var e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
@Test
- public void iteratorOverDotenvWithFilter() {
- Dotenv dotenv = Dotenv.configure()
- .ignoreIfMalformed()
- .load();
+ void iteratorOverDotenvWithFilter() {
+ final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
- Set entriesInFile = dotenv.entries(Dotenv.Filter.DECLARED_IN_ENV_FILE);
- Set entriesAll = dotenv.entries();
+ final var entriesInFile = dotenv.entries(Dotenv.Filter.DECLARED_IN_ENV_FILE);
+ final var entriesAll = dotenv.entries();
assertTrue(entriesInFile.size() < entriesAll.size());
- for (Map.Entry e: envVars.entrySet()) {
+ for (final var e: envVars.entrySet()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
- @Test(expected = UnsupportedOperationException.class)
- public void failToRemoveFromDotenv() {
- Dotenv dotenv = Dotenv.configure()
- .ignoreIfMalformed()
- .load();
+ @Test
+ void failToRemoveFromDotenv() {
+ final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
+ assertThrows(UnsupportedOperationException.class, () -> iterateEntries(dotenv));
+ }
+
+ private static void iterateEntries(final Dotenv dotenv) {
+ final var iter = dotenv.entries().iterator();
- Iterator iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
}
- @Test(expected = UnsupportedOperationException.class)
- public void failToAddToDotenv() {
-
- Dotenv dotenv = Dotenv.configure()
- .ignoreIfMalformed()
- .load();
-
- dotenv.entries().add(new DotenvEntry("new", "value"));
+ @Test
+ void failToAddToDotenv() {
+ final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
+ final var entries = dotenv.entries();
+ assertThrows(UnsupportedOperationException.class, () -> entries.add(new DotenvEntry("new", "value")));
}
@Test
- public void configureWithIgnoreMalformed() {
- Dotenv dotenv = Dotenv.configure()
- .ignoreIfMalformed()
- .load();
-
- for (String envName : envVars.keySet()) {
+ void configureWithIgnoreMalformed() {
+ final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
+ for (final var envName : envVars.keySet()) {
assertEquals(envVars.get(envName), dotenv.get(envName));
}
}
@Test
- public void configureWithIgnoreMissingAndMalformed() {
- Dotenv dotenv = Dotenv.configure()
+ void configureWithIgnoreMissingAndMalformed() {
+ final var dotenv = Dotenv.configure()
.directory("/missing/dir")
.ignoreIfMalformed()
.ignoreIfMissing()