Skip to content
Merged
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
14 changes: 13 additions & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ repositories {
}

dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation platform('org.junit:junit-bom:6.1.3')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.junit.jupiter:junit-jupiter-params'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

jar {
Expand All @@ -22,6 +25,15 @@ jar {
}
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs += ['--module-path', classpath.asPath]
options.javaModuleVersion = project.version.toString()
}

test {
useJUnitPlatform()
}

javadoc {
title = "applesingle-api ${project.version}"
source = sourceSets.main.allJava
Expand Down
52 changes: 26 additions & 26 deletions api/src/main/java/org/applecommander/applesingle/AppleSingle.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,13 @@
*/
package org.applecommander.applesingle;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Consumer;

/**
Expand All @@ -56,7 +47,20 @@ public class AppleSingle {

public static final String VERSION;
static {
VERSION = AppleSingle.class.getPackage().getImplementationVersion();
String version = AppleSingle.class.getPackage().getImplementationVersion();
if (version == null && AppleSingle.class.getModule().getDescriptor() != null) {
// When run under the Java Module system (JPMS) we need to get it a different way.
version = AppleSingle.class.getModule()
.getDescriptor()
.version()
.map(Object::toString)
.orElse("Development Build");
}
// For unit tests, the descriptor (apparently) doesn't exist, so we skip it.
if (version == null) {
version = "Unit Testing";
}
VERSION = version;
}

private Map<Integer,Consumer<Entry>> entryConsumers = new HashMap<>();
Expand Down Expand Up @@ -269,30 +273,21 @@ private Builder() {
this.as = new AppleSingle();
}
private Builder(AppleSingle original) {
Objects.requireNonNull(original);
this.as = original;
}
public Builder realName(String realName) {
if (!Character.isAlphabetic(realName.charAt(0))) {
throw new IllegalArgumentException("ProDOS file names must begin with a letter");
}
as.realName = realName.chars()
.map(this::sanitize)
.limit(15)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
Objects.requireNonNull(realName);
as.realName = realName;
return this;
}
private int sanitize(int ch) {
if (Character.isAlphabetic(ch) || Character.isDigit(ch)) {
return Character.toUpperCase(ch);
}
return '.';
}
public Builder dataFork(byte[] dataFork) {
Objects.requireNonNull(dataFork);
as.dataFork = dataFork;
return this;
}
public Builder resourceFork(byte[] resourceFork) {
Objects.requireNonNull(resourceFork);
as.resourceFork = resourceFork;
return this;
}
Expand All @@ -313,6 +308,7 @@ public Builder creationDate(int creation) {
return this;
}
public Builder creationDate(Instant creation) {
Objects.requireNonNull(creation);
as.fileDatesInfo.creation = FileDatesInfo.fromInstant(creation);
return this;
}
Expand All @@ -321,6 +317,7 @@ public Builder modificationDate(int modification) {
return this;
}
public Builder modificationDate(Instant modification) {
Objects.requireNonNull(modification);
as.fileDatesInfo.modification = FileDatesInfo.fromInstant(modification);
return this;
}
Expand All @@ -329,6 +326,7 @@ public Builder backupDate(int backup) {
return this;
}
public Builder backupDate(Instant backup) {
Objects.requireNonNull(backup);
as.fileDatesInfo.backup = FileDatesInfo.fromInstant(backup);
return this;
}
Expand All @@ -337,10 +335,12 @@ public Builder accessDate(int access) {
return this;
}
public Builder accessDate(Instant access) {
Objects.requireNonNull(access);
as.fileDatesInfo.access = FileDatesInfo.fromInstant(access);
return this;
}
public Builder allDates(Instant instant) {
Objects.requireNonNull(instant);
return creationDate(instant).modificationDate(instant).backupDate(instant).accessDate(instant);
}
public AppleSingle build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
*/
package org.applecommander.applesingle;

import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;

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

public class AppleSingleReaderTest {
@Test(expected = NullPointerException.class)
@Test
public void testDoesNotAcceptNull() {
AppleSingleReader.builder(null);
assertThrows(NullPointerException.class, () -> {
AppleSingleReader.builder(null);
});

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
*/
package org.applecommander.applesingle;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;

public class AppleSingleTest {
private static final String AS_HELLO_BIN = "/hello.applesingle.bin";
Expand Down Expand Up @@ -63,7 +58,7 @@ public void testCreateAndReadAppleSingle() throws IOException {
.allDates(instant)
.build();
assertNotNull(createdAS);
assertEquals(realName.toUpperCase(), createdAS.getRealName());
assertEquals(realName, createdAS.getRealName());
assertArrayEquals(dataFork, createdAS.getDataFork());
assertNull(createdAS.getResourceFork());
assertNotNull(createdAS.getProdosFileInfo());
Expand All @@ -74,7 +69,7 @@ public void testCreateAndReadAppleSingle() throws IOException {

AppleSingle readAS = AppleSingle.read(actualBytes.toByteArray());
assertNotNull(readAS);
assertEquals(realName.toUpperCase(), readAS.getRealName());
assertEquals(realName, readAS.getRealName());
assertArrayEquals(dataFork, readAS.getDataFork());
assertNull(readAS.getResourceFork());
assertNotNull(readAS.getProdosFileInfo());
Expand All @@ -85,24 +80,6 @@ public void testCreateAndReadAppleSingle() throws IOException {
assertEquals(instant, readAS.getFileDatesInfo().getBackupInstant());
}

@Test
public void testProdosFileNameLengthRequirements() {
AppleSingle as = AppleSingle.builder().realName("superlongnamethatneedstobetruncated").build();
assertEquals(15, as.getRealName().length());
}

@Test
public void testProdosFileNameCharacterRequirements() {
AppleSingle as = AppleSingle.builder().realName("bad-~@").build();
assertEquals("BAD...", as.getRealName());
}

@Test(expected = IllegalArgumentException.class)
public void testProdosFileNameFirstCharacter() {
// Fails due to the first character being a digit.
AppleSingle.builder().realName("1st-file").build();
}

@Test
public void testTest() throws IOException {
// Known valid
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Universal applesingle version number. Used for:
# - Naming JAR file.
# - The build will insert this into a file that is read at run time as well.
version=2.0
version=2.1
9 changes: 9 additions & 0 deletions scripts/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Usage: 'shell env.sh'

# Try to accommodate Mac and Linux
platform=$(uname | tr '[:upper:]' '[:lower:]' | sed 's/darwin/macosx/')
machine=$(uname -m | tr '[:upper:]' '[:lower:]' | sed 's/arm64/aarch64/' | sed 's/x86_64/amd64/')

# These are useful aliases while developing
alias asu="java -jar ${PWD}/tools/asu/build/libs/applesingle-tools-asu-*.jar"
alias asun="${PWD}/tools/asu/build/native/nativeCompile/asu-${platform}-${machine}-*"
Loading