Skip to content

Commit

Permalink
Modified JUnit test executors to run tests inside autorelease pools.
Browse files Browse the repository at this point in the history
This caught three places where fields were not retained correctly.
	Change on 2013/10/22 by tball <tball@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=55422690
  • Loading branch information
tomball committed Oct 25, 2013
1 parent 0fd85c3 commit 4541140
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jre_emul/Classes/java/lang/Throwable.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ - (JavaLangThrowable *)initCauseWithJavaLangThrowable:
#endif
@throw exception;
}
self->cause = causeArg;
self->cause = RETAIN_(causeArg);
return self;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ private native boolean casPair(Pair<V> cmp, Pair<V> val) /*-[
void * volatile tmp = (__bridge void * volatile) pair_;
return OSAtomicCompareAndSwapPtrBarrier((__bridge void *) cmp, (__bridge void *) val, &tmp);
#else
return OSAtomicCompareAndSwapPtrBarrier(cmp, val, (void * volatile *) &pair_);
id tmp = pair_;
if (OSAtomicCompareAndSwapPtrBarrier(cmp, val, (void * volatile *) &pair_)) {
[pair_ retain];
[tmp release];
return YES;
}
return NO;
#endif
]-*/;
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ private native boolean casPair(Pair<V> cmp, Pair<V> val) /*-[
void * volatile tmp = (__bridge void * volatile) pair_;
return OSAtomicCompareAndSwapPtrBarrier((__bridge void *) cmp, (__bridge void *) val, &tmp);
#else
return OSAtomicCompareAndSwapPtrBarrier(cmp, val, (void * volatile *) &pair_);
id tmp = pair_;
if (OSAtomicCompareAndSwapPtrBarrier(cmp, val, (void * volatile *) &pair_)) {
[pair_ retain];
[tmp release];
return YES;
}
return NO;
#endif
]-*/;
}
24 changes: 18 additions & 6 deletions junit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ JUNIT_PUBLIC_SOURCES = \
junit/framework/JUnit4TestCaseFacade.java \
junit/framework/Protectable.java \
junit/framework/Test.java \
junit/framework/TestCase.java \
junit/framework/TestFailure.java \
junit/framework/TestListener.java \
junit/framework/TestResult.java \
Expand Down Expand Up @@ -79,7 +78,6 @@ JUNIT_PUBLIC_SOURCES = \
org/junit/runners/Suite.java \
org/junit/runners/model/FrameworkField.java \
org/junit/runners/model/FrameworkMember.java \
org/junit/runners/model/FrameworkMethod.java \
org/junit/runners/model/InitializationError.java \
org/junit/runners/model/MultipleFailureException.java \
org/junit/runners/model/NoGenericTypeParametersValidator.java \
Expand Down Expand Up @@ -126,6 +124,10 @@ JUNIT_INTERNAL_SOURCES = \
org/junit/internal/runners/SuiteMethod.java \
org/junit/internal/TextListener.java

MODIFIED_SOURCES = \
junit/framework/TestCase.java \
org/junit/runners/model/FrameworkMethod.java


JUNIT_SOURCES = $(JUNIT_PUBLIC_SOURCES) $(JUNIT_INTERNAL_SOURCES)

Expand Down Expand Up @@ -173,7 +175,8 @@ DUPLICATE_NAME_OBJS = \

OBJS := $(JUNIT_SOURCES:%.java=%.o) \
$(HAMCREST_SOURCES:%.java=%.o) \
$(DUPLICATE_NAME_OBJS)
$(DUPLICATE_NAME_OBJS) \
$(MODIFIED_SOURCES:%.java=%.o)
OBJS := $(filter-out $(DUPLICATE_NAME_SOURCES:%.java=%.o),$(OBJS))

