From 480e8cf27598a5bf4d8c43ad398e9301b46867e5 Mon Sep 17 00:00:00 2001 From: Bruno Bowden Date: Mon, 29 Jun 2015 16:47:13 -0700 Subject: [PATCH] Revert "CycleFinderTask and TranslateTask Unit Tests" This reverts commit 9331f5a912320b9d7f374abc654e5bc2a3798ffa. --- README.md | 2 +- .../j2objcgradle/tasks/CycleFinderTask.groovy | 39 ++-- .../j2objcgradle/tasks/TranslateTask.groovy | 33 ++-- .../j2objcgradle/tasks/Utils.groovy | 13 +- .../j2objcgradle/tasks/XcodeTask.groovy | 6 +- .../tasks/CycleFinderTaskTest.groovy | 167 ------------------ .../j2objcgradle/tasks/MockProjectExec.groovy | 127 ------------- .../tasks/TranslateTaskTest.groovy | 93 ---------- 8 files changed, 33 insertions(+), 447 deletions(-) delete mode 100644 src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTaskTest.groovy delete mode 100644 src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/MockProjectExec.groovy delete mode 100644 src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTaskTest.groovy diff --git a/README.md b/README.md index 5ad3f136..54ebfdff 100644 --- a/README.md +++ b/README.md @@ -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 `jCF`, `jTr`, `jA`, `jTe`, `jB` and `jX`. +The other shorthand expressions are `jC`, `jTr`, `jA`, `jTe`, `jB` and `jX`. #### Task Enable and Disable diff --git a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTask.groovy b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTask.groovy index 7b849328..e3c8519e 100644 --- a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTask.groovy +++ b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTask.groovy @@ -16,16 +16,13 @@ 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 @@ -73,7 +70,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 @@ -97,18 +94,13 @@ 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 windowsOnlyArgs = new ArrayList() 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) @@ -129,27 +121,22 @@ class CycleFinderTask extends DefaultTask { } String classPathArg = Utils.getClassPathArg( - project, getJ2objcHome(), getTranslateClassPaths(), getTranslateJ2objcLibs()) + project, getJ2ObjCHome(), getTranslateClassPaths(), getTranslateJ2objcLibs()) ByteArrayOutputStream output = new ByteArrayOutputStream() try { - // Might be injected project, otherwise project.exec {...} - projectExec.exec { + project.exec { executable cycleFinderExec - windowsOnlyArgs.each { String windowsOnlyArg -> - args windowsOnlyArg - } - // Arguments + args windowsOnlyArgs args "-sourcepath", sourcepath + if (classPathArg.size() > 0) { args "-classpath", classPathArg } - getCycleFinderArgs().each { String cycleFinderArg -> - args cycleFinderArg - } - // File Inputs + args getCycleFinderArgs() + fullSrcFiles.each { File file -> args file.path } @@ -161,9 +148,7 @@ class CycleFinderTask extends DefaultTask { logger.debug "CycleFinder found 0 cycles" assert 0 == getCycleFinderExpectedCycles() - - } catch (ExecException exception) { - // Expected exception for non-zero exit of process + } catch (Exception exception) { String outputStr = output.toString() // matchNumberRegex throws exception if regex isn't found @@ -171,7 +156,7 @@ class CycleFinderTask extends DefaultTask { if (cyclesFound != getCycleFinderExpectedCycles()) { logger.error outputStr String message = - "Unexpected number of cycles found:\n" + + "Unexpected number of cycles founder:\n" + "Expected Cycles: ${getCycleFinderExpectedCycles()}\n" + "Actual Cycles: $cyclesFound\n" + "\n" + @@ -180,7 +165,7 @@ class CycleFinderTask extends DefaultTask { "j2objcConfig {\n" + " cycleFinderExpectedCycles $cyclesFound\n" + "}\n" - throw new IllegalArgumentException(message) + throw new Exception(message) } // Suppress exception when cycles found == cycleFinderExpectedCycles } diff --git a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTask.groovy b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTask.groovy index dec245bc..d19bde4f 100644 --- a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTask.groovy +++ b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTask.groovy @@ -16,10 +16,8 @@ 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 @@ -28,7 +26,6 @@ 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. @@ -82,7 +79,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 getTranslateArgs() { return project.j2objcConfig.translateArgs } @@ -104,11 +101,6 @@ class TranslateTask extends DefaultTask { @TaskAction void translate(IncrementalTaskInputs inputs) { - translateWithExec(project, inputs) - } - - @VisibleForTesting - void translateWithExec(Project projectExec, IncrementalTaskInputs inputs) { List translateArgs = getTranslateArgs() // Don't evaluate this expensive property multiple times. FileCollection originalSrcFiles = getSrcFiles() @@ -213,12 +205,12 @@ class TranslateTask extends DefaultTask { } } - String j2objcExecutable = "${getJ2objcHome()}/j2objc" + String j2objcExecutable = "${getJ2ObjCHome()}/j2objc" List windowsOnlyArgs = new ArrayList() 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) @@ -238,38 +230,33 @@ 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 { - projectExec.exec { + project.exec { executable j2objcExecutable - windowsOnlyArgs.each { String windowsArg -> - args windowsOnlyArg - } - // Arguments + args windowsOnlyArgs args "-d", srcGenDir args "-sourcepath", sourcepath + if (classPathArg.size() > 0) { args "-classpath", classPathArg } - translateArgs.each { String translateArg -> - args translateArg - } - // File Inputs + args translateArgs + srcFilesChanged.each { File file -> args file.path } - standardOutput output errorOutput output } - } catch (ExecException exception) { + } catch (Exception exception) { String outputStr = output.toString() logger.debug 'Translation output:' logger.debug outputStr diff --git a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/Utils.groovy b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/Utils.groovy index eb20fde7..681e816b 100644 --- a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/Utils.groovy +++ b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/Utils.groovy @@ -42,6 +42,7 @@ 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) } @@ -69,8 +70,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 @@ -212,16 +213,16 @@ class Utils { Matcher matcher = (str =~ regex) if (!matcher.find()) { throw new IllegalArgumentException( - "Content:\n" + "$str\n" + - "Regex couldn't match number: $regex") + "\n" + + "Regex couldn't match number in output: $regex") } else { String value = matcher[0][1] if (!value.isInteger()) { throw new IllegalArgumentException( - "Content:\n" + "$str\n" + - "Regex couldn't match number: $regex") + "\n" + + "Regex didn't find number in output: $regex, value: $value") } return value.toInteger() } diff --git a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/XcodeTask.groovy b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/XcodeTask.groovy index 89514859..0b25c2e5 100644 --- a/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/XcodeTask.groovy +++ b/src/main/groovy/com/github/j2objccontrib/j2objcgradle/tasks/XcodeTask.groovy @@ -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 } @@ -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() diff --git a/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTaskTest.groovy b/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTaskTest.groovy deleted file mode 100644 index 675304ab..00000000 --- a/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/CycleFinderTaskTest.groovy +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (c) 2015 the authors of j2objc-gradle (see AUTHORS file) - * - * 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 com.github.j2objccontrib.j2objcgradle.tasks - -import com.github.j2objccontrib.j2objcgradle.J2objcConfig -import org.gradle.api.Project -import org.gradle.api.plugins.JavaPlugin -import org.gradle.process.internal.ExecException -import org.gradle.testfixtures.ProjectBuilder -import org.junit.Before -import org.junit.Test - -/** - * CycleFinderTask tests. - */ -public class CycleFinderTaskTest { - - private Project proj - private String j2objcHome - - @Before - void setUp() { - proj = ProjectBuilder.builder().build() - - // For Utils.throwIfNoJavaPlugin() - proj.pluginManager.apply(JavaPlugin) - - // For Utils.J2objcHome() - j2objcHome = File.createTempDir('J2OBJC_HOME', '').path - File localProperties = proj.file('local.properties') - localProperties.write("j2objc.home=$j2objcHome\n") - - // For reportFile - proj.getBuildDir().mkdir() - File reportDir = new File("${proj.buildDir}/reports") - reportDir.mkdir() - } - - // TODO: add java source files to the test cases - // TODO: perhaps even better, point the project towards an existing example - - @Test - public void cycleFinderWithExec_SimpleModeSuccess() { - - J2objcConfig j2objcConfig = proj.extensions.create('j2objcConfig', J2objcConfig, proj) - assert 40 == j2objcConfig.cycleFinderExpectedCycles - - CycleFinderTask j2objcCycleFinder = (CycleFinderTask) proj.tasks.create( - name: 'j2objcCycleFinder', type: CycleFinderTask) { - } - - MockProjectExec mockProjectExec = new MockProjectExec(proj, j2objcHome) - mockProjectExec.demandExec( - [ - '/J2OBJC_HOME/cycle_finder', - '-sourcepath', '/PROJECT_DIR/src/main/java:/PROJECT_DIR/src/test/java', - '-classpath', '/J2OBJC_HOME/lib/j2objc_annotations.jar:/J2OBJC_HOME/lib/j2objc_guava.jar:/J2OBJC_HOME/lib/j2objc_junit.jar:/J2OBJC_HOME/lib/jre_emul.jar:/J2OBJC_HOME/lib/javax.inject-1.jar:/J2OBJC_HOME/lib/jsr305-3.0.0.jar:/J2OBJC_HOME/lib/mockito-core-1.9.5.jar', - ], - 'IGNORE\n40 CYCLES FOUND\nIGNORE', - null, - new ExecException('Non-Zero Exit')) - - j2objcCycleFinder.cycleFinderWithExec(mockProjectExec.projectProxyInstance()) - - mockProjectExec.verify() - } - - @Test(expected = IllegalArgumentException.class) - public void cycleFinderWithExec_SimpleModeFailure() { - - J2objcConfig j2objcConfig = proj.extensions.create('j2objcConfig', J2objcConfig, proj) - assert 40 == j2objcConfig.cycleFinderExpectedCycles - - CycleFinderTask j2objcCycleFinder = (CycleFinderTask) proj.tasks.create( - name: 'j2objcCycleFinder', type: CycleFinderTask) { - } - - MockProjectExec mockProjectExec = new MockProjectExec(proj, j2objcHome) - mockProjectExec.demandExec( - [ - '/J2OBJC_HOME/cycle_finder', - '-sourcepath', '/PROJECT_DIR/src/main/java:/PROJECT_DIR/src/test/java', - '-classpath', '/J2OBJC_HOME/lib/j2objc_annotations.jar:/J2OBJC_HOME/lib/j2objc_guava.jar:/J2OBJC_HOME/lib/j2objc_junit.jar:/J2OBJC_HOME/lib/jre_emul.jar:/J2OBJC_HOME/lib/javax.inject-1.jar:/J2OBJC_HOME/lib/jsr305-3.0.0.jar:/J2OBJC_HOME/lib/mockito-core-1.9.5.jar', - ], - // Note the '50' cycles instead of expected 45 - 'IGNORE\n50 CYCLES FOUND\nIGNORE', - null, - new ExecException('Non-Zero Exit')) - - j2objcCycleFinder.cycleFinderWithExec(mockProjectExec.projectProxyInstance()) - - mockProjectExec.verify() - } - - @Test - public void cycleFinderWithExec_AdvancedModeSuccess() { - - J2objcConfig j2objcConfig = proj.extensions.create('j2objcConfig', J2objcConfig, proj) - j2objcConfig.translateArgs('--no-package-directories') - j2objcConfig.cycleFinderExpectedCycles = 0 - j2objcConfig.cycleFinderArgs('--whitelist', '/J2OBJC_REPO/jre_emul/cycle_whitelist.txt') - j2objcConfig.cycleFinderArgs('--sourcefilelist', '/J2OBJC_REPO/jre_emul/build_result/java_sources.mf') - - CycleFinderTask j2objcCycleFinder = (CycleFinderTask) proj.tasks.create( - name: 'j2objcCycleFinder', type: CycleFinderTask) { - } - - MockProjectExec mockProjectExec = new MockProjectExec(proj, j2objcHome) - mockProjectExec.demandExec( - [ - '/J2OBJC_HOME/cycle_finder', - '-sourcepath', '/PROJECT_DIR/src/main/java:/PROJECT_DIR/src/test/java', - '-classpath', '/J2OBJC_HOME/lib/j2objc_annotations.jar:/J2OBJC_HOME/lib/j2objc_guava.jar:/J2OBJC_HOME/lib/j2objc_junit.jar:/J2OBJC_HOME/lib/jre_emul.jar:/J2OBJC_HOME/lib/javax.inject-1.jar:/J2OBJC_HOME/lib/jsr305-3.0.0.jar:/J2OBJC_HOME/lib/mockito-core-1.9.5.jar', - '--whitelist', '/J2OBJC_REPO/jre_emul/cycle_whitelist.txt', - '--sourcefilelist', '/J2OBJC_REPO/jre_emul/build_result/java_sources.mf' - ]) - - j2objcCycleFinder.cycleFinderWithExec(mockProjectExec.projectProxyInstance()) - - mockProjectExec.verify() - } - - @Test(expected = IllegalArgumentException.class) - public void cycleFinderWithExec_AdvancedModeFailure() { - - J2objcConfig j2objcConfig = proj.extensions.create('j2objcConfig', J2objcConfig, proj) - j2objcConfig.translateArgs('--no-package-directories') - j2objcConfig.cycleFinderExpectedCycles = 0 - j2objcConfig.cycleFinderArgs('--whitelist', '/J2OBJC_REPO/jre_emul/cycle_whitelist.txt') - j2objcConfig.cycleFinderArgs('--sourcefilelist', '/J2OBJC_REPO/jre_emul/build_result/java_sources.mf') - - CycleFinderTask j2objcCycleFinder = (CycleFinderTask) proj.tasks.create( - name: 'j2objcCycleFinder', type: CycleFinderTask) { - } - - MockProjectExec mockProjectExec = new MockProjectExec(proj, j2objcHome) - mockProjectExec.demandExec( - [ - '/J2OBJC_HOME/cycle_finder', - '-sourcepath', '/PROJECT_DIR/src/main/java:/PROJECT_DIR/src/test/java', - '-classpath', '/J2OBJC_HOME/lib/j2objc_annotations.jar:/J2OBJC_HOME/lib/j2objc_guava.jar:/J2OBJC_HOME/lib/j2objc_junit.jar:/J2OBJC_HOME/lib/jre_emul.jar:/J2OBJC_HOME/lib/javax.inject-1.jar:/J2OBJC_HOME/lib/jsr305-3.0.0.jar:/J2OBJC_HOME/lib/mockito-core-1.9.5.jar', - '--whitelist', '/J2OBJC_REPO/jre_emul/cycle_whitelist.txt', - '--sourcefilelist', '/J2OBJC_REPO/jre_emul/build_result/java_sources.mf' - ], - 'IGNORE\n2 CYCLES FOUND\nIGNORE', - null, - new ExecException('Non-Zero Exit')) - - j2objcCycleFinder.cycleFinderWithExec(mockProjectExec.projectProxyInstance()) - - mockProjectExec.verify() - } -} diff --git a/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/MockProjectExec.groovy b/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/MockProjectExec.groovy deleted file mode 100644 index 1da45470..00000000 --- a/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/MockProjectExec.groovy +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2015 the authors of j2objc-gradle (see AUTHORS file) - * - * 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 com.github.j2objccontrib.j2objcgradle.tasks - -import groovy.mock.interceptor.MockFor -import org.gradle.api.Project -import org.gradle.util.ConfigureUtil - -/** - * Used for mocking on the project.exec {...} command used by many of the plugin tasks. - * - * It operates in the following manner: - * 1) Allows dependency injection and interception of the project.exec {...} command - * 2) Verifies the executable and args compared to expected values - * 3) Write to stdout and stderr as specified - * 4) Throws an error, e.g. for non-zero exits from the command - */ -public class MockProjectExec { - - private Project project - private String j2objcHome - private static final String j2objcHomeStd = '/J2OBJC_HOME' - private static final String projectDirStd = '/PROJECT_DIR' - - private MockFor mockForProj = new MockFor(Project) - private MockExec mockExec = new MockExec() - private GroovyObject proxyInstance - - MockProjectExec(Project project, String j2objcHome) { - this.project = project - this.j2objcHome = j2objcHome - } - - Project projectProxyInstance() { - proxyInstance = mockForProj.proxyInstance() - return ( Project ) proxyInstance - } - - void demandExec( - List expectedCommandLine) { - demandExec(expectedCommandLine, null, null, null) - } - - void demandExec( - List expectedCommandLine, - String errorOutputToWrite, - String standardOutputToWrite, - Exception exceptionToThrow) { - - mockForProj.demand.exec { Closure closure -> - - ConfigureUtil.configure(closure, mockExec) - - assert expectedCommandLine[0] == mockExec.executable.replace(j2objcHome, j2objcHomeStd) - expectedCommandLine.remove(0) - - List canonicalizedArgs = mockExec.args.collect { String arg -> - return arg - .replace(j2objcHome, j2objcHomeStd) - .replace(project.projectDir.path, projectDirStd) - } - assert expectedCommandLine == canonicalizedArgs - - if (errorOutputToWrite) { - mockExec.errorOutput.write(errorOutputToWrite.getBytes('utf-8')) - mockExec.errorOutput.flush() - } - if (standardOutputToWrite) { - mockExec.standardOutput.write(standardOutputToWrite.getBytes('utf-8')) - mockExec.standardOutput.flush() - } - - if (exceptionToThrow != null) { - throw exceptionToThrow - } - } - } - - void verify() { - mockForProj.verify(proxyInstance) - } - - // Basically mocks Gradle's AbstractExecTask - private class MockExec { - String executable - List args = new ArrayList<>() - OutputStream errorOutput; - OutputStream standardOutput; - - public void executable(Object executable) { - this.executable = (String) executable - } - - public void args(Object... args) { - args.each { Object arg -> - String newArgStr = (String) arg - this.args.add(newArgStr) - } - } - - public void errorOutput(OutputStream errorOutput) { - this.errorOutput = errorOutput - } - - public void standardOutput(OutputStream standardOutput) { - this.standardOutput = standardOutput - } - - public String toString() { - return executable + ' ' + args.join(' ') - } - } -} diff --git a/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTaskTest.groovy b/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTaskTest.groovy deleted file mode 100644 index 34124755..00000000 --- a/src/test/groovy/com/github/j2objccontrib/j2objcgradle/tasks/TranslateTaskTest.groovy +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2015 the authors of j2objc-gradle (see AUTHORS file) - * - * 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 com.github.j2objccontrib.j2objcgradle.tasks - -import com.github.j2objccontrib.j2objcgradle.J2objcConfig -import org.gradle.api.Action -import org.gradle.api.Project -import org.gradle.api.plugins.JavaPlugin -import org.gradle.api.tasks.incremental.IncrementalTaskInputs -import org.gradle.api.tasks.incremental.InputFileDetails -import org.gradle.process.internal.ExecException -import org.gradle.testfixtures.ProjectBuilder -import org.junit.Before -import org.junit.Test - -/** - * TestTask tests. - */ -public class TranslateTaskTest { - - private Project proj - private String j2objcHome - - @Before - void setUp() { - proj = ProjectBuilder.builder().build() - - // For Utils.throwIfNoJavaPlugin() - proj.pluginManager.apply(JavaPlugin) - - // For Utils.J2objcHome() - j2objcHome = File.createTempDir('J2OBJC_HOME', '').path - File localProperties = proj.file('local.properties') - localProperties.write("j2objc.home=$j2objcHome\n") - } - - // TODO: add java source files to the test cases - // TODO: perhaps even better, point the project towards an existing example - - @Test - public void translateWithExec_BasicArguments() { - - J2objcConfig j2objcConfig = proj.extensions.create('j2objcConfig', J2objcConfig, proj) - - TranslateTask j2objcTranslate = (TranslateTask) proj.tasks.create( - name: 'j2objcTranslate', type: TranslateTask) { - srcGenDir = proj.file("${proj.buildDir}/j2objcSrcGen") - } - - MockProjectExec mockProjectExec = new MockProjectExec(proj, j2objcHome) - mockProjectExec.demandExec( - [ - '/J2OBJC_HOME/j2objc', - '-d', '/PROJECT_DIR/build/j2objcSrcGen', - '-sourcepath', '/PROJECT_DIR/src/main/java:/PROJECT_DIR/src/test/java', - '-classpath', '/J2OBJC_HOME/lib/j2objc_annotations.jar:/J2OBJC_HOME/lib/j2objc_guava.jar:/J2OBJC_HOME/lib/j2objc_junit.jar:/J2OBJC_HOME/lib/jre_emul.jar:/J2OBJC_HOME/lib/javax.inject-1.jar:/J2OBJC_HOME/lib/jsr305-3.0.0.jar:/J2OBJC_HOME/lib/mockito-core-1.9.5.jar:/PROJECT_DIR/build/classes' - ]) - - IncrementalTaskInputs incrementalTaskInputs = new IncrementalTaskInputs() { - @Override - boolean isIncremental() { - return false - } - - @Override - void outOfDate(Action outOfDateAction) { - } - - @Override - void removed(Action removedAction) { - } - } - - j2objcTranslate.translateWithExec(mockProjectExec.projectProxyInstance(), incrementalTaskInputs) - - mockProjectExec.verify() - } - -}