diff --git a/grails-plugin-testing/src/main/groovy/grails/test/AbstractCliTestCase.groovy b/grails-plugin-testing/src/main/groovy/grails/test/AbstractCliTestCase.groovy deleted file mode 100644 index 3c6d8da0f56..00000000000 --- a/grails-plugin-testing/src/main/groovy/grails/test/AbstractCliTestCase.groovy +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright 2011 SpringSource - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package grails.test - -import grails.core.GrailsApplication -import grails.util.BuildSettings -import grails.util.Metadata - -import java.util.concurrent.TimeUnit -import java.util.concurrent.locks.Condition -import java.util.concurrent.locks.Lock -import java.util.concurrent.locks.ReentrantLock - -/** - * This abstract test case makes it easy to run a Grails command and - * query its output. It's currently configured via a set of system - * properties: - * - */ -abstract class AbstractCliTestCase extends GroovyTestCase { - static final String EOL = System.getProperty("line.separator") - - private final Lock lock = new ReentrantLock() - private final Condition condition = lock.newCondition() - private final Condition waiting = lock.newCondition() - - private String commandOutput - private String grailsHome = BuildSettings.GRAILS_HOME - private String grailsVersion = GrailsApplication.getPackage().getImplementationVersion() - private File workDir = new File(System.getProperty("grails.cli.work.dir") ?: ".") - private Process process - private boolean streamsProcessed - - File outputDir = new File(BuildSettings.TARGET_DIR ?: new File("target"), "cli-output") - long timeout = 2 * 60 * 1000 // min * sec/min * ms/sec - - /** - * Executes a Grails command. The path to the Grails script is - * inserted at the front, so the first element of command - * should be the name of the Grails command you want to start, - * e.g. "help" or "run-app". - * @param a list of command arguments (minus the Grails script/executable). - */ - protected void execute(List command) { - // Make sure the working and output directories exist before - // running the command. - workDir.mkdirs() - outputDir.mkdirs() - - // Add the path to the Grails script as the first element of - // the command. Note that we use an absolute path. - def cmd = new ArrayList(command.size() + 5) - def quoteArgs = false - if (windows) { - cmd << "cmd" << "/c" - cmd << "${grailsHome}\\bin\\grails.bat".toString() - quoteArgs = true - } - else { - cmd << "${grailsHome}/bin/grails".toString() - } - if (System.getProperty("grails.work.dir")) { - def arg = "-Dgrails.work.dir=${System.getProperty('grails.work.dir')}" - cmd.add (quoteArgs ? '"' + arg + '"' : arg.toString()) - } - cmd << "--plain-output" - cmd.addAll command.collect { quoteArgs ? '"' + it + '"' : it } - - // Prepare to execute Grails as a separate process in the - // configured working directory. - def pb = new ProcessBuilder(cmd) - pb.redirectErrorStream(true) - pb.directory(workDir) - pb.environment()["GRAILS_HOME"] = grailsHome - - process = pb.start() - - // Read the process output on a separate thread. This is - // necessary to deal with output that overflows the buffer - // and when a command requires user input at some stage. - final currProcess = process - Thread.startDaemon { - output = currProcess.in.getText("UTF-8") - - // Once we've finished reading the process output, signal - // the main thread. - signalDone() - } - } - - /** - * Returns the process output as a string. - */ - String getOutput() { - return commandOutput - } - - void setOutput(String output) { - this.commandOutput = output - } - - /** - * Returns the working directory for the current command. This - * may be the base working directory or a project. - */ - File getWorkDir() { - return workDir - } - - void setWorkDir(File dir) { - this.workDir = dir - } - - /** - * Allows you to provide user input for any commands that require - * it. In other words, you can run commands in interactive mode. - * For example, you could pass "app1" as the input parameter - * when running the "create-app" command. - */ - void enterInput(String input) { - process << input << EOL - } - - /** - * Waits for the current command to finish executing. It returns - * the exit code from the external process. It also dumps the - * process output into the "cli-tests/output" directory to aid - * debugging. - */ - int waitForProcess() { - // Interrupt the main thread if we hit the timeout. - final monitor = "monitor" - final mainThread = Thread.currentThread() - final timeout = this.timeout - final timeoutThread = Thread.startDaemon { - try { - Thread.sleep(timeout) - - // Timed out. Interrupt the main thread. - mainThread.interrupt() - } - catch (InterruptedException ex) { - // We're expecting this interruption. - } - } - - // First wait for the process to finish. - int code - try { - code = process.waitFor() - - // Process completed normally, so kill the timeout thread. - timeoutThread.interrupt() - } - catch (InterruptedException ex) { - code = 111 - - // The process won't finish, so we shouldn't wait for the - // output stream to be processed. - lock.lock() - streamsProcessed = true - lock.unlock() - - // Now kill the process since it appears to be stuck. - process.destroy() - } - - // Now wait for the stream reader threads to finish. - lock.lock() - try { - while (!streamsProcessed) condition.await(2, TimeUnit.MINUTES) - } - finally { - lock.unlock() - } - - // DEBUG - Dump the process output to a file. - int i = 1 - def outFile = new File(outputDir, "${getClass().simpleName}-out-${i}.txt") - while (outFile.exists()) { - i++ - outFile = new File(outputDir, "${getClass().simpleName}-out-${i}.txt") - } - outFile << commandOutput - // END DEBUG - - return code - } - - /** - * Signals any threads waiting on condition to inform them - * that the process output stream has been read. Should only be used - * by this class (not sub-classes). It's protected so that it can be - * called from the reader thread closure (some strange Groovy behaviour). - */ - protected void signalDone() { - // Signal waiting threads that we're done. - lock.lock() - try { - streamsProcessed = true - condition.signalAll() - } - finally { - lock.unlock() - } - } - - /** - * Checks that the output of the current command starts with the - * expected header, which includes the Grails version and the - * location of GRAILS_HOME. - */ - protected void verifyHeader() { - assert output.contains("|Loading Grails ${grailsVersion}") - } - - boolean isWindows() { - return System.getProperty("os.name").startsWith("Windows") - } -} diff --git a/grails-plugin-testing/src/main/groovy/grails/test/GroovyPagesTestCase.groovy b/grails-plugin-testing/src/main/groovy/grails/test/GroovyPagesTestCase.groovy deleted file mode 100644 index df102654a62..00000000000 --- a/grails-plugin-testing/src/main/groovy/grails/test/GroovyPagesTestCase.groovy +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2004-2005 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package grails.test - -import org.grails.buffer.GrailsPrintWriter -import org.grails.gsp.GroovyPagesTemplateEngine -import org.springframework.web.context.request.RequestContextHolder -/** - * A test harness that eases testing of GSP and tag libraries for Grails. - * - * @author Graeme Rocher - */ -class GroovyPagesTestCase extends GroovyTestCase { - - /** - * The GroovyPagesTemplateEngine which gets wired into this GSP. - */ - GroovyPagesTemplateEngine groovyPagesTemplateEngine - - /** - * Sets the controller name to use. Should be called to override the defaut "test" value. - */ - void setControllerName(String name) { - RequestContextHolder.currentRequestAttributes().controllerName = name - } - - /** - * Asserts the output of a given template against the specified expected value. - * - * @param expected The expected output - * @param template A snippet of GSP - * @param params An optional parameter that allows variables to be placed in the binding of the GSP - * @param transform An optional parameter that allows the specification of a closure to transform the passed StringWriter - */ - void assertOutputEquals(expected, template, params = [:], Closure transform = { it.toString() }) { - def sw = new StringWriter() - applyTemplate sw, template, params - assertEquals expected, transform(sw) - } - - /** - * Applies a GSP template and returns its output as a String. - * - * @param template The GSP template - * @param params An optional parameter that allows the specification of the binding - */ - String applyTemplate(template, params = [:]) { - def sw = new StringWriter() - applyTemplate sw, template, params - return sw.toString() - } - - void applyTemplate(StringWriter sw, template, params = [:]) { - def webRequest = RequestContextHolder.currentRequestAttributes() - def engine = groovyPagesTemplateEngine - - assert engine - def t = engine.createTemplate(template, "test_" + System.currentTimeMillis()) - - def w = t.make(params) - - def out = new GrailsPrintWriter(sw) - webRequest.out = out - w.writeTo(out) - } -}