diff options
author | Andy Clement <aclement@pivotal.io> | 2019-01-23 12:28:35 -0800 |
---|---|---|
committer | Andy Clement <aclement@pivotal.io> | 2019-01-23 12:28:35 -0800 |
commit | 6245346ab0fe0f19db977c86389e31517ed068ec (patch) | |
tree | 7487d1248777170b1bdaf505282cd78a4a354278 /util/src | |
parent | 02337b3edca74530cd523f099915757a29a4fcdb (diff) | |
download | aspectj-6245346ab0fe0f19db977c86389e31517ed068ec.tar.gz aspectj-6245346ab0fe0f19db977c86389e31517ed068ec.zip |
mavenized util module
Diffstat (limited to 'util/src')
-rw-r--r-- | util/src/main/java/org/aspectj/util/FileUtil.java (renamed from util/src/org/aspectj/util/FileUtil.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/FuzzyBoolean.java (renamed from util/src/org/aspectj/util/FuzzyBoolean.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/GenericSignature.java (renamed from util/src/org/aspectj/util/GenericSignature.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/GenericSignatureParser.java (renamed from util/src/org/aspectj/util/GenericSignatureParser.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/IStructureModel.java (renamed from util/src/org/aspectj/util/IStructureModel.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/LangUtil.java (renamed from util/src/org/aspectj/util/LangUtil.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/PartialOrder.java (renamed from util/src/org/aspectj/util/PartialOrder.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/Reflection.java (renamed from util/src/org/aspectj/util/Reflection.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/SoftHashMap.java (renamed from util/src/org/aspectj/util/SoftHashMap.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/TypeSafeEnum.java (renamed from util/src/org/aspectj/util/TypeSafeEnum.java) | 0 | ||||
-rw-r--r-- | util/src/main/java/org/aspectj/util/UtilClassLoader.java (renamed from util/src/org/aspectj/util/UtilClassLoader.java) | 0 | ||||
-rw-r--r-- | util/src/test/java/org/aspectj/util/FileUtilTest.java | 707 | ||||
-rw-r--r-- | util/src/test/java/org/aspectj/util/GenericSignatureParserTest.java | 233 | ||||
-rw-r--r-- | util/src/test/java/org/aspectj/util/LangUtilTest.java | 270 |
14 files changed, 1210 insertions, 0 deletions
diff --git a/util/src/org/aspectj/util/FileUtil.java b/util/src/main/java/org/aspectj/util/FileUtil.java index f3db2bda7..f3db2bda7 100644 --- a/util/src/org/aspectj/util/FileUtil.java +++ b/util/src/main/java/org/aspectj/util/FileUtil.java diff --git a/util/src/org/aspectj/util/FuzzyBoolean.java b/util/src/main/java/org/aspectj/util/FuzzyBoolean.java index 807d98087..807d98087 100644 --- a/util/src/org/aspectj/util/FuzzyBoolean.java +++ b/util/src/main/java/org/aspectj/util/FuzzyBoolean.java diff --git a/util/src/org/aspectj/util/GenericSignature.java b/util/src/main/java/org/aspectj/util/GenericSignature.java index bfefc007e..bfefc007e 100644 --- a/util/src/org/aspectj/util/GenericSignature.java +++ b/util/src/main/java/org/aspectj/util/GenericSignature.java diff --git a/util/src/org/aspectj/util/GenericSignatureParser.java b/util/src/main/java/org/aspectj/util/GenericSignatureParser.java index 28c8db83d..28c8db83d 100644 --- a/util/src/org/aspectj/util/GenericSignatureParser.java +++ b/util/src/main/java/org/aspectj/util/GenericSignatureParser.java diff --git a/util/src/org/aspectj/util/IStructureModel.java b/util/src/main/java/org/aspectj/util/IStructureModel.java index 9008f0872..9008f0872 100644 --- a/util/src/org/aspectj/util/IStructureModel.java +++ b/util/src/main/java/org/aspectj/util/IStructureModel.java diff --git a/util/src/org/aspectj/util/LangUtil.java b/util/src/main/java/org/aspectj/util/LangUtil.java index 82bd9426b..82bd9426b 100644 --- a/util/src/org/aspectj/util/LangUtil.java +++ b/util/src/main/java/org/aspectj/util/LangUtil.java diff --git a/util/src/org/aspectj/util/PartialOrder.java b/util/src/main/java/org/aspectj/util/PartialOrder.java index 8bb9f3b77..8bb9f3b77 100644 --- a/util/src/org/aspectj/util/PartialOrder.java +++ b/util/src/main/java/org/aspectj/util/PartialOrder.java diff --git a/util/src/org/aspectj/util/Reflection.java b/util/src/main/java/org/aspectj/util/Reflection.java index 83ceaac23..83ceaac23 100644 --- a/util/src/org/aspectj/util/Reflection.java +++ b/util/src/main/java/org/aspectj/util/Reflection.java diff --git a/util/src/org/aspectj/util/SoftHashMap.java b/util/src/main/java/org/aspectj/util/SoftHashMap.java index 94ae83441..94ae83441 100644 --- a/util/src/org/aspectj/util/SoftHashMap.java +++ b/util/src/main/java/org/aspectj/util/SoftHashMap.java diff --git a/util/src/org/aspectj/util/TypeSafeEnum.java b/util/src/main/java/org/aspectj/util/TypeSafeEnum.java index 99d223143..99d223143 100644 --- a/util/src/org/aspectj/util/TypeSafeEnum.java +++ b/util/src/main/java/org/aspectj/util/TypeSafeEnum.java diff --git a/util/src/org/aspectj/util/UtilClassLoader.java b/util/src/main/java/org/aspectj/util/UtilClassLoader.java index 091e022a3..091e022a3 100644 --- a/util/src/org/aspectj/util/UtilClassLoader.java +++ b/util/src/main/java/org/aspectj/util/UtilClassLoader.java diff --git a/util/src/test/java/org/aspectj/util/FileUtilTest.java b/util/src/test/java/org/aspectj/util/FileUtilTest.java new file mode 100644 index 000000000..8ce18c700 --- /dev/null +++ b/util/src/test/java/org/aspectj/util/FileUtilTest.java @@ -0,0 +1,707 @@ +/* ******************************************************************* + * 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 Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + +package org.aspectj.util; + +//import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.StringBufferInputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; +import junit.textui.TestRunner; + +/** + * + */ +public class FileUtilTest extends TestCase { + public static final String[] NONE = new String[0]; + public static boolean log = false; + + public static void main(String[] args) { + TestRunner.main(new String[] { "org.aspectj.util.FileUtilTest" }); + } + + public static void assertSame(String prefix, String[] lhs, String[] rhs) { // XXX cheap diff + String srcPaths = LangUtil.arrayAsList(lhs).toString(); + String destPaths = LangUtil.arrayAsList(rhs).toString(); + if (!srcPaths.equals(destPaths)) { + log("expected: " + srcPaths); + log(" actual: " + destPaths); + assertTrue(prefix + " expected=" + srcPaths + " != actual=" + destPaths, false); + } + } + + /** + * Verify that dir contains files with names, and return the names of other files in dir. + * + * @return the contents of dir after excluding files or NONE if none + * @throws AssertionFailedError if any names are not in dir + */ + public static String[] dirContains(File dir, final String[] filenames) { + final ArrayList<String> sought = new ArrayList<>(LangUtil.arrayAsList(filenames)); + FilenameFilter filter = new FilenameFilter() { + public boolean accept(File d, String name) { + return !sought.remove(name); + } + }; + // remove any found from sought and return remainder + String[] found = dir.list(filter); + if (0 < sought.size()) { + assertTrue("found " + LangUtil.arrayAsList(dir.list()).toString() + " expected " + sought, false); + } + return (found.length == 0 ? NONE : found); + } + + /** @return sorted String[] of all paths to all files/dirs under dir */ + public static String[] dirPaths(File dir) { + return dirPaths(dir, new String[0]); + } + + /** + * Get a sorted String[] of all paths to all files/dirs under dir. Files with names starting with "." are ignored, as are + * directory paths containing "CVS". The directory prefix of the path is stripped. Thus, given directory: + * + * <pre> + * path/to + * .cvsignore + * CVS/ + * Root + * Repository + * Base.java + * com/ + * parc/ + * messages.properties + * org/ + * aspectj/ + * Version.java + * </pre> + * + * a call + * + * <pre> + * dirPaths(new File("path/to"), new String[0]); + * </pre> + * + * returns + * + * <pre> + * { "Base.java", "com/parc/messages.properties", "org/aspectj/Version.java" } + * </pre> + * + * while the call + * + * <pre> + * dirPaths(new File("path/to"), new String[] { ".java" }); + * </pre> + * + * returns + * + * <pre> + * { "Base.java", "org/aspectj/Version.java" } + * </pre> + * + * @param dir the File path to the directory to inspect + * @param suffixes if not empty, require files returned to have this suffix + * @return sorted String[] of all paths to all files under dir ending with one of the listed suffixes but not starting with "." + */ + public static String[] dirPaths(File dir, String[] suffixes) { + ArrayList<String> result = new ArrayList<String>(); + doDirPaths(dir, result); + // if suffixes required, remove those without suffixes + if (!LangUtil.isEmpty(suffixes)) { + for (ListIterator<String> iter = result.listIterator(); iter.hasNext();) { + String path = iter.next().toString(); + boolean hasSuffix = false; + for (int i = 0; !hasSuffix && (i < suffixes.length); i++) { + hasSuffix = path.endsWith(suffixes[i]); + } + if (!hasSuffix) { + iter.remove(); + } + } + } + Collections.sort(result); + // trim prefix + final String prefix = dir.getPath(); + final int len = prefix.length() + 1; // plus directory separator + String[] ra = (String[]) result.toArray(new String[0]); + for (int i = 0; i < ra.length; i++) { + // assertTrue(ra[i].startsWith(prefix)); + assertTrue(ra[i], ra[i].length() > len); + ra[i] = ra[i].substring(len); + } + return ra; + } + + /** + * @param dir the File to read - ignored if null, not a directory, or has "CVS" in its path + * @param useSuffix if true, then use dir as suffix to path + */ + private static void doDirPaths(File dir, ArrayList<String> paths) { + if ((null == dir) || !dir.canRead() || (-1 != dir.getPath().indexOf("CVS"))) { + return; + } + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) { + String path = files[i].getPath(); + if (!files[i].getName().startsWith(".")) { + if (files[i].isFile()) { + paths.add(path); + } else if (files[i].isDirectory()) { + doDirPaths(files[i], paths); + } else { + log("not file or dir: " + dir + "/" + path); + } + } + } + } + + /** Print s if logging is enabled */ + private static void log(String s) { + if (log) { + System.err.println(s); + } + } + + /** List of File files or directories to delete when exiting */ + final ArrayList<File> tempFiles; + + public FileUtilTest(String s) { + super(s); + tempFiles = new ArrayList<File>(); + } + + public void tearDown() { + for (ListIterator<File> iter = tempFiles.listIterator(); iter.hasNext();) { + File dir = (File) iter.next(); + log("removing " + dir); + FileUtil.deleteContents(dir); + dir.delete(); + iter.remove(); + } + } + + public void testNotIsFileIsDirectory() { + File noSuchFile = new File("foo"); + assertTrue(!noSuchFile.isFile()); + assertTrue(!noSuchFile.isDirectory()); + } + + public void testGetBestFile() { + assertNull(FileUtil.getBestFile((String[]) null)); + assertNull(FileUtil.getBestFile(new String[0])); + assertNull(FileUtil.getBestFile(new String[] { "!" })); + File f = FileUtil.getBestFile(new String[] { "." }); + assertNotNull(f); + f = FileUtil.getBestFile(new String[] { "!", "." }); + assertNotNull(f); + assertTrue(f.canRead()); + boolean setProperty = false; + try { + System.setProperty("bestfile", "."); + setProperty = true; + } catch (Throwable t) { + // ignore Security, etc. + } + if (setProperty) { + f = FileUtil.getBestFile(new String[] { "sp:bestfile" }); + assertNotNull(f); + assertTrue(f.canRead()); + } + } + + public void testCopyFiles() { + // bad input + Class<?> iaxClass = IllegalArgumentException.class; + + checkCopyFiles(null, null, iaxClass, false); + + File noSuchFile = new File("foo"); + checkCopyFiles(noSuchFile, null, iaxClass, false); + checkCopyFiles(noSuchFile, noSuchFile, iaxClass, false); + + File tempDir = FileUtil.getTempDir("testCopyFiles"); + tempFiles.add(tempDir); + File fromFile = new File(tempDir, "fromFile"); + String err = FileUtil.writeAsString(fromFile, "contents of from file"); + assertTrue(err, null == err); + checkCopyFiles(fromFile, null, iaxClass, false); + checkCopyFiles(fromFile, fromFile, iaxClass, false); + + // file-file + File toFile = new File(tempDir, "toFile"); + checkCopyFiles(fromFile, toFile, null, true); + + // file-dir + File toDir = new File(tempDir, "toDir"); + assertTrue(toDir.mkdirs()); + checkCopyFiles(fromFile, toDir, null, true); + + // dir-dir + File fromDir = new File(tempDir, "fromDir"); + assertTrue(fromDir.mkdirs()); + checkCopyFiles(fromFile, fromDir, null, false); + File toFile2 = new File(fromDir, "toFile2"); + checkCopyFiles(fromFile, toFile2, null, false); + checkCopyFiles(fromDir, toDir, null, true); + } + + void checkCopyFiles(File from, File to, Class exceptionClass, boolean clean) { + try { + FileUtil.copyFile(from, to); + assertTrue(null == exceptionClass); + if (to.isFile()) { + assertTrue(from.length() == to.length()); // XXX cheap test + } else if (!from.isDirectory()) { + File toFile = new File(to, from.getName()); + assertTrue(from.length() == toFile.length()); + } else { + // from is a dir and to is a dir, toDir should be created, and have the + // same contents as fromDir. + assertTrue(to.exists()); + assertTrue(from.listFiles().length == to.listFiles().length); + } + } catch (Throwable t) { + assertTrue(null != exceptionClass); + assertTrue(exceptionClass.isAssignableFrom(t.getClass())); + } finally { + if (clean && (null != to) && (to.exists())) { + if (to.isDirectory()) { + FileUtil.deleteContents(to); + } + to.delete(); + } + } + } + + public void testDirCopySubdirs() throws IOException { + File srcDir = new File("src"); + File destDir = FileUtil.getTempDir("testDirCopySubdirs"); + tempFiles.add(destDir); + FileUtil.copyDir(srcDir, destDir); + assertSame("testDirCopySubdirs", dirPaths(srcDir), dirPaths(destDir)); + } + + public void testDirCopySubdirsSuffix() throws IOException { + File srcDir = new File("src"); + File destDir = FileUtil.getTempDir("testDirCopySubdirsSuffix"); + tempFiles.add(destDir); + FileUtil.copyDir(srcDir, destDir, ".java", ".aj"); + + String[] sources = dirPaths(srcDir, new String[] { ".java" }); + for (int i = 0; i < sources.length; i++) { + sources[i] = sources[i].substring(0, sources[i].length() - 4); + } + String[] sinks = dirPaths(destDir, new String[] { ".aj" }); + for (int i = 0; i < sinks.length; i++) { + sinks[i] = sinks[i].substring(0, sinks[i].length() - 2); + } + assertSame("testDirCopySubdirs", sources, sinks); + } + + public void testGetURL() { + String[] args = new String[] { ".", "../util/testdata", "../lib/test/aspectjrt.jar" }; + for (int i = 0; i < args.length; i++) { + checkGetURL(args[i]); + } + } + + /** + * Method checkGetURL. + * + * @param string + * @param uRL + */ + private void checkGetURL(String arg) { + assertTrue(null != arg); + File f = new File(arg); + URL url = FileUtil.getFileURL(f); + assertTrue(null != url); + log("url " + url); + if (!f.exists()) { + log("not exist " + f); + } else if (f.isDirectory()) { + log("directory " + f); + } else { + log(" file " + f); + InputStream in = null; + try { + in = url.openStream(); + } catch (IOException e) { + assertTrue("IOException: " + e, false); + } finally { + if (null != in) { + try { + in.close(); + } catch (IOException e) { + } + } + } + } + } + + public void testGetTempDir() { + boolean pass = true; + boolean delete = true; + checkGetTempDir("parent", null, pass, delete); + checkGetTempDir(null, "child", pass, delete); + tempFiles.add(checkGetTempDir("parent", "child", pass, !delete).getParentFile()); + tempFiles.add(checkGetTempDir("parent", "child", pass, !delete).getParentFile()); + tempFiles.add(checkGetTempDir("parent", "child", pass, !delete).getParentFile()); + } + + File checkGetTempDir(String parent, String child, boolean ok, boolean delete) { + File parentDir = FileUtil.getTempDir(parent); + assertTrue("unable to create " + parent, null != parentDir); + File dir = FileUtil.makeNewChildDir(parentDir, child); + log("parent=" + parent + " child=" + child + " -> " + dir); + assertTrue("dir: " + dir, ok == (dir.canWrite() && dir.isDirectory())); + if (delete) { + dir.delete(); + parentDir.delete(); + } + return dir; + } + + public void testRandomFileString() { + ArrayList<String> results = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + String s = FileUtil.randomFileString(); + if (results.contains(s)) { + log("warning: got duplicate at iteration " + i); + } + results.add(s); + // System.err.print(" " + s); + // if (0 == (i % 5)) { + // System.err.println(""); + // } + } + } + + public void testNormalizedPath() { + File tempFile = null; + try { + tempFile = File.createTempFile("FileUtilTest_testNormalizedPath", "tmp"); + tempFiles.add(tempFile); + } catch (IOException e) { + log("aborting test - unable to create temp file"); + return; + } + File parentDir = tempFile.getParentFile(); + String tempFilePath = FileUtil.normalizedPath(tempFile, parentDir); + assertEquals(tempFile.getName(), tempFilePath); + } + + public void testFileToClassName() { + + File basedir = new File("/base/dir"); // never created + File classFile = new File(basedir, "foo/Bar.class"); + assertEquals("foo.Bar", FileUtil.fileToClassName(basedir, classFile)); + + classFile = new File(basedir, "foo\\Bar.class"); + assertEquals("foo.Bar", FileUtil.fileToClassName(basedir, classFile)); + + assertEquals("Bar", FileUtil.fileToClassName(null, classFile)); + + classFile = new File("/home/classes/org/aspectj/lang/JoinPoint.class"); + assertEquals("org.aspectj.lang.JoinPoint", FileUtil.fileToClassName(null, classFile)); + + classFile = new File("/home/classes/com/sun/tools/Javac.class"); + assertEquals("com.sun.tools.Javac", FileUtil.fileToClassName(null, classFile)); + } + + public void testDeleteContents() { + File tempDir = FileUtil.getTempDir("testDeleteContents"); + tempFiles.add(tempDir); + File f = new File(tempDir, "foo"); + f.mkdirs(); + File g = new File(f, "bar"); + g.mkdirs(); + File h = new File(g, "bash"); + h.mkdirs(); + int d = FileUtil.deleteContents(f); + assertTrue(0 == d); + assertTrue(0 == f.list().length); + f.delete(); + assertTrue(!f.exists()); + } + + public void testLineSeek() { + File tempDir = FileUtil.getTempDir("testLineSeek"); + tempFiles.add(tempDir); + File file = new File(tempDir, "testLineSeek"); + String path = file.getPath(); + String contents = "0123456789" + LangUtil.EOL; + contents += contents; + FileUtil.writeAsString(file, contents); + tempFiles.add(file); + List<String> sourceList = new ArrayList<String>(); + sourceList.add(file.getPath()); + + final ArrayList<String> errors = new ArrayList<String>(); + final PrintStream errorSink = new PrintStream(System.err, true) { + public void println(String error) { + errors.add(error); + } + }; + for (int i = 0; i < 10; i++) { + List<String> result = FileUtil.lineSeek("" + i, sourceList, true, errorSink); + assertEquals(2, result.size()); + assertEquals(path + ":1:" + i, result.get(0)); + assertEquals(path + ":2:" + i, result.get(1)); + if (!LangUtil.isEmpty(errors)) { // XXX prefer fast-fail? + assertTrue("errors: " + errors, false); + } + } + + } + + public void testLineSeekMore() { + final int MAX = 3; // 1..10 + File tempDir = FileUtil.getTempDir("testLineSeekMore"); + tempFiles.add(tempDir); + final String prefix = new File(tempDir, "testLineSeek").getPath(); + // setup files 0..MAX with 2*MAX lines + String[] sources = new String[MAX]; + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < sources.length; i++) { + sources[i] = new File(prefix + i).getPath(); + sb.append("not matched"); + sb.append(LangUtil.EOL); + sb.append("0123456789"); + sb.append(LangUtil.EOL); + } + final String contents = sb.toString(); + for (int i = 0; i < sources.length; i++) { + File file = new File(sources[i]); + FileUtil.writeAsString(file, contents); + tempFiles.add(file); + } + // now test + final ArrayList<String> errors = new ArrayList<>(); + final PrintStream errorSink = new PrintStream(System.err, true) { + public void println(String error) { + errors.add(error); + } + }; + List<String> sourceList = new ArrayList<>(); + sourceList.addAll(Arrays.asList(sources)); + sourceList = Collections.unmodifiableList(sourceList); + for (int k = 0; k < sources.length; k++) { + List<String> result = FileUtil.lineSeek("" + k, sourceList, true, errorSink); + // number k found in every other line of every file at index k + Iterator<String> iter = result.iterator(); + for (int i = 0; i < MAX; i++) { // for each file + for (int j = 1; j < (MAX + 1); j++) { // for every other line + assertTrue(iter.hasNext()); + assertEquals(prefix + i + ":" + 2 * j + ":" + k, iter.next()); + } + } + if (!LangUtil.isEmpty(errors)) { // XXX prefer fast-fail? + assertTrue("errors: " + errors, false); + } + } + } + + public void testDirCopyNoSubdirs() throws IOException { + String[] srcFiles = new String[] { "one.java", "two.java", "three.java" }; + String[] destFiles = new String[] { "three.java", "four.java", "five.java" }; + String[] allFiles = new String[] { "one.java", "two.java", "three.java", "four.java", "five.java" }; + File srcDir = makeTempDir("FileUtilUT_srcDir", srcFiles); + File destDir = makeTempDir("FileUtilUT_destDir", destFiles); + assertTrue(null != srcDir); + assertTrue(null != destDir); + assertTrue(NONE == dirContains(srcDir, srcFiles)); + assertTrue(NONE == dirContains(destDir, destFiles)); + + FileUtil.copyDir(srcDir, destDir); + String[] resultOne = dirContains(destDir, allFiles); + FileUtil.copyDir(srcDir, destDir); + String[] resultTwo = dirContains(destDir, allFiles); + + assertTrue(NONE == resultOne); + assertTrue(NONE == resultTwo); + } + + public void testDirCopyNoSubdirsWithSuffixes() throws IOException { + String[] srcFiles = new String[] { "one.java", "two.java", "three.java" }; + String[] destFiles = new String[] { "three.java", "four.java", "five.java" }; + String[] allFiles = new String[] { "one.aj", "two.aj", "three.aj", "three.java", "four.java", "five.java" }; + File srcDir = makeTempDir("FileUtilUT_srcDir", srcFiles); + File destDir = makeTempDir("FileUtilUT_destDir", destFiles); + assertTrue(null != srcDir); + assertTrue(null != destDir); + assertTrue(NONE == dirContains(srcDir, srcFiles)); + assertTrue(NONE == dirContains(destDir, destFiles)); + + FileUtil.copyDir(srcDir, destDir, ".java", ".aj"); + FileUtil.copyDir(srcDir, destDir, ".java", ".aj"); + + assertTrue(NONE == dirContains(destDir, allFiles)); + assertTrue(NONE == dirContains(destDir, allFiles)); + } + + public void testDirCopySubdirsSuffixRoundTrip() throws IOException { + final File srcDir = new File("src"); + final File one = FileUtil.getTempDir("testDirCopySubdirsSuffixRoundTrip_1"); + final File two = FileUtil.getTempDir("testDirCopySubdirsSuffixRoundTrip_2"); + FileUtil.copyDir(srcDir, one); // no selection + FileUtil.copyDir(two, one, ".java", ".aj"); // only .java files + FileUtil.copyDir(one, two, ".aj", ".java"); + + FileUtil.deleteContents(one); + one.delete(); + FileUtil.deleteContents(two); + two.delete(); + } + + /** + * Create temp dir at loc containing temp files files. Result is registered for deletion on cleanup. + */ + File makeTempDir(String loc, String[] filenames) throws IOException { + File d = new File(loc); + d.mkdirs(); + assertTrue(d.exists()); + tempFiles.add(d); + assertTrue(d.canWrite()); + for (int i = 0; i < filenames.length; i++) { + File f = new File(d, filenames[i]); + assertTrue(filenames[i], f.createNewFile()); + } + return d; + } + + public void testPipeEmpty() { + checkPipe(""); + } + + public void testPipeMin() { + checkPipe("0"); + } + + public void testPipe() { + String str = "The quick brown fox jumped over the lazy dog"; + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < 4096; i++) { + sb.append(str); + } + checkPipe(sb.toString()); + } + + void checkPipe(String data) { + StringBufferInputStream in = new StringBufferInputStream(data); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileUtil.Pipe pipe = new FileUtil.Pipe(in, out, 100l, true, true); + pipe.run(); + assertTrue(data.equals(out.toString())); + assertTrue(null == pipe.getThrown()); + assertEquals("totalWritten", data.length(), pipe.totalWritten()); + } + + public void testPipeThrown() { + final String data = "The quick brown fox jumped over the lazy dog"; + final IOException thrown = new IOException("test"); + StringBufferInputStream in = new StringBufferInputStream(data); + OutputStream out = new OutputStream() { + public void write(int b) throws IOException { + throw thrown; + } + }; + + FileUtil.Pipe pipe = new FileUtil.Pipe(in, out, 100l, true, true); + pipe.run(); + assertEquals("totalWritten", 0, pipe.totalWritten()); + assertTrue(thrown == pipe.getThrown()); + } + + public void xtestPipeHalt() { // this test periodically fails on the build machine - + // disabling till we have time to figure out why + final long MAX = 1000000; + InputStream in = new InputStream() { + long max = 0; + + public int read() throws IOException { + if (max++ > MAX) { + throw new IOException("test failed"); + } + return 1; + } + + }; + final int minWritten = 20; + class Flag { + boolean hit; + } + final Flag flag = new Flag(); + OutputStream out = new OutputStream() { + long max = 0; + + public void write(int b) throws IOException { + if (max++ > MAX) { + throw new IOException("test failed"); + } else if (max > minWritten) { + if (!flag.hit) { + flag.hit = true; + } + } + } + }; + class Result { + long totalWritten; + Throwable thrown; + boolean set; + } + final Result result = new Result(); + FileUtil.Pipe pipe = new FileUtil.Pipe(in, out, 100l, true, true) { + protected void completing(long totalWritten, Throwable thrown) { + result.totalWritten = totalWritten; + result.thrown = thrown; + result.set = true; + } + }; + // start it up + new Thread(pipe).start(); + // wait for minWritten input + while (!flag.hit) { + try { + Thread.sleep(5l); + } catch (InterruptedException e) { + // ignore + } + } + // halt + assertTrue(pipe.halt(true, true)); + assertTrue(result.set); + assertTrue("Expected null but result.thrown = " + result.thrown, null == result.thrown); + assertTrue(null == pipe.getThrown()); + assertEquals("total written", result.totalWritten, pipe.totalWritten()); + if (minWritten > pipe.totalWritten()) { + assertTrue("written: " + pipe.totalWritten(), false); + } + } + +} diff --git a/util/src/test/java/org/aspectj/util/GenericSignatureParserTest.java b/util/src/test/java/org/aspectj/util/GenericSignatureParserTest.java new file mode 100644 index 000000000..5a9e083e4 --- /dev/null +++ b/util/src/test/java/org/aspectj/util/GenericSignatureParserTest.java @@ -0,0 +1,233 @@ +/* ******************************************************************* + * Copyright (c) 2005-2008 Contributors + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * ******************************************************************/ + +package org.aspectj.util; + +import junit.framework.TestCase; + +import org.aspectj.util.GenericSignature.ClassSignature; +import org.aspectj.util.GenericSignature.ClassTypeSignature; +import org.aspectj.util.GenericSignature.FieldTypeSignature; +import org.aspectj.util.GenericSignature.SimpleClassTypeSignature; + +/** + * @author Adrian Colyer + * @author Andy Clement + */ +public class GenericSignatureParserTest extends TestCase { + + GenericSignatureParser parser; + + public void testSimpleTokenize() { + String[] tokens = parser.tokenize("Ljava/lang/String;"); + assertEquals(new String[] { "Ljava", "/", "lang", "/", "String", ";" }, tokens); + } + + public void testTokenizeWithWildTypeArguments() { + String[] tokens = parser.tokenize("Ljava/lang/String<*>;"); + assertEquals(new String[] { "Ljava", "/", "lang", "/", "String", "<", "*", ">", ";" }, tokens); + } + + public void testTokenizeWithExtendsTypeArguments() { + String[] tokens = parser.tokenize("Ljava/util/List<+TE>;"); + assertEquals(new String[] { "Ljava", "/", "util", "/", "List", "<", "+", "TE", ">", ";" }, tokens); + } + + public void testTokenizeWithSuperTypeArguments() { + String[] tokens = parser.tokenize("Ljava/util/List<-TE>;"); + assertEquals(new String[] { "Ljava", "/", "util", "/", "List", "<", "-", "TE", ">", ";" }, tokens); + } + + public void testTokenizeArrayType() { + String[] tokens = parser.tokenize("[Ljava/lang/String;"); + assertEquals(new String[] { "[", "Ljava", "/", "lang", "/", "String", ";" }, tokens); + } + + public void testTokenizeFormalTypeParameters() { + String[] tokens = parser.tokenize("<T:Ljava/lang/String;:Ljava/util/Comparable;>"); + assertEquals(new String[] { "<", "T", ":", "Ljava", "/", "lang", "/", "String", ";", ":", "Ljava", "/", "util", "/", + "Comparable", ";", ">" }, tokens); + } + + public void testParseClassSignatureSimple() { + ClassSignature sig = parser.parseAsClassSignature("Ljava/lang/String;"); + assertEquals("No type parameters", 0, sig.formalTypeParameters.length); + assertEquals("No superinterfaces", 0, sig.superInterfaceSignatures.length); + assertEquals("Ljava/lang/String;", sig.superclassSignature.classSignature); + SimpleClassTypeSignature outerType = sig.superclassSignature.outerType; + assertEquals("Ljava/lang/String;", outerType.identifier); + assertEquals("No type args", 0, outerType.typeArguments.length); + } + + public void testParseClassSignatureTypeArgs() { + ClassSignature sig = parser.parseAsClassSignature("Ljava/util/List<+Ljava/lang/String;>;"); + assertEquals("No type parameters", 0, sig.formalTypeParameters.length); + assertEquals("No superinterfaces", 0, sig.superInterfaceSignatures.length); + assertEquals("Ljava/util/List<+Ljava/lang/String;>;", sig.superclassSignature.classSignature); + SimpleClassTypeSignature outerType = sig.superclassSignature.outerType; + assertEquals("Ljava/util/List", outerType.identifier); + assertEquals("One type arg", 1, outerType.typeArguments.length); + assertTrue(outerType.typeArguments[0].isPlus); + assertEquals("+Ljava/lang/String;", outerType.typeArguments[0].toString()); + } + + public void testParseClassSignatureTheFullMonty() { + ClassSignature sig = parser + .parseAsClassSignature("<E:Ljava/lang/String;:Ljava/lang/Number<TE;>;>Ljava/util/List<TE;>;Ljava/util/Comparable<-TE;>;"); + assertEquals("1 formal parameter", 1, sig.formalTypeParameters.length); + assertEquals("E", sig.formalTypeParameters[0].identifier); + ClassTypeSignature fsig = (ClassTypeSignature) sig.formalTypeParameters[0].classBound; + assertEquals("Ljava/lang/String;", fsig.classSignature); + assertEquals("1 interface bound", 1, sig.formalTypeParameters[0].interfaceBounds.length); + ClassTypeSignature isig = (ClassTypeSignature) sig.formalTypeParameters[0].interfaceBounds[0]; + assertEquals("Ljava/lang/Number<TE;>;", isig.classSignature); + assertEquals("Ljava/util/List<TE;>;", sig.superclassSignature.classSignature); + assertEquals("1 type argument", 1, sig.superclassSignature.outerType.typeArguments.length); + assertEquals("TE;", sig.superclassSignature.outerType.typeArguments[0].toString()); + assertEquals("1 super interface", 1, sig.superInterfaceSignatures.length); + assertEquals("Ljava/util/Comparable<-TE;>;", sig.superInterfaceSignatures[0].toString()); + } + + public void testFunky_406167() { + ClassSignature sig = parser + .parseAsClassSignature("Lcom/google/android/gms/internal/hb<TT;>.com/google/android/gms/internal/hb$b<Ljava/lang/Boolean;>;"); + // Note the package prefix on the nested types has been dropped + assertEquals("Lcom/google/android/gms/internal/hb<TT;>.hb$b<Ljava/lang/Boolean;>;",sig.superclassSignature.toString()); + sig = parser + .parseAsClassSignature("Lcom/a/a/b/t<TK;TV;>.com/a/a/b/af.com/a/a/b/ag;Ljava/util/ListIterator<TV;>;"); + // Note the package prefix on the nested types has been dropped + assertEquals("Lcom/a/a/b/t<TK;TV;>.af.ag;",sig.superclassSignature.toString()); + assertEquals("Ljava/util/ListIterator<TV;>;",sig.superInterfaceSignatures[0].toString()); + sig = parser.parseAsClassSignature("Lcom/google/android/gms/internal/hb.com/google/android/gms/internal/hb$b<Ljava/lang/Boolean;>;"); + // Note the package prefix on the nested types has been dropped + assertEquals("Lcom/google/android/gms/internal/hb.hb$b<Ljava/lang/Boolean;>;",sig.superclassSignature.toString()); + sig = parser + .parseAsClassSignature("Lcom/a/a/b/t.com/a/a/b/af.com/a/a/b/ag;Ljava/util/ListIterator<TV;>;"); + // Note the package prefix on the nested types has been dropped + assertEquals("Lcom/a/a/b/t.af.ag;",sig.superclassSignature.toString()); + assertEquals("Ljava/util/ListIterator<TV;>;",sig.superInterfaceSignatures[0].toString()); + } + + + public void testFieldSignatureParsingClassType() { + FieldTypeSignature fsig = parser.parseAsFieldSignature("Ljava/lang/String;"); + assertTrue("ClassTypeSignature", fsig instanceof ClassTypeSignature); + assertEquals("Ljava/lang/String;", fsig.toString()); + } + + public void testFieldSignatureParsingArrayType() { + FieldTypeSignature fsig = parser.parseAsFieldSignature("[Ljava/lang/String;"); + assertTrue("ArrayTypeSignature", fsig instanceof GenericSignature.ArrayTypeSignature); + assertEquals("[Ljava/lang/String;", fsig.toString()); + } + + public void testFieldSignatureParsingTypeVariable() { + FieldTypeSignature fsig = parser.parseAsFieldSignature("TT;"); + assertTrue("TypeVariableSignature", fsig instanceof GenericSignature.TypeVariableSignature); + assertEquals("TT;", fsig.toString()); + } + + public void testSimpleMethodSignatureParsing() { + GenericSignature.MethodTypeSignature mSig = parser.parseAsMethodSignature("()V"); + assertEquals("No type parameters", 0, mSig.formalTypeParameters.length); + assertEquals("No parameters", 0, mSig.parameters.length); + assertEquals("Void return type", "V", mSig.returnType.toString()); + assertEquals("No throws", 0, mSig.throwsSignatures.length); + } + + public void testMethodSignatureTypeParams() { + GenericSignature.MethodTypeSignature mSig = parser.parseAsMethodSignature("<T:>(TT;)V"); + assertEquals("One type parameter", 1, mSig.formalTypeParameters.length); + assertEquals("T", mSig.formalTypeParameters[0].identifier); + assertEquals("Ljava/lang/Object;", mSig.formalTypeParameters[0].classBound.toString()); + assertEquals("One parameter", 1, mSig.parameters.length); + assertEquals("TT;", mSig.parameters[0].toString()); + assertEquals("Void return type", "V", mSig.returnType.toString()); + assertEquals("No throws", 0, mSig.throwsSignatures.length); + } + + public void testMethodSignatureGenericReturn() { + GenericSignature.MethodTypeSignature mSig = parser.parseAsMethodSignature("<T:>()TT;"); + assertEquals("One type parameter", 1, mSig.formalTypeParameters.length); + assertEquals("T", mSig.formalTypeParameters[0].identifier); + assertEquals("Ljava/lang/Object;", mSig.formalTypeParameters[0].classBound.toString()); + assertEquals("No parameters", 0, mSig.parameters.length); + assertEquals("'T' return type", "TT;", mSig.returnType.toString()); + assertEquals("No throws", 0, mSig.throwsSignatures.length); + } + + public void testMethodSignatureThrows() { + GenericSignature.MethodTypeSignature mSig = parser + .parseAsMethodSignature("<T:>(TT;)V^Ljava/lang/Exception;^Ljava/lang/RuntimeException;"); + assertEquals("One type parameter", 1, mSig.formalTypeParameters.length); + assertEquals("T", mSig.formalTypeParameters[0].identifier); + assertEquals("Ljava/lang/Object;", mSig.formalTypeParameters[0].classBound.toString()); + assertEquals("One parameter", 1, mSig.parameters.length); + assertEquals("TT;", mSig.parameters[0].toString()); + assertEquals("Void return type", "V", mSig.returnType.toString()); + assertEquals("2 throws", 2, mSig.throwsSignatures.length); + assertEquals("Ljava/lang/Exception;", mSig.throwsSignatures[0].toString()); + assertEquals("Ljava/lang/RuntimeException;", mSig.throwsSignatures[1].toString()); + } + + public void testMethodSignaturePrimitiveParams() { + GenericSignature.MethodTypeSignature mSig = parser.parseAsMethodSignature("(ILjava/lang/Object;)V"); + assertEquals("2 parameters", 2, mSig.parameters.length); + assertEquals("I", mSig.parameters[0].toString()); + assertEquals("Ljava/lang/Object;", mSig.parameters[1].toString()); + } + + public void testFullyQualifiedSuperclassAfterTypeParams() { + try { + GenericSignature.FieldTypeSignature cSig = parser.parseAsFieldSignature("Ljava/util/List</;"); + fail("Expected IllegalStateException"); + } catch (IllegalStateException ex) { + assertTrue(ex.getMessage().indexOf("Ljava/util/List</;") != -1); + } + } + + public void testPr107784() { + parser + .parseAsMethodSignature("(Lcom/cibc/responders/mapping/CommonDataBeanScenario;Ljava/lang/Object;)Lcom/cibc/responders/response/Formatter<[BLjava/lang/Object;>;"); + parser.parseAsClassSignature("<Parent:Ljava/lang/Object;Child:Ljava/lang/Object;>Ljava/lang/Object;"); + } + + private void assertEquals(String[] expected, String[] actual) { + if (actual.length != expected.length) { + int shorter = Math.min(expected.length, actual.length); + for (int i = 0; i < shorter; i++) { + if (!actual[i].equals(expected[i])) { + fail("Expected " + expected[i] + " at position " + i + " but found " + actual[i]); + } + } + fail("Expected " + expected.length + " tokens but got " + actual.length + tokensToString(actual)); + } + for (int i = 0; i < actual.length; i++) { + if (!actual[i].equals(expected[i])) { + fail("Expected " + expected[i] + " at position " + i + " but found " + actual[i]); + } + } + } + + private String tokensToString(String[] tokens) { + StringBuffer sb = new StringBuffer(); + sb.append(tokens[0]); + for (int i = 1; i < tokens.length; i++) { + sb.append(","); + sb.append(tokens[i]); + } + return sb.toString(); + } + + protected void setUp() throws Exception { + super.setUp(); + parser = new GenericSignatureParser(); + } +} diff --git a/util/src/test/java/org/aspectj/util/LangUtilTest.java b/util/src/test/java/org/aspectj/util/LangUtilTest.java new file mode 100644 index 000000000..4cb44795d --- /dev/null +++ b/util/src/test/java/org/aspectj/util/LangUtilTest.java @@ -0,0 +1,270 @@ +/* ******************************************************************* + * 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 Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Xerox/PARC initial implementation + * ******************************************************************/ + +package org.aspectj.util; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.TestCase; + +/** + * + */ +public class LangUtilTest extends TestCase { + + public LangUtilTest(String name) { + super(name); + } + + // /** @see LangUtil.extractOptions(String[], String[], int[], List) */ + // public void testExtractOptions() { + // ArrayList extracted = new ArrayList(); + // String[] args = new String[] { "-d", "classes", "-classpath", "foo.jar", "-verbose", "Bar.java" }; + // String[] validOptions = new String[] { "-classpath", "-d", "-verbose", "-help" }; + // int[] optionArgs = new int[] { 1, 1, 0, 0 }; + // String[] result = LangUtil.extractOptions(args, validOptions, optionArgs, extracted); + // String resultString = "" + Arrays.asList(result); + // String EXP = "[Bar.java]"; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // EXP = "[-d, classes, -classpath, foo.jar, -verbose]"; + // resultString = "" + extracted; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // + // // no input, no output + // extracted.clear(); + // args = new String[] {}; + // result = LangUtil.extractOptions(args, validOptions, optionArgs, extracted); + // resultString = "" + Arrays.asList(result); + // EXP = "[]"; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // resultString = "" + extracted; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // + // // one input, nothing extracted + // extracted.clear(); + // args = new String[] {"Bar.java"}; + // result = LangUtil.extractOptions(args, validOptions, optionArgs, extracted); + // resultString = "" + Arrays.asList(result); + // EXP = "[Bar.java]"; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // EXP = "[]"; + // resultString = "" + extracted; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // + // // one input, extracted + // extracted.clear(); + // args = new String[] {"-verbose"}; + // result = LangUtil.extractOptions(args, validOptions, optionArgs, extracted); + // resultString = "" + Arrays.asList(result); + // EXP = "[]"; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // EXP = "[-verbose]"; + // resultString = "" + extracted; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // + // // ------- booleans + // validOptions = new String[] { "-help", "-verbose" }; + // optionArgs = null; + // + // // one input, extracted + // extracted.clear(); + // args = new String[] {"-verbose"}; + // result = LangUtil.extractOptions(args, validOptions, optionArgs, extracted); + // resultString = "" + Arrays.asList(result); + // EXP = "[]"; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // EXP = "[-verbose]"; + // resultString = "" + extracted; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // + // // one input, not extracted + // extracted.clear(); + // args = new String[] {"Bar.java"}; + // result = LangUtil.extractOptions(args, validOptions, optionArgs, extracted); + // resultString = "" + Arrays.asList(result); + // EXP = "[Bar.java]"; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // EXP = "[]"; + // resultString = "" + extracted; + // assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + // } + + public void testVersion() { + assertTrue(LangUtil.is13VMOrGreater()); // min vm now - floor may change + if (LangUtil.is15VMOrGreater()) { + assertTrue(LangUtil.is14VMOrGreater()); + } + } + + /** @see LangUtil.extractOptions(String[], String[][]) */ + public void testExtractOptionsArrayCollector() { + String[] args = new String[] { "-d", "classes", "-classpath", "foo.jar", "-verbose", "Bar.java" }; + String[][] OPTIONS = new String[][] { new String[] { "-classpath", null }, new String[] { "-d", null }, + new String[] { "-verbose" }, new String[] { "-help" } }; + + String[][] options = LangUtil.copyStrings(OPTIONS); + + String[] result = LangUtil.extractOptions(args, options); + String resultString = "" + Arrays.asList(result); + String EXP = "[Bar.java]"; + assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + assertTrue("-verbose".equals(options[2][0])); + assertTrue("foo.jar".equals(options[0][1])); + assertTrue("classes".equals(options[1][1])); + assertTrue("-classpath".equals(options[0][0])); + assertTrue("-d".equals(options[1][0])); + assertTrue(null == options[3][0]); + + // get args back, no options set + args = new String[] { "Bar.java" }; + options = LangUtil.copyStrings(OPTIONS); + + result = LangUtil.extractOptions(args, options); + resultString = "" + Arrays.asList(result); + EXP = "[Bar.java]"; + assertTrue(resultString + " != " + EXP, resultString.equals(EXP)); + assertTrue(null == options[0][0]); + assertTrue(null == options[1][0]); + assertTrue(null == options[2][0]); + assertTrue(null == options[3][0]); + } + + // public void testOptionVariants() { + // String[] NONE = new String[0]; + // String[] one = new String[] {"-1"}; + // String[] two = new String[] {"-2"}; + // String[] three= new String[] {"-3"}; + // String[] both = new String[] {"-1", "-2" }; + // String[] oneB = new String[] {"-1-"}; + // String[] bothB = new String[] {"-1-", "-2-" }; + // String[] onetwoB = new String[] {"-1", "-2-" }; + // String[] oneBtwo = new String[] {"-1-", "-2" }; + // String[] threeB = new String[] {"-1-", "-2-", "-3-"}; + // String[] athreeB = new String[] {"a", "-1-", "-2-", "-3-"}; + // String[] threeaB = new String[] {"-1-", "a", "-2-", "-3-"}; + // + // checkOptionVariants(NONE, new String[][] { NONE }); + // checkOptionVariants(one, new String[][] { one }); + // checkOptionVariants(both, new String[][] { both }); + // checkOptionVariants(oneB, new String[][] { NONE, one }); + // checkOptionVariants(bothB, new String[][] { NONE, one, new String[] {"-2"}, both }); + // checkOptionVariants(onetwoB, new String[][] { one, new String[] {"-1", "-2"}}); + // checkOptionVariants(oneBtwo, new String[][] { two, new String[] {"-1", "-2"}}); + // checkOptionVariants(threeB, new String[][] + // { + // NONE, + // one, + // two, + // new String[] {"-1", "-2"}, + // three, + // new String[] {"-1", "-3"}, + // new String[] {"-2", "-3"}, + // new String[] {"-1", "-2", "-3"} + // }); + // checkOptionVariants(athreeB, new String[][] + // { + // new String[] {"a"}, + // new String[] {"a", "-1"}, + // new String[] {"a", "-2"}, + // new String[] {"a", "-1", "-2"}, + // new String[] {"a", "-3"}, + // new String[] {"a", "-1", "-3"}, + // new String[] {"a", "-2", "-3"}, + // new String[] {"a", "-1", "-2", "-3"} + // }); + // checkOptionVariants(threeaB, new String[][] + // { + // new String[] {"a"}, + // new String[] {"-1", "a"}, + // new String[] {"a", "-2"}, + // new String[] {"-1", "a", "-2"}, + // new String[] {"a", "-3"}, + // new String[] {"-1", "a", "-3"}, + // new String[] {"a", "-2", "-3"}, + // new String[] {"-1", "a", "-2", "-3"} + // }); + // } + + // void checkOptionVariants(String[] options, String[][] expected) { + // String[][] result = LangUtil.optionVariants(options); + // if (expected.length != result.length) { + // assertTrue("exp=" + expected.length + " actual=" + result.length, false); + // } + // for (int i = 0; i < expected.length; i++) { + // assertEquals(""+i, + // "" + Arrays.asList(expected[i]), + // "" + Arrays.asList(result[i])); + // } + // } + + /** @see XMLWriterTest#testUnflattenList() */ + public void testCommaSplit() { + checkCommaSplit("", new String[] { "" }); + checkCommaSplit("1", new String[] { "1" }); + checkCommaSplit(" 1 2 ", new String[] { "1 2" }); + checkCommaSplit(" 1 , 2 ", new String[] { "1", "2" }); + checkCommaSplit("1,2,3,4", new String[] { "1", "2", "3", "4" }); + } + + void checkCommaSplit(String input, String[] expected) { + List actual = LangUtil.commaSplit(input); + String a = "" + actual; + String e = "" + Arrays.asList(expected); + assertTrue(e + "==" + a, e.equals(a)); + } + + public void testElideEndingLines() { + StringBuffer stackBuffer = LangUtil.stackToString(new RuntimeException(""), true); + LangUtil.elideEndingLines(LangUtil.StringChecker.TEST_PACKAGES, stackBuffer, 10); + String result = stackBuffer.toString(); + + if (-1 == result.indexOf("(... ")) { + // brittle - will fail under different top-level drivers + String m = "when running under eclipse or Ant, expecting (... in trace: "; + assertTrue(m + result, false); + } + + stackBuffer = new StringBuffer( + "java.lang.RuntimeException: unimplemented" + + "\n at org.aspectj.ajdt.internal.core.builder.EclipseUnwovenClassFile.writeWovenBytes(EclipseUnwovenClassFile.java:59)" + + "\n at org.aspectj.weaver.bcel.BcelWeaver.dump(BcelWeaver.java:271)" + + "\n at org.aspectj.weaver.bcel.BcelWeaver.weave(BcelWeaver.java:233)" + + "\n at org.aspectj.weaver.bcel.BcelWeaver.weave(BcelWeaver.java:198)" + + "\n at org.aspectj.ajdt.internal.core.builder.AjBuildManager.weaveAndGenerateClassFiles(AjBuildanager.java:230)" + + "\n at org.aspectj.ajdt.internal.core.builder.AjBuildManager.batchBuild(AjBuildManager.java:50)" + + "\n at org.aspectj.ajdt.ajc.AjdtCommand.runCommand(AjdtCommand.java:42)" + + "\n at org.aspectj.testing.harness.bridge.CompilerRun.run(CompilerRun.java:222)" + + "\n at org.aspectj.testing.run.Runner.runPrivate(Runner.java:363)" + + "\n at org.aspectj.testing.run.Runner.runChild(Runner.java:167)" + + "\n at org.aspectj.testing.run.Runner.runChild(Runner.java:126)" + + "\n at org.aspectj.testing.run.Runner$IteratorWrapper.run(Runner.java:441)" + + "\n at org.aspectj.testing.run.Runner.runPrivate(Runner.java:363)" + + "\n at org.aspectj.testing.run.Runner.runChild(Runner.java:167)" + + "\n at org.aspectj.testing.run.Runner.runChild(Runner.java:126)" + + "\n at org.aspectj.testing.run.Runner$IteratorWrapper.run(Runner.java:441)" + + "\n at org.aspectj.testing.run.Runner.runPrivate(Runner.java:363)" + + "\n at org.aspectj.testing.run.Runner.run(Runner.java:114)" + + "\n at org.aspectj.testing.run.Runner.run(Runner.java:105)" + + "\n at org.aspectj.testing.run.Runner.runIterator(Runner.java:228)" + + "\n at org.aspectj.testing.drivers.Harness.run(Harness.java:254)" + + "\n at org.aspectj.testing.drivers.Harness.runMain(Harness.java:217)" + + "\n at org.aspectj.testing.drivers.Harness.main(Harness.java:99)" + + "\n at org.aspectj.testing.Harness.main(Harness.java:37)" + "\n clip me"); + + LangUtil.elideEndingLines(LangUtil.StringChecker.TEST_PACKAGES, stackBuffer, 25); + result = stackBuffer.toString(); + assertTrue(result, -1 != result.indexOf("(... ")); + assertTrue(result, -1 == result.indexOf("org.aspectj.testing")); + } +} |