mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
Rename the class more generic
This commit is contained in:
parent
7a49b9fda9
commit
8eca0b491a
@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -32,13 +33,17 @@ import org.junit.rules.TemporaryFolder;
|
|||||||
*
|
*
|
||||||
* @author Volker Berlin
|
* @author Volker Berlin
|
||||||
*/
|
*/
|
||||||
public class WasmNodeRule extends TemporaryFolder {
|
public class WasmRule extends TemporaryFolder {
|
||||||
|
|
||||||
private final Class<?>[] classes;
|
private static final SpiderMonkey spiderMonkey = new SpiderMonkey();
|
||||||
|
|
||||||
private File wasmFile;
|
private final Class<?>[] classes;
|
||||||
|
|
||||||
private File scriptFile;
|
private File wasmFile;
|
||||||
|
|
||||||
|
private File nodeScript;
|
||||||
|
|
||||||
|
private File spiderMonkeyScript;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the given classes to a Wasm and save it to a file.
|
* Compile the given classes to a Wasm and save it to a file.
|
||||||
@ -46,7 +51,7 @@ public class WasmNodeRule extends TemporaryFolder {
|
|||||||
* @param classes
|
* @param classes
|
||||||
* list of classes to compile
|
* list of classes to compile
|
||||||
*/
|
*/
|
||||||
public WasmNodeRule( Class<?>... classes ) {
|
public WasmRule( Class<?>... classes ) {
|
||||||
if( classes == null || classes.length == 0 ) {
|
if( classes == null || classes.length == 0 ) {
|
||||||
throw new IllegalArgumentException( "You need to set minimum one test class" );
|
throw new IllegalArgumentException( "You need to set minimum one test class" );
|
||||||
}
|
}
|
||||||
@ -68,28 +73,46 @@ public class WasmNodeRule extends TemporaryFolder {
|
|||||||
}
|
}
|
||||||
wasm.compileToBinary( wasmFile );
|
wasm.compileToBinary( wasmFile );
|
||||||
|
|
||||||
scriptFile = newFile( "test.js" );
|
nodeScript = createScript( "nodetest.js" );
|
||||||
URL scriptUrl = getClass().getResource( "nodetest.js" );
|
spiderMonkeyScript = createScript( "SpiderMonkeyTest.js" );
|
||||||
String expected = readStream( scriptUrl.openStream() );
|
|
||||||
expected = expected.replace( "{test.wasm}", wasmFile.getAbsolutePath().replace( '\\', '/' ) );
|
|
||||||
try (FileOutputStream scriptStream = new FileOutputStream( scriptFile )) {
|
|
||||||
scriptStream.write( expected.getBytes( StandardCharsets.UTF_8 ) );
|
|
||||||
}
|
|
||||||
} catch( Exception ex ) {
|
} catch( Exception ex ) {
|
||||||
throwException( ex );
|
throwException( ex );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load a script resource, patch it and save it
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* the resource name
|
||||||
|
* @return The saved file name
|
||||||
|
* @throws IOException
|
||||||
|
* if any IO error occur
|
||||||
|
*/
|
||||||
|
private File createScript( String name ) throws IOException {
|
||||||
|
File file = newFile( name );
|
||||||
|
URL scriptUrl = getClass().getResource( name );
|
||||||
|
String expected = readStream( scriptUrl.openStream() );
|
||||||
|
expected = expected.replace( "{test.wasm}", wasmFile.getName() );
|
||||||
|
try (FileOutputStream scriptStream = new FileOutputStream( file )) {
|
||||||
|
scriptStream.write( expected.getBytes( StandardCharsets.UTF_8 ) );
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a test single test. It run the method in Java and call it via node in the WenAssembly. If the result are
|
* Run a test single test. It run the method in Java and call it via node in the WenAssembly. If the result are
|
||||||
* different it fire an error.
|
* different it fire an error.
|
||||||
*
|
*
|
||||||
* @param methodName
|
* @param methodName
|
||||||
* the method name of the test.
|
* the method name of the test.
|
||||||
|
* @param script
|
||||||
|
* The script engine
|
||||||
* @param params
|
* @param params
|
||||||
* the parameters for the method
|
* the parameters for the method
|
||||||
*/
|
*/
|
||||||
public void test( String methodName, Object... params ) {
|
public void test( ScriptEngine script, String methodName, Object... params ) {
|
||||||
|
Object expected;
|
||||||
try {
|
try {
|
||||||
Class<?>[] types = new Class[params.length];
|
Class<?>[] types = new Class[params.length];
|
||||||
for( int i = 0; i < types.length; i++ ) {
|
for( int i = 0; i < types.length; i++ ) {
|
||||||
@ -113,29 +136,60 @@ public class WasmNodeRule extends TemporaryFolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
method.setAccessible( true );
|
method.setAccessible( true );
|
||||||
Object expected = method.invoke( null, params );
|
expected = method.invoke( null, params );
|
||||||
|
|
||||||
String command = System.getProperty( "node.dir" );
|
ProcessBuilder processBuilder;
|
||||||
command = command == null ? "node" : command + "/node";
|
switch( script ) {
|
||||||
ProcessBuilder processBuilder =
|
case SpiderMonkey:
|
||||||
new ProcessBuilder( command, scriptFile.getAbsolutePath(), methodName );
|
processBuilder = spiderMonkeyCommand();
|
||||||
|
break;
|
||||||
|
case NodeJS:
|
||||||
|
processBuilder = nodeJsCommand();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException( script.toString() );
|
||||||
|
}
|
||||||
|
processBuilder.command().add( methodName );
|
||||||
for( int i = 0; i < params.length; i++ ) {
|
for( int i = 0; i < params.length; i++ ) {
|
||||||
processBuilder.command().add( String.valueOf( params[i] ) );
|
processBuilder.command().add( String.valueOf( params[i] ) );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
processBuilder.directory( getRoot() );
|
||||||
Process process = processBuilder.start();
|
Process process = processBuilder.start();
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
String result = readStream( process.getInputStream() );
|
String actual = readStream( process.getInputStream() ).trim();
|
||||||
if( exitCode != 0 ) {
|
if( exitCode != 0 ) {
|
||||||
String errorMessage = readStream( process.getErrorStream() );
|
String errorMessage = readStream( process.getErrorStream() );
|
||||||
assertEquals( errorMessage, 0, exitCode );
|
assertEquals( errorMessage, 0, exitCode );
|
||||||
}
|
}
|
||||||
assertEquals( String.valueOf( expected ), result );
|
assertEquals( String.valueOf( expected ), actual );
|
||||||
} catch( Exception ex ) {
|
} catch( Exception ex ) {
|
||||||
throwException( ex );
|
throwException( ex );
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a ProcessBuilder for spider monkey script shell.
|
||||||
|
*
|
||||||
|
* @return the value from the script
|
||||||
|
* @throws IOException
|
||||||
|
* if the download failed
|
||||||
|
*/
|
||||||
|
private ProcessBuilder spiderMonkeyCommand() throws IOException {
|
||||||
|
return new ProcessBuilder( spiderMonkey.getCommand(), spiderMonkeyScript.getAbsolutePath() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a ProcessBuilder for node.js
|
||||||
|
*
|
||||||
|
* @return the value from the script
|
||||||
|
*/
|
||||||
|
private ProcessBuilder nodeJsCommand() {
|
||||||
|
String command = System.getProperty( "node.dir" );
|
||||||
|
command = command == null ? "node" : command + "/node";
|
||||||
|
return new ProcessBuilder( command, nodeScript.getAbsolutePath() );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a stream into a String.
|
* Reads a stream into a String.
|
||||||
*
|
*
|
||||||
@ -144,7 +198,7 @@ public class WasmNodeRule extends TemporaryFolder {
|
|||||||
* @return the string
|
* @return the string
|
||||||
*/
|
*/
|
||||||
public static String readStream( InputStream input ) {
|
public static String readStream( InputStream input ) {
|
||||||
try (Scanner scanner = new Scanner( input ).useDelimiter( "\\A" ) ) {
|
try (Scanner scanner = new Scanner( input ).useDelimiter( "\\A" )) {
|
||||||
return scanner.hasNext() ? scanner.next() : "";
|
return scanner.hasNext() ? scanner.next() : "";
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user