Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Change log for releases.

# SNAPSHOT

* Allow to disable auto-running `_read` in constructor
* Update default version of Kaitai compiler to `0.9`

# 0.1.6

# 0.1.5
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ See [kaitai-java-demo](https://github.com/valery1707/kaitai-java-demo).
|-----------------|--------------|-------|-------------------------------------------------------------------------------------------------------------------------|
| skip | boolean | 0.1.0 | Skip plugin execution (don't read/validate any files, don't generate any java types).<br><br>**Default**: `false` |
| url | java.net.URL | 0.1.0 | Direct link onto [KaiTai universal zip archive](http://kaitai.io/#download).<br><br>**Default**: Detected from version |
| version | String | 0.1.0 | Version of [KaiTai](http://kaitai.io/#download) library.<br><br>**Default**: `0.8` |
| version | String | 0.1.0 | Version of [KaiTai](http://kaitai.io/#download) library.<br><br>**Default**: `0.9` |
| cacheDir | java.io.File | 0.1.0 | Cache directory for download KaiTai library.<br><br>**Default**: `build/tmp/kaitai-cache` |
| sourceDirectory | java.io.File | 0.1.0 | Source directory with [Kaitai Struct language](http://formats.kaitai.io/) files.<br><br>**Default**: src/main/resources/kaitai |
| includes | String[] | 0.1.0 | Include wildcard pattern list.<br><br>**Default**: ["*.ksy"] |
Expand Down
3 changes: 3 additions & 0 deletions src/it/it-source-exist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<version>0.8</version>
</configuration>
<executions>
<execution>
<id>generate</id>
Expand Down
3 changes: 3 additions & 0 deletions src/it/it-source-failed/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<version>0.8</version>
</configuration>
<executions>
<execution>
<id>generate</id>
Expand Down
1 change: 1 addition & 0 deletions src/it/it-withOption-exactOutput-target-create/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<version>@project.version@</version>
<configuration>
<output>${project.build.sourceDirectory}</output>
<version>0.8</version>
<exactOutput>true</exactOutput>
</configuration>
<executions>
Expand Down
1 change: 1 addition & 0 deletions src/it/it-withOption-exactOutput-target-overwrite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<version>@project.version@</version>
<configuration>
<output>${project.build.sourceDirectory}</output>
<version>0.8</version>
<exactOutput>true</exactOutput>
</configuration>
<executions>
Expand Down
1 change: 1 addition & 0 deletions src/it/it-withOption-fromFileClass/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<version>0.8</version>
<fromFileClass>name.valery1707.kaitai.it.CustomStream</fromFileClass>
</configuration>
<executions>
Expand Down
1 change: 1 addition & 0 deletions src/it/it-withOption-opaqueTypes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<version>0.8</version>
<opaqueTypes>true</opaqueTypes>
</configuration>
<executions>
Expand Down
3 changes: 3 additions & 0 deletions src/it/lib-commons-io_24-exist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<version>0.8</version>
</configuration>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
Expand Down
3 changes: 3 additions & 0 deletions src/it/lib-commons-io_24-failed/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<version>0.8</version>
</configuration>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/name/valery1707/kaitai/KaitaiGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.buildobjects.process.TimeoutException;
import org.slf4j.Logger;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -246,6 +247,7 @@ public KaitaiGenerator noVersionCheck(boolean noVersionCheck) {
setNoVersionCheck(noVersionCheck);
return this;
}

/**
* Get no auto read mode.
*
Expand All @@ -254,6 +256,7 @@ public KaitaiGenerator noVersionCheck(boolean noVersionCheck) {
public boolean isNoAutoRead() {
return noAutoRead;
}

/**
* Set no auto read mode.
*
Expand Down Expand Up @@ -356,7 +359,13 @@ public Path generate(Logger log) throws KaitaiException {

log.info("Kaitai: generate");
execute(builder);
output = output.resolve("src");

// Kaitai compiler version `0.8` generate all files inside directory `src`, generated by `ksc` itself.
Path src = output.resolve("src");
if (!getPackageName().startsWith("src.") && Files.isDirectory(src)) {
output = src;
}

if (isExactOutput()) {
Path root = getOutput();
List<Path> generated = scanFiles(output, new String[]{"*"}, new String[0]);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/name/valery1707/kaitai/KaitaiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
, defaultPhase = LifecyclePhase.GENERATE_SOURCES
)
public class KaitaiMojo extends AbstractMojo {
static final String KAITAI_VERSION = "0.8";
static final String KAITAI_VERSION_08 = "0.8";
static final String KAITAI_VERSION_09 = "0.9";
static final String KAITAI_VERSION_LATEST = KAITAI_VERSION_09;
static final String KAITAI_VERSION = KAITAI_VERSION_LATEST;

/**
* Version of <a href="http://kaitai.io/#download">KaiTai</a> library.
Expand Down Expand Up @@ -161,7 +164,7 @@ public class KaitaiMojo extends AbstractMojo {
* Disable auto-running `_read` in constructor.
*/
@Parameter(property = "kaitai.noAutoRead", defaultValue = "false")
private Boolean noAutoRead;
private boolean noAutoRead;

/**
* Allow to disable Java version check.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/name/valery1707/kaitai/KaitaiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc != null) throw exc;
if (exc != null) {
throw exc;
}
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
Expand Down
42 changes: 29 additions & 13 deletions src/test/java/name/valery1707/kaitai/KaitaiGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.nio.file.Paths;

import static java.nio.charset.StandardCharsets.UTF_8;
import static name.valery1707.kaitai.KaitaiMojo.KAITAI_VERSION;
import static name.valery1707.kaitai.KaitaiMojo.*;
import static name.valery1707.kaitai.KaitaiUtils.downloadKaitai;
import static name.valery1707.kaitai.KaitaiUtils.prepareUrl;
import static name.valery1707.kaitai.KaitaiUtilsTest.copy;
Expand Down Expand Up @@ -53,9 +53,9 @@ private Path findIt() throws URISyntaxException {
.resolve("src/it");
}

private KaitaiGenerator generator(Path... sources) throws IOException, KaitaiException {
private KaitaiGenerator generator(String version, Path... sources) throws IOException, KaitaiException {
Path cache = temporaryFolder.newFolder().toPath();
Path kaitai = downloadKaitai(prepareUrl(null, KAITAI_VERSION), cache, LOG);
Path kaitai = downloadKaitai(prepareUrl(null, version), cache, LOG);
Path generated = cache.resolve("generated");
Files.createDirectory(generated);
return KaitaiGenerator
Expand All @@ -65,10 +65,10 @@ private KaitaiGenerator generator(Path... sources) throws IOException, KaitaiExc
}

@Test
public void testGenerate_success() throws IOException, URISyntaxException, KaitaiException {
public void testGenerate_success_ver_0_8() throws IOException, URISyntaxException, KaitaiException {
Path source = findIt()
.resolve("it-source-exist/src/main/resources/kaitai/ico.ksy");
KaitaiGenerator generator = generator(source);
KaitaiGenerator generator = generator(KAITAI_VERSION_08, source);
Path target = generator.generate(LOG);
assertThat(target).isDirectory().hasFileName("src");
Path pkg = target.resolve(generator.getPackageName().replace('.', '/'));
Expand All @@ -80,19 +80,35 @@ public void testGenerate_success() throws IOException, URISyntaxException, Kaita
}
}

@Test
public void testGenerate_success_ver_0_9() throws IOException, URISyntaxException, KaitaiException {
Path source = findIt()
.resolve("it-source-exist/src/main/resources/kaitai/ico.ksy");
KaitaiGenerator generator = generator(KAITAI_VERSION_09, source);
Path target = generator.generate(LOG);
assertThat(target).isDirectory();
Path pkg = target.resolve(generator.getPackageName().replace('.', '/'));
assertThat(pkg).isDirectory();
for (Path src : generator.getSources()) {
String kaitaiName = src.getFileName().toString();
String javaName = capitalize(removeExtension(kaitaiName)) + ".java";
assertThat(pkg.resolve(javaName)).isRegularFile();
}
}

@Test
public void testGenerate_failed() throws IOException, URISyntaxException, KaitaiException {
Path source = findIt()
.resolve("it-source-failed/src/main/resources/kaitai/demo.ksy");
KaitaiGenerator generator = generator(source);
KaitaiGenerator generator = generator(KAITAI_VERSION_LATEST, source);
try {
generator.generate(LOG);
fail("Must generate exception because of problems in specification");
} catch (KaitaiException e) {
assertThat(e)
.hasMessageContaining("/types/header/seq/0/id: invalid attribute ID: 'Magic', expected /^[a-z][a-z0-9_]*$/")
;
assertThat(e.getMessage()).doesNotContain(KAITAI_VERSION);
assertThat(e.getMessage()).doesNotContain(KAITAI_VERSION_LATEST);

assertThat(e.getCause())
.isInstanceOf(ExternalProcessFailureException.class)
Expand Down Expand Up @@ -151,12 +167,12 @@ public void testExecutionTimeout_negativeSuccess() throws IOException, KaitaiExc
}

@Test
public void testOption_fromFileClass() throws URISyntaxException, IOException, KaitaiException {
public void testOption_fromFileClass_ver_0_8() throws URISyntaxException, IOException, KaitaiException {
Path source = findIt()
.resolve("it-source-exist/src/main/resources/kaitai/ico.ksy");
String fromFileClassName = "TestStream";
String fromFileClass = "name.valery1707.kaitai.test" + "." + fromFileClassName;
KaitaiGenerator generator = generator(source)
KaitaiGenerator generator = generator(KAITAI_VERSION_08, source)
.fromFileClass(fromFileClass);
Path target = generator.generate(LOG);
assertThat(target).isDirectory().hasFileName("src");
Expand All @@ -175,10 +191,10 @@ public void testOption_fromFileClass() throws URISyntaxException, IOException, K
}

@Test
public void testOption_opaqueTypes_enabled() throws URISyntaxException, IOException, KaitaiException {
public void testOption_opaqueTypes_enabled_ver_0_8() throws URISyntaxException, IOException, KaitaiException {
Path source = findIt()
.resolve("it-withOption-opaqueTypes/src/main/resources/kaitai/doc_container.ksy");
KaitaiGenerator generator = generator(source)
KaitaiGenerator generator = generator(KAITAI_VERSION_08, source)
.opaqueTypes(true);
Path target = generator.generate(LOG);
assertThat(target).isDirectory().hasFileName("src");
Expand All @@ -199,7 +215,7 @@ public void testOption_opaqueTypes_enabled() throws URISyntaxException, IOExcept
public void testOption_opaqueTypes_disabled() throws URISyntaxException, IOException, KaitaiException {
Path source = findIt()
.resolve("it-withOption-opaqueTypes/src/main/resources/kaitai/doc_container.ksy");
KaitaiGenerator generator = generator(source)
KaitaiGenerator generator = generator(KAITAI_VERSION_LATEST, source)
.opaqueTypes(false);
try {
generator.generate(LOG);
Expand All @@ -208,7 +224,7 @@ public void testOption_opaqueTypes_disabled() throws URISyntaxException, IOExcep
assertThat(e)
.hasMessageContaining("/seq/0: unable to find type 'custom_encrypted_object', searching from doc_container")
;
assertThat(e.getMessage()).doesNotContain(KAITAI_VERSION);
assertThat(e.getMessage()).doesNotContain(KAITAI_VERSION_LATEST);

assertThat(e.getCause())
.isInstanceOf(ExternalProcessFailureException.class)
Expand Down
50 changes: 29 additions & 21 deletions src/test/java/name/valery1707/kaitai/KaitaiUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;

Expand All @@ -30,9 +30,7 @@
import static name.valery1707.kaitai.KaitaiMojo.KAITAI_VERSION;
import static name.valery1707.kaitai.KaitaiUtils.*;
import static org.apache.commons.io.FilenameUtils.getName;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.hamcrest.CoreMatchers.containsString;
import static org.assertj.core.api.Assertions.*;
import static org.slf4j.helpers.NOPLogger.NOP_LOGGER;

public class KaitaiUtilsTest {
Expand All @@ -41,9 +39,6 @@ public class KaitaiUtilsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public ExpectedException exception = ExpectedException.none();

static Path copy(String resource, Path file) throws IOException {
Files.createDirectories(file.getParent());
Files.copy(KaitaiUtilsTest.class.getResourceAsStream(resource), file, StandardCopyOption.REPLACE_EXISTING);
Expand Down Expand Up @@ -448,13 +443,17 @@ public void testDownload_noDownloadIfExists() throws IOException, KaitaiExceptio
.hasBinaryContent(new byte[0]);
}

@Test(expected = KaitaiException.class)
public void testDownload_404() throws IOException, KaitaiException {
Path target = temporaryFolder.newFile("assertj-core-2.9.0.jar").toPath();
@Test
public void testDownload_404() throws IOException {
final Path target = temporaryFolder.newFile("assertj-core-2.9.0.jar").toPath();
Files.delete(target);
URL source = new URL("https://search.maven.org/remotecontent?filepath=org/assertj/assertj-core/2.7.0/assertj-core-2.9.0.jar");
download(source, target, LOG);
throw new IllegalStateException("Unreachable statement");
final URL source = new URL("https://search.maven.org/remotecontent?filepath=org/assertj/assertj-core/2.7.0/assertj-core-2.9.0.jar");
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
download(source, target, LOG);
}
}).isInstanceOf(KaitaiException.class);
}

@Test
Expand All @@ -473,9 +472,13 @@ public void testPrepareUrl_generateIfInputNull() throws KaitaiException {

@Test
@Ignore
public void testPrepareUrl_failedIfBadVersion() throws KaitaiException {
exception.expect(KaitaiException.class);
prepareUrl(null, "?#~@");
public void testPrepareUrl_failedIfBadVersion() {
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
prepareUrl(null, "?#~@");
}
}).isInstanceOf(KaitaiException.class);
}

@Test
Expand All @@ -489,10 +492,15 @@ public void testPrepareCache_useExternal() throws IOException, KaitaiException {
}

@Test
public void testDownloadKaitai_invalidZipContent() throws IOException, KaitaiException {
exception.expect(KaitaiException.class);
exception.expectMessage(containsString("Fail to find start script"));
Path cache = temporaryFolder.newFolder().toPath();
downloadKaitai(getClass().getResource("/demo-vertx.zip"), cache, LOG);
public void testDownloadKaitai_invalidZipContent() throws IOException {
final Path cache = temporaryFolder.newFolder().toPath();
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
downloadKaitai(getClass().getResource("/demo-vertx.zip"), cache, LOG);
}
})
.isInstanceOf(KaitaiException.class)
.hasMessageContaining("Fail to find start script");
}
}