Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public Artifact expand(Artifact artifact) throws MacroEvaluationException, IOExc
Artifact expanded = new Artifact(groupId, artifactId, version);
expanded.setClassifier(expand(artifact.getClassifier()));
expanded.setExtension(expand(artifact.getExtension()));
expanded.setPomFile(expand(artifact.getPomFile()));
expanded.setTargetFileName(expand(artifact.getTargetFileName()));

expanded.setFailOnError(artifact.isFailOnError());
expanded.setDeployToLocal(artifact.isDeployToLocal());
expanded.setDeployToRemote(artifact.isDeployToRemote());

return expanded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected Artifact createArtifact(boolean failOnError) throws Exception {
artifact.setFailOnError(failOnError);

artifacts.add(artifact);
// expand() actually recreates the Artifact, but this is good enough for this mock.
when(mockExpander.expand(artifact)).thenReturn(artifact);

return artifact;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.jvnet.hudson.plugins.repositoryconnector.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Files;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.plugins.repositoryconnector.Artifact;
import org.jvnet.hudson.plugins.repositoryconnector.util.TokenMacroExpander;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;

public class TokenMacroExpanderTest {

@Mock
protected TaskListener mockListener;

@Mock
protected PrintStream mockPrintStream;

@Mock
protected Run<?, ?> mockRun;

protected FilePath workspace;

private TokenMacroExpander tokenExpander;

@After
public void after() throws Exception {
workspace.deleteRecursive();
}

@Before
public void before() throws Exception {
MockitoAnnotations.initMocks(this);

when(mockListener.getLogger()).thenReturn(mockPrintStream);

workspace = new FilePath(Files.createTempDirectory(null).toFile());

tokenExpander = new TokenMacroExpander(mockRun, mockListener, workspace);
}

@Test
public void testNothingToExpand() throws Exception {
Artifact artifact = createArtifact();

Artifact expanded = tokenExpander.expand(artifact);

// This list of asserts is alphabetized like in Artifact
assertEquals(artifact.getArtifactId(), expanded.getArtifactId());
assertEquals(artifact.getClassifier(), expanded.getClassifier());
assertEquals(artifact.isDeployToLocal(), expanded.isDeployToLocal());
assertEquals(artifact.isDeployToRemote(), expanded.isDeployToRemote());
assertEquals(artifact.getExtension(), expanded.getExtension());
assertEquals(artifact.isFailOnError(), expanded.isFailOnError());
assertEquals(artifact.getGroupId(), expanded.getGroupId());
assertEquals(artifact.getPomFile(), expanded.getPomFile());
assertEquals(artifact.getTargetFileName(), expanded.getTargetFileName());
assertEquals(artifact.getVersion(), expanded.getVersion());
}

@Test
public void testNothingToExpandWithOptionalParams() throws Exception {
Artifact artifact = createArtifact();
artifact.setClassifier("dummy");
artifact.setDeployToLocal(false);
artifact.setDeployToRemote(false);
artifact.setExtension("war");
artifact.setFailOnError(true);
artifact.setPomFile(getTestPom().getAbsolutePath());

Artifact expanded = tokenExpander.expand(artifact);

// required params
assertEquals(artifact.getArtifactId(), expanded.getArtifactId());
assertEquals(artifact.getGroupId(), expanded.getGroupId());
assertEquals(artifact.getVersion(), expanded.getVersion());
assertEquals(artifact.getTargetFileName(), expanded.getTargetFileName());

// optional params
assertEquals("dummy", expanded.getClassifier());
assertFalse(expanded.isDeployToLocal());
assertFalse(expanded.isDeployToRemote());
assertEquals("war", expanded.getExtension());
assertTrue(expanded.isFailOnError());
assertEquals(artifact.getPomFile(), expanded.getPomFile());
}

protected File getTestJar() throws URISyntaxException {
return new File(this.getClass().getResource("../test.jar").toURI());
}

protected File getTestPom() throws URISyntaxException {
return new File(this.getClass().getResource("../test-pom.xml").toURI());
}

protected Artifact createArtifact() throws Exception {
Artifact artifact = new Artifact("org.junit.jupiter", "junit-jupiter", "5.7.0");
artifact.setTargetFileName(getTestJar().getAbsolutePath());

return artifact;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- this is a test pom.xml file -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<packaging>jar</packaging>
</project>