package org.aspectj.testing.harness.bridge;
import java.io.File;
+import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.MessageHandler;
import org.aspectj.bridge.MessageUtil;
+import org.aspectj.testing.xml.MessageListXmlReader;
import org.aspectj.testing.xml.XMLWriter;
import org.aspectj.util.LangUtil;
public class DirChangesTest extends TestCase {
private static final boolean PRINTING = false;
+ /** name of file in srcBaseDir with any expected messages */
+ public static final String EXPECTED_NAME = "expectedMessages.xml";
+
public DirChangesTest(String name) {
super(name);
}
* Uses testdata/dirChangesTestDir/same
*/
public void testSameExpDir() {
- doCheck("same", true);
+ doCheck("same");
}
/**
* Uses testdata/dirChangesTestDir/diff
*/
public void testDiffExpDir() {
- doCheck("diff", false);
+ doCheck("diff");
}
public void testWriteEmpty() {
assertEquals(expected, actual.toString());
}
- private void doCheck(String dir, boolean expectPass) {
+ private void doCheck(String dir) {
DirChanges.Spec spec = new DirChanges.Spec();
File srcBaseDir = new File("testdata/dirChangesTestDir/" + dir);
// actual = baseDir
File baseDir = new File(srcBaseDir, "actual");
// expected = srcBaseDir + spec.expDir
spec.setExpDir("expected");
- checkDirChanges(spec, expectPass, baseDir, srcBaseDir, null);
+ IMessage[] expected = null;
+ File expMssgFile = new File(srcBaseDir, EXPECTED_NAME);
+ if (expMssgFile.canRead()) {
+ try {
+ expected = new MessageListXmlReader().readMessages(expMssgFile);
+ } catch (IOException e) {
+ System.err.println("Continuing after error reading " + expMssgFile);
+ e.printStackTrace(System.err);
+ }
+ }
+ checkDirChanges(spec, baseDir, srcBaseDir, null, expected);
}
// XXX WEAK upgrade to read expected-diffs from file in directory
private void checkDirChanges(
DirChanges.Spec spec,
- boolean shouldPass,
File baseDir,
File srcBaseDir,
- Runnable dirChanger) {
+ Runnable dirChanger,
+ IMessage[] expected) {
DirChanges dc = new DirChanges(spec);
MessageHandler handler = new MessageHandler();
try {
if (!dc.start(handler, baseDir)) {
- assertTrue(!shouldPass);
+ //assertTrue(!shouldPass);
+ assertSameMessages(expected, handler);
return; // exiting after (XXX) undertested expected failure?
} else {
assertTrue(0 == handler.numMessages(IMessage.ERROR, true));
dirChanger.run();
}
if (!dc.end(handler, srcBaseDir)) {
- assertTrue(!shouldPass);
+ //assertTrue(!shouldPass);
+ assertSameMessages(expected, handler);
} else {
assertTrue(0 == handler.numMessages(IMessage.ERROR, true));
- assertTrue(shouldPass);
}
} catch (Throwable t) {
- if (PRINTING && shouldPass) {
+ if (PRINTING) {
t.printStackTrace(System.err);
}
throw new AssertionFailedError(LangUtil.renderException(t));
} finally {
- if (PRINTING && 0 < handler.numMessages(null, true)) {
- MessageUtil.print(System.err, handler, "checkDirChanges: ");
+ if (0 < handler.numMessages(null, true)) {
+ if (PRINTING) {
+ MessageUtil.print(System.err, handler, "checkDirChanges: ");
+ }
+ IMessage[] toprint = handler.getMessages(null, true);
+ File output = new File(srcBaseDir, EXPECTED_NAME);
+ try {
+ //toprint[0].getISourceLocation().getSourceFile();
+ System.out.println("XXX writing to " + output
+ + " messages " + LangUtil.arrayAsList(toprint));
+ new MessageListXmlReader().writeMessages(output, toprint);
+ } catch (IOException e) {
+ System.err.println("Error writing to " + output
+ + " messages " + LangUtil.arrayAsList(toprint));
+ e.printStackTrace(System.err);
+ }
}
}
}
+
+ /**
+ * Assert unless messages in handler match all expected messages.
+ * @param expected
+ * @param handler
+ */
+ private void assertSameMessages(
+ IMessage[] expected,
+ MessageHandler handler) {
+ IMessage[] actual = handler.getMessages(null, true);
+ for (int i = 0; i < actual.length; i++) {
+ int found = find(actual[i], expected);
+ if (-1 != found) {
+ expected[found] = null;
+ actual[i] = null;
+ }
+ }
+ StringBuffer sb = new StringBuffer();
+ {
+ IMessage[] expNotFound = (IMessage[])
+ LangUtil.safeCopy(expected, new IMessage[0]);
+ if (0 < expNotFound.length) {
+ sb.append("expected not found: ");
+ sb.append(LangUtil.arrayAsList(expNotFound).toString());
+ }
+ }
+ {
+ IMessage[] actFound = (IMessage[])
+ LangUtil.safeCopy(actual, new IMessage[0]);
+ if (0 < actFound.length) {
+ sb.append(" not expected but found: ");
+ sb.append(LangUtil.arrayAsList(actFound).toString());
+ }
+ }
+
+ if (0 < sb.length()) {
+ assertTrue(sb.toString(), false);
+ }
+ }
+
+ /**
+ * Find message in array, comparing by contents
+ * but ignoring exceptions thrown and source location XXX.
+ * @param message the IMessage to find
+ * @param expected the
+ * @return int
+ */
+ private int find(IMessage message, IMessage[] expected) {
+ if ((null != expected) && (0 != expected.length)
+ && (null != message)) {
+ final IMessage.Kind kind = message.getKind();
+ final String text = message.getMessage();
+ for (int i = 0; i < expected.length; i++) {
+ IMessage exp = expected[i];
+ if (null != exp) {
+ if (kind.equals(exp.getKind())
+ && text.equals(exp.getMessage())) {
+ return i;
+ }
+ }
+ }
+ }
+ return -1;
+ }
+
+
}
--- /dev/null
+/* *******************************************************************
+ * 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.xml;
+
+import org.aspectj.bridge.IMessage;
+import org.aspectj.util.LangUtil;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ */
+public class MessageListXmlReaderTest extends TestCase {
+
+ ArrayList tempFiles = new ArrayList();
+ public MessageListXmlReaderTest(String name) {
+ super(name);
+ }
+
+ public void setUp() {
+ tempFiles.clear();
+ }
+
+ public void tearDown() {
+ if (!LangUtil.isEmpty(tempFiles)) {
+ for (Iterator iter = tempFiles.iterator(); iter.hasNext();) {
+ File file = (File) iter.next();
+ if (file.canRead()) {
+ file.delete();
+ }
+ }
+ }
+ }
+ /** XXX not in suite until this is fixed */
+ public void testMessageReading() throws Exception {
+ assertTrue("XXX need better XML wrapping - has < character", false);
+ //checkXmlRoundTrip("testdata/dirChangesTestDir/diff/expectedMessages");
+ //checkXmlRoundTrip("testdata/dirChangesTestDir/same/expectedMessages");
+ }
+
+ void checkXmlRoundTrip(String path) throws Exception {
+ String xmlPath = path + ".xml";
+ String xml2Path = path + ".tmp.xml";
+ final File file1 = new File(xmlPath);
+ assertTrue("" + file1, file1.canRead());
+
+ MessageListXmlReader reader = new MessageListXmlReader();
+ IMessage[] messages = reader.readMessages(file1);
+ assertNotNull(messages);
+ assertTrue("empty messages", 0 != messages.length);
+
+ File file2 = new File(xml2Path);
+ String warning = reader.writeMessages(file2, messages);
+ assertTrue(warning, null == warning);
+ tempFiles.add(file2);
+
+ IMessage[] messages2 = reader.readMessages(file2);
+ assertEquals(LangUtil.arrayAsList(messages).toString(),
+ LangUtil.arrayAsList(messages2).toString());
+ }
+}