Skip to content

Commit

Permalink
gh-31 unit tests now pass after adding pmfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew O. Smith committed Mar 13, 2015
1 parent 053d2d1 commit 6480c53
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,70 @@ import java.util.logging.ConsoleHandler;
class MalabarServer {
def cache = [:];
def config = [ cache :cache ];

def MAVEN = 'maven';


def standardizeParams(params) {
Map rtnval = params;

if( rtnval['pm'] == null ) {
throw new IllegalArgumentException("pm is required");
}

if( rtnval['repo'] == null ) {
rtnval['repo'] = MalabarUtil.expandFile("~/.m2/repository/");
}

if(rtnval['pmfile'] != null ) {
rtnval['pmfile'] = MalabarUtil.expandFile(rtnval['pmfile']);
} else {
throw new IllegalArgumentException("pmfile is required");
}


return rtnval;

}

def start(String port) {

def mph = new MavenProjectHandler(config);
def addr = new InetSocketAddress(Integer.parseInt(port))
def httpServer = com.sun.net.httpserver.HttpServer.create(addr, 0)

def context = httpServer.createContext('/pi/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.projectInfo(params["repo"], pm);}));
def context = httpServer.createContext('/pi/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
mph.projectInfo(params["repo"], params['pm'], params['pmfile']);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/parse/', new JsonHandlerFactory(config).build({params ->
String pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.parse(params["repo"], pm, params["script"], params["scriptBody"],
params['parser']);}));
context = httpServer.createContext('/parse/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
mph.parse(params["repo"], params['pm'], params['pmfile'],
params["script"], params["scriptBody"], params['parser']);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/test/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.unitTest(params["repo"], pm, params["script"], params["method"],
params['parser']);}));
context = httpServer.createContext('/test/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
mph.unitTest(params["repo"], params['pm'], params['pmfile'],
params["script"], params["method"], params['parser']);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/exec/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.exec(params["repo"], pm, params["class"], params["arg"]);}));
context = httpServer.createContext('/exec/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
mph.exec(params["repo"], params['pm'], params['pmfile'],
params["class"], params["arg"]);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/tags/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.tags(params["repo"], pm, params["class"]);}));
context = httpServer.createContext('/tags/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
mph.tags(params["repo"], params['pm'], params['pmfile'],
params["class"]);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/debug/', new JsonHandlerFactory(config).build({params ->
context = httpServer.createContext('/debug/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
def lm = LogManager.getLogManager();
lm.loggerNames.each( { if( it.startsWith("com.software_ninja")) {
def l = lm.getLogger(it);
Expand All @@ -69,12 +93,12 @@ class MalabarServer {

def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.debug(params["repo"], pm)}));
mph.debug(params["repo"], params['pm'], params['pmfile'])}));

context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/spawn/', new JsonHandlerFactory(config).build({params ->

//def params = standardizeParams(paramsIn);
com.software_ninja.malabar.lang.NewVM.startSecondJVM(params["version"], params["jdk"],
params["port"], params["cwd"],
true);
Expand All @@ -85,10 +109,10 @@ class MalabarServer {
"class" : params['class']] }));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/resource/', new JsonHandlerFactory(config).build({params ->
def pmIn = params["pm"];
def pm = (pmIn == null ? null : MalabarUtil.expandFile(pmIn));
mph.resource(params["repo"], pm, params["pattern"], params["max"] as int, params['isClass'] as boolean,
context = httpServer.createContext('/resource/', new JsonHandlerFactory(config).build({paramsIn ->
def params = standardizeParams(paramsIn);
mph.resource(params["repo"], params['pm'], params['pmfile'],
params["pattern"], params["max"] as int, params['isClass'] as boolean,
params['useRegex'] as boolean);}));
context.getFilters().add(new ParameterFilter());

Expand All @@ -97,7 +121,7 @@ class MalabarServer {
mph.additionalClasspath(params["relative"], params["absolute"]);}));
context.getFilters().add(new ParameterFilter());

context = httpServer.createContext('/stop/', new JsonHandlerFactory(config).build({params -> httpServer.stop(1); System.exit(0); }));
context = httpServer.createContext('/stop/', new JsonHandlerFactory(config).build({paramsIn -> httpServer.stop(1); System.exit(0); }));
context.getFilters().add(new ParameterFilter());


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,22 @@ public class MavenProjectHandler {
if( f != null ) f.clear();
}

def lookInCache(pom, func) {
def lookInCache(pm, pmfile, func) {
def name = this.getClass().getName();
def pomFile = new File(MalabarUtil.expandFile(pom as String));
def pomFile = new File(MalabarUtil.expandFile(pmfile as String));
def mod = pomFile.lastModified();
def cache1 = cache[name];
if(cache1 == null) {
cache1 = [:]
cache.put(name, cache1);
}

def rtnval = cache1[pom];
def key = [pm, pmfile];
def rtnval = cache1[key];

if(rtnval == null || rtnval['timestamp'] != mod) {
log.fine("cache miss:" + pm + " " + pmfile);
rtnval = createCacheEntry(pomFile, mod , func);
cache[name].put( pom , rtnval);
cache[name].put( key , rtnval);
}
return rtnval;
}
Expand Down Expand Up @@ -231,12 +232,12 @@ public class MavenProjectHandler {
/**
* Load and execute the class. Assumes the class has been compiled/parsed already.
*/
def exec(repo, pom, clazzName, args) {
def exec(repo, pm, pmfile, clazzName, args) {
log.fine "Start Exec of " + clazzName;

try{

def cached = lookInCache( pom, { fecthProjectInfo(repo, pom)});
def cached = lookInCache( pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)});

def cl = cached['classLoader'];
def clazz = Class.forName(clazzName, true, cl);
Expand All @@ -250,11 +251,11 @@ public class MavenProjectHandler {
/**
* Parse the script on disk. Return errors as a list
*/
def parse(repo, pom, scriptIn, scriptBody, parserName) {
def parse(repo, pm, pmfile, scriptIn, scriptBody, parserName) {
log.fine "Start Parse";
try{

def cached = lookInCache( pom, { fecthProjectInfo(repo, pom)});
def cached = lookInCache( pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)});

def parser = cached['parsers'][parserName];
def rtnval = null;
Expand Down Expand Up @@ -282,9 +283,9 @@ public class MavenProjectHandler {
* Run a unit test. Return a list of failures.
*/

def unitTest (repo, pm, scriptIn, method, parserName) {
def unitTest (repo, pm, pmfile, scriptIn, method, parserName) {
String script = MalabarUtil.expandFile(scriptIn);
def cached = lookInCache( pm, { fecthProjectInfo(repo, pm)});
def cached = lookInCache( pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)});
try{
def parser = cached['parsers'][parserName];
def parseResult = parser.parse(new File(script));
Expand Down Expand Up @@ -329,8 +330,8 @@ public class MavenProjectHandler {
* @param isClass If null or true, look for only class names
* @param useRegex If null or true, treat pattern as a regex
*/
def resource(repo, pm, pattern, max, isClass, useRegex){
def cached = lookInCache( pm, { fecthProjectInfo(repo, pm)});
def resource(repo, pm, pmfile, pattern, max, isClass, useRegex){
def cached = lookInCache( pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)});
def resourceCache = cached['resourceCache'];
log.fine "RESOURCE:" + resourceCache + " " + isClass + " " + useRegex + " " + max;
if( isClass == null || isClass ){
Expand All @@ -350,8 +351,8 @@ public class MavenProjectHandler {
* @param isClass If null or true, look for only class names
* @param useRegex If null or true, treat pattern as a regex
*/
def tags(repo, pm, className){
def cached = lookInCache( pm, { fecthProjectInfo(repo, pm)});
def tags(repo, pm, pmfile, className){
def cached = lookInCache( pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)});
def classLoader = cached.get('classLoader');
new SemanticReflector().asSemanticTag(classLoader.loadClass(className));

Expand All @@ -363,8 +364,8 @@ public class MavenProjectHandler {
*
*/

def debug(repo, pm){
def cached = lookInCache( pm, { fecthProjectInfo(repo, pm)});
def debug(repo, pm, pmfile){
def cached = lookInCache( pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)});
def classLoader = cached.get('classLoader');
[ classpath : classLoader.classPath,
projectInfo : cached['projectInfo'],
Expand All @@ -379,12 +380,12 @@ public class MavenProjectHandler {
// Project Info
//

def fecthProjectInfo = { repo, pom ->
def fecthProjectInfo = { repo, pm, pmfile ->
def repox = (repo == null ? "~/.m2/repository" : repo);

try {
def x = new MavenProjectsCreator();
def pjs = x.create(MalabarUtil.expandFile(repox), MalabarUtil.expandFile(pom))
def pjs = x.create(MalabarUtil.expandFile(repox), MalabarUtil.expandFile(pmfile))
return [runtime: x.resolveDependencies(pjs[0], repox, "runtime"),
systemProperties : System.getProperties(),
test: x.resolveDependencies(pjs[0], repox, "test")]
Expand All @@ -395,12 +396,12 @@ public class MavenProjectHandler {
ex.printStackTrace();
throw new Exception( ex.getMessage() + " repo:" +
MalabarUtil.expandFile(repox) +
" pom:" + MalabarUtil.expandFile(pom), ex);
" pmfile:" + MalabarUtil.expandFile(pmfile), ex);
}
}

def projectInfo(repo, pom) {
return lookInCache(pom, { fecthProjectInfo(repo, pom)} )['projectInfo'];
def projectInfo(repo, pm, pmfile) {
return lookInCache(pm, pmfile, { fecthProjectInfo(repo, pm, pmfile)} )['projectInfo'];
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class TestMalabarServer {

@Test
public void testPi(){
URL url = createGetURL("pi", [pm:root+"/src/test/resources/projects/simple/pom.xml"]);
URL url = createGetURL("pi", [pmfile:root+"/src/test/resources/projects/simple/pom.xml",
pm:'maven']);
println url
InputStream is = url.openStream();
println readInputStreamAsString(is);
Expand All @@ -97,13 +98,15 @@ class TestMalabarServer {

public
@Test void testParse(){
URL url = createGetURL("parse", [pm:root + "/src/test/resources/projects/simple/pom.xml",
URL url = createGetURL("parse", [pmfile:root + "/src/test/resources/projects/simple/pom.xml",
pm:'maven',
script: root + "/src/test/projects/simple/src/test/java/com/software_ninja/test/project/AppTest.java" ]);

println url
InputStream is = url.openStream();

url = createGetURL("parse", [pm : root + "/src/test/resources/projects/simple/pom.xml",
url = createGetURL("parse", [pmfile : root + "/src/test/resources/projects/simple/pom.xml",
pm : 'maven',
scriptBody: "x=;" ]);
is = url.openStream();
println readInputStreamAsString(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public class TestGroovyParser {

String simple = 'src/test/resources/projects/simple/';
String scriptIn = simple + '/src/test/java/com/software_ninja/test/project/AppTest.java';
String pm = simple + "pom.xml";
String pmfile = simple + "pom.xml";
String pm = 'maven';
String repo = "~/.m2/repository";

def mavenProjectHandler = new MavenProjectHandler([cache:[:]]);
def cacheEntry = mavenProjectHandler.lookInCache( pm, { mavenProjectHandler.fecthProjectInfo(repo, pm)});
def cacheEntry = mavenProjectHandler.lookInCache( pm, pmfile, { mavenProjectHandler.fecthProjectInfo(repo, pm, pmfile)});


def groovyParser = cacheEntry['parsers']['groovy'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public class TestJavaParser {
String simple = 'src/test/resources/projects/simple/';
String scriptIn = simple + '/src/test/java/com/software_ninja/test/project/AppTest.java';
String errorScriptIn = simple + '/src/test/java/com/software_ninja/test/project/ParserTargetWithError.java';
String pm = simple + "pom.xml";
String pmfile = simple + "pom.xml";
String pm = 'maven';
String repo = "~/.m2/repository";

def mavenProjectHandler = new MavenProjectHandler([cache:[:]]);
def cacheEntry = mavenProjectHandler.lookInCache( pm, { mavenProjectHandler.fecthProjectInfo(repo, pm)});
def cacheEntry = mavenProjectHandler.lookInCache( pm,pmfile, { mavenProjectHandler.fecthProjectInfo(repo, pm, pmfile)});


def javaParser = cacheEntry['parsers']['java'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class TestExecImpl {
private Map config;
private MavenProjectHandler mph;
private String defaultRepo = System.getProperty("user.home") + "/.m2/repository";
private String pm = 'maven';

@Before
public void init() {
Expand All @@ -26,13 +27,13 @@ public class TestExecImpl {
public void testExec() {
String simple = 'src/test/resources/projects/simple/';
String scriptIn = simple + '/src/test/java/com/software_ninja/test/project/App.java';
String pm = simple + "pom.xml";
String pmfile = simple + "pom.xml";

String clazzName = "com.software_ninja.test.project.App";
println 'http://localhost:4429/exec/?pm=' + pm +'&class=' + clazzName + "&repo=" + defaultRepo;
mph.parse(defaultRepo, pm, scriptIn, null, "java");
mph.exec (defaultRepo, pm, clazzName, null);
mph.exec (defaultRepo, pm, clazzName, ["a", "b", "c"] as String[]);
//println 'http://localhost:4429/exec/?pm=' + pm +'&class=' + clazzName + "&repo=" + defaultRepo;
mph.parse(defaultRepo, pm, pmfile, scriptIn, null, "java");
mph.exec (defaultRepo, pm, pmfile, clazzName, null);
mph.exec (defaultRepo, pm, pmfile, clazzName, ["a", "b", "c"] as String[]);
}

}
Expand Down
Loading

0 comments on commit 6480c53

Please sign in to comment.