Skip to content

Commit 11573eb

Browse files
Maskim BuryshynetsMaskim Buryshynets
authored andcommitted
tests migrated to kotest;
fixed an issue with resoling default value as a reference;
1 parent 412dbe9 commit 11573eb

File tree

9 files changed

+482
-296
lines changed

9 files changed

+482
-296
lines changed

build.gradle

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ plugins {
22
id 'jacoco'
33
id 'java-library'
44
id 'maven-publish'
5+
id 'org.jetbrains.kotlin.jvm'
56
}
67

78
group = 'com.github.arhor'
8-
version = '0.1.3'
9+
version = '0.1.4'
910

1011
sourceCompatibility = property('version.java')
1112
targetCompatibility = property('version.java')
@@ -24,13 +25,9 @@ repositories {
2425
}
2526

2627
dependencies {
27-
implementation "com.github.spotbugs:spotbugs-annotations:${property('version.spotbugs-annotations')}"
28+
implementation "com.github.spotbugs:spotbugs-annotations:${property('version.spotbugs')}"
2829

29-
testImplementation "org.assertj:assertj-core:${property('version.assertj-core')}"
30-
testImplementation "org.junit.jupiter:junit-jupiter-params:${property('version.junit-jupiter')}"
31-
testImplementation "org.junit.jupiter:junit-jupiter-api:${property('version.junit-jupiter')}"
32-
33-
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${property('version.junit-jupiter')}"
30+
testImplementation "io.kotest:kotest-runner-junit5-jvm:${property('version.kotest')}"
3431
}
3532

3633
tasks.withType(JavaCompile) {
@@ -41,13 +38,23 @@ tasks.withType(JavaCompile) {
4138
]
4239
}
4340

41+
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
42+
kotlinOptions {
43+
freeCompilerArgs = [
44+
'-Xjsr305=strict',
45+
'-Xjvm-default=all',
46+
]
47+
jvmTarget = project.property('version.java')
48+
javaParameters = true
49+
}
50+
}
51+
4452
jacoco {
4553
toolVersion = property('version.jacoco')
4654
}
4755

