Skip to content

Commit 3875e3a

Browse files
Merge branch 'eugenp:master' into master
2 parents 712fafa + dda61ce commit 3875e3a

File tree

201 files changed

+2566
-640
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+2566
-640
lines changed

core-java-modules/core-java-classloader/pom.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
<version>0.0.1-SNAPSHOT</version>
1212
</parent>
1313

14+
<dependencies>
15+
<dependency>
16+
<groupId>com.google.guava</groupId>
17+
<artifactId>guava</artifactId>
18+
<version>${guava.version}</version>
19+
<scope>test</scope>
20+
</dependency>
21+
</dependencies>
22+
1423
<build>
1524
<plugins>
1625
<plugin>
@@ -36,11 +45,4 @@
3645
</plugins>
3746
</build>
3847

39-
<properties>
40-
<maven.compiler.source>22</maven.compiler.source>
41-
<maven.compiler.target>22</maven.compiler.target>
42-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43-
<java.version>22</java.version>
44-
</properties>
45-
4648
</project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import com.baeldung.classloader.internal.InternalClasspathResolver;
1616
import com.baeldung.classloader.internal.InternalJdkSupport;
1717

18-
class ClassloaderDelegationModelTest {
18+
class ClassloaderDelegationModelUnitTest {
1919

2020
private static final String CLASS_TO_LOAD = "com.google.common.base.Function";
2121

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.baeldung.classloader.internal.InternalClasspathResolver;
2121
import com.baeldung.classloader.internal.InternalJdkSupport;
2222

23-
class GetURLsFromClassloaderTest {
23+
class GetURLsFromClassloaderUnitTest {
2424

2525
final Logger log = LoggerFactory.getLogger(getClass());
2626

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.net.MalformedURLException;
1010
import java.net.URI;
1111
import java.net.URL;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
1214
import java.util.Arrays;
1315
import java.util.HashSet;
1416
import java.util.Objects;
@@ -24,27 +26,26 @@
2426
import com.baeldung.classloader.internal.InternalJdkSupport;
2527
import com.baeldung.classloader.spi.ClasspathResolver;
2628

27-
class ScopedClassLoadingTest {
29+
class ScopedClassLoadingUnitTest {
2830

2931
final Logger log = LoggerFactory.getLogger(getClass());
3032

3133
/**
3234
* Some ides may treat test-classes as a dynamic module-path.
33-
*
3435
*/
3536
private void ammendTestClasspath(Set<URL> classpath) {
3637
var testCp = classpath.stream()
37-
.filter(url -> Objects.equals(url.getProtocol(), "file") && url.getPath()
38-
.contains("test-classes"))
39-
.findFirst()
40-
.orElse(null);
38+
.filter(url -> Objects.equals(url.getProtocol(), "file") && url.getPath()
39+
.contains("test-classes"))
40+
.findFirst()
41+
.orElse(null);
4142

4243
if (testCp == null) {
4344
log.info("Amending test classpath for Eclipse");
4445

4546
var loc = getClass().getProtectionDomain()
46-
.getCodeSource()
47-
.getLocation();
47+
.getCodeSource()
48+
.getLocation();
4849

4950
testCp = toURL(loc.toString());
5051

@@ -60,15 +61,15 @@ private Set<URL> createNarrowClasspath(Predicate<URL> filter) {
6061
var loader = getClass().getClassLoader();
6162

6263
var full = ClasspathResolver.get()
63-
.getFullClasspath(loader);
64+
.getFullClasspath(loader);
6465

6566
ammendTestClasspath(full);
6667

6768
mergeClasspathWithModulePath(full, filter);
6869

6970
var classpath = full.stream()
70-
.filter(filter)
71-
.collect(Collectors.toCollection(HashSet::new));
71+
.filter(filter)
72+
.collect(Collectors.toCollection(HashSet::new));
7273

7374
log.info("Narrowed Classpath: \n[\n{}\n]", classpath);
7475

@@ -80,14 +81,14 @@ void givenAForkedJVM_whenClassPathIsNarrowed_thenAccessWillBeLimitedToItsScope()
8081
var scope = Pattern.compile("(test-classes|slf|logback)");
8182

8283
var classpath = createNarrowClasspath(url -> scope.matcher(url.toString())
83-
.find()).stream()
84-
.map(URL::toString)
85-
.collect(Collectors.joining(":"));
84+
.find()).stream()
85+
.map(URL::toString)
86+
.collect(Collectors.joining(":"));
8687

8788
var executable = ProcessHandle.current()
88-
.info()
89-
.command()
90-
.orElse("java");
89+
.info()
90+
.command()
91+
.orElse("java");
9192

9293
var pb = new ProcessBuilder(executable, "-cp");
9394
var command = pb.command();
@@ -99,7 +100,7 @@ void givenAForkedJVM_whenClassPathIsNarrowed_thenAccessWillBeLimitedToItsScope()
99100
pb.redirectError(Redirect.INHERIT);
100101

101102
log.info("VM at PID {} will fork another JVM with narrowed classpath", ProcessHandle.current()
102-
.pid());
103+
.pid());
103104

104105
var process = pb.start();
105106

@@ -110,7 +111,7 @@ void givenAForkedJVM_whenClassPathIsNarrowed_thenAccessWillBeLimitedToItsScope()
110111

111112
@Test
112113
void givenScopedClassLoader_whenClasspathIsNarrowed_thenAccessWillBeLimitedToItsScope() throws InterruptedException, IOException,
113-
ReflectiveOperationException {
114+
ReflectiveOperationException {
114115
var thread = Thread.currentThread();
115116
var current = thread.getContextClassLoader();
116117

@@ -119,20 +120,20 @@ void givenScopedClassLoader_whenClasspathIsNarrowed_thenAccessWillBeLimitedToIts
119120
var scope = Pattern.compile("(test-classes|slf|logback)");
120121

121122
var classpath = createNarrowClasspath(url -> scope.matcher(url.toString())
122-
.find()).toArray(URL[]::new);
123+
.find()).toArray(URL[]::new);
123124

124125
var loader = new CustomClassLoader(classpath);
125126

126127
thread.setContextClassLoader(loader);
127128

128129
try {
129130
var service = Class.forName(ForkedService.class.getName(), true, Thread.currentThread()
130-
.getContextClassLoader());
131+
.getContextClassLoader());
131132

132133
assertEquals(loader, service.getClassLoader());
133134

134135
((Runnable) service.getConstructor()
135-
.newInstance()).run();
136+
.newInstance()).run();
136137
} finally {
137138
thread.setContextClassLoader(current);
138139
}
@@ -144,22 +145,27 @@ private void mergeClasspathWithModulePath(Set<URL> files, Predicate<URL> filter)
144145
if (modules != null && !modules.isBlank()) {
145146
log.info("Converting module-path ({}) to classpath", modules);
146147

147-
Arrays.stream(modules.split(":"))
148-
.map(this::toURL)
149-
.filter(filter)
150-
.forEach(files::add);
148+
String pathSeparator = System.getProperty("path.separator");
149+
150+
Arrays.stream(modules.split(Pattern.quote(pathSeparator)))
151+
.map(this::toURL)
152+
.filter(filter)
153+
.forEach(files::add);
151154
} else {
152155
log.info("No module path");
153156
}
154157
}
155158

156159
private URL toURL(String name) {
157-
if (!name.startsWith("file:")) {
158-
name = "file://" + name;
159-
}
160160
try {
161-
return URI.create(name)
162-
.toURL();
161+
// If it's already a valid URL, use it as-is
162+
if (name.startsWith("file:")) {
163+
return URI.create(name).toURL();
164+
}
165+
166+
Path path = Paths.get(name);
167+
return path.toUri().toURL();
168+
163169
} catch (MalformedURLException e) {
164170
throw new UncheckedIOException(e);
165171
}

core-java-modules/core-java-collections-conversions-2/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
<version>${modelmapper.version}</version>
2626
</dependency>
2727
<dependency>
28-
<groupId>io.vavr</groupId>
29-
<artifactId>vavr</artifactId>
30-
<version>${vavr.version}</version>
28+
<groupId>org.apache.commons</groupId>
29+
<artifactId>commons-collections4</artifactId>
30+
<version>${commons-collections4.version}</version>
3131
</dependency>
3232
</dependencies>
3333

@@ -42,7 +42,7 @@
4242
</build>
4343

4444
<properties>
45-
<vavr.version>0.10.3</vavr.version>
4645
<modelmapper.version>3.2.0</modelmapper.version>
4746
</properties>
47+
4848
</project>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.hashmaptoarraylist;
22

3+
import com.google.common.collect.Lists;
4+
import com.google.common.collect.Maps;
5+
import com.google.common.collect.Maps.EntryTransformer;
6+
37
import java.util.ArrayList;
48
import java.util.HashMap;
59
import java.util.Map;
610
import java.util.stream.Collectors;
711

8-
import com.google.common.collect.Lists;
9-
import com.google.common.collect.Maps;
10-
import com.google.common.collect.Maps.EntryTransformer;
11-
1212
public class HashMapToArrayListConverterUtils {
1313

1414
static ArrayList<String> convertUsingConstructor(HashMap<Integer, String> hashMap) {
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.baeldung.convertcollectiontoarraylist;
22

3-
import java.util.ArrayList;
4-
import java.util.Collection;
5-
import java.util.Comparator;
6-
import java.util.HashSet;
7-
import java.util.Iterator;
8-
import static java.util.stream.Collectors.toCollection;
93
import org.junit.BeforeClass;
104
import org.junit.Test;
5+
6+
import java.util.*;
7+
8+
import static java.util.stream.Collectors.toCollection;
119
import static org.junit.Assert.*;
1210

1311
/**
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.baeldung.convertiteratortolist;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
5-
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
3+
import com.google.common.collect.ImmutableList;
4+
import com.google.common.collect.Lists;
5+
import org.apache.commons.collections4.IteratorUtils;
6+
import org.junit.Before;
7+
import org.junit.Test;
68

79
import java.util.ArrayList;
810
import java.util.Arrays;
@@ -11,12 +13,9 @@
1113
import java.util.stream.Collectors;
1214
import java.util.stream.StreamSupport;
1315

14-
import org.apache.commons.collections4.IteratorUtils;
15-
import org.junit.Before;
16-
import org.junit.Test;
17-
18-
import com.google.common.collect.ImmutableList;
19-
import com.google.common.collect.Lists;
16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
18+
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
2019

2120
public class ConvertIteratorToListServiceUnitTest {
2221

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.baeldung.hashmaptoarraylist;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.containsInAnyOrder;
3+
import org.junit.Before;
4+
import org.junit.Test;
55

66
import java.util.ArrayList;
77
import java.util.HashMap;
88

9-
import org.junit.Before;
10-
import org.junit.Test;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.Matchers.containsInAnyOrder;
1111

1212
public class HashMapToArrayListConverterUtilsUnitTest {
1313

0 commit comments

Comments
 (0)