Skip to content

Commit

Permalink
Migrate AsciidoctorZipMojoTest from Groovy to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Mar 22, 2021
1 parent c4dd5ba commit 9b1eb49
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 221 deletions.

This file was deleted.

This file was deleted.

161 changes: 161 additions & 0 deletions src/test/java/org/asciidoctor/maven/AsciidoctorZipMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package org.asciidoctor.maven;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.asciidoctor.maven.io.TestFilesHelper;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.asciidoctor.maven.TestUtils.mockAsciidoctorZipMojo;
import static org.assertj.core.api.Assertions.assertThat;


public class AsciidoctorZipMojoTest {


@Test
public void should_create_simple_zip() throws IOException, MojoFailureException, MojoExecutionException {
// given: 'an empty output directory'
File outputDir = TestFilesHelper.newOutputTestDirectory("asciidoctor-zip-output");

File zip = new File("target/asciidoctor-zip.zip");
zip.delete();

// when: 'zip mojo is called'
File srcDir = new File("target/test-classes/src/asciidoctor-zip");
srcDir.mkdirs();


FileUtils.write(new File(srcDir, "sample.adoc"),
"= Title\n\ntest", UTF_8);

AsciidoctorZipMojo mojo = mockAsciidoctorZipMojo();
mojo.backend = "html";
mojo.sourceDirectory = srcDir;
mojo.outputDirectory = outputDir;
mojo.zipDestination = zip;
mojo.zip = true;
mojo.execute();

// then: 'a zip is created'
mojo.zipDestination.exists();

ZipFile zipfile = new ZipFile(zip);
List<String> names = getNames(zipfile.entries())
.stream()
.map(value -> value.replaceAll("\\\\", "/"))
.map(value -> value.replaceAll("/" + outputDir.toString(), ""))
.collect(Collectors.toList());
assertThat(names).hasSize(1);
assertThat(names.get(0).replaceAll("\\\\", "/"))
.isEqualTo("asciidoctor-zip/sample.html");
}

@Test
public void should_replicate_source_structure_in_zip_standard_paths() throws MojoFailureException, MojoExecutionException, IOException {
// given:
File srcDir = new File("src/test/resources/src/asciidoctor/relative-path-treatment");
File outputDir = TestFilesHelper.newOutputTestDirectory("asciidoctor-zip-output");

File zip = new File(outputDir, "asciidoctor-zip.zip");

// when:
AsciidoctorZipMojo mojo = mockAsciidoctorZipMojo();
mojo.backend = "html5";
mojo.sourceDirectory = srcDir;
mojo.outputDirectory = outputDir;
mojo.preserveDirectories = true;
mojo.relativeBaseDir = true;
mojo.zipDestination = zip;
mojo.zip = true;
mojo.execute();

// then:
ZipFile zipfile = new ZipFile(zip);
List<String> names = getNames(zipfile.entries())
.stream()
.map(value -> value.replaceAll("\\\\", "/"))
.map(value -> value.replaceAll("/" + outputDir.toString(), ""))
.collect(Collectors.toList());
zipfile.close();
// Paths are adapted for the test are do not match the real paths inside the zip
List<String> expected = Arrays.asList(
"asciidoctor-zip/HelloWorld.groovy",
"asciidoctor-zip/HelloWorld.html",
"asciidoctor-zip/level-1-1/asciidoctor-icon.jpg",
"asciidoctor-zip/level-1-1/HelloWorld2.groovy",
"asciidoctor-zip/level-1-1/HelloWorld2.html",
"asciidoctor-zip/level-1-1/HelloWorld22.html",
"asciidoctor-zip/level-1-1/level-2-1/HelloWorld3.groovy",
"asciidoctor-zip/level-1-1/level-2-1/HelloWorld3.html",
"asciidoctor-zip/level-1-1/level-2-2/HelloWorld3.groovy",
"asciidoctor-zip/level-1-1/level-2-2/HelloWorld3.html",
"asciidoctor-zip/level-1-1/level-2-2/level-3-1/HelloWorld4.groovy",
"asciidoctor-zip/level-1-1/level-2-2/level-3-1/HelloWorld4.html"
);
assertThat(names)
.containsAll(expected);
}

@Test
public void should_not_replicate_source_structure_in_zip_standard_paths() throws IOException, MojoFailureException, MojoExecutionException {
// setup:
File srcDir = new File("src/test/resources/src/asciidoctor/relative-path-treatment");
File outputDir = TestFilesHelper.newOutputTestDirectory("asciidoctor-zip-output");

File zip = new File(outputDir, "asciidoctor-zip.zip");

// when:
AsciidoctorZipMojo mojo = mockAsciidoctorZipMojo();
mojo.backend = "html5";
mojo.sourceDirectory = srcDir;
mojo.outputDirectory = outputDir;
mojo.zipDestination = zip;
mojo.zip = true;
mojo.execute();

// then:
ZipFile zipfile = new ZipFile(zip);
List<String> names = getNames(zipfile.entries())
.stream()
.map(value -> value.replaceAll("\\\\", "/"))
.map(value -> value.replaceAll("/" + outputDir.toString(), ""))
.collect(Collectors.toList());
zipfile.close();
// Paths are adapted for the test are do not match the real paths inside the zip
List<String> expected = Arrays.asList(
"asciidoctor-zip/HelloWorld.groovy",
"asciidoctor-zip/HelloWorld.html",
"asciidoctor-zip/HelloWorld2.html",
"asciidoctor-zip/HelloWorld22.html",
"asciidoctor-zip/HelloWorld3.html",
"asciidoctor-zip/HelloWorld4.html",
"asciidoctor-zip/level-1-1/asciidoctor-icon.jpg",
"asciidoctor-zip/level-1-1/HelloWorld2.groovy",
"asciidoctor-zip/level-1-1/level-2-1/HelloWorld3.groovy",
"asciidoctor-zip/level-1-1/level-2-2/HelloWorld3.groovy",
"asciidoctor-zip/level-1-1/level-2-2/level-3-1/HelloWorld4.groovy"
);
assertThat(names)
.containsAll(expected);
}

private List<String> getNames(Enumeration<? extends ZipEntry> entries) {
final List<String> names = new ArrayList<>();
while (entries.hasMoreElements()) {
names.add(entries.nextElement().getName());
}
return names;
}
}
5 changes: 5 additions & 0 deletions src/test/java/org/asciidoctor/maven/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static AsciidoctorHttpMojo mockAsciidoctorHttpMojo() {
return mockAsciidoctorMojo(AsciidoctorHttpMojo.class, null, null);
}

@SneakyThrows
public static AsciidoctorZipMojo mockAsciidoctorZipMojo() {
return mockAsciidoctorMojo(AsciidoctorZipMojo.class, null, null);
}

@SneakyThrows
public static AsciidoctorMojo mockAsciidoctorMojo(Map<String, String> mavenProperties) {
return mockAsciidoctorMojo(AsciidoctorMojo.class, mavenProperties, null);
Expand Down

0 comments on commit 9b1eb49

Please sign in to comment.