Skip to content

Commit

Permalink
Add some tests, pull out sink iface
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent 8169a25 commit fef841b
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.shared;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.util.Collection;
import org.eclipse.aether.artifact.Artifact;

/**
* Construction to accept collection of artifacts, for example like a filesystem directory.
*/
public interface ArtifactSink {
default void accept(Collection<Artifact> artifacts) throws IOException {
requireNonNull(artifacts, "artifacts");
for (Artifact artifact : artifacts) {
accept(artifact);
}
}

void accept(Artifact artifact) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
import eu.maveniverse.maven.toolbox.shared.internal.ArtifactMatcher;
import eu.maveniverse.maven.toolbox.shared.internal.ArtifactNameMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.HashSet;
import java.util.function.Consumer;
import org.eclipse.aether.artifact.Artifact;

/**
* Construction to accept collection of artifacts, for example like a filesystem directory.
*/
public final class DirectorySink implements Consumer<Collection<Artifact>> {
public final class DirectorySink implements ArtifactSink {
/**
* Creates plain "flat" directory sink that accepts all artifacts and writes them out having filenames as
* "A[-C]-V.E" and prevents overwrite (what you usually want).
Expand Down Expand Up @@ -87,7 +85,7 @@ private DirectorySink(
}

@Override
public void accept(Collection<Artifact> artifacts) {
public void accept(Collection<Artifact> artifacts) throws IOException {
requireNonNull(artifacts, "artifacts");
output.verbose("Copying {} artifacts to directory {}", artifacts.size(), directory);
try {
Expand All @@ -96,11 +94,12 @@ public void accept(Collection<Artifact> artifacts) {
}
} catch (IOException e) {
cleanup();
throw new UncheckedIOException(e);
throw e;
}
}

private void accept(Artifact artifact) throws IOException {
@Override
public void accept(Artifact artifact) throws IOException {
output.verbose("Accept artifact {}", artifact);
if (artifactMatcher.test(artifact)) {
output.verbose(" matched");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.RemoteRepository;
Expand Down Expand Up @@ -65,12 +64,12 @@ default ResolutionRoot loadGav(String gav) throws ArtifactDescriptorException {

boolean classpath(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, Output output);

boolean copy(Collection<Artifact> artifacts, Consumer<Collection<Artifact>> consumer, Output output);
boolean copy(Collection<Artifact> artifacts, ArtifactSink sink, Output output);

boolean copyTransitive(
ResolutionScope resolutionScope,
Collection<ResolutionRoot> resolutionRoots,
Consumer<Collection<Artifact>> consumer,
ArtifactSink sink,
Output output);

boolean deploy(String remoteRepositorySpec, Supplier<Collection<Artifact>> artifactSupplier, Output output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static boolean matches(String pattern, String str) {
} else if (pattern.endsWith("*")) {
return str.startsWith(pattern.substring(0, pattern.length() - 1));
} else if (pattern.startsWith("*")) {
return str.endsWith(pattern.substring(0, pattern.length() - 1));
return str.endsWith(pattern.substring(1));
} else {
return Objects.equals(pattern, str);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import eu.maveniverse.maven.mima.context.MavenUserHome;
import eu.maveniverse.maven.mima.context.Runtime;
import eu.maveniverse.maven.mima.context.internal.RuntimeSupport;
import eu.maveniverse.maven.toolbox.shared.ArtifactSink;
import eu.maveniverse.maven.toolbox.shared.Output;
import eu.maveniverse.maven.toolbox.shared.ResolutionRoot;
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
Expand All @@ -41,7 +42,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -212,12 +212,11 @@ public boolean classpath(ResolutionScope resolutionScope, ResolutionRoot resolut
}

@Override
public boolean copy(Collection<Artifact> artifacts, Consumer<Collection<Artifact>> consumer, Output output) {
public boolean copy(Collection<Artifact> artifacts, ArtifactSink sink, Output output) {
try {
output.verbose("Resolving {}", artifacts);
List<ArtifactResult> resolveResult = toolboxResolver.resolveArtifacts(artifacts);
consumer.accept(
resolveResult.stream().map(ArtifactResult::getArtifact).collect(Collectors.toList()));
sink.accept(resolveResult.stream().map(ArtifactResult::getArtifact).collect(Collectors.toList()));
return !resolveResult.isEmpty();
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -228,7 +227,7 @@ public boolean copy(Collection<Artifact> artifacts, Consumer<Collection<Artifact
public boolean copyTransitive(
ResolutionScope resolutionScope,
Collection<ResolutionRoot> resolutionRoots,
Consumer<Collection<Artifact>> consumer,
ArtifactSink sink,
Output output) {
try {
ArrayList<ArtifactResult> artifactResults = new ArrayList<>();
Expand All @@ -241,7 +240,7 @@ public boolean copyTransitive(
resolutionRoot.getManagedDependencies());
artifactResults.addAll(dependencyResult.getArtifactResults());
}
consumer.accept(
sink.accept(
artifactResults.stream().map(ArtifactResult::getArtifact).collect(Collectors.toList()));
return !artifactResults.isEmpty();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.shared;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class DirectorySinkTest {
@Test
void smoke(@TempDir Path source, @TempDir Path target) throws IOException {
DirectorySink sink = DirectorySink.flat(new NullOutput(), target);
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
sink.accept(Arrays.asList(
new DefaultArtifact("g:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g:a2:1").setFile(a2.toFile())));

Path a1target = target.resolve("a1-1.jar");
Path a2target = target.resolve("a2-1.jar");
assertTrue(Files.isRegularFile(a1target));
assertEquals(Files.readString(a1target, StandardCharsets.UTF_8), "one");
assertTrue(Files.isRegularFile(a2target));
assertEquals(Files.readString(a2target, StandardCharsets.UTF_8), "two");
}

@Test
void sameA(@TempDir Path source, @TempDir Path target) throws IOException {
DirectorySink sink = DirectorySink.flat(new NullOutput(), target);
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
assertThrows(
IOException.class,
() -> sink.accept(Arrays.asList(
new DefaultArtifact("g1:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g2:a1:1").setFile(a2.toFile()))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.shared.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.junit.jupiter.api.Test;

public class ArtifactMapperTest {
@Test
void identity() {
DefaultArtifact a = new DefaultArtifact("a:b:c");
Artifact other = ArtifactMapper.identity().map(a);
assertEquals(a, other);
}

@Test
void allOfThemComposed() {
Artifact mapped = ArtifactMapper.compose(
ArtifactMapper.identity(),
ArtifactMapper.baseVersion(),
ArtifactMapper.omitClassifier(),
ArtifactMapper.rename("g1", "a1", null))
.map(new DefaultArtifact("g:a:jar:classifier:1.0-20240322.090900-12"));
assertEquals(new DefaultArtifact("g1:a1:1.0-SNAPSHOT"), mapped);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.shared.internal;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.junit.jupiter.api.Test;

/**
* Mapper that maps artifact to artifact.
*/
public class ArtifactMatcherTest {
private final Artifact artifact = new DefaultArtifact("g:a:v");

@Test
void any() {
assertTrue(ArtifactMatcher.any().test(artifact));
assertTrue(ArtifactMatcher.artifact("*").test(artifact));
}

@Test
void unique() {
ArtifactMatcher matcher = ArtifactMatcher.unique();
assertTrue(matcher.test(artifact));
assertFalse(matcher.test(artifact));
}

@Test
void composedAnd() {
assertTrue(ArtifactMatcher.and(
ArtifactMatcher.artifact("g:*:*"),
ArtifactMatcher.artifact("*:a:*"),
ArtifactMatcher.artifact("*:*:v"))
.test(artifact));
}

@Test
void composedOr() {
assertTrue(ArtifactMatcher.or(
ArtifactMatcher.artifact("foo:*:*"),
ArtifactMatcher.artifact("*:foo:*"),
ArtifactMatcher.artifact("*:*:v"))
.test(artifact));
}

@Test
void startsWith() {
assertTrue(ArtifactMatcher.artifact("g*:a*:v*").test(artifact));
}

@Test
void endsWith() {
assertTrue(ArtifactMatcher.artifact("*g:*a:*v").test(artifact));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023-2024 Maveniverse Org.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*/
package eu.maveniverse.maven.toolbox.shared.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.junit.jupiter.api.Test;

public class ArtifactNameMapperTest {
private final Artifact artifact = new DefaultArtifact("g:a:v");

@Test
void compose() {
ArtifactNameMapper mapper =
ArtifactNameMapper.compose(ArtifactNameMapper.prefix("foo/"), ArtifactNameMapper.ACVE());
assertEquals(mapper.map(artifact), "foo/a-v.jar");
}
}

0 comments on commit fef841b

Please sign in to comment.