Skip to content

Commit

Permalink
Merge pull request #250 from brunobowden/testing
Browse files Browse the repository at this point in the history
CycleFinderTask and TranslateTask Unit Tests
  • Loading branch information
brunobowden committed Jun 28, 2015
2 parents c486032 + 9331f5a commit fc66add
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ These are the main tasks for the plugin:
j2objcXcode - Configure Xcode to link to static library and header files

Note that you can use the Gradle shorthand of "$ gradlew jA" to do the j2objcAssemble task.
The other shorthand expressions are `jC`, `jTr`, `jA`, `jTe`, `jB` and `jX`.
The other shorthand expressions are `jCF`, `jTr`, `jA`, `jTe`, `jB` and `jX`.


#### Task Enable and Disable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.github.j2objccontrib.j2objcgradle.tasks

import com.google.common.annotations.VisibleForTesting
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.process.internal.ExecException

/**
* CycleFinder task checks for memory cycles that can cause memory leaks
Expand Down Expand Up @@ -70,7 +73,7 @@ class CycleFinderTask extends DefaultTask {
int getCycleFinderExpectedCycles() { return project.j2objcConfig.cycleFinderExpectedCycles }

@Input
String getJ2ObjCHome() { return Utils.j2objcHome(project) }
String getJ2objcHome() { return Utils.j2objcHome(project) }

@Input
@Optional
Expand All @@ -94,13 +97,18 @@ class CycleFinderTask extends DefaultTask {

@TaskAction
void cycleFinder() {
cycleFinderWithExec(project)
}

@VisibleForTesting
void cycleFinderWithExec(Project projectExec) {

String cycleFinderExec = "${getJ2ObjCHome()}/cycle_finder"
String cycleFinderExec = "${getJ2objcHome()}/cycle_finder"
List<String> windowsOnlyArgs = new ArrayList<String>()
if (Utils.isWindows()) {
cycleFinderExec = 'java'
windowsOnlyArgs.add('-jar')
windowsOnlyArgs.add("${getJ2ObjCHome()}/lib/cycle_finder.jar")
windowsOnlyArgs.add("${getJ2objcHome()}/lib/cycle_finder.jar")
}

String sourcepath = Utils.sourcepathJava(project)
Expand All @@ -121,22 +129,27 @@ class CycleFinderTask extends DefaultTask {
}

String classPathArg = Utils.getClassPathArg(
project, getJ2ObjCHome(), getTranslateClassPaths(), getTranslateJ2objcLibs())
project, getJ2objcHome(), getTranslateClassPaths(), getTranslateJ2objcLibs())

ByteArrayOutputStream output = new ByteArrayOutputStream()
try {
project.exec {
// Might be injected project, otherwise project.exec {...}
projectExec.exec {
executable cycleFinderExec
windowsOnlyArgs.each { String windowsOnlyArg ->
args windowsOnlyArg
}

args windowsOnlyArgs
// Arguments
args "-sourcepath", sourcepath

if (classPathArg.size() > 0) {
args "-classpath", classPathArg
}
getCycleFinderArgs().each { String cycleFinderArg ->
args cycleFinderArg
}

args getCycleFinderArgs()

// File Inputs
fullSrcFiles.each { File file ->
args file.path
}
Expand All @@ -148,15 +161,17 @@ class CycleFinderTask extends DefaultTask {
logger.debug "CycleFinder found 0 cycles"
assert 0 == getCycleFinderExpectedCycles()

} catch (Exception exception) {

} catch (ExecException exception) {
// Expected exception for non-zero exit of process

String outputStr = output.toString()
// matchNumberRegex throws exception if regex isn't found
int cyclesFound = Utils.matchNumberRegex(outputStr, /(\d+) CYCLES FOUND/)
if (cyclesFound != getCycleFinderExpectedCycles()) {
logger.error outputStr
String message =
"Unexpected number of cycles founder:\n" +
"Unexpected number of cycles found:\n" +
"Expected Cycles: ${getCycleFinderExpectedCycles()}\n" +
"Actual Cycles: $cyclesFound\n" +
"\n" +
Expand All @@ -165,7 +180,7 @@ class CycleFinderTask extends DefaultTask {
"j2objcConfig {\n" +
" cycleFinderExpectedCycles $cyclesFound\n" +
"}\n"
throw new Exception(message)
throw new IllegalArgumentException(message)
}
// Suppress exception when cycles found == cycleFinderExpectedCycles
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.github.j2objccontrib.j2objcgradle.tasks

import com.google.common.annotations.VisibleForTesting
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
Expand All @@ -26,6 +28,7 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
import org.gradle.api.tasks.incremental.InputFileDetails
import org.gradle.process.internal.ExecException

/**
* Translation task for Java to Objective-C using j2objc tool.
Expand Down Expand Up @@ -79,7 +82,7 @@ class TranslateTask extends DefaultTask {

// j2objcConfig dependencies for UP-TO-DATE checks
@Input
String getJ2ObjCHome() { return Utils.j2objcHome(project) }
String getJ2objcHome() { return Utils.j2objcHome(project) }

@Input
List<String> getTranslateArgs() { return project.j2objcConfig.translateArgs }
Expand All @@ -101,6 +104,11 @@ class TranslateTask extends DefaultTask {

@TaskAction
void translate(IncrementalTaskInputs inputs) {
translateWithExec(project, inputs)
}

@VisibleForTesting
void translateWithExec(Project projectExec, IncrementalTaskInputs inputs) {
List<String> translateArgs = getTranslateArgs()
// Don't evaluate this expensive property multiple times.
FileCollection originalSrcFiles = getSrcFiles()
Expand Down Expand Up @@ -205,12 +213,12 @@ class TranslateTask extends DefaultTask {
}
}

String j2objcExecutable = "${getJ2ObjCHome()}/j2objc"
String j2objcExecutable = "${getJ2objcHome()}/j2objc"
List<String> windowsOnlyArgs = new ArrayList<String>()
if (Utils.isWindows()) {
j2objcExecutable = 'java'
windowsOnlyArgs.add('-jar')
windowsOnlyArgs.add("${getJ2ObjCHome()}/lib/j2objc.jar")
windowsOnlyArgs.add("${getJ2objcHome()}/lib/j2objc.jar")
}

String sourcepath = Utils.sourcepathJava(project)
Expand All @@ -230,33 +238,38 @@ class TranslateTask extends DefaultTask {
}

String classPathArg = Utils.getClassPathArg(
project, getJ2ObjCHome(), getTranslateClassPaths(), getTranslateJ2objcLibs())
project, getJ2objcHome(), getTranslateClassPaths(), getTranslateJ2objcLibs())

classPathArg += ":${project.buildDir}/classes"

ByteArrayOutputStream output = new ByteArrayOutputStream()
try {
project.exec {
projectExec.exec {
executable j2objcExecutable
windowsOnlyArgs.each { String windowsArg ->
args windowsOnlyArg
}

args windowsOnlyArgs
// Arguments
args "-d", srcGenDir
args "-sourcepath", sourcepath

if (classPathArg.size() > 0) {
args "-classpath", classPathArg
}
translateArgs.each { String translateArg ->
args translateArg
}

args translateArgs

// File Inputs
srcFilesChanged.each { File file ->
args file.path
}

standardOutput output
errorOutput output
}

} catch (Exception exception) {
} catch (ExecException exception) {
String outputStr = output.toString()
logger.debug 'Translation output:'
logger.debug outputStr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Utils {
String message =
"j2objc plugin didn't find the 'java' plugin in the '${proj.name}' project.\n" +
"This is a requirement for using j2objc. Please see usage information at:\n" +
"\n" +
"https://github.com/j2objc-contrib/j2objc-gradle/#usage"
throw new InvalidUserDataException(message)
}
Expand Down Expand Up @@ -70,8 +69,8 @@ class Utils {
return javaRoots.join(':')
}

// MUST be used only in @Input getJ2ObjCHome() methods to ensure UP-TO-DATE checks are correct
// @Input getJ2ObjCHome() method can be used freely inside the task action
// MUST be used only in @Input getJ2objcHome() methods to ensure UP-TO-DATE checks are correct
// @Input getJ2objcHome() method can be used freely inside the task action
static String j2objcHome(Project proj) {
File localPropertiesFile = new File(proj.rootDir, 'local.properties')
String result = null
Expand Down Expand Up @@ -213,16 +212,16 @@ class Utils {
Matcher matcher = (str =~ regex)
if (!matcher.find()) {
throw new IllegalArgumentException(
"Content:\n" +
"$str\n" +
"\n" +
"Regex couldn't match number in output: $regex")
"Regex couldn't match number: $regex")
} else {
String value = matcher[0][1]
if (!value.isInteger()) {
throw new IllegalArgumentException(
"Content:\n" +
"$str\n" +
"\n" +
"Regex didn't find number in output: $regex, value: $value")
"Regex couldn't match number: $regex")
}
return value.toInteger()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class XcodeTask extends DefaultTask {

// j2objcConfig dependencies for UP-TO-DATE checks
@Input
String getJ2ObjCHome() { return Utils.j2objcHome(project) }
String getJ2objcHome() { return Utils.j2objcHome(project) }

@Input @Optional
String getXcodeProjectDir() { return project.j2objcConfig.xcodeProjectDir }
Expand Down Expand Up @@ -106,8 +106,8 @@ class XcodeTask extends DefaultTask {
"s.requires_arc = true\n" +
"s.preserve_paths = '${srcGenDirRelativeToBuildDir}**/*.a'\n" +
"s.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore', '${project.name}-j2objc'\n" +
"s.xcconfig = { 'HEADER_SEARCH_PATHS' => '${getJ2ObjCHome()}/include', " +
"'LIBRARY_SEARCH_PATHS' => '${getJ2ObjCHome()}/lib ${project.buildDir}/j2objcOutputs/lib/iosDebug' }\n" +
"s.xcconfig = { 'HEADER_SEARCH_PATHS' => '${getJ2objcHome()}/include', " +
"'LIBRARY_SEARCH_PATHS' => '${getJ2objcHome()}/lib ${project.buildDir}/j2objcOutputs/lib/iosDebug' }\n" +
"end\n"
logger.debug 'podspecFileContents creation...\n\n' + podspecFileContents
File podspecFile = getPodspecFile()
Expand Down
Loading

0 comments on commit fc66add

Please sign in to comment.