From f8784f2e1e259aaec50915c6767f8d7ffba4b024 Mon Sep 17 00:00:00 2001 From: tball Date: Mon, 8 Feb 2016 13:49:33 -0800 Subject: [PATCH] Fixed translator annotation processing tests to run in Eclipse. Change on 2016/02/08 by tball ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=114145835 --- translator/Makefile | 1 - .../devtools/j2objc/GenerationTest.java | 3 +- .../google/devtools/j2objc/J2ObjCTest.java | 33 +++++++------------ .../devtools/j2objc/annotations/Example.java | 21 ------------ 4 files changed, 14 insertions(+), 44 deletions(-) delete mode 100644 translator/src/test/resources/com/google/devtools/j2objc/annotations/Example.java diff --git a/translator/Makefile b/translator/Makefile index 6235aa529a..d47544dcf3 100644 --- a/translator/Makefile +++ b/translator/Makefile @@ -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)/%) diff --git a/translator/src/test/java/com/google/devtools/j2objc/GenerationTest.java b/translator/src/test/java/com/google/devtools/j2objc/GenerationTest.java index 66f2352fd8..1e85397653 100644 --- a/translator/src/test/java/com/google/devtools/j2objc/GenerationTest.java +++ b/translator/src/test/java/com/google/devtools/j2objc/GenerationTest.java @@ -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(); } /** diff --git a/translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java b/translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java index e57ea3c9ba..4b90e34cc7 100644 --- a/translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java +++ b/translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java @@ -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(); @@ -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); @@ -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); @@ -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"); - } } diff --git a/translator/src/test/resources/com/google/devtools/j2objc/annotations/Example.java b/translator/src/test/resources/com/google/devtools/j2objc/annotations/Example.java deleted file mode 100644 index 410ade86dc..0000000000 --- a/translator/src/test/resources/com/google/devtools/j2objc/annotations/Example.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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.google.devtools.j2objc.annotations; - -import com.google.j2objc.annotations.ObjectiveCName; - -@ObjectiveCName("foo") -public class Example { -}