JS: fix ClassLoader.getResourceBundle for non-existent resources

This commit is contained in:
Alexey Andreev 2017-05-29 23:19:07 +03:00
parent 39cdf3bfed
commit 25b298b1d0
2 changed files with 16 additions and 8 deletions

View File

@ -17,15 +17,12 @@ package org.teavm.classlib.java.lang;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import org.teavm.classlib.impl.Base64;
import org.teavm.backend.javascript.spi.InjectedBy; import org.teavm.backend.javascript.spi.InjectedBy;
import org.teavm.classlib.impl.Base64;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSIndexer; import org.teavm.jso.JSIndexer;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
public abstract class TClassLoader extends TObject { public abstract class TClassLoader extends TObject {
private TClassLoader parent; private TClassLoader parent;
private static TSystemClassLoader systemClassLoader = new TSystemClassLoader(); private static TSystemClassLoader systemClassLoader = new TSystemClassLoader();
@ -51,15 +48,19 @@ public abstract class TClassLoader extends TObject {
if (resources == null) { if (resources == null) {
resources = supplyResources(); resources = supplyResources();
} }
String data = resources.getResource(name); JSObject data = resources.getResource(name);
return data == null ? null : new ByteArrayInputStream(Base64.decode(data)); String dataString = resourceToString(data);
return dataString == null ? null : new ByteArrayInputStream(Base64.decode(dataString));
} }
@JSBody(params = "resource", script = "return resource !== null && resource !== void 0 ? resource : null;")
private static native String resourceToString(JSObject resource);
@InjectedBy(ClassLoaderNativeGenerator.class) @InjectedBy(ClassLoaderNativeGenerator.class)
private static native ResourceContainer supplyResources(); private static native ResourceContainer supplyResources();
interface ResourceContainer extends JSObject { interface ResourceContainer extends JSObject {
@JSIndexer @JSIndexer
String getResource(String name); JSObject getResource(String name);
} }
} }

View File

@ -18,6 +18,7 @@ package org.teavm.classlib.java.lang;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -37,6 +38,12 @@ public class ClassLoaderTest {
assertEquals("qwertyui", loadResource("8")); assertEquals("qwertyui", loadResource("8"));
} }
@Test
public void returnsNullForNonExistentResource() {
InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream("non-existent-resource.txt");
assertNull(input);
}
private static String loadResource(String name) { private static String loadResource(String name) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader(); ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream( try (BufferedReader reader = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(