Skip to content

Commit

Permalink
Fixed translator annotation processing tests to run in Eclipse.
Browse files Browse the repository at this point in the history
	Change on 2016/02/08 by tball <tball@google.com>
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=114145835
  • Loading branch information
tomball committed Feb 8, 2016
1 parent 52b4e08 commit f8784f2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 44 deletions.
1 change: 0 additions & 1 deletion translator/Makefile
Expand Up @@ -263,7 +263,6 @@ RESOURCE_FILES = $(RESOURCES:%=$(CLASS_DIR)/$(BASE_PACKAGE)/%)
TEST_RESOURCES = \
com/google/devtools/j2objc/mappings.j2objc \
com/google/devtools/j2objc/testMappings.j2objc \
com/google/devtools/j2objc/annotations/Example.java \
com/google/devtools/j2objc/annotations/Processor.jar \
com/google/devtools/j2objc/util/example.jar
TEST_RESOURCE_FILES = $(TEST_RESOURCES:%=$(TEST_DIR)/%)
Expand Down
Expand Up @@ -391,10 +391,11 @@ protected void preprocessFiles(String... fileNames) {
new InputFilePreprocessor(parser).processInputs(batch.getInputs());
}

protected void addSourceFile(String source, String fileName) throws IOException {
protected String addSourceFile(String source, String fileName) throws IOException {
File file = new File(tempDir, fileName);
file.getParentFile().mkdirs();
Files.write(source, file, Options.getCharset());
return file.getPath();
}

/**
Expand Down
33 changes: 12 additions & 21 deletions translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java
Expand Up @@ -31,6 +31,14 @@ public class J2ObjCTest extends GenerationTest {
String exampleJavaPath;
String packageInfoPath;

private static final String EXAMPLE_JAVA_SOURCE =
"package com.google.devtools.j2objc.annotations;\n\n"
+ "import com.google.j2objc.annotations.ObjectiveCName;\n\n"
+ "@ObjectiveCName(\"foo\")\n"
+ "public class Example {\n"
+ "}\n";

@Override
public void setUp() throws IOException {
super.setUp();

Expand All @@ -50,6 +58,7 @@ public void setUp() throws IOException {
+ "import com.google.j2objc.annotations.ObjectiveCName;", packageInfoPath);
}

@Override
public void tearDown() throws Exception {
Options.getClassPathEntries().clear();
Options.setBatchTranslateMaximum(0);
Expand Down Expand Up @@ -183,14 +192,10 @@ public void testSourceDirsOption() throws Exception {

// Test a simple annotation processor on the classpath.
public void testAnnotationProcessing() throws Exception {
if (runningInEclipse()) {
// Eclipse doesn't copy resources ending in .java into its build.
return;
}
String processorPath = getResourceAsFile("annotations/Processor.jar");
Options.getClassPathEntries().add(processorPath);

String examplePath = getResourceAsFile("annotations/Example.java");
String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath));
assertErrorCount(0);

Expand All @@ -208,36 +213,22 @@ public void testAnnotationProcessing() throws Exception {

// Test a simple annotation processor on the processor path.
public void testAnnotationProcessingWithProcessorPath() throws Exception {
if (runningInEclipse()) {
// Eclipse doesn't copy resources ending in .java into its build.
return;
}
String processorPath = getResourceAsFile("annotations/Processor.jar");
Options.getProcessorPathEntries().add(processorPath);

String examplePath = getResourceAsFile("annotations/Example.java");
String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath));
assertErrorCount(0);
}

// Test a specified annotation processor.
public void testSpecifiedAnnotationProcessing() throws Exception {
if (runningInEclipse()) {
// Eclipse doesn't copy resources ending in .java into its build.
return;
}
String processorPath = getResourceAsFile("annotations/Processor.jar");
Options.getClassPathEntries().add(processorPath);
Options.setProcessors("com.google.devtools.j2objc.annotations.J2ObjCTestProcessor");

String examplePath = getResourceAsFile("annotations/Example.java");
String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath));
assertErrorCount(0);
}

private boolean runningInEclipse() {
Throwable t = new Throwable();
StackTraceElement[] trace = t.getStackTrace();
return trace[trace.length - 1].getClassName().startsWith("org.eclipse");
}
}

This file was deleted.

0 comments on commit f8784f2

Please sign in to comment.