Skip to content

Commit

Permalink
SHRINKRES-230 refactoring and changed a test case to test the issue
Browse files Browse the repository at this point in the history
The preceding commits fixed the issue, so this commit just refactor the
implementation - mainly the test case to not change the visibility of
the inner class MavenResolvedArtifactImpl.PackageDirHelper
  • Loading branch information
MatousJobanek committed Sep 10, 2015
1 parent 7fdc237 commit cb9ac81
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ target
build

atlassian-ide-plugin.xml

!impl-maven/src/test/resources/repository/org/jboss/shrinkwrap/test/test-pom/1.0.0/target
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.resolution.ArtifactResult;
import org.jboss.shrinkwrap.resolver.api.maven.MavenArtifactInfo;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.maven.ScopeType;
Expand All @@ -38,9 +41,6 @@
import org.jboss.shrinkwrap.resolver.impl.maven.util.Validate;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessor;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessors;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.resolution.ArtifactResult;

/**
* Immutable implementation of {@link MavenResolvedArtifact}.
Expand Down Expand Up @@ -165,7 +165,7 @@ private static File artifactToFile(final Artifact artifact) throws IllegalArgume
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
protected static class PackageDirHelper {
private static class PackageDirHelper {

private PackageDirHelper() {
throw new UnsupportedOperationException("No instances should be created; stateless class");
Expand Down Expand Up @@ -222,17 +222,20 @@ private static List<String> fileListing(final File directory) {
private static void generateFileList(final List<String> list, final File root, final File file) {
if (file.isFile()) {
// SHRINKRES-94 replacing all OS dependent separators with jar independent separator
list.add(file.getAbsolutePath().substring(
root.getAbsolutePath().length() + 1).replace(File.separatorChar, '/'));
list.add(getEntryPath(root, file));
} else if (file.isDirectory()) {
if (!file.equals(root)) {
list.add(file.getAbsolutePath().substring(
root.getAbsolutePath().length() + 1).replace(File.separatorChar, '/') + File.separatorChar);
list.add(getEntryPath(root, file) + File.separatorChar);
}
for (File next : file.listFiles()) {
generateFileList(list, root, next);
}
}
}

private static String getEntryPath(final File root, final File file) {
return file.getAbsolutePath().substring(root.getAbsolutePath().length() + 1)
.replace(File.separatorChar, '/');
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jboss.shrinkwrap.resolver.impl.maven;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.graph.DefaultDependencyNode;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

/**
* This test case simulates behavior of an IDE - in IDE an artifact, that is also another module loaded in the IDE,
* is not fetched from local/remote repository, but is referenced to the location of the module's directory.
* The module's pom.xml file is taken as the artifact's pom and the subdirectories containing the compiled classes
* are packaged into a zip archive. This zip archive is then returned as a resulting artifact file.
*
* NOTE: this testcase is in the {@code util} package because of visibility of
* {@link MavenResolvedArtifactImpl#fromArtifactResult(ArtifactResult)} method
*
* @author <a href="mailto:mjobanek@redhat.com">Matous Jobanek</a>
* @author <a href="mailto:olivts@free.fr">Olivier Spieser</a>
*
*/
public class PackageDirectoriesWithClassesTestCase {

/**
* Test zip archive creation from directory located in. Check if directory entries are added to the archive.
*/
@Test
public void packageDirectoriesWithClasses() throws IOException {
File artifactFile = new File(
System.getProperty("user.dir") + "/target/repository/org/jboss/shrinkwrap/test/test-pom/1.0.0/pom.xml");

Artifact testPomArtifactMock = Mockito.mock(Artifact.class);
Mockito.when(testPomArtifactMock.getGroupId()).thenReturn("org.jboss.shrinkwrap.test");
Mockito.when(testPomArtifactMock.getArtifactId()).thenReturn("test-pom");
Mockito.when(testPomArtifactMock.getExtension()).thenReturn("xml");
Mockito.when(testPomArtifactMock.getClassifier()).thenReturn("");
Mockito.when(testPomArtifactMock.getVersion()).thenReturn("1.0.0");
Mockito.when(testPomArtifactMock.getFile()).thenReturn(artifactFile);
Mockito.when(testPomArtifactMock.getProperty(ArtifactProperties.TYPE, testPomArtifactMock.getExtension()))
.thenReturn("pom");

ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setDependencyNode(new DefaultDependencyNode(new Dependency(testPomArtifactMock, "test")));
ArtifactResult mockedArtResult = new ArtifactResult(artifactRequest);
mockedArtResult.setArtifact(testPomArtifactMock);

MavenResolvedArtifact mavenResolvedArtifact = MavenResolvedArtifactImpl.fromArtifactResult(mockedArtResult);
ZipFile outputZipFile = new ZipFile(mavenResolvedArtifact.asFile());

//Check if existing files and folders and in zip.
Assert.assertNotNull(outputZipFile.getEntry("b/c"));
Assert.assertNotNull(outputZipFile.getEntry("a/a.file"));

//Check if non existing items are null !
Assert.assertNull(outputZipFile.getEntry("a/non-exist/"));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/maven-v4_0_0.xsd">

<!-- Model Version -->
<modelVersion>4.0.0</modelVersion>

<!-- Artifact Configuration -->
<groupId>org.jboss.shrinkwrap.test</groupId>
<artifactId>test-pom</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.jboss.shrinkwrap.test</groupId>
<artifactId>test-pom</artifactId>
<versioning>
<release>1.0.0</release>
<versions>
<version>1.0.0</version>
</versions>
<lastUpdated>20140920183335</lastUpdated>
</versioning>
</metadata>

0 comments on commit cb9ac81

Please sign in to comment.