4856
jacocoTestReport {
49-
shouldRunAfter test
50-
57+
dependsOn test
5158
reports {
5259
xml.required.set(false)
5360
csv.required.set(false)
@@ -59,14 +66,15 @@ jacocoTestCoverageVerification {
5966
violationRules {
6067
rule {
6168
limit {
62-
minimum = 0.90
69+
minimum = 0.95
6370
}
6471
}
6572
}
6673
}
6774

6875
test {
6976
useJUnitPlatform()
77+
finalizedBy jacocoTestReport
7078
environment 'TEST_RESOURCES_DIR', file('src/test/resources')
7179
}
7280

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ org.gradle.parallel=false
66

77
version.java=11
88
version.jacoco=0.8.7
9+
version.kotest=5.5.2
10+
version.kotlin=1.7.20
911
version.gradle=7.5.1
10-
version.spotbugs-annotations=4.5.3
11-
version.assertj-core=3.22.0
12-
version.junit-jupiter=5.8.2
12+
version.spotbugs=4.5.3

settings.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
pluginManagement {
2+
plugins {
3+
id 'org.jetbrains.kotlin.jvm' version settings['version.kotlin']
4+
}
5+
}
6+
17
rootProject.name = 'java-dotenv-revised'

src/main/java/io/github/arhor/dotenv/DotenvConfigurer.java

Lines changed: 90 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,38 @@
22

33
import javax.annotation.Nonnull;
44
import java.util.Objects;
5+
import java.util.StringJoiner;
56

67
public final class DotenvConfigurer {
78

89
private static final class LazyHolder {
910
private static final DotenvConfigurer DEFAULT = new DotenvConfigurer(
10-
/* strictMode = */ false,
11-
/* includeSystemVariables = */ true,
12-
/* allowOverrideSystemVariables = */ true,
13-
/* location = */ ".",
14-
/* filename = */ ".env"
11+
/* location = */ ".",
12+
/* filename = */ ".env",
13+
/* strictMode = */ false,
14+
/* includeSystemVariables = */ true,
15+
/* replaceSystemVariables = */ true
1516
);
1617
}
1718

18-
private final boolean strictMode;
19-
private final boolean includeSystemVariables;
20-
private final boolean allowOverrideSystemVariables;
2119
private final String location;
2220
private final String filename;
21+
private final boolean strictMode;
22+
private final boolean includeSystemVariables;
23+
private final boolean replaceSystemVariables;
2324

2425
private DotenvConfigurer(
26+
final String location,
27+
final String filename,
2528
final boolean strictMode,
2629
final boolean includeSystemVariables,
27-
final boolean allowOverrideSystemVariables,
28-
final String location,
29-
final String filename
30+
final boolean replaceSystemVariables
3031
) {
31-
Objects.requireNonNull(location, "location must not be null");
32-
Objects.requireNonNull(filename, "filename must not be null");
33-
32+
this.location = Objects.requireNonNull(location, "location must not be null");
33+
this.filename = Objects.requireNonNull(filename, "filename must not be null");
3434
this.strictMode = strictMode;
3535
this.includeSystemVariables = includeSystemVariables;
36-
this.allowOverrideSystemVariables = allowOverrideSystemVariables;
37-
this.location = location;
38-
this.filename = filename;
36+
this.replaceSystemVariables = replaceSystemVariables;
3937
}
4038

4139
static DotenvConfigurer getInstance() {
@@ -45,70 +43,78 @@ static DotenvConfigurer getInstance() {
4543
@Nonnull
4644
public Dotenv load() {
4745
try {
48-
final var fileContent = DotenvFileLoader.readDotenvFileAsProperties(location, filename);
49-
return new DotenvImpl(this, fileContent);
46+
final var properties = DotenvFileLoader.readDotenvFileAsProperties(location, filename);
47+
return new DotenvImpl(this, properties);
5048
} catch (final Exception e) {
5149
throw new LoadingException(e);
5250
}
5351
}
5452

5553
@Nonnull
56-
public DotenvConfigurer strictMode(final boolean strictMode) {
54+
public DotenvConfigurer location(@Nonnull final String location) {
5755
return new DotenvConfigurer(
58-
strictMode,
56+
location,
57+
this.filename,
58+
this.strictMode,
5959
this.includeSystemVariables,
60-
this.allowOverrideSystemVariables,
61-
this.location,
62-
this.filename
60+
this.replaceSystemVariables
6361
);
6462
}
6563

6664
@Nonnull
67-
public DotenvConfigurer includeSystemVariables(final boolean includeSystemVariables) {
65+
public DotenvConfigurer filename(@Nonnull final String filename) {
6866
return new DotenvConfigurer(
69-
this.strictMode,
70-
includeSystemVariables,
71-
this.allowOverrideSystemVariables,
7267
this.location,
73-
this.filename
68+
filename,
69+
this.strictMode,
70+
this.includeSystemVariables,
71+
this.replaceSystemVariables
7472
);
7573
}
7674

77-
7875
@Nonnull
79-
public DotenvConfigurer allowOverrideSystemVariables(final boolean allowOverrideSystemVariables) {
76+
public DotenvConfigurer strictMode(final boolean strictMode) {
8077
return new DotenvConfigurer(
81-
this.strictMode,
82-
this.includeSystemVariables,
83-
allowOverrideSystemVariables,
8478
this.location,
85-
this.filename
79+
this.filename,
80+
strictMode,
81+
this.includeSystemVariables,
82+
this.replaceSystemVariables
8683
);
8784
}
8885

89-
9086
@Nonnull
91-
public DotenvConfigurer location(@Nonnull final String location) {
87+
public DotenvConfigurer includeSystemVariables(final boolean includeSystemVariables) {
9288
return new DotenvConfigurer(
89+
this.location,
90+
this.filename,
9391
this.strictMode,
94-
this.includeSystemVariables,
95-
this.allowOverrideSystemVariables,
96-
location,
97-
this.filename
92+
includeSystemVariables,
93+
this.replaceSystemVariables
9894
);
9995
}
10096

10197
@Nonnull
102-
public DotenvConfigurer filename(@Nonnull final String filename) {
98+
public DotenvConfigurer replaceSystemVariables(final boolean replaceSystemVariables) {
10399
return new DotenvConfigurer(
104-
strictMode,
105-
includeSystemVariables,
106-
allowOverrideSystemVariables,
107-
location,
108-
filename
100+
this.location,
101+
this.filename,
102+
this.strictMode,
103+
this.includeSystemVariables,
104+
replaceSystemVariables
109105
);
110106
}
111107

108+
@Nonnull
109+
public String getLocation() {
110+
return location;
111+
}
112+
113+
@Nonnull
114+
public String getFilename() {
115+
return filename;
116+
}
117+
112118
public boolean isStrictMode() {
113119
return strictMode;
114120
}
@@ -117,17 +123,46 @@ public boolean isIncludeSystemVariables() {
117123
return includeSystemVariables;
118124
}
119125

120-
public boolean isAllowOverrideSystemVariables() {
121-
return allowOverrideSystemVariables;
126+
public boolean isReplaceSystemVariables() {
127+
return replaceSystemVariables;
122128
}
123129

124-
@Nonnull
125-
public String getLocation() {
126-
return location;
130+
@Override
131+
public boolean equals(final Object o) {
132+
if (this == o) {
133+
return true;
134+
}
135+
if (o == null || getClass() != o.getClass()) {
136+
return false;
137+
}
138+
139+
DotenvConfigurer that = (DotenvConfigurer) o;
140+
141+
return strictMode == that.strictMode
142+
&& includeSystemVariables == that.includeSystemVariables
143+
&& replaceSystemVariables == that.replaceSystemVariables
144+
&& location.equals(that.location)
145+
&& filename.equals(that.filename);
127146
}
128147

129-
@Nonnull
130-
public String getFilename() {
131-
return filename;
148+
@Override
149+
public int hashCode() {
150+
int result = location.hashCode();
151+
result = 31 * result + filename.hashCode();
152+
result = 31 * result + (strictMode ? 1 : 0);
153+
result = 31 * result + (includeSystemVariables ? 1 : 0);
154+
result = 31 * result + (replaceSystemVariables ? 1 : 0);
155+
return result;
156+
}
157+
158+
@Override
159+
public String toString() {
160+
return new StringJoiner(", ", DotenvConfigurer.class.getSimpleName() + "(", ")")
161+
.add("location='" + location + "'")
162+
.add("filename='" + filename + "'")
163+
.add("strictMode=" + strictMode)
164+
.add("includeSystemVariables=" + includeSystemVariables)
165+
.add("replaceSystemVariables=" + replaceSystemVariables)
166+
.toString();
132167
}
133168
}

src/main/java/io/github/arhor/dotenv/DotenvImpl.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,47 @@ final class DotenvImpl implements Dotenv {
2424
private final Set<String> currentSearchHistory = new LinkedHashSet<>();
2525

2626
DotenvImpl(final DotenvConfigurer configurer, final Properties properties) {
27-
Objects.requireNonNull(configurer, "config must not be null");
28-
Objects.requireNonNull(properties, "fileContent must not be null");
29-
30-
this.configurer = configurer;
31-
this.properties = properties;
27+
this.configurer = Objects.requireNonNull(configurer, "configurer must not be null");
28+
this.properties = Objects.requireNonNull(properties, "properties must not be null");
3229
}
3330

3431
@Nullable
3532
@Override
3633
public String get(@Nullable final String name) {
37-
return getPropertyThenClearSearchHistory(name);
34+
return getPropertyThenClearSearchHistory(name, null, false);
3835
}
3936

4037
@Nullable
4138
@Override
4239
public String get(@Nullable final String name, @Nullable final String defaultValue) {
43-
final var property = getPropertyThenClearSearchHistory(name);
44-
return (property != null)
45-
? property
46-
: defaultValue;
40+
return getPropertyThenClearSearchHistory(name, defaultValue, true);
4741
}
4842

4943
@Nonnull
5044
@Override
5145
public String getRequired(@Nonnull final String name) throws MissingPropertyException {
52-
final var property = getPropertyThenClearSearchHistory(name);
46+
final var property = getPropertyThenClearSearchHistory(name, null, false);
5347
if (property != null) {
5448
return property;
5549
}
5650
throw new MissingPropertyException(name);
5751
}
5852

59-
private String getPropertyThenClearSearchHistory(final String name) {
53+
private String getPropertyThenClearSearchHistory(
54+
final String name,
55+
final String defaultValue,
56+
final boolean useDefaultValue
57+
) {
6058
try {
61-
return getProperty(name);
59+
final var property = getProperty(name);
60+
61+
if (property != null || !useDefaultValue) {
62+
return property;
63+
} else if (defaultValue != null) {
64+
return resolveReferences(defaultValue);
65+
} else {
66+
return null;
67+
}
6268
} finally {
6369
currentSearchHistory.clear();
6470
}
@@ -78,7 +84,7 @@ private String findProperty(final String name) {
7884
final var property = configurer.isIncludeSystemVariables()
7985
? System.getenv().get(name)
8086
: null;
81-
return ((property == null) || configurer.isAllowOverrideSystemVariables())
87+
return ((property == null) || configurer.isReplaceSystemVariables())
8288
? properties.getProperty(name)
8389
: property;
8490
}

0 commit comments

Comments
 (0)