diff options
author | wisberg <wisberg> | 2002-12-16 18:14:27 +0000 |
---|---|---|
committer | wisberg <wisberg> | 2002-12-16 18:14:27 +0000 |
commit | 1025cde05abe95be6ca65aab3ba126258efc647f (patch) | |
tree | 7d40d6fa06fef869e4d38cbe44ce431011e2eff8 /testing-util | |
parent | d842c4f1139629c1f062b74ba818d233b2c31043 (diff) | |
download | aspectj-1025cde05abe95be6ca65aab3ba126258efc647f.tar.gz aspectj-1025cde05abe95be6ca65aab3ba126258efc647f.zip |
initial version
Diffstat (limited to 'testing-util')
15 files changed, 1390 insertions, 0 deletions
diff --git a/testing-util/.classpath b/testing-util/.classpath new file mode 100644 index 000000000..c6752e35d --- /dev/null +++ b/testing-util/.classpath @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="src" path="testsrc"/> + <classpathentry kind="var" path="JRE_LIB" rootpath="JRE_SRCROOT" sourcepath="JRE_SRC"/> + <classpathentry kind="lib" path="/lib/junit/junit.jar" sourcepath="/lib/junit/junit-src.jar"/> + <classpathentry kind="src" path="/util"/> + <classpathentry kind="lib" path="/lib/jdiff/jdiff.jar"/> + <classpathentry kind="src" path="/bridge"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/testing-util/.project b/testing-util/.project new file mode 100644 index 000000000..917eccbff --- /dev/null +++ b/testing-util/.project @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>testing-util</name> + <comment></comment> + <projects> + <project>bridge</project> + <project>util</project> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/testing-util/TestUtilTest.launch b/testing-util/TestUtilTest.launch new file mode 100644 index 000000000..645c3ee88 --- /dev/null +++ b/testing-util/TestUtilTest.launch @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<launchConfiguration type="org.eclipse.jdt.junit.launchconfig"> + <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.aspectj.testing.util.TestUtilTest"/> + <booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/> + <booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/> + <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="testing-util"/> + <listAttribute key="org.eclipse.jdt.launching.CLASSPATH"> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry containerPath="JRE_LIB" path="2" + sourceAttachmentPath="JRE_SRC" sourceRootPath="JRE_SRCROOT" type="3"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry path="3" projectName="testing-util" type="1"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry internalArchive="/lib/junit/junit.jar" path="3" + sourceAttachmentPath="/lib/junit/junit-src.jar" type="2"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry path="3" projectName="util" type="1"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry internalArchive="/lib/jdiff/jdiff.jar" path="3" type="2"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry path="3" projectName="bridge" type="1"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry path="3" projectName="bcweaver" type="1"/> +"/> + <listEntry value="<?xml version="1.0" encoding="UTF-8"?> +<runtimeClasspathEntry internalArchive="/lib/bcel/bcel.jar" path="3" type="2"/> +"/> + </listAttribute> + <stringAttribute key="org.eclipse.debug.ui.target_run_perspective" value="perspective_default"/> + <stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.jdt.debug.ui.javaSourceLocator"/> + <stringAttribute key="org.eclipse.debug.ui.target_debug_perspective" value="perspective_default"/> +</launchConfiguration> diff --git a/testing-util/src/.cvsignore b/testing-util/src/.cvsignore new file mode 100644 index 000000000..a3f0b1b77 --- /dev/null +++ b/testing-util/src/.cvsignore @@ -0,0 +1 @@ +*.lst diff --git a/testing-util/src/org/aspectj/testing/util/TestUtil.java b/testing-util/src/org/aspectj/testing/util/TestUtil.java new file mode 100644 index 000000000..b33e1fac9 --- /dev/null +++ b/testing-util/src/org/aspectj/testing/util/TestUtil.java @@ -0,0 +1,616 @@ +/* ******************************************************************* + * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + +package org.aspectj.testing.util; + +import org.aspectj.bridge.IMessageHandler; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.util.FileUtil; +import org.aspectj.util.LangUtil; +import org.aspectj.util.Reflection; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import jdiff.text.FileLine; +import jdiff.util.Diff; +import jdiff.util.DiffNormalOutput; +import junit.framework.Assert; +import junit.framework.TestCase; +import junit.runner.TestCaseClassLoader; + +/** + * Things that junit should perhaps have, but doesn't. + * Note the file-comparison methods require JDiff to run, + * but JDiff types are not required to resolve this class. + * Also, the bytecode weaver is required to compare class + * files, but not to compare other files. + */ + +public final class TestUtil { + + private TestUtil() { + super(); + } + + // ---- arrays + + public static void assertArrayEquals(String msg, Object[] expected, Object[] found) { + TestCase.assertEquals(msg, Arrays.asList(expected), Arrays.asList(found)); + } + + // ---- unordered + + public static void assertSetEquals(Collection expected, Collection found) { + assertSetEquals(null, expected, found); + } + + public static void assertSetEquals(String msg, Object[] expected, Object[] found) { + assertSetEquals(msg, Arrays.asList(expected), Arrays.asList(found)); + } + + public static void assertSetEquals( + String msg, + Collection expected, + Collection found) { + msg = (msg == null) ? "" : msg + ": "; + + Set results1 = new HashSet(found); + results1.removeAll(expected); + + Set results2 = new HashSet(expected); + results2.removeAll(found); + + if (results1.isEmpty()) { + TestCase.assertTrue( + msg + "Expected but didn't find: " + results2.toString(), + results2.isEmpty()); + } else if (results2.isEmpty()) { + TestCase.assertTrue( + msg + "Didn't expect: " + results1.toString(), + results1.isEmpty()); + } else { + TestCase.assertTrue( + msg + + "Expected but didn't find: " + + results2.toString() + + "\nDidn't expect: " + + results1.toString(), + false); + } + } + + // ---- objects + + public static void assertCommutativeEquals(Object a, Object b, boolean should) { + TestCase.assertEquals(a + " equals " + b, should, a.equals(b)); + TestCase.assertEquals(b + " equals " + a, should, b.equals(a)); + assertHashEquals(a, b, should); + } + + private static void assertHashEquals(Object s, Object t, boolean should) { + if (should) { + TestCase.assertTrue( + s + " does not hash to same as " + t, + s.hashCode() == t.hashCode()); + } else { + if (s.hashCode() == t.hashCode()) { + System.err.println("warning: hash collision with hash = " + t.hashCode()); + System.err.println(" for " + s); + System.err.println(" and " + t); + } + } + } + + // -- reflective stuff + + public static void runMain(String classPath, String className) { + runMethod(classPath, className, "main", new Object[] { new String[0] }); + } + + + public static Object runMethod(String classPath, String className, String methodName, Object[] args) { + classPath += File.pathSeparator + System.getProperty("java.class.path"); + + ClassLoader loader = new TestCaseClassLoader(classPath); + + Class c=null; + try { + c = loader.loadClass(className); + } catch (ClassNotFoundException e) { + Assert.assertTrue("unexpected exception: " + e, false); + } + return Reflection.invokestaticN(c, methodName, args); + } + + + /** + * Checks that two multi-line strings have the same value. + * Each line is trimmed before comparision + * Produces an error on the particular line of conflict + */ + public static void assertMultiLineStringEquals(String message, String s1, String s2) { + try { + BufferedReader r1 = new BufferedReader(new StringReader(s1)); + BufferedReader r2 = new BufferedReader(new StringReader(s2)); + + + List lines = new ArrayList(); + String l1, l2; + + int index = 1; + while(true) { + l1 = readNonBlankLine(r1); + l2 = readNonBlankLine(r2); + if (l1 == null || l2 == null) break; + if (l1.equals(l2)) { + lines.add(l1); + } else { + showContext(lines); + Assert.assertEquals(message +"(line " + index +")", l1, l2); + } + index++; + } + if (l1 != null) showContext(lines); + Assert.assertTrue(message + ": unexpected " + l1, l1 == null); + if (l2 != null) showContext(lines); + Assert.assertTrue(message + ": unexpected " + l2, l2 == null); + } catch (IOException ioe) { + Assert.assertTrue(message + ": caught " + ioe.getMessage(), false); + } + } + + private static void showContext(List lines) { + int n = lines.size(); + for (int i = Math.max(0, n - 8); i < n; i++) { + System.err.println(lines.get(i)); + } + } + + private static String readNonBlankLine(BufferedReader r) throws IOException { + String l = r.readLine(); + if (l == null) return null; + l = l.trim(); + // comment to include comments when reading + int commentLoc = l.indexOf("//"); + if (-1 != commentLoc) { + l = l.substring(0, commentLoc).trim(); + } + if ("".equals(l)) return readNonBlankLine(r); + return l; + } + + /** + * If there is an expected dir, expect each file in its subtree + * to match a corresponding actual file in the base directory. + * @return boolean + */ + public static boolean sameDirectoryContents( + final IMessageHandler handler, + final File expectedBaseDir, + final File actualBaseDir, + final boolean fastFail) { + LangUtil.throwIaxIfNull(handler, "handler"); + FileUtil.throwIaxUnlessCanReadDir(actualBaseDir, "actualBaseDir"); + if (!FileUtil.canReadDir(expectedBaseDir)) { + MessageUtil.fail(handler, " expected dir not found: " + expectedBaseDir); + return false; + } + if (!FileUtil.canReadDir(actualBaseDir)) { + MessageUtil.fail(handler, " actual dir not found: " + actualBaseDir); + return false; + } + String[] paths = FileUtil.listFiles(expectedBaseDir); + boolean result = true; + for (int i = 0; i < paths.length; i++) { + if (!sameFiles(handler, expectedBaseDir, actualBaseDir, paths[i]) && !result) { + result = false; + if (fastFail) { + break; + } + } + } + return result; + } + + //------------ File-comparison utilities (XXX need their own class...) + /** + * Compare two files, line by line, and report differences as one FAIL message + * if a handler is supplied. This preprocesses .class files by disassembling. + * @param handler the IMessageHandler for any FAIL messages (null to ignore) + * @param expectedFile the File path to the canonical file + * @param actualFile the File path to the actual file, if any + * @return true if the input files are the same, based on per-line comparisons + */ + public static boolean sameFiles ( + IMessageHandler handler, + File expectedFile, + File actualFile) { + return doSameFile(handler, null, null, expectedFile, actualFile); + } + + /** + * Compare two files, line by line, and report differences as one FAIL message + * if a handler is supplied. This preprocesses .class files by disassembling. + * This method assumes that the files are at the same offset from two + * respective base directories. + * @param handler the IMessageHandler for any FAIL messages (null to ignore) + * @param expectedBaseDir the File path to the canonical file base directory + * @param actualBaseDir the File path to the actual file base directory + * @param path the String path offset from the base directories + * @return true if the input files are the same, based on per-line comparisons + */ + public static boolean sameFiles ( + IMessageHandler handler, + File expectedBaseDir, + File actualBaseDir, + String path) { + File actualFile = new File(actualBaseDir, path); + File expectedFile = new File(expectedBaseDir, path); + return doSameFile(handler, expectedBaseDir, actualBaseDir, expectedFile, actualFile); + } + + /** + * This does the work, selecting a lineator subclass and converting public + * API's to JDiff APIs for comparison. + * Currently, all jdiff interfaces are method-local, so this class with load + * without it; if we do use it, we can avoid the duplication. + */ + private static boolean doSameFile( + IMessageHandler handler, + File expectedBaseDir, + File actualBaseDir, + File expectedFile, + File actualFile) { + String path = expectedFile.getPath(); + // XXX permit user to specify lineator + ILineator lineator = Lineator.TEXT; + if (path.endsWith(".class")) { + if (ClassLineator.haveDisassembler() ) { + lineator = Lineator.CLASS; + } else { + MessageUtil.abort(handler, "skipping - dissassembler not available"); + return false; + } + } + CanonicalLine[] actualLines = null; + CanonicalLine[] expectedLines = null; + try { + actualLines = lineator.getLines(handler, actualFile, actualBaseDir); + expectedLines = lineator.getLines(handler, expectedFile, expectedBaseDir); + } catch (IOException e) { + MessageUtil.fail(handler, "rendering lines ", e); + return false; + } + if (!LangUtil.isEmpty(actualLines) && !LangUtil.isEmpty(expectedLines)) { + // here's the transmutation back to jdiff - extract if publishing JDiff + CanonicalLine[][] clines = new CanonicalLine[][] { expectedLines, actualLines }; + FileLine[][] flines = new FileLine[2][]; + for (int i = 0; i < clines.length; i++) { + CanonicalLine[] cline = clines[i]; + FileLine[] fline = new FileLine[cline.length]; + for (int j = 0; j < fline.length; j++) { + fline[j] = new FileLine(cline[j].canonical, cline[j].line); + } + flines[i] = fline; + } + + Diff.change edits = new Diff(flines[0], flines[1]).diff_2(false); + if ((null == edits) || (0 == (edits.inserted + edits.deleted))) { + // XXX confirm with jdiff that null means no edits + return true; + } else { + //String m = render(handler, edits, flines[0], flines[1]); + StringWriter writer = new StringWriter(); + DiffNormalOutput out = new DiffNormalOutput(flines[0], flines[1]); + out.setOut(writer); + out.setLineSeparator(LangUtil.EOL); + try { + out.writeScript(edits); + } catch (IOException e) { + MessageUtil.fail(handler, "rendering edits", e); + } finally { + if (null != writer) { + try { writer.close(); } + catch (IOException e) { + MessageUtil.fail(handler, "closing after rendering edits", e); + } + } + } + String message = "diff between " + + path + + " in expected dir " + + expectedBaseDir + + " and actual dir " + + actualBaseDir + + LangUtil.EOL + + writer.toString(); + MessageUtil.fail(handler, message); + } + } + return false; + } + + + /** component that reduces file to CanonicalLine[] */ + public static interface ILineator { + /** Lineator suitable for text files */ + public static final ILineator TEXT = new TextLineator(); + + /** Lineator suitable for class files (disassembles first) */ + public static final ILineator CLASS = new ClassLineator(); + + /** + * Reduce file to CanonicalLine[]. + * @param handler the IMessageHandler for errors (may be null) + * @param file the File to render + * @param basedir the File for the base directory (may be null) + * @return CanonicalLine[] of lines - not null, but perhaps empty + */ + CanonicalLine[] getLines( + IMessageHandler handler, + File file, + File basedir) throws IOException; + } + + /** alias for jdiff FileLine to avoid client binding */ + public static class CanonicalLine { + public static final CanonicalLine[] NO_LINES = new CanonicalLine[0]; + + /** canonical variant of line for comparison */ + public final String canonical; + + /** actual line, for logging */ + public final String line; + public CanonicalLine(String canonical, String line) { + this.canonical = canonical; + this.line = line; + } + public String toString() { + return line; + } + } + + private abstract static class Lineator implements ILineator { + /** + * Reduce file to CanonicalLine[]. + * @param handler the IMessageHandler for errors (may be null) + * @param file the File to render + * @param basedir the File for the base directory (may be null) + */ + public CanonicalLine[] getLines( + IMessageHandler handler, + File file, + File basedir) + throws IOException { + + if (!file.canRead() || !file.isFile()) { + MessageUtil.error(handler, "not readable file: " + basedir + " - " + file); + return null; + } + // capture file as FileLine[] + InputStream in = null; + String path = FileUtil.normalizedPath(file, basedir); + LineStream capture = new LineStream(); + try { + lineate(capture, handler, basedir, file); + } catch (IOException e) { + MessageUtil.fail(handler, + "NormalizedCompareFiles IOException reading " + file, e); + return null; + } finally { + if (null != in) { + try { in.close(); } + catch (IOException e) {} // ignore + } + capture.flush(); + capture.close(); + } + String missed = capture.getMissed(); + if (!LangUtil.isEmpty(missed)) { + MessageUtil.warn(handler, + "NormalizedCompareFiles missed input: " + + missed); + return null; + } else { + String[] lines = capture.getLines(); + CanonicalLine[] result = new CanonicalLine[lines.length]; + for (int i = 0; i < lines.length; i++) { + result[i] = new CanonicalLine(lines[i], lines[i]); + } + return result; + } + } + + protected abstract void lineate( + PrintStream sink, + IMessageHandler handler, + File basedir, + File file) throws IOException; + } + + private static class TextLineator extends Lineator { + + protected void lineate( + PrintStream sink, + IMessageHandler handler, + File basedir, + File file) throws IOException { + InputStream in = null; + try { + in = new FileInputStream(file); + FileUtil.copyStream(new DataInputStream(in), sink); + } finally { + try { in.close(); } + catch (IOException e) {} // ignore + } + } + } + + public static class ClassLineator extends Lineator { + + protected void lineate( + PrintStream sink, + IMessageHandler handler, + File basedir, + File file) throws IOException { + String name = FileUtil.fileToClassName(basedir, file); + // XXX re-enable preflight? +// if ((null != basedir) && (path.length()-6 != name.length())) { +// MessageUtil.error(handler, "unexpected class name \"" +// + name + "\" for path " + path); +// return null; +// } + disassemble(handler, basedir, name, sink); + } + + public static boolean haveDisassembler() { + try { + return (null != Class.forName("org.aspectj.weaver.bcel.LazyClassGen")); + } catch (ClassNotFoundException e) { + // XXX fix + //System.err.println(e.getMessage()); + //e.printStackTrace(System.err); + return false; + } + } + + /** XXX dependency on bcweaver/bcel */ + private static void disassemble( + IMessageHandler handler, + File basedir, + String name, + PrintStream out) throws IOException { + // LazyClassGen.disassemble(FileUtil.normalizedPath(basedir), name, capture); + + Throwable thrown = null; + String basedirPath = FileUtil.normalizedPath(basedir); + // XXX use reflection utilities to invoke dissassembler? + try { + // XXX need test to detect when this is refactored + Class c = Class.forName("org.aspectj.weaver.bcel.LazyClassGen"); + Method m = c.getMethod("disassemble", + new Class[] {String.class, String.class, PrintStream.class}); + m.invoke(null, new Object[] { basedirPath, name, out}); + } catch (ClassNotFoundException e) { + thrown = e; + } catch (NoSuchMethodException e) { + thrown = e; + } catch (IllegalAccessException e) { + thrown = e; + } catch (InvocationTargetException e) { + Throwable t = e.getTargetException(); + if (t instanceof IOException) { + throw (IOException) t; + } + thrown = t; + } + if (null != thrown) { + MessageUtil.fail(handler, "disassembling " + name + " path: " + basedirPath, + thrown); + } + } + } + + + /** + * Capture PrintStream output to String[] + * (delimiting component String on println()), + * also showing any missed text. + */ + public static class LineStream extends PrintStream { + StringBuffer sb = new StringBuffer(); + ByteArrayOutputStream missed; + ArrayList sink; + public LineStream() { + super(new ByteArrayOutputStream()); + this.sink = new ArrayList(); + missed = (ByteArrayOutputStream) out; + } + + /** @return any text not captured by our overrides */ + public String getMissed() { + return missed.toString(); + } + + /** clear captured lines (but not missed text) */ + public void clear() { + sink.clear(); + } + + /** + * Get String[] of lines printed, + * delimited by println(..) calls. + * @return lines printed, exclusive of any not yet terminated by newline + */ + public String[] getLines() { + return (String[]) sink.toArray(new String[0]); + } + + // ---------- PrintStream overrides + public void println(Object x) { + println(x.toString()); + } + + public void print(Object obj) { + print(obj.toString()); + } + + public void println(char c) { + sb.append(c); + println(); + } + public void println(char[] c) { + sb.append(c); + println(); + } + public void print(char c) { + sb.append(c); + } + + public void print(char[] c) { + sb.append(c); + } + + public void println(String s) { + print(s); + println(); + } + public void print(String s) { + sb.append(s); + } + public void println() { + String line = sb.toString(); + sink.add(line); + sb.setLength(0); + } + } + + +} diff --git a/testing-util/testdata/testCompareClassFiles/org/aspectj/testing/util/TestCompareClassFile$1.class b/testing-util/testdata/testCompareClassFiles/org/aspectj/testing/util/TestCompareClassFile$1.class Binary files differnew file mode 100644 index 000000000..c5db4e907 --- /dev/null +++ b/testing-util/testdata/testCompareClassFiles/org/aspectj/testing/util/TestCompareClassFile$1.class diff --git a/testing-util/testdata/testCompareClassFiles/org/aspectj/testing/util/TestCompareClassFile.class b/testing-util/testdata/testCompareClassFiles/org/aspectj/testing/util/TestCompareClassFile.class Binary files differnew file mode 100644 index 000000000..52c60e6ee --- /dev/null +++ b/testing-util/testdata/testCompareClassFiles/org/aspectj/testing/util/TestCompareClassFile.class diff --git a/testing-util/testdata/testCompareTextFiles/differentFile/actual/TestUtilTest.java b/testing-util/testdata/testCompareTextFiles/differentFile/actual/TestUtilTest.java new file mode 100644 index 000000000..bb8d92904 --- /dev/null +++ b/testing-util/testdata/testCompareTextFiles/differentFile/actual/TestUtilTest.java @@ -0,0 +1,95 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * This file is part of the compiler and core tools for the AspectJ(tm) + * programming language; see http://aspectj.org + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is AspectJ. + * + * The Initial Developer of the Original Code is Xerox Corporation. Portions + * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation. + * All Rights Reserved. + * + * Contributor(s): + */ +package org.aspectj.testing.util; + +import org.aspectj.bridge.IMessageHolder; +import org.aspectj.bridge.MessageHandler; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * + */ +public class TestUtilTest extends TestCase { + + public TestUtilTest(String name) { + super(name); + } + + public void testFileCompareNonClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File thisFile = new File("testsrc/org/aspectj/testing/util/TestUtilTest.java"); + //File thisFile = new File("src/testing-util.lst"); + assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile)); + + File tempFile = File.createTempFile("TestUtilTest", ".tmp"); + FileUtil.copyFile(thisFile, tempFile); + long len = tempFile.length(); + assertTrue(0 != len); + long tlen = thisFile.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile)); + try { + String path = thisFile.getName(); + File basedir = tempFile.getParentFile(); + File renamed = new File(basedir, path); + if (!tempFile.renameTo(renamed)) { + MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed); + } else { + len = renamed.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path)); + } + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + holder.clear(); + } + tempFile.delete(); + } + } + + /** added line here */ + public void testFileCompareClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File classBase = new File("testdata/testCompareClassFiles"); + String path = "org/aspectj/testing/util/TestCompareClassFile.class"; + File classFile = new File(classBase, path); + + try { + assertTrue(TestUtil.sameFiles(holder, classFile, classFile)); + assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path)); + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + } + } + } + +} diff --git a/testing-util/testdata/testCompareTextFiles/differentFile/expected/TestUtilTest.java b/testing-util/testdata/testCompareTextFiles/differentFile/expected/TestUtilTest.java new file mode 100644 index 000000000..fb87e8ad9 --- /dev/null +++ b/testing-util/testdata/testCompareTextFiles/differentFile/expected/TestUtilTest.java @@ -0,0 +1,94 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * This file is part of the compiler and core tools for the AspectJ(tm) + * programming language; see http://aspectj.org + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is AspectJ. + * + * The Initial Developer of the Original Code is Xerox Corporation. Portions + * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation. + * All Rights Reserved. + * + * Contributor(s): + */ +package org.aspectj.testing.util; + +import org.aspectj.bridge.IMessageHolder; +import org.aspectj.bridge.MessageHandler; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * + */ +public class TestUtilTest extends TestCase { + + public TestUtilTest(String name) { + super(name); + } + + public void testFileCompareNonClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File thisFile = new File("testsrc/org/aspectj/testing/util/TestUtilTest.java"); + //File thisFile = new File("src/testing-util.lst"); + assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile)); + + File tempFile = File.createTempFile("TestUtilTest", ".tmp"); + FileUtil.copyFile(thisFile, tempFile); + long len = tempFile.length(); + assertTrue(0 != len); + long tlen = thisFile.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile)); + try { + String path = thisFile.getName(); + File basedir = tempFile.getParentFile(); + File renamed = new File(basedir, path); + if (!tempFile.renameTo(renamed)) { + MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed); + } else { + len = renamed.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path)); + } + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + holder.clear(); + } + tempFile.delete(); + } + } + + public void testFileCompareClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File classBase = new File("testdata/testCompareClassFiles"); + String path = "org/aspectj/testing/util/TestCompareClassFile.class"; + File classFile = new File(classBase, path); + + try { + assertTrue(TestUtil.sameFiles(holder, classFile, classFile)); + assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path)); + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + } + } + } + +} diff --git a/testing-util/testdata/testCompareTextFiles/sameFile/actual/TestUtilTest.java b/testing-util/testdata/testCompareTextFiles/sameFile/actual/TestUtilTest.java new file mode 100644 index 000000000..fb87e8ad9 --- /dev/null +++ b/testing-util/testdata/testCompareTextFiles/sameFile/actual/TestUtilTest.java @@ -0,0 +1,94 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * This file is part of the compiler and core tools for the AspectJ(tm) + * programming language; see http://aspectj.org + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is AspectJ. + * + * The Initial Developer of the Original Code is Xerox Corporation. Portions + * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation. + * All Rights Reserved. + * + * Contributor(s): + */ +package org.aspectj.testing.util; + +import org.aspectj.bridge.IMessageHolder; +import org.aspectj.bridge.MessageHandler; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * + */ +public class TestUtilTest extends TestCase { + + public TestUtilTest(String name) { + super(name); + } + + public void testFileCompareNonClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File thisFile = new File("testsrc/org/aspectj/testing/util/TestUtilTest.java"); + //File thisFile = new File("src/testing-util.lst"); + assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile)); + + File tempFile = File.createTempFile("TestUtilTest", ".tmp"); + FileUtil.copyFile(thisFile, tempFile); + long len = tempFile.length(); + assertTrue(0 != len); + long tlen = thisFile.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile)); + try { + String path = thisFile.getName(); + File basedir = tempFile.getParentFile(); + File renamed = new File(basedir, path); + if (!tempFile.renameTo(renamed)) { + MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed); + } else { + len = renamed.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path)); + } + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + holder.clear(); + } + tempFile.delete(); + } + } + + public void testFileCompareClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File classBase = new File("testdata/testCompareClassFiles"); + String path = "org/aspectj/testing/util/TestCompareClassFile.class"; + File classFile = new File(classBase, path); + + try { + assertTrue(TestUtil.sameFiles(holder, classFile, classFile)); + assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path)); + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + } + } + } + +} diff --git a/testing-util/testdata/testCompareTextFiles/sameFile/expected/TestUtilTest.java b/testing-util/testdata/testCompareTextFiles/sameFile/expected/TestUtilTest.java new file mode 100644 index 000000000..fb87e8ad9 --- /dev/null +++ b/testing-util/testdata/testCompareTextFiles/sameFile/expected/TestUtilTest.java @@ -0,0 +1,94 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * This file is part of the compiler and core tools for the AspectJ(tm) + * programming language; see http://aspectj.org + * + * The contents of this file are subject to the Mozilla Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is AspectJ. + * + * The Initial Developer of the Original Code is Xerox Corporation. Portions + * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation. + * All Rights Reserved. + * + * Contributor(s): + */ +package org.aspectj.testing.util; + +import org.aspectj.bridge.IMessageHolder; +import org.aspectj.bridge.MessageHandler; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * + */ +public class TestUtilTest extends TestCase { + + public TestUtilTest(String name) { + super(name); + } + + public void testFileCompareNonClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File thisFile = new File("testsrc/org/aspectj/testing/util/TestUtilTest.java"); + //File thisFile = new File("src/testing-util.lst"); + assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile)); + + File tempFile = File.createTempFile("TestUtilTest", ".tmp"); + FileUtil.copyFile(thisFile, tempFile); + long len = tempFile.length(); + assertTrue(0 != len); + long tlen = thisFile.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile)); + try { + String path = thisFile.getName(); + File basedir = tempFile.getParentFile(); + File renamed = new File(basedir, path); + if (!tempFile.renameTo(renamed)) { + MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed); + } else { + len = renamed.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path)); + } + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + holder.clear(); + } + tempFile.delete(); + } + } + + public void testFileCompareClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File classBase = new File("testdata/testCompareClassFiles"); + String path = "org/aspectj/testing/util/TestCompareClassFile.class"; + File classFile = new File(classBase, path); + + try { + assertTrue(TestUtil.sameFiles(holder, classFile, classFile)); + assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path)); + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + } + } + } + +} diff --git a/testing-util/testsrc/TestingUtilModuleTests.java b/testing-util/testsrc/TestingUtilModuleTests.java new file mode 100644 index 000000000..32084c1a2 --- /dev/null +++ b/testing-util/testsrc/TestingUtilModuleTests.java @@ -0,0 +1,32 @@ +/* ******************************************************************* + * Copyright (c) 1999-2001 Xerox Corporation, + * 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + + +// default package + +import org.aspectj.testing.util.UtilTests; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestingUtilModuleTests extends TestCase { + + public static Test suite() { + TestSuite suite = new TestSuite(TestingUtilModuleTests.class.getName()); + suite.addTest(UtilTests.suite()); + return suite; + } + + public TestingUtilModuleTests(String name) { super(name); } +} diff --git a/testing-util/testsrc/org/aspectj/testing/util/TestCompareClassFile.java b/testing-util/testsrc/org/aspectj/testing/util/TestCompareClassFile.java new file mode 100644 index 000000000..e4dcf8050 --- /dev/null +++ b/testing-util/testsrc/org/aspectj/testing/util/TestCompareClassFile.java @@ -0,0 +1,148 @@ +/* ******************************************************************* + * Copyright (c) 1999-2001 Xerox Corporation, + * 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + +package org.aspectj.testing.util; + +import org.aspectj.util.LangUtil; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.SortedSet; +import java.util.StringTokenizer; +import java.util.TreeSet; + +/** + * This is source for a sample .class file. + * It is compiled and the corresponding .class files are + * checked in under the testdata directory. + * It has no other purpose. + */ +public class TestCompareClassFile implements Runnable { + public static final String STATIC_CONST = "STATIC_CONST"; + public static void main(String[] args) { + // tc static references + long l = Math.abs(System.currentTimeMillis()); + String s = STATIC_CONST + " is constant"; + } + public static void runStatic() { + } + private static void privateRunStatic() { + } + static void defaultRunStatic() { + } + protected static void protectedRunStatic() { + } + + private long privateLong; + private final Object privateFinalObject; + + private TestCompareClassFile() { + super(); + privateLong = System.currentTimeMillis(); + // method-local inner class + privateFinalObject = new Runnable() { public void run(){}}; + } + + /** implement Runnable */ + public void run() { + } + private void privateRun() { + } + void defaultRun() { + } + protected void protectedRun() { + } + + // ------- misc stolen utility code + // Collections Util + public static List getListInMap(Map map, Object key) { + List list = (List)map.get(key); + if (list == null) { + list = new ArrayList(); + map.put(key, list); + } + return list; + } + + public static SortedSet getSortedSetInMap(Map map, Object key) { + SortedSet list = (SortedSet)map.get(key); + if (list == null) { + list = new TreeSet(); + map.put(key, list); + } + return list; + } + + // LangUtil + /** + * Make a copy of the array. + * @return an array with the same component type as source + * containing same elements, even if null. + * @throws IllegalArgumentException if source is null + */ + public static final Object[] copy(Object[] source) { + final Class c = source.getClass().getComponentType(); + Object[] result = (Object[]) Array.newInstance(c, source.length); + System.arraycopy(source, 0, result, 0, result.length); + return result; + } + /** + * Trim ending lines from a StringBuffer, + * clipping to maxLines and further removing any number of + * trailing lines accepted by checker. + * @param checker returns true if trailing line should be elided. + * @param stack StringBuffer with lines to elide + * @param maxLines int for maximum number of resulting lines + */ + static void elideEndingLines(StringBuffer stack, int maxLines) { + if ((null == stack) || (0 == stack.length())) { + return; + } + final LinkedList lines = new LinkedList(); + StringTokenizer st = new StringTokenizer(stack.toString(),"\n\r"); + while (st.hasMoreTokens() && (0 < --maxLines)) { + lines.add(st.nextToken()); + } + st = null; + + String line; + int elided = 0; + while (!lines.isEmpty()) { + line = (String) lines.getLast(); + if (null == line) { + break; + } else { + elided++; + lines.removeLast(); + } + } + if ((elided > 0) || (maxLines < 1)) { + final int EOL_LEN = LangUtil.EOL.length(); + int totalLength = 0; + while (!lines.isEmpty()) { + totalLength += EOL_LEN + ((String) lines.getFirst()).length(); + lines.removeFirst(); + } + if (stack.length() > totalLength) { + stack.setLength(totalLength); + if (elided > 0) { + stack.append(" (... " + elided + " lines...)"); + } + } + } + } + +} diff --git a/testing-util/testsrc/org/aspectj/testing/util/TestUtilTest.java b/testing-util/testsrc/org/aspectj/testing/util/TestUtilTest.java new file mode 100644 index 000000000..73da9cf83 --- /dev/null +++ b/testing-util/testsrc/org/aspectj/testing/util/TestUtilTest.java @@ -0,0 +1,116 @@ +/* ******************************************************************* + * Copyright (c) 1999-2001 Xerox Corporation, + * 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + +package org.aspectj.testing.util; + +import org.aspectj.bridge.IMessageHolder; +import org.aspectj.bridge.MessageHandler; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.util.FileUtil; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * + */ +public class TestUtilTest extends TestCase { + + public TestUtilTest(String name) { + super(name); + } + + public void testFileCompareNonClass() throws IOException { + MessageHandler holder = new MessageHandler(); + File thisFile = new File("testsrc/org/aspectj/testing/util/TestUtilTest.java"); + //File thisFile = new File("src/testing-util.lst"); + assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile)); + + File tempFile = File.createTempFile("TestUtilTest", ".tmp"); + FileUtil.copyFile(thisFile, tempFile); + long len = tempFile.length(); + assertTrue(0 != len); + long tlen = thisFile.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile)); + try { + String path = thisFile.getName(); + File basedir = tempFile.getParentFile(); + File renamed = new File(basedir, path); + if (!tempFile.renameTo(renamed)) { + MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed); + } else { + len = renamed.length(); + assertEquals(tlen, len); + assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path)); + } + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + holder.clearMessages(); + } + tempFile.delete(); + } + } + + public void testFileCompareNonClassStaticPositive() throws IOException { + MessageHandler holder = new MessageHandler(); + File basedir = new File("testdata/testCompareTextFiles/sameFile"); + File expectedBaseDir = new File(basedir, "expected"); + File actualBaseDir = new File(basedir, "actual"); + String filename = "TestUtilTest.java"; + File expected = new File(expectedBaseDir, filename); + File actual = new File(actualBaseDir, filename); + + assertTrue(TestUtil.sameFiles(holder, expected, actual)); + + assertTrue(TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename)); + } + + public void testFileCompareNonClassStaticNegative() throws IOException { + MessageHandler holder = new MessageHandler(); + File basedir = new File("testdata/testCompareTextFiles/differentFile"); + File expectedBaseDir = new File(basedir, "expected"); + File actualBaseDir = new File(basedir, "actual"); + String filename = "TestUtilTest.java"; + File expected = new File(expectedBaseDir, filename); + File actual = new File(actualBaseDir, filename); + + assertTrue(!TestUtil.sameFiles(holder, expected, actual)); + + assertTrue(!TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename)); + } + + public void testFileCompareClass() throws IOException { + if (!TestUtil.ClassLineator.haveDisassembler()) { + System.err.println("skipping testFileCompareClass - no disassembler on classpath"); + return; + } + MessageHandler holder = new MessageHandler(); + File classBase = new File("testdata/testCompareClassFiles"); + String path = "org/aspectj/testing/util/TestCompareClassFile.class"; + File classFile = new File(classBase, path); + + try { + assertTrue(TestUtil.sameFiles(holder, classFile, classFile)); + assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path)); + } finally { + if (0 < holder.numMessages(null, true)) { + MessageUtil.print(System.out, holder); + } + } + } + +} diff --git a/testing-util/testsrc/org/aspectj/testing/util/UtilTests.java b/testing-util/testsrc/org/aspectj/testing/util/UtilTests.java new file mode 100644 index 000000000..a32bd424c --- /dev/null +++ b/testing-util/testsrc/org/aspectj/testing/util/UtilTests.java @@ -0,0 +1,32 @@ +/* ******************************************************************* + * Copyright (c) 1999-2001 Xerox Corporation, + * 2002 Palo Alto Research Center, Incorporated (PARC). + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + + +package org.aspectj.testing.util; + +import junit.framework.*; + +public class UtilTests extends TestCase { + + public static Test suite() { + TestSuite suite = new TestSuite(UtilTests.class.getName()); + // for now, do not include SuiteTest because it would take 15 minutes + //$JUnit-BEGIN$ + suite.addTestSuite(TestUtilTest.class); + //$JUnit-END$ + return suite; + } + + public UtilTests(String name) { super(name); } + +} |