IPHONE_SDK_DIR = $(shell bash ../scripts/sysroot_path.sh --iphoneos)
Expand All @@ -183,10 +186,12 @@ JAVA_SOURCE_LIST = $(BUILD_DIR)/junit.classes.list
EXTRACTED_JAVA = $(JUNIT_SOURCES:%=$(JAVA_SRC_DIR)/%) \
$(HAMCREST_SOURCES:%=$(JAVA_SRC_DIR)/%)
TRANSLATED_OBJC = $(JUNIT_SOURCES:%.java=$(BUILD_DIR)/%.m) \
$(HAMCREST_SOURCES:%.java=$(BUILD_DIR)/%.m)
$(HAMCREST_SOURCES:%.java=$(BUILD_DIR)/%.m) \
$(MODIFIED_SOURCES:%.java=$(BUILD_DIR)/%.m)
PUBLIC_HEADERS = \
$(JUNIT_PUBLIC_SOURCES:%.java=%.h) \
$(HAMCREST_PUBLIC_SOURCES:%.java=%.h)
$(HAMCREST_PUBLIC_SOURCES:%.java=%.h) \
$(MODIFIED_SOURCES:%.java=%.h)
INTERNAL_HEADERS = \
$(JUNIT_INTERNAL_SOURCES:%.java=%.h) \
$(HAMCREST_INTERNAL_SOURCES:%.java=%.h)
Expand Down Expand Up @@ -221,7 +226,8 @@ lib: translate $(JUNIT_LIB)

translate: pre_translate $(EXTRACTED_JAVA) $(TRANSLATED_OBJC)
@if [ `cat $(JAVA_SOURCE_LIST) | wc -l` -ge 1 ] ; then \
$(J2OBJC) -sourcepath $(JAVA_SRC_DIR) `cat $(JAVA_SOURCE_LIST)`; \
$(J2OBJC) -sourcepath $(MODIFIED_JAVA_SRC_DIR):$(JAVA_SRC_DIR) \
`cat $(JAVA_SOURCE_LIST)`; \
fi

pre_translate:
Expand All @@ -241,6 +247,9 @@ $(EXTRACTED_JAVA): $(BUILD_DIR)/.extracted
$(BUILD_DIR)/%.h $(BUILD_DIR)/%.m: $(JAVA_SRC_DIR)/%.java | pre_translate
@echo "$?" >> $(JAVA_SOURCE_LIST)

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

ifdef TARGET_TEMP_DIR
$(JUNIT_LIB): $(OBJS:%=$(TARGET_TEMP_DIR)/%)
@mkdir -p $(@D)
Expand Down Expand Up @@ -278,6 +287,9 @@ $(1)/$(4): $(BUILD_DIR)/$(3) | translate
@mkdir -p $$(@D)
$$(CLANG) -c $$? -o $$@ $(2) $$(OBJCFLAGS) -I$$(BUILD_DIR) \
-I$$(ARCH_INCLUDE_DIR)

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

arch_compile_rules = $(eval $(call compile_rules,$(1),$(2))) \
Expand Down
1 change: 1 addition & 0 deletions junit/environment.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ JAVA_SRC_DIR = $(BUILD_DIR)/java

JUNIT_SRC_JAR = $(JAVA_DEPS_JAR_DIR)/$(JUNIT_SOURCE_JAR)
HAMCREST_SRC_JAR = $(JAVA_DEPS_JAR_DIR)/$(HAMCREST_SOURCE_JAR)
MODIFIED_JAVA_SRC_DIR = src/main/java

JUNIT_LIB = $(ARCH_BUILD_DIR)/libjunit.a
JUNIT_LIB_DIST = $(ARCH_LIB_DIR)/libjunit.a
Expand Down
220 changes: 220 additions & 0 deletions junit/src/main/java/junit/framework/TestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package junit.framework;

