use of com.disney.groovity.source.GroovitySourceLocator in project groovity by disney.
the class GroovityTestMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
try {
if (skip) {
return;
}
getLog().info("STARTING Groovity test");
populateSystemProperties();
ClassLoader testLoader = createClassLoader(ClassLoaderScope.TEST);
GroovityServletContainerBuilder builder = new GroovityServletContainerBuilder().setPort(Integer.valueOf(port)).setWebappDirectory(new File(project.getBasedir(), "src/main/webapp")).setClassLoader(testLoader);
if (groovitySourceDirectory != null && groovitySourceDirectory.exists()) {
builder.addSourceLocation(groovitySourceDirectory.toURI(), true);
}
if (groovityTestDirectory != null && groovityTestDirectory.exists()) {
builder.addSourceLocation(groovityTestDirectory.toURI(), true);
}
if (sources != null) {
for (File source : sources) {
builder.addSourceLocation(source.toURI(), true);
}
}
GroovityStatistics.reset();
GroovityServletContainer container = builder.build();
container.start();
Groovity groovity = container.getGroovity();
ArrayList<String> appSources = new ArrayList<String>();
try {
if (failOnError) {
validateFactory(groovity);
}
if (skipTests) {
return;
}
GroovitySourceLocator[] sourceLocators = groovity.getSourceLocators();
for (GroovitySourceLocator locator : sourceLocators) {
if (!interactive && ((FileGroovitySourceLocator) locator).getDirectory().equals(groovityTestDirectory)) {
if (path != null) {
String[] pathParts = path.split("\\s*,\\s*");
for (String pathPart : pathParts) {
container.run(pathPart);
}
} else {
for (GroovitySource source : locator) {
String scriptPath = source.getPath();
scriptPath = scriptPath.substring(0, scriptPath.lastIndexOf("."));
String scriptName = scriptPath.substring(scriptPath.lastIndexOf('/') + 1);
if (scriptName.startsWith("test")) {
container.run(scriptPath);
}
}
}
}
if (((FileGroovitySourceLocator) locator).getDirectory().equals(groovitySourceDirectory)) {
for (GroovitySource source : locator) {
appSources.add(source.getPath());
}
}
}
if (interactive) {
// in interactive mode we wait for instructions and compile each time to allow
// real-time development
container.enterConsole();
}
} finally {
container.stop();
}
Map<String, CodeCoverage> coverageMap = new TreeMap<String, GroovityTestMojo.CodeCoverage>();
Collection<Class<Script>> scriptClasses = groovity.getGroovityScriptClasses();
for (Class<Script> sc : scriptClasses) {
String sourcePath = groovity.getSourcePath(sc);
if (appSources.contains(sourcePath)) {
String scriptLabel = sourcePath.substring(0, sourcePath.length() - 5);
CodeCoverage cc = new CodeCoverage(sc, scriptLabel);
if (cc.isCoverable()) {
coverageMap.put(scriptLabel, cc);
}
for (Class<?> c : ((GroovityClassLoader) sc.getClassLoader()).getLoadedClasses()) {
if (!c.equals(sc) && !(Closure.class.isAssignableFrom(c)) && !c.isInterface()) {
String cname = getClassLabel(c);
int p = 0;
if ((p = cname.indexOf("$Trait")) > 0) {
cname = cname.substring(0, p);
}
String classLabel = scriptLabel + "->" + cname;
CodeCoverage icc = new CodeCoverage(c, classLabel);
if (icc.isCoverable()) {
coverageMap.put(classLabel, icc);
}
}
}
}
}
List<Statistics> stats = GroovityStatistics.getStatistics();
for (Statistics stat : stats) {
String sks = stat.key.toString();
int dot = sks.indexOf(".");
if (dot > 0) {
String className = sks.substring(0, dot);
String method = sks.substring(dot + 1);
CodeCoverage cc = coverageMap.get(className);
if (cc != null) {
if (method.equals("run()") && cc.runnable) {
cc.ran = true;
} else {
if (cc.methods.containsKey(method)) {
cc.methods.put(method, true);
}
}
}
}
}
Collection<CodeCoverage> ccs = coverageMap.values();
double total = 0;
for (CodeCoverage cc : ccs) {
total += cc.getCoverage();
}
total /= ccs.size();
getLog().info("TEST COVERAGE " + ((int) (100 * total)) + "% TOTAL");
for (Entry<String, CodeCoverage> entry : coverageMap.entrySet()) {
CodeCoverage cc = entry.getValue();
double covered = cc.getCoverage();
getLog().info(" " + ((int) (100 * covered)) + "% coverage for " + cc.label);
if (covered < 1.0) {
if (cc.runnable && !cc.ran) {
getLog().warn(" Script body did not run during tests");
}
List<String> uncovered = cc.getUncoveredMethods();
if (!uncovered.isEmpty()) {
for (String m : cc.getUncoveredMethods()) {
getLog().warn(" " + m + " did not execute during tests");
}
}
}
/*
for(String m: cc.getCoveredMethods()) {
getLog().info(" " + m + " executed during tests");
}
*/
}
} catch (MojoFailureException e) {
throw e;
} catch (Throwable e) {
getLog().error("ERROR in Groovity test", e);
throw new MojoFailureException(e.getMessage());
}
}
use of com.disney.groovity.source.GroovitySourceLocator in project groovity by disney.
the class Groovity method start.
protected void start() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
log.info("Initializing Groovity");
if (httpClient == null) {
httpClient = HttpClients.createDefault();
}
if (argsLookup == null) {
argsLookup = new ArgsLookup();
}
asyncExecutor = Executors.newFixedThreadPool(asyncThreads, new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Groovity Async " + t.getName());
return t;
}
});
cacheRefreshExecutor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 4, new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Groovity Cache " + t.getName());
return t;
}
});
cacheRefreshExecutor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
GroovityStatistics.warnStuckThreads();
}
}, 1, 1, TimeUnit.MINUTES);
cacheTimeExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Groovity Cache Time " + t.getName());
return t;
}
});
cacheTimeExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Caches.currentCacheTime = (System.currentTimeMillis() / 1000);
}
}, 1, 1, TimeUnit.SECONDS);
interruptFactory = new InterruptFactory();
// intialize configurator first so class inits can pick up system properties
if (configurator != null) {
configurator.init();
}
init(true);
if (configurator != null) {
// initialize configurator again to pick up any changes to system properties that happened during class init
configurator.init();
// schedule 10-second configuration refresh
configExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("Groovity Config Applier " + t.getName());
return t;
}
});
configExecutor.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
configureAll();
} catch (Exception e) {
log.log(Level.SEVERE, "Error updating configuration ", e);
}
}
}, 10, 10, TimeUnit.SECONDS);
configureAll();
}
// now start all scripts
for (Class<?> c : getGroovityScriptClasses()) {
startClass(c);
}
// init locators AFTER compile, to avoid scheduled compilation starting before startup is complete
if (sourceLocators != null && sourceLocators.length > 0) {
for (GroovitySourceLocator locator : sourceLocators) {
locator.addSourceListener(this.groovitySourceListener);
locator.init();
}
}
started.set(true);
}
use of com.disney.groovity.source.GroovitySourceLocator in project groovity by disney.
the class Groovity method getChangedSources.
public List<GroovitySource> getChangedSources() {
ArrayList<GroovitySource> changedSources = new ArrayList<GroovitySource>();
if (sourceLocators != null) {
ArrayList<String> currentScripts = new ArrayList<String>(((Map<String, Long>) scriptDates).keySet());
for (GroovitySourceLocator sourceLocator : sourceLocators) {
for (GroovitySource source : sourceLocator) {
String key = fixCase(source.getPath());
if (key.endsWith(GROOVITY_SOURCE_EXTENSION)) {
key = key.substring(0, key.length() - 5);
}
if (scriptDates.containsKey(key)) {
currentScripts.remove(key);
if (!source.exists() || source.getLastModified() != scriptDates.get(key)) {
changedSources.add(source);
}
} else if (source.exists()) {
changedSources.add(source);
}
}
}
for (final String script : currentScripts) {
// these have been deleted
Class<Script> delClass = scripts.get(script);
final String path = getSourcePath(delClass);
changedSources.add(new GroovitySource() {
public String getSourceCode() throws IOException {
return "";
}
public String getPath() {
return path;
}
public long getLastModified() {
return System.currentTimeMillis();
}
public boolean exists() {
return false;
}
});
}
}
return changedSources;
}
use of com.disney.groovity.source.GroovitySourceLocator in project groovity by disney.
the class Groovity method compileAll.
/**
* Compile all scripts known to the configured source locators
*
* @param force if true, recompile all sources, if false recompile only changed sources
*/
public void compileAll(boolean force, boolean init) {
if (inCompile.compareAndSet(false, true)) {
try {
compileEvents.clear();
List<GroovitySource> sources = new ArrayList<GroovitySource>();
// track existing views so we can keep track of files that may have since been deleted
HashSet<String> oldViews = new HashSet<String>(((Map<String, Class<Script>>) scripts).keySet());
for (GroovitySourceLocator sourceLocator : sourceLocators) {
for (GroovitySource source : sourceLocator) {
try {
Matcher matcher = sourcePattern.matcher(source.getPath());
if (matcher.matches()) {
String name = matcher.group(1);
oldViews.remove(fixCase(name));
sources.add(source);
}
} catch (Exception e) {
log.log(Level.SEVERE, "Unable to load source " + source.getPath(), e);
}
}
}
// remaining values in oldViews correspond to deleted files IF they are not embedded
for (String del : oldViews) {
Class<Script> cgs = scripts.get(del);
final String path = getSourcePath(cgs);
try {
sources.add(new GroovitySource() {
public String getSourceCode() throws IOException {
return "";
}
public String getPath() {
return path;
}
public long getLastModified() {
return System.currentTimeMillis();
}
public boolean exists() {
return false;
}
});
} catch (Exception e) {
log.log(Level.SEVERE, "Error deleting grvt " + del, e);
}
}
compile(force, init, sources.toArray(new GroovitySource[0]));
} finally {
inCompile.set(false);
}
} else {
throw new RuntimeException("Groovy compiler is already busy, please retry later");
}
}
use of com.disney.groovity.source.GroovitySourceLocator in project groovity by disney.
the class Groovity method compile.
/**
* Compile a named set of source paths; compilation also handles static init and destroy lifecycle
* management and jar file creation
*
* @param force if true, recompile all sources, if false recompile only changed sources
* @param sourcePaths a list of source paths to compile
* @throws Exception
*/
public void compile(boolean force, boolean init, List<String> sourcePaths) throws Exception {
if (inCompile.compareAndSet(false, true)) {
try {
compileEvents.clear();
List<GroovitySource> sources = new ArrayList<GroovitySource>();
for (int i = 0; i < sourcePaths.size(); i++) {
String path = sourcePaths.get(i);
try {
GroovitySource source = null;
Exception error = null;
for (GroovitySourceLocator sourceLocator : sourceLocators) {
try {
source = sourceLocator.getGroovityScriptSource(path);
if (source != null && source.exists()) {
break;
}
} catch (Exception e) {
error = e;
}
}
if (source != null) {
sources.add(source);
} else if (error != null) {
throw error;
} else {
throw new FileNotFoundException(path);
}
} catch (Exception e) {
log.log(Level.SEVERE, "Unable to load source " + path, e);
}
}
compile(force, init, sources.toArray(new GroovitySource[0]));
} finally {
inCompile.set(false);
}
} else {
throw new RuntimeException("Groovy compiler is already busy, please retry later");
}
}
Aggregations