[FalconJX] full AS project test capability Added the ability to test full AS projects to TestBase.java. Since some functionalities of AS only become apparent when compiling projects consisting of 2 or more classes, this ability is needed as part of the full test suite.
Note: this commit adds 2 libraries to the compiler.jx.tests build path: '/compiler.jx/lib/commons-io.jar' and '/compiler.jx/lib/google/closure-compiler/compiler.jar', both of which are already part of the compiler.jx project. Signed-off-by: Erik de Bruin <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/248da586 Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/248da586 Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/248da586 Branch: refs/heads/develop Commit: 248da5867e49f6a5f59c4fd455fdf4a1152ba402 Parents: 0fa98cf Author: Erik de Bruin <[email protected]> Authored: Wed Apr 10 12:02:40 2013 +0200 Committer: Erik de Bruin <[email protected]> Committed: Wed Apr 10 12:02:40 2013 +0200 ---------------------------------------------------------------------- compiler.jx.tests/.classpath | 2 + .../flex/compiler/internal/test/TestBase.java | 102 ++++++++++++++- 2 files changed, 103 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/248da586/compiler.jx.tests/.classpath ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/.classpath b/compiler.jx.tests/.classpath index fedc702..d268df0 100644 --- a/compiler.jx.tests/.classpath +++ b/compiler.jx.tests/.classpath @@ -5,5 +5,7 @@ <classpathentry combineaccessrules="false" kind="src" path="/compiler"/> <classpathentry kind="lib" path="lib/junit-4.10.jar"/> <classpathentry combineaccessrules="false" kind="src" path="/compiler.jx"/> + <classpathentry kind="lib" path="/compiler.jx/lib/google/closure-compiler/compiler.jar"/> + <classpathentry kind="lib" path="/compiler.jx/lib/commons-io.jar"/> <classpathentry kind="output" path="classes"/> </classpath> http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/248da586/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java b/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java index 7317422..45721a0 100644 --- a/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java +++ b/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java @@ -23,6 +23,7 @@ import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -38,11 +39,13 @@ import java.util.List; import org.apache.flex.compiler.codegen.as.IASEmitter; import org.apache.flex.compiler.codegen.mxml.IMXMLEmitter; +import org.apache.flex.compiler.config.Configurator; import org.apache.flex.compiler.driver.IBackend; import org.apache.flex.compiler.internal.codegen.as.ASFilterWriter; import org.apache.flex.compiler.internal.projects.FlexProject; import org.apache.flex.compiler.internal.projects.FlexProjectConfigurator; import org.apache.flex.compiler.internal.projects.ISourceFileHandler; +import org.apache.flex.compiler.internal.targets.JSTarget; import org.apache.flex.compiler.internal.tree.as.FunctionNode; import org.apache.flex.compiler.internal.workspaces.Workspace; import org.apache.flex.compiler.mxml.IMXMLNamespaceMapping; @@ -60,6 +63,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Ignore; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + @Ignore public class TestBase implements ITestBase { @@ -201,6 +207,95 @@ public class TestBase implements ITestBase return fileNode; } + protected List<String> compileProject(String inputFileName, String inputDirName) + { + List<String> compiledFileNames = new ArrayList<String>(); + + String mainFileName = inputDirName + File.separator + inputFileName + + inputFileExtension; + + addDependencies(); + + ICompilationUnit mainCU = Iterables + .getOnlyElement(workspace.getCompilationUnits( + FilenameNormalization.normalize(mainFileName), project)); + + Configurator projectConfigurator = backend.createConfigurator(); + + JSTarget target = (JSTarget) backend.createTarget(project, + projectConfigurator.getTargetSettings(null), null); + + target.build(mainCU, new ArrayList<ICompilerProblem>()); + + List<ICompilationUnit> reachableCompilationUnits = project + .getReachableCompilationUnitsInSWFOrder(ImmutableSet.of(mainCU)); + for (final ICompilationUnit cu : reachableCompilationUnits) + { + try + { + ICompilationUnit.UnitType cuType = cu.getCompilationUnitType(); + + if (cuType == ICompilationUnit.UnitType.AS_UNIT + || cuType == ICompilationUnit.UnitType.MXML_UNIT) + { + File outputRootDir = new File( + FilenameNormalization.normalize(tempDir + + File.separator + inputDirName)); + + String qname = cu.getQualifiedNames().get(0); + + compiledFileNames.add(qname.replace(".", "/")); + + final File outputClassFile = getOutputClassFile(qname + + "_output", outputRootDir); + + ASFilterWriter writer = backend.createWriterBuffer(project); + IASEmitter emitter = backend.createEmitter(writer); + IASBlockWalker walker = backend.createWalker(project, + (List<ICompilerProblem>) errors, emitter); + + walker.visitCompilationUnit(cu); + + //System.out.println(writer.toString()); + + BufferedOutputStream out = new BufferedOutputStream( + new FileOutputStream(outputClassFile)); + + out.write(writer.toString().getBytes()); + out.flush(); + out.close(); + } + } + catch (Exception e) + { + //System.out.println(e.getMessage()); + } + } + + return compiledFileNames; + } + + private File getOutputClassFile(String qname, File outputFolder) + { + String[] cname = qname.split("\\."); + String sdirPath = outputFolder + File.separator; + if (cname.length > 0) + { + for (int i = 0, n = cname.length - 1; i < n; i++) + { + sdirPath += cname[i] + File.separator; + } + + File sdir = new File(sdirPath); + if (!sdir.exists()) + sdir.mkdirs(); + + qname = cname[cname.length - 1]; + } + + return new File(sdirPath + qname + "." + backend.getOutputExtension()); + } + protected IMXMLFileNode compileMXML(String input) { return compileMXML(input, false, ""); @@ -320,11 +415,16 @@ public class TestBase implements ITestBase + File.separator + sourceDir + File.separator + fileName + (isJS ? ".js" : inputFileExtension)); + return readCodeFile(testFile); + } + + protected String readCodeFile(File file) + { String code = ""; try { BufferedReader in = new BufferedReader(new InputStreamReader( - new FileInputStream(testFile), "UTF8")); + new FileInputStream(file), "UTF8")); String line = in.readLine();
