From 8f567473ba4ab68c253125b5b81f5fd9f10bcfb5 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 17:14:37 -0300 Subject: [PATCH 01/11] Define automatic pom.xml name for the copy-rename-maven-plugin, using the project artifactId and version tags. Signed-off-by: Manoel Campos --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b491a92..5f412e8 100644 --- a/pom.xml +++ b/pom.xml @@ -290,7 +290,7 @@ ${project.basedir}/pom.xml - ${project.build.directory}/dotenv-java-2.2.4.pom + ${project.build.directory}/${project.artifactId}-${project.version}.pom From 25c2353896a26d25212b90cc37748e635143af01 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 17:50:19 -0300 Subject: [PATCH 02/11] Upgrade to JDK 11 - Using Java 11 everywhere (instead of 1.8 for sources and 11 for tests) - Update dependency versions Signed-off-by: Manoel Campos --- pom.xml | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 5f412e8..1be3a33 100644 --- a/pom.xml +++ b/pom.xml @@ -55,23 +55,21 @@ io.github.cdimascio.dotenv.java UTF-8 - 1.8 - 11 - 8 + 11 docs/javadoc 4.13.2 - 3.1.0 - 3.5.1 - 3.0.1 - 3.2.0 - 2.22.0 - 0.8.6 + 3.3.0 + 3.11.0 + 3.2.1 + 3.5.0 + 3.0.0 + 0.8.10 4.3.0 1.0.1 - 1.0.0.RC2 + 1.0.0.RC3 cdimascio maven @@ -127,17 +125,10 @@ - org.apache.maven.plugins maven-compiler-plugin ${maven.compiler.plugin} - - ${compile.source} - ${compile.source} - ${compile.test.source} - ${compile.test.source} - @@ -315,7 +306,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.7 + 1.6.13 true ossrh @@ -355,7 +346,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.5 + 3.0.1 sign-artifacts From 13e267a6a8ef2e5d3316f09bf472ef45de6f2904 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 17:54:51 -0300 Subject: [PATCH 03/11] Remove maven.coveralls.plugin Since the project is already using https://codacy.com, they already provide coverage service which doesn't require a maven plugin. Uploading coverage reports can be done inside a GitHub Action workflow. This way, a single service can be used for code quality and coverage. The coveralls plugin is abandoned, has old dependencies and doesn't support new JDK versions. Signed-off-by: Manoel Campos --- pom.xml | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/pom.xml b/pom.xml index 1be3a33..9f9d763 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,6 @@ 3.5.0 3.0.0 0.8.10 - 4.3.0 1.0.1 1.0.0.RC3 @@ -248,23 +247,6 @@ - - - org.eluder.coveralls - coveralls-maven-plugin - ${maven.coveralls.plugin} - - r92OLg0S3kCJakLAMW6HlLsDgNSMaolGS - - - - javax.xml.bind - jaxb-api - 2.2.3 - - - - com.coderplus.maven.plugins From 53e42dad649ee9f6665a80174ac674d90238b38e Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 17:55:50 -0300 Subject: [PATCH 04/11] Update pom to use JUnit 5 Signed-off-by: Manoel Campos --- pom.xml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 9f9d763..5108d23 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ docs/javadoc - 4.13.2 + 5.9.2 3.3.0 3.11.0 @@ -85,8 +85,14 @@ - junit - junit + org.junit.jupiter + junit-jupiter-api + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-engine ${junit.version} test @@ -184,13 +190,6 @@ org.apache.maven.plugins maven-surefire-plugin ${maven.surefire.plugin} - - - org.apache.maven.surefire - surefire-junit4 - 2.22.0 - - **/*.java From d2d97f108d5c305f4a3e08912396275f78c3fb99 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 17:57:14 -0300 Subject: [PATCH 05/11] Migrate tests from JUnit 4 to JUnit 5 - Refactor tests - Organize test code Signed-off-by: Manoel Campos --- src/test/java/tests/BasicTests.java | 109 ++++++++++++--------------- src/test/java/tests/DotenvTests.java | 97 +++++++++++------------- 2 files changed, 90 insertions(+), 116 deletions(-) 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() From 3e154099ddb1f75d2f182b3694313bf79594fa1a Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 18:18:55 -0300 Subject: [PATCH 06/11] Refactoring to use java var keyword where it makes code cleaner. - Define final variable and parameters. - Code organization. Signed-off-by: Manoel Campos --- .../cdimascio/dotenv/DotenvBuilder.java | 52 +++++++++++-------- .../dotenv/internal/ClasspathHelper.java | 13 ++--- .../dotenv/internal/DotenvParser.java | 51 +++++++++--------- 3 files changed, 64 insertions(+), 52 deletions(-) diff --git a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java index 6d6a60b..79f9b84 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,20 +112,22 @@ public Set entries() { } @Override - public Set entries(Dotenv.Filter filter) { - if (filter != null) return setInFile; + public Set entries(final Dotenv.Filter filter) { + if (filter != null) + return setInFile; + return entries(); } @Override - public String get(String key) { - String value = System.getenv(key); + public String get(final String key) { + final String value = System.getenv(key); return value != null ? value : envVars.get(key); } @Override public String get(String key, String defaultValue) { - String value = this.get(key); + final String value = this.get(key); return value != null ? value : defaultValue; } } 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..183578d 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,20 +57,25 @@ 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; + final var entries = new ArrayList(); + for (final var line : lines()) { + final var l = line.trim(); + if (isWhiteSpace.test(l) || isComment.test(l) || isBlank(l)) + continue; - DotenvEntry entry = parseLine.apply(l); + final var entry = parseLine.apply(l); if (entry == null) { - if (throwIfMalformed) throw new DotenvException("Malformed entry " + l); + 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 key = entry.getKey(); + final var value = normalizeValue(entry.getValue()); + final var newEntry = new DotenvEntry(key, value); + entries.add(newEntry); } + return entries; } @@ -79,29 +83,28 @@ 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)); } From a9ef35e3b4d4c9c7ac055dc555681e33f95fe028 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 18:20:26 -0300 Subject: [PATCH 07/11] Use afirmative conditions for clarity (without changing behaviour) Signed-off-by: Manoel Campos --- .../java/io/github/cdimascio/dotenv/DotenvBuilder.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java index 79f9b84..237370b 100644 --- a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java +++ b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java @@ -113,22 +113,20 @@ public Set entries() { @Override public Set entries(final Dotenv.Filter filter) { - if (filter != null) - return setInFile; + return filter == null ? entries() : setInFile; - return entries(); } @Override public String get(final String key) { final String value = System.getenv(key); - return value != null ? value : envVars.get(key); + return value == null ? envVars.get(key) : value; } @Override public String get(String key, String defaultValue) { final String value = this.get(key); - return value != null ? value : defaultValue; + return value == null ? defaultValue : value; } } } From d473516397d0447c932bf404f758d820cb271bfb Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 18:25:28 -0300 Subject: [PATCH 08/11] Refactors DotenvParser.parse() extracting a new method to improve clarity. Signed-off-by: Manoel Campos --- .../dotenv/internal/DotenvParser.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) 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 183578d..a7a7988 100644 --- a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java +++ b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java @@ -59,26 +59,28 @@ public DotenvParser(final DotenvReader reader, final boolean throwIfMissing, fin public List parse() throws DotenvException { final var entries = new ArrayList(); for (final var line : lines()) { - final var l = line.trim(); - if (isWhiteSpace.test(l) || isComment.test(l) || isBlank(l)) - continue; - - final var entry = parseLine.apply(l); - if (entry == null) { - if (throwIfMalformed) - throw new DotenvException("Malformed entry " + l); - continue; - } - - final var key = entry.getKey(); - final var value = normalizeValue(entry.getValue()); - final var newEntry = new DotenvEntry(key, value); - entries.add(newEntry); + 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(); From 6e5f336e68071fdbdbe08fbf2373ea85efe9c745 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 18:27:00 -0300 Subject: [PATCH 09/11] Gets the number of lines from the env file inside the DotenvParser.parse() and creates an ArrayList with that exact size for memory improvement. Signed-off-by: Manoel Campos --- .../io/github/cdimascio/dotenv/internal/DotenvParser.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 a7a7988..38ad017 100644 --- a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java +++ b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java @@ -57,8 +57,9 @@ public DotenvParser(final DotenvReader reader, final boolean throwIfMissing, fin * @throws DotenvException if an error is encountered during the parse */ public List parse() throws DotenvException { - final var entries = new ArrayList(); - for (final var line : lines()) { + final var lines = lines(); + final var entries = new ArrayList(lines.size()); + for (final var line : lines) { addNewEntry(entries, line.trim()); } From 5ef79c83ba76362dbb557ae1656594d7e2c7c4c7 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Fri, 28 Apr 2023 18:28:12 -0300 Subject: [PATCH 10/11] Version bump to 2.4.0 Signed-off-by: Manoel Campos --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5108d23..3ba764a 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ io.github.cdimascio dotenv-java - 2.3.2 + 2.4.0 From 01321e8f3fcc1878d2f35ebd5d990c3740d78632 Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Sat, 29 Apr 2023 00:07:45 -0300 Subject: [PATCH 11/11] Removes .travis.yml file and adds GitHub Action workflow build.yml - Update build badge - Remove coveralls badge since the plugin was removed (since it's abandoned) and codacy can be configured to show code coverage. Signed-off-by: Manoel Campos --- .github/workflows/build.yml | 22 ++++++++++++++++++++++ .travis.yml | 10 ---------- README.md | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .travis.yml 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.