import com.google.j2objc.annotations.AutoreleasePool;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
* A test case defines the fixture to run multiple tests. To define a test case<br/>
* <ol>
* <li>implement a subclass of <code>TestCase</code></li>
* <li>define instance variables that store the state of the fixture</li>
* <li>initialize the fixture state by overriding {@link #setUp()}</li>
* <li>clean-up after a test by overriding {@link #tearDown()}.</li>
* </ol>
* Each test runs in its own fixture so there
* can be no side effects among test runs.
* Here is an example:
* <pre>
* public class MathTest extends TestCase {
* protected double fValue1;
* protected double fValue2;
*
* protected void setUp() {
* fValue1= 2.0;
* fValue2= 3.0;
* }
* }
* </pre>
*
* For each test implement a method which interacts
* with the fixture. Verify the expected results with assertions specified
* by calling {@link junit.framework.Assert#assertTrue(String, boolean)} with a boolean.
* <pre>
* public void testAdd() {
* double result= fValue1 + fValue2;
* assertTrue(result == 5.0);
* }
* </pre>
*
* Once the methods are defined you can run them. The framework supports
* both a static type safe and more dynamic way to run a test.
* In the static way you override the runTest method and define the method to
* be invoked. A convenient way to do so is with an anonymous inner class.
* <pre>
* TestCase test= new MathTest("add") {
* public void runTest() {
* testAdd();
* }
* };
* test.run();
* </pre>
*
* The dynamic way uses reflection to implement {@link #runTest()}. It dynamically finds
* and invokes a method.
* In this case the name of the test case has to correspond to the test method
* to be run.
* <pre>
* TestCase test= new MathTest("testAdd");
* test.run();
* </pre>
*
* The tests to be run can be collected into a TestSuite. JUnit provides
* different <i>test runners</i> which can run a test suite and collect the results.
* A test runner either expects a static method <code>suite</code> as the entry
* point to get a test to run or it will extract the suite automatically.
* <pre>
* public static Test suite() {
* suite.addTest(new MathTest("testAdd"));
* suite.addTest(new MathTest("testDivideByZero"));
* return suite;
* }
* </pre>
* @see TestResult
* @see TestSuite
*/
public abstract class TestCase extends Assert implements Test {
/**
* the name of the test case
*/
private String fName;

/**
* No-arg constructor to enable serialization. This method
* is not intended to be used by mere mortals without calling setName().
*/
public TestCase() {
fName= null;
}
/**
* Constructs a test case with the given name.
*/
public TestCase(String name) {
fName= name;
}
/**
* Counts the number of test cases executed by run(TestResult result).
*/
public int countTestCases() {
return 1;
}
/**
* Creates a default TestResult object
*
* @see TestResult
*/
protected TestResult createResult() {
return new TestResult();
}
/**
* A convenience method to run this test, collecting the results with a
* default TestResult object.
*
* @see TestResult
*/
public TestResult run() {
TestResult result= createResult();
run(result);
return result;
}
/**
* Runs the test case and collects the results in TestResult.
*/
public void run(TestResult result) {
result.run(this);
}
/**
* Runs the bare test sequence.
* @throws Throwable if any exception is thrown
*/
public void runBare() throws Throwable {
Throwable exception= null;
setUp();
try {
runTest();
} catch (Throwable running) {
exception= running;
}
finally {
try {
tearDown();
} catch (Throwable tearingDown) {
if (exception == null) exception= tearingDown;
}
}
if (exception != null) throw exception;
}
/**
* Override to run the test and assert its state.
* @throws Throwable if any exception is thrown
*/
protected void runTest() throws Throwable {
assertNotNull("TestCase.fName cannot be null", fName); // Some VMs crash when calling getMethod(null,null);
Method runMethod= null;
try {
// use getMethod to get all public inherited
// methods. getDeclaredMethods returns all
// methods of this class but excludes the
// inherited ones.
runMethod= getClass().getMethod(fName, (Class[])null);
} catch (NoSuchMethodException e) {
fail("Method \""+fName+"\" not found");
}
if (!Modifier.isPublic(runMethod.getModifiers())) {
fail("Method \""+fName+"\" should be public");
}

try {
runTest0(runMethod);
}
catch (InvocationTargetException e) {
e.fillInStackTrace();
throw e.getTargetException();
}
catch (IllegalAccessException e) {
e.fillInStackTrace();
throw e;
}
}

@AutoreleasePool
private void runTest0(Method m) throws InvocationTargetException, IllegalAccessException {
m.invoke(this);
}

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected void setUp() throws Exception {
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*/
protected void tearDown() throws Exception {
}
/**
* Returns a string representation of the test case
*/
@Override
public String toString() {
return getName() + "(" + getClass().getName() + ")";
}
/**
* Gets the name of a TestCase
* @return the name of the TestCase
*/
public String getName() {
return fName;
}
/**
* Sets the name of a TestCase
* @param name the name to set
*/
public void setName(String name) {
fName= name;
}
}
Loading

0 comments on commit 4541140

Please sign in to comment.