Skip to content

Commit

Permalink
Create a method that runs the J2ObjC pipeline, and test that method.
Browse files Browse the repository at this point in the history
	Change on 2015/02/26 by mthvedt <mthvedt@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=87271005
  • Loading branch information
mthvedt authored and tomball committed Feb 27, 2015
1 parent 0081c3a commit 974af8e
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 42 deletions.
71 changes: 44 additions & 27 deletions translator/src/main/java/com/google/devtools/j2objc/J2ObjC.java
Expand Up @@ -18,9 +18,11 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.Files;
import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.DeadCodeMap;
import com.google.devtools.j2objc.util.ErrorUtil;
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.PathClassLoader;
import com.google.devtools.j2objc.util.ProGuardUsageParser;

Expand All @@ -30,6 +32,7 @@
import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.logging.Level;
Expand All @@ -56,11 +59,6 @@ public class J2ObjC {

private static final Logger logger = Logger.getLogger(J2ObjC.class.getName());

private static void exit() {
Options.deleteTemporaryDirectory();
System.exit(ErrorUtil.errorCount());
}

public static String getFileHeader(String sourceFileName) {
return String.format(Options.getFileHeader(), sourceFileName);
}
Expand Down Expand Up @@ -109,18 +107,13 @@ private static void initPlugins(String[] pluginPaths, String pluginOptionString)
}
}

public static void error(Exception e) {
logger.log(Level.SEVERE, "Exiting due to exception", e);
System.exit(1);
}

private static void checkErrors() {
int errors = ErrorUtil.errorCount();
if (Options.treatWarningsAsErrors()) {
errors += ErrorUtil.warningCount();
}
if (errors > 0) {
System.exit(1);
System.exit(errors);
}
}

Expand Down Expand Up @@ -148,11 +141,47 @@ private static DeadCodeMap loadDeadCodeMap() {
return null;
}

/**
* Runs the entire J2ObjC pipeline.
* @param fileArgs the files to process, same format as command-line args to {@link #main}.
*/
public static void run(List<String> fileArgs) {
try {
JdtParser parser = createParser();

PackageInfoPreProcessor packageInfoPreProcessor = new PackageInfoPreProcessor(parser);
packageInfoPreProcessor.processFiles(fileArgs);
if (ErrorUtil.errorCount() > 0) {
return;
}

if (Options.shouldPreProcess()) {
HeaderMappingPreProcessor headerMappingPreProcessor = new HeaderMappingPreProcessor(parser);
headerMappingPreProcessor.processFiles(fileArgs);
if (ErrorUtil.errorCount() > 0) {
return;
}
}

TranslationProcessor translationProcessor
= new TranslationProcessor(parser, loadDeadCodeMap());
translationProcessor.processFiles(fileArgs);
if (ErrorUtil.errorCount() > 0) {
return;
}
translationProcessor.postProcess();
} finally {
NameTable.cleanup();
Types.cleanup();
Options.deleteTemporaryDirectory();
}
}

/**
* Entry point for tool.
* Initializes {@link Options}, calls {@link #run}, and exits.
*
* @param args command-line arguments: flags and source file names
* @throws IOException
*/
public static void main(String[] args) {
if (args.length == 0) {
Expand All @@ -172,24 +201,12 @@ public static void main(String[] args) {
try {
initPlugins(Options.getPluginPathEntries(), Options.getPluginOptionString());
} catch (IOException e) {
error(e);
ErrorUtil.error(e.getMessage());
System.exit(1);
}

JdtParser parser = createParser();

PackageInfoPreProcessor packageInfoPreProcessor = new PackageInfoPreProcessor(parser);
packageInfoPreProcessor.processFiles(Arrays.asList(files));
run(Arrays.asList(files));

if (Options.shouldPreProcess()) {
HeaderMappingPreProcessor headerMappingPreProcessor = new HeaderMappingPreProcessor(parser);
headerMappingPreProcessor.processFiles(Arrays.asList(files));
}

TranslationProcessor translationProcessor = new TranslationProcessor(parser, loadDeadCodeMap());
translationProcessor.processFiles(Arrays.asList(files));
translationProcessor.postProcess();
checkErrors();

exit();
}
}
Expand Up @@ -145,7 +145,7 @@ public void processFiles(Iterable<String> files) {

if (ErrorUtil.errorCount() > 0) {
// True if any classpath entry is malformed, or batch compilation failed.
System.exit(ErrorUtil.errorCount());
return;
}

super.processFiles(files);
Expand Down
Expand Up @@ -14,7 +14,6 @@
package com.google.devtools.j2objc.util;

import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.file.JarredInputFile;
Expand Down
Expand Up @@ -286,7 +286,9 @@ public static void initialize() {

public static void cleanup() {
try {
instance.classLoader.close();
if (instance != null) {
instance.classLoader.close();
}
} catch (IOException e) {
// Ignore, any open files will be closed on exit.
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.MethodDeclaration;
import com.google.devtools.j2objc.ast.Statement;
Expand All @@ -40,9 +41,12 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
Expand Down Expand Up @@ -380,6 +384,29 @@ protected String getTranslatedFile(String fileName) throws IOException {
return Files.toString(f, Charset.defaultCharset());
}

/**
* When running Java tests in a build, there is no formal guarantee that these resources
* be available as filesystem files. This copies a resource to a file in the temp dir,
* and returns the new path.
* The given resource name is relative to the class to which this method belongs
* (which might be a subclass of GenerationTest, in a different package).
*/
public String getResourceAsFile(String resourceName) throws IOException {
URL url;
try {
url = getClass().getResource(resourceName).toURI().toURL();
} catch (URISyntaxException e) {
throw new IOException(e);
}
File file = new File(tempDir + "/resources/"
+ getClass().getPackage().getName().replace('.', File.separatorChar)
+ resourceName);
file.getParentFile().mkdirs();
OutputStream ostream = new FileOutputStream(file);
Resources.copy(url, ostream);
return file.getPath();
}

/**
* Asserts that the correct number of warnings were reported during the
* last translation.
Expand Down
141 changes: 141 additions & 0 deletions translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java
@@ -0,0 +1,141 @@
/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* 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;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Tests for {@link com.google.devtools.j2objc.J2ObjC#run(List)}.
*/
public class J2ObjCTest extends GenerationTest {
String jarPath;
String exampleJavaPath;
String packageInfoPath;

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

List<String> classpathEntries = Options.getClassPathEntries();
for (String entry : getComGoogleDevtoolsJ2objcPath()) {
classpathEntries.add(entry);
}
jarPath = getResourceAsFile("util/example.jar");
exampleJavaPath = getResourceAsFile("util/Example.java");
packageInfoPath = getResourceAsFile("util/package-info.java");
}

public void tearDown() throws Exception {
Options.getClassPathEntries().clear();
Options.setBatchTranslateMaximum(0);
super.tearDown();
}

private void makeAssertions(
String example_h, String example_m, String package_info_h, String package_info_m)
throws Exception {
// These assertions should hold for any generated files, including (in the future)
// combined jar generation.
assertTranslation(example_h, "Generated by the J2ObjC translator.");
assertTranslation(example_h, "#ifndef _CBTExample_H_");
assertTranslation(example_h, "interface CBTExample : NSObject");
assertTranslation(example_h, "- (instancetype)init;");
assertTranslation(example_h, "J2OBJC_EMPTY_STATIC_INIT(CBTExample)");
assertTranslation(example_h, "typedef CBTExample");
assertTranslation(example_h, "J2OBJC_TYPE_LITERAL_HEADER(CBTExample)");
assertTranslation(example_m, "@implementation CBTExample");
assertTranslation(example_m, "- (instancetype)init {");
assertTranslation(example_m, "+ (const J2ObjcClassInfo *)__metadata {");
assertTranslation(example_m, "J2OBJC_CLASS_TYPE_LITERAL_SOURCE(CBTExample)");
assertTranslation(package_info_h, "#ifndef _CBTpackage_info_H_");
assertTranslation(package_info_m, "#include \"IOSClass.h\"");
assertTranslation(
package_info_m, "#include \"com/google/j2objc/annotations/ObjectiveCName.h\"");
assertTranslation(package_info_m, "+ (IOSObjectArray *)__annotations {");

assertErrorCount(0);
}

private void makeAssertionsForJar() throws Exception {
String example_h = getTranslatedFile("com/google/test/Example.h");
String example_m = getTranslatedFile("com/google/test/Example.m");
String package_info_h = getTranslatedFile("com/google/test/package-info.h");
String package_info_m = getTranslatedFile("com/google/test/package-info.m");
// Test the file header comments.
assertTranslation(example_h, "source: jar:file:");
assertTranslation(example_m, "source: jar:file:");
assertTranslation(package_info_h, "source: jar:file:");
assertTranslation(package_info_m, "source: jar:file:");
assertTranslation(example_h, jarPath);
assertTranslation(example_m, jarPath);
assertTranslation(package_info_h, jarPath);
assertTranslation(package_info_m, jarPath);
assertTranslation(example_h, "com/google/test/Example.java");
assertTranslation(example_m, "com/google/test/Example.java");
assertTranslation(package_info_h, "com/google/test/package-info.java");
assertTranslation(package_info_m, "com/google/test/package-info.java");
// Test the includes
assertTranslation(example_m, "#include \"com/google/test/Example.h\"");
assertTranslation(package_info_m, "@interface ComGoogleTestpackage_info : NSObject");
assertTranslation(package_info_m, "@implementation ComGoogleTestpackage_info");
// All other assertions
makeAssertions(example_h, example_m, package_info_h, package_info_m);
}

public void testCompilingFromJar() throws Exception {
J2ObjC.run(Collections.singletonList(jarPath));
makeAssertionsForJar();
}

public void testBatchCompilingFromJar() throws Exception {
Options.setBatchTranslateMaximum(2);
J2ObjC.run(Collections.singletonList(jarPath));
makeAssertionsForJar();
}

private void makeAssertionsForJavaFiles() throws Exception {
String example_h = getTranslatedFile("com/google/devtools/j2objc/util/Example.h");
String example_m = getTranslatedFile("com/google/devtools/j2objc/util/Example.m");
String package_info_h = getTranslatedFile("com/google/devtools/j2objc/util/package-info.h");
String package_info_m = getTranslatedFile("com/google/devtools/j2objc/util/package-info.m");
// Test the file header comments.
assertTranslation(example_h, exampleJavaPath);
assertTranslation(example_m, exampleJavaPath);
assertTranslation(package_info_h, packageInfoPath);
assertTranslation(package_info_m, packageInfoPath);
// Test the includes
assertTranslation(example_m, "#include \"com/google/devtools/j2objc/util/Example.h\"");
assertTranslation(
package_info_m, "@interface ComGoogleDevtoolsJ2objcUtilpackage_info : NSObject");
assertTranslation(package_info_m, "@implementation ComGoogleDevtoolsJ2objcUtilpackage_info");
// All other assertions
makeAssertions(example_h, example_m, package_info_h, package_info_m);
}

public void testCompilingFromFiles() throws Exception {
J2ObjC.run(Arrays.asList(exampleJavaPath, packageInfoPath));
makeAssertionsForJavaFiles();
}

public void testBatchCompilingFromFiles() throws Exception {
Options.setBatchTranslateMaximum(2);
J2ObjC.run(Arrays.asList(exampleJavaPath, packageInfoPath));
makeAssertionsForJavaFiles();
}
}
Expand Up @@ -62,12 +62,11 @@
import com.google.devtools.j2objc.util.BindingUtilTest;
import com.google.devtools.j2objc.util.DeadCodeMapTest;
import com.google.devtools.j2objc.util.ErrorUtilTest;
import com.google.devtools.j2objc.util.FileUtilTest;
import com.google.devtools.j2objc.util.NameTableTest;
import com.google.devtools.j2objc.util.ProGuardUsageParserTest;
import com.google.devtools.j2objc.util.UnicodeUtilsTest;

import com.google.devtools.j2objc.util.FileUtilTest;

import junit.framework.Test;
import junit.framework.TestSuite;

Expand Down Expand Up @@ -101,6 +100,7 @@ public class SmallTests {
ImplementationImportCollectorTest.class,
InitializationNormalizerTest.class,
InnerClassExtractorTest.class,
J2ObjCTest.class,
JavaCloneWriterTest.class,
JavaToIOSMethodTranslatorTest.class,
LineDirectivesTest.class,
Expand Down

0 comments on commit 974af8e

Please sign in to comment.