Skip to content

Commit

Permalink
Issue pledbrook#44 WIP : cli-tests project bugfixs
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowsnow committed Jul 24, 2015
1 parent a91f8fc commit a1a0588
Show file tree
Hide file tree
Showing 1,068 changed files with 382 additions and 13 deletions.
1 change: 0 additions & 1 deletion settings.gradle

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -17,7 +17,7 @@ plugins {
}

version "0.1"
group "default"
group "cli.tests"

apply plugin: "spring-boot"
apply plugin: "war"
Expand Down Expand Up @@ -56,6 +56,7 @@ dependencies {
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.codehaus.groovy:groovy-test"

compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
Expand All @@ -71,6 +72,9 @@ dependencies {
testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'

console "org.grails:grails-console"

compile project(":grails-shiro")

}

task wrapper(type: Wrapper) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -9,13 +9,13 @@ import org.apache.shiro.web.util.WebUtils
class AuthController {
def shiroSecurityManager

def index = { redirect(action: "login", params: params) }
def index(){ redirect(action: "login", params: params) }

def login = {
def login(){
return [username: params.username, rememberMe: (params.rememberMe != null), targetUri: params.targetUri]
}

def signIn = {
def signIn(){
def authToken = new UsernamePasswordToken(params.username, params.password as String)

// Support for "remember me"
Expand Down Expand Up @@ -66,7 +66,7 @@ class AuthController {
}
}

def signOut = {
def signOut(){
// Log the user out of the application.
SecurityUtils.subject?.logout()
webRequest.getCurrentRequest().session = null
Expand All @@ -75,7 +75,7 @@ class AuthController {
redirect(uri: "/")
}

def unauthorized = {
def unauthorized(){
render "You do not have permission to access this page."
}
}
File renamed without changes.
Expand Up @@ -59,7 +59,6 @@
<ul>
<li>App version: <g:meta name="app.version"></g:meta></li>
<li>Grails version: <g:meta name="app.grails.version"></g:meta></li>
<li>Groovy version: ${org.codehaus.groovy.runtime.InvokerHelper.getVersion()}</li>
<li>JVM version: ${System.getProperty('java.version')}</li>
<li>Controllers: ${grailsApplication.controllerClasses.size()}</li>
<li>Domains: ${grailsApplication.domainClasses.size()}</li>
Expand Down
@@ -0,0 +1,2 @@
Error |
Error occurred running Grails CLI: No such property: includeTargets for class: ShiroQuickStart (Use --stacktrace to see the full trace)
@@ -0,0 +1,240 @@
/*
* 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.util.BuildSettings
import grails.util.GrailsUtil

import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock
import groovy.util.GroovyTestCase

/**
* 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:
* <ul>
* <li><tt>grails.home</tt> - location of Grails distribution to test</li>
* <li><tt>grails.version</tt> - version of Grails we're testing</li>
* <li><tt>grails.cli.work.dir</tt> - location of the test case's working directory</li>
* </ul>
*/
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 = System.getProperty("grails.home") ?: System.env.GRAILS_HOME
private String grailsVersion = System.getProperty("grails.version") ?: GrailsUtil.grailsVersion
private File workDir = new File(System.getProperty("grails.cli.work.dir") ?: ".")
private Process process
private boolean streamsProcessed

File outputDir = new File(BuildSettings.PROJECT_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 <tt>command</tt>
* 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<String> 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<String>(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.
println cmd
def pb = new ProcessBuilder(cmd)
pb.redirectErrorStream(true)
pb.directory(workDir)
def env = pb.environment()
assert env
env["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 <tt>input</tt> 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 <tt>condition</tt> 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")
}
}

0 comments on commit a1a0588

Please sign in to comment.