Skip to content

Commit

Permalink
Removed translator special handling for main methods and JUnit tests.
Browse files Browse the repository at this point in the history
Changed JUnitRunner to invoke junit.textui.TestRunner with a test class name,
so that multiple test files can be in a single test executable. Tests that extend
other tests can now be translated without special handling.
	Change on 2013/06/13 by tball <tball@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=47979949
  • Loading branch information
tomball committed Jun 21, 2013
1 parent 28549df commit 82b9de8
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 495 deletions.
11 changes: 7 additions & 4 deletions jre_emul/Tests/java/lang/ClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
public class ClassTest extends TestCase {

public ClassTest() {}
public ClassTest(String s) {}

public ClassTest(Double d) {
super();
}

public int answerToLife() {
return 42;
Expand All @@ -48,21 +51,21 @@ public void testForName() throws Exception {

public void testGetDefaultConstructor() throws Exception {
Class<?> foo = Class.forName("java.lang.ClassTest");
Constructor c = foo.getConstructor();
Constructor<?> c = foo.getConstructor();
Class<?>[] paramTypes = c.getParameterTypes();
assertEquals(0, paramTypes.length);
}

public void testGetConstructor() throws Exception {
Class<?> foo = Class.forName("java.lang.ClassTest");
Constructor c = foo.getConstructor(String.class);
Constructor<?> c = foo.getConstructor(Double.class);
Class<?>[] paramTypes = c.getParameterTypes();
assertEquals(1, paramTypes.length);
}

public void testGetDeclaredConstructor() throws Exception {
Class<?> foo = Class.forName("java.lang.ClassTest");
Constructor c = foo.getConstructor();
Constructor<?> c = foo.getConstructor();
Class<?>[] paramTypes = c.getParameterTypes();
assertEquals(0, paramTypes.length);
}
Expand Down
414 changes: 203 additions & 211 deletions jre_emul/tests.mk

Large diffs are not rendered by default.

26 changes: 6 additions & 20 deletions junit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,25 @@ OBJC_SOURCES = \
JUnitRunner.m

MODIFIED_SOURCES = \
junit/textui/TestRunner.java \
junit/runner/Version.java

OBJS = $(JAVA_SOURCES:%.java=$(ARCH_BUILD_DIR)/%.o) \
$(OBJC_SOURCES:%.m=$(ARCH_BUILD_DIR)/%.o) \
$(MODIFIED_SOURCES:%.java=$(ARCH_BUILD_DIR)/%.o)

# TestRunner is in a separate library, so that its main function doesn't
# collide with main functions defined elsewhere.
RUNNER_SOURCES = \
junit/textui/TestRunner.java

RUNNER_OBJS = $(RUNNER_SOURCES:%.java=$(ARCH_BUILD_DIR)/%.o)

JAVA_SOURCE_LIST = $(TMPDIR).classes.list
EXTRACTED_JAVA = $(JAVA_SOURCES:%=$(JAVA_SRC_DIR)/%)
TRANSLATED_OBJC = $(JAVA_SOURCES:%.java=$(BUILD_DIR)/%.m) \
$(MODIFIED_SOURCES:%.java=$(BUILD_DIR)/%.m)
RUNNER_OBJC = $(RUNNER_SOURCES:%.java=$(BUILD_DIR)/%.m)
SOURCE_HEADERS = $(shell find $(OBJC_SOURCE_DIR) -name '*.h')
SOURCE_HEADERS := $(SOURCE_HEADERS:$(OBJC_SOURCE_DIR)/%=%)
GENERATED_HEADERS = $(JAVA_SOURCES:%.java=%.h) $(RUNNER_SOURCES:%.java=%.h)
GENERATED_HEADERS = $(JAVA_SOURCES:%.java=%.h)
RELATIVE_HEADERS = $(SOURCE_HEADERS) $(GENERATED_HEADERS)
HEADERS = $(SOURCE_HEADERS:%=$(OBJC_SOURCE_DIR)/%) $(GENERATED_HEADERS:%=$(BUILD_DIR)/%)
DIST_HEADERS = $(RELATIVE_HEADERS:%=$(DIST_INCLUDE_DIR)/%)

translate: pre_translate $(EXTRACTED_JAVA) $(TRANSLATED_OBJC) $(RUNNER_OBJC)
translate: pre_translate $(EXTRACTED_JAVA) $(TRANSLATED_OBJC)
@if [ `cat $(JAVA_SOURCE_LIST) | wc -l` -ge 1 ] ; then \
$(J2OBJC) -sourcepath .:$(SOURCE_BASE)/java `cat $(JAVA_SOURCE_LIST)` ; \
fi
Expand All @@ -76,7 +69,7 @@ pre_translate:
@rm -f $(JAVA_SOURCE_LIST)
@touch $(JAVA_SOURCE_LIST)

lib: translate $(JUNIT_LIB) $(RUNNER_LIB)
lib: translate $(JUNIT_LIB)
@:

$(EXTRACTED_JAVA): $(JUNIT_SRC_JAR)
Expand All @@ -87,9 +80,6 @@ $(EXTRACTED_JAVA): $(JUNIT_SRC_JAR)
$(JUNIT_LIB): $(OBJS)
$(LIBTOOL) -static -o $@ $^

$(RUNNER_LIB): $(RUNNER_OBJS)
$(LIBTOOL) -static -o $@ $^

$(BUILD_DIR)/%.h $(BUILD_DIR)/%.m: $(JAVA_SRC_DIR)/%.java
@echo "$?" >> $(JAVA_SOURCE_LIST)

Expand All @@ -108,10 +98,6 @@ $(JUNIT_LIB_DIST): $(JUNIT_LIB)
@mkdir -p $(@D)
install -m 0644 $< $@

$(RUNNER_LIB_DIST): $(RUNNER_LIB)
@mkdir -p $(@D)
install -m 0644 $< $@

$(DIST_INCLUDE_DIR)/%.h: $(BUILD_DIR)/%.h
@mkdir -p $(@D)
install -m 0644 $< $@
Expand All @@ -121,6 +107,6 @@ $(DIST_INCLUDE_DIR)/%.h: $(OBJC_SOURCE_DIR)/%.h
install -m 0644 $< $@

clean:
@rm -rf $(BUILD_DIR) $(JUNIT_LIB_DIST) $(RUNNER_LIB_DIST) $(DIST_HEADERS)
@rm -rf $(BUILD_DIR) $(JUNIT_LIB_DIST) $(DIST_HEADERS)

dist: lib $(JUNIT_LIB_DIST) $(RUNNER_LIB_DIST) $(DIST_HEADERS)
dist: lib $(JUNIT_LIB_DIST) $(DIST_HEADERS)
40 changes: 0 additions & 40 deletions junit/src/main/native/junit/JUnitRunner.h

This file was deleted.

129 changes: 54 additions & 75 deletions junit/src/main/native/junit/JUnitRunner.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
// JreEmulation
//
// Created by Tom Ball on 11/10/11.
// Copyright 2011 Google Inc. All rights reserved.
//

#import "JUnitRunner.h"
#import "junit/framework/TestFailure.h"
#import "junit/framework/TestResult.h"
#include "IOSClass.h"
#include "IOSObjectArray.h"
#include "java/io/PrintStream.h"
#include "java/lang/ClassNotFoundException.h"
#include "java/lang/IllegalAccessException.h"
#include "java/lang/NoSuchMethodException.h"
#include "java/lang/System.h"
#include "java/lang/reflect/InvocationTargetException.h"
#include "java/lang/reflect/Method.h"
#include "junit/framework/TestFailure.h"
#include "junit/framework/TestResult.h"
#include "junit/framework/TestSuite.h"
#include "junit/textui/TestRunner.h"

#import "java/lang/Throwable.h"
#import "java/util/Enumeration.h"
#include <execinfo.h>

#import <execinfo.h>

static void signalHandler(int sig) {
// Get void*'s for all entries on the stack.
Expand All @@ -35,75 +42,47 @@ void installSignalHandler() {
signal(SIGPIPE, signalHandler);
}

@implementation JUnitRunner

void listFailedTests(id<JavaUtilEnumeration> problems);

+ (int)runTests:(Class)testClass, ... {
// Variant of J2ObjCMain main function, hard-coded to invoke JUnit's
// junit.textui.TestRunner.
int main( int argc, const char *argv[] ) {
int exitCode = 0;
installSignalHandler();
va_list args;
va_start(args, testClass);
JunitFrameworkTestSuite *suite =
[JUnitRunner testSuite:testClass withArguments:args];
return [JUnitRunner runTest:suite];
}

+ (JunitFrameworkTestSuite *)testSuite:(Class)testClass, ... {
va_list args;
va_start(args, testClass);
return [JUnitRunner testSuite:testClass withArguments:args];
}

+ (JunitFrameworkTestSuite *)testSuite:(Class)testClass
withArguments:(va_list)args {
JunitFrameworkTestSuite *suite =
[[JunitFrameworkTestSuite alloc]
initWithNSString:NSStringFromClass(testClass)];
NSString *name;
while ((name = va_arg(args, NSString *)) != nil) {
JunitFrameworkTestCase *test = [testClass new];
#if ! __has_feature(objc_arc)
[test autorelease];
#endif
[test setNameWithNSString:name];
[suite addTestWithJunitFrameworkTest:test];
}
#if ! __has_feature(objc_arc)
[suite autorelease];
#endif
return suite;
}

+ (int)runTest:(id<JunitFrameworkTest>)test {
JunitFrameworkTestResult *result =
[[JunitFrameworkTestResult alloc] init];
#if ! __has_feature(objc_arc)
[result autorelease];
#endif
[test runWithJunitFrameworkTestResult:result];
if ([result wasSuccessful]) {
NSLog(@"OK (%d test%@)", [result runCount],
[result runCount] == 1 ? @"" : @"s");
}
else {
listFailedTests([result failures]);
listFailedTests([result errors]);
NSLog(@"FAILURES!!!\nTests run: %d, Failures: %d, Errors: %d",
[result runCount], [result failureCount], [result errorCount]);
}
return [result failureCount] + [result errorCount];
}

void listFailedTests(id<JavaUtilEnumeration> problems) {
while ([problems hasMoreElements]) {
JunitFrameworkTestFailure *problem =
(JunitFrameworkTestFailure *) [problems nextElement];
NSLog(@"%@", problem);
JavaLangThrowable *exception = [problem thrownException];
if (exception) {
[exception printStackTrace];
@autoreleasepool {
@try {
IOSClass *clazz = [JunitTextuiTestRunner getClass];
IOSClass *stringArrayClass =
[IOSObjectArray iosClassWithType:[NSString getClass]];
IOSObjectArray *paramTypes =
[IOSObjectArray arrayWithObjects:(id[]) { stringArrayClass }
count:1
type:[IOSClass getClass]];
JavaLangReflectMethod *mainMethod =
[clazz getDeclaredMethod:@"main" parameterTypes:paramTypes];
IOSObjectArray *mainArgs = JreEmulationMainArguments(argc, argv);
IOSObjectArray *params =
[IOSObjectArray arrayWithObjects:(id[]) { mainArgs }
count:1
type:[NSObject getClass]];
(void) [mainMethod invokeWithId:nil withNSObjectArray:params];
}
@catch (JavaLangClassNotFoundException *e) {
fprintf(stderr,
"Error: could not find or load junit.textui.TestRunner\n");
exitCode = 1;
}
@catch (JavaLangNoSuchMethodException *e) {
fprintf(stderr,
"Error: main method not found in class junit.textui.TestRunner\n");
exitCode = 1;
}
@catch (JavaLangReflectInvocationTargetException *e) {
[[JavaLangSystem err] printlnWithId:e];
exitCode = 1;
}
@catch (JavaLangIllegalAccessException *e) {
[[JavaLangSystem err] printlnWithId:e];
exitCode = 1;
}
}
return exitCode;
}

@end
12 changes: 7 additions & 5 deletions scripts/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
#
# Author: Tom Ball
#
# Run specified unit test executables, or all tests in the build/tests
# directory.
# Run a list of test classes with a specified unit test executable.

if [ $# -eq 0 ]; then
TESTS=$(/usr/bin/find build_result/tests -perm 755 -a -type f)
if [ $# -lt 2 ]; then
echo usage: $0 test_executable test-class [, test-class2, ...]
exit 1
else
TEST_BINARY=$1
shift
TESTS=$*
fi

Expand All @@ -45,7 +47,7 @@ function add_results {

function runtest {
echo "starting $(basename $@)"
eval "$@"
eval "${TEST_BINARY} $@"
add_results $?
echo
}
Expand Down
Loading

0 comments on commit 82b9de8

Please sign in to comment.