Skip to content

Commit

Permalink
update tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Apr 27, 2021
1 parent efecb91 commit b226e0b
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.kt text
*.json text
*.asar -text
*.zip -text
14 changes: 14 additions & 0 deletions url/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ dependencies {
implementation(project(":common"))
implementation(project(":file"))
}

val createAsarZip by tasks.creating(Zip::class) {
archiveFileName.set("test.zip")
destinationDirectory.set(file("testData"))
from("testData") {
include("test.asar")
}
}

tasks.test {
dependsOn(createAsarZip)
systemProperty("com.anatawa12.asar4j.test-asar", file("testData/test.asar").toString())
systemProperty("com.anatawa12.asar4j.test-zip", createAsarZip.archiveFile.get().toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -43,7 +45,11 @@ AsarFile getAsarFile(URL url, boolean useCache) throws IOException {

private AsarFile createURLAsarFile(URL url) throws IOException {
if (isLocalFileUrl(url)) {
return new AsarFile(Paths.get(UrlUtil.decodeURL(url.getFile())));
try {
return new AsarFile(Paths.get(new URI("file:" + url.getFile())));
} catch (URISyntaxException e) {
throw new AssertionError(e);
}
} else {
return getAndCreate(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AsarURLConnection extends URLConnection {
public AsarURLConnection(URL url) throws IOException {
super(url);
parseSpec(url.getFile());
asarFileConnection = url.openConnection();
asarFileConnection = asarFileURL.openConnection();
}

private void parseSpec(String spec) throws MalformedURLException {
Expand Down
44 changes: 44 additions & 0 deletions url/src/test/java/com/anatawa12/asar4j/url/ConnectionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.anatawa12.asar4j.url;

import com.google.common.io.ByteStreams;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

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

public class ConnectionTest {
static {
FactoryRegistrar.register();
}

@Test
void onFileSystem() throws IOException {
URL asarFile = new File(System.getProperty("com.anatawa12.asar4j.test-asar")).toURI().toURL();
URL testText = AsarURLStreamHandlerFactory.createUrl(asarFile, "test.txt");
// link
URL testTextLink = AsarURLStreamHandlerFactory.createUrl(asarFile, "test.txt.1");

assertArrayEquals("test text file\n".getBytes(StandardCharsets.UTF_8),
ByteStreams.toByteArray(testText.openStream()));
assertArrayEquals("test text file\n".getBytes(StandardCharsets.UTF_8),
ByteStreams.toByteArray(testTextLink.openStream()));
}

@Test
void onZipFile() throws IOException {
URL zipFile = new File(System.getProperty("com.anatawa12.asar4j.test-zip")).toURI().toURL();
URL asarFile = new URL("jar:" + zipFile + "!/test.asar");
URL testText = AsarURLStreamHandlerFactory.createUrl(asarFile, "test.txt");
// link
URL testTextLink = AsarURLStreamHandlerFactory.createUrl(asarFile, "test.txt.1");

assertArrayEquals("test text file\n".getBytes(StandardCharsets.UTF_8),
ByteStreams.toByteArray(testText.openStream()));
assertArrayEquals("test text file\n".getBytes(StandardCharsets.UTF_8),
ByteStreams.toByteArray(testTextLink.openStream()));
}
}
14 changes: 14 additions & 0 deletions url/src/test/java/com/anatawa12/asar4j/url/FactoryRegistrar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.anatawa12.asar4j.url;

import java.net.URL;

public class FactoryRegistrar {
static {
URL.setURLStreamHandlerFactory(AsarURLStreamHandlerFactory.INSTANCE);
}

// run in static initializer
public static void register() {

}
}
2 changes: 1 addition & 1 deletion url/src/test/java/com/anatawa12/asar4j/url/URLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class URLTest {
static {
URL.setURLStreamHandlerFactory(AsarURLStreamHandlerFactory.INSTANCE);
FactoryRegistrar.register();
}

@Test
Expand Down
1 change: 1 addition & 0 deletions url/testData/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.zip
Binary file added url/testData/test.asar
Binary file not shown.

0 comments on commit b226e0b

Please sign in to comment.