Skip to content

Commit

Permalink
feat(core,cmds,tests): fix bindings, configuration, tests, update httpd
Browse files Browse the repository at this point in the history
  • Loading branch information
giflw committed Feb 28, 2024
1 parent 9c2dee5 commit 5432a3c
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class ConfigurationCommand extends AbstractCommand implements CallableCommand<Co
throw new JMacroException("Property missing redirect is disabled in configuration")
}

def methodMissing(String name, def args) {
scriptLogger.debug("Calling method '$name' missing on configuration")
return configuration.invokeMethod(name, args)
}

@Override
Iterator iterator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ class Httpd implements AutoCloseable {
this.stop()
}

boolean isReady() {
return this.server.jettyServer().server().isStarted();
}

Httpd waitBeReady() {
while(!this.isReady()) {
Thread.sleep(25);
}
return this
}

Httpd exit(String route = '/exit', String message = 'Bye') {
exit(route, { ctx -> ctx.json([message: message]) })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public void close() {
@Override
public void onShutdown() {
try {
getScriptLogger().warn("Closing all HTTPD instances as we are shutting down)");
this.close();
if (!this.servers.isEmpty()) {
getScriptLogger().warn("Closing all HTTPD instances as we are shutting down)");
this.close();
}
} catch (Exception e) {
getScriptLogger().error("Error closing HTTPD instances on shutdown", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,4 @@ default Object propertyMissing(String name) {
return CommandUtils.propertyMissingOn(this.getBindings(), name);
}

default Object methodMissing(String name, Object args) {
return CommandUtils.callMethodAliasOrOnBindings(this, name, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public interface CommandProvider<C extends AbstractCommand> {
* @return Command new instance.
*/
C getCommand(Core core, ScriptEngineAware scriptEngineAware);

default String getName() {
return Command.nameOf(this.getCommandType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ public Map<String, ScriptEngineFactory> getEngines() {

context.setWriter(new PrintWriter(System.out));

// FIXME where this is used??? tests break without this!!!
var globalScope = context.getBindings(GLOBAL_SCOPE);
if (globalScope == null) {
context.setBindings(engine.createBindings(), ScriptContext.GLOBAL_SCOPE);
globalScope = context.getBindings(GLOBAL_SCOPE);
}

var engineScope = context.getBindings(ENGINE_SCOPE);
Expand All @@ -223,12 +223,12 @@ public Map<String, ScriptEngineFactory> getEngines() {
"@" + scriptPath.substring(scriptPath.lastIndexOf('.') + 1);
final var scriptLogger = LogManager.getLogger(loggerName);

globalScope.put("__MAIN__", normalExecution);
globalScope.put("id", id);
globalScope.put("uuid", UUID.randomUUID());
globalScope.put("logger", scriptLogger);
engineScope.put("__MAIN__", normalExecution);
engineScope.put("id", id);
engineScope.put("uuid", UUID.randomUUID());
engineScope.put("logger", scriptLogger);

ScriptEngineAware scriptEngineAware = ScriptEngineAware.of(globalScope, engine, scriptLogger);
ScriptEngineAware scriptEngineAware = ScriptEngineAware.of(engineScope, engine, scriptLogger);

if (engine instanceof GroovyScriptEngineImpl) {
engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "weak", ENGINE_SCOPE);
Expand Down
1 change: 0 additions & 1 deletion jmacro-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
<scope>test</scope>
</dependency>
<dependency>
<!-- FIXME move def to BOM -->
<groupId>com.athaydes</groupId>
<artifactId>spock-reports</artifactId>
<scope>test</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.itquasar.multiverse.jmacro.commands


import com.itquasar.multiverse.jmacro.core.command.CommandProvider
import com.itquasar.multiverse.jmacro.core.configuration.Configuration
import com.itquasar.multiverse.jmacro.core.engine.Core
Expand All @@ -10,13 +11,14 @@ import com.itquasar.multiverse.jmacro.core.script.Script
import com.itquasar.multiverse.jmacro.core.script.ScriptResult
import com.itquasar.multiverse.jmacro.core.util.SPILoader
import groovy.util.logging.Log4j2
import org.opentest4j.TestSkippedException
import spock.lang.Specification
import spock.lang.Unroll

@Log4j2
class CommandSpec extends Specification {

private static final String singleCommandTest = null
private static final String singleCommandTest = "configuration"

Core core

Expand All @@ -35,7 +37,7 @@ class CommandSpec extends Specification {
}

@Unroll
void "Command [#provider.name] of type [#provider.commandType.simpleName]"(CommandProvider provider) {
void 'Command #provider.name of type #provider.commandType.simpleName'(CommandProvider provider) {
when: "Script found"
Script script = loadScript(provider.name)
ScriptResult result = core.engine.execute(script, "foo=bar", "--foo", "bar=false")
Expand All @@ -57,21 +59,21 @@ class CommandSpec extends Specification {

where: "Command providers loaded through Java SPI"
provider << SPILoader.load(CommandProvider.class).toList().findAll {
(singleCommandTest != null && singleCommandTest == it.name)
||
(singleCommandTest == null && ((it.name == 'tn3270' && System.getProperty('tn3270j.url') != null) || it.name != 'tn3270'))
(singleCommandTest != null && singleCommandTest == it.name) || (singleCommandTest == null && ((it.name == 'tn3270' && System.getProperty('tn3270j.url') != null) || it.name != 'tn3270'))
}
}

static Script loadScript(String basename) {
String filename = basename + ".groovy"
URL location = CommandSpec.class.getResource("/tests/commands/" + filename)
if (location == null) {
throw new IllegalArgumentException("Could not find $filename")
log.error("Could not find $filename")
throw new TestSkippedException("Command ${basename} (${filename}) have no test")
}
String source = location.text
Metadata metadata = Metadata.extractMetadata(source)
return new Script(metadata, 'command-spec', filename, location.toString(), source)

}

static class CommandTestRepository implements ScriptRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def server = httpd {
it.get('/foo', ctx -> ctx.result(it.config.toString()))
}

pause 1
server.waitBeReady()

def req = request {
it.GET "${server.url}foo"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
START METADATA
name: NUMBERUTILS
infos:
expectedResult: 123.4
END METADATA
*/

result(n("123.4"))
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def server = httpd {
it.post('/foo', ctx -> ctx.result(ctx.header("Some-Header") + "#" + ctx.formParam("user") + ":" + ctx.formParam("senha")))
}

pause 1
server.waitBeReady()

def req = request {
it.with {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
START METADATA
name: WRAPPING
infos:
expectedResult: "org.apache.logging.log4j.core.Logger"
END METADATA
*/

result(logger.class.name)
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<version>0.1.0-SNAPSHOT</version>

<name>JMacro</name>
<description>JMacro parent pom</description>
<!-- JScrorm ??? J SCRipt platfORM-->
<description>JMacro Script Platform</description>

<pluginRepositories>
<pluginRepository>
Expand Down

0 comments on commit 5432a3c

Please sign in to comment.