001: /*
002: * UmlGraph class diagram testing framework
003: *
004: * Contibuted by Andrea Aime
005: * (C) Copyright 2005 Diomidis Spinellis
006: *
007: * Permission to use, copy, and distribute this software and its
008: * documentation for any purpose and without fee is hereby granted,
009: * provided that the above copyright notice appear in all copies and that
010: * both that copyright notice and this permission notice appear in
011: * supporting documentation.
012: *
013: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
014: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
015: * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * $Id$
018: *
019: */
020:
021: package org.umlgraph.test;
022:
023: import java.io.BufferedReader;
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.IOException;
027: import java.io.PrintWriter;
028: import java.util.List;
029:
030: /**
031: * Collection of utility methods used by the test classes
032: * @author wolf
033: *
034: */
035: public class TestUtils {
036:
037: /**
038: * Simple text file diffing: will tell you if two text files are line by
039: * line equals, and will stop at the first difference found.
040: * @throws IOException
041: */
042: public static boolean textFilesEquals(PrintWriter pw,
043: File refTextFile, File outTextFile) throws IOException {
044: BufferedReader refReader = null, outReader = null;
045: String refLine = null, outFile = null;
046: try {
047: pw.println("Performing diff:");
048: pw.println("out: " + outTextFile.getAbsolutePath());
049: pw.println("ref: " + refTextFile.getAbsolutePath());
050:
051: refReader = new BufferedReader(new FileReader(refTextFile));
052: outReader = new BufferedReader(new FileReader(outTextFile));
053:
054: /*
055: * Line by line scan, exit when one file ends or lines are not
056: * equal.
057: * Ignore the "Generated by javadoc ..." comments, and the
058: * "<META NAME="date"" tags
059: */
060: for (;;) {
061: refLine = refReader.readLine();
062: outFile = outReader.readLine();
063: if (refLine == null || outFile == null) {
064: break;
065: } else if (refLine.startsWith("<META NAME=\"date\"")) {
066: if (!outFile.startsWith("<META NAME=\"date\""))
067: break;
068: } else if (refLine
069: .startsWith("<!-- Generated by javadoc ")) {
070: if (!outFile
071: .startsWith("<!-- Generated by javadoc "))
072: break;
073: } else if (!refLine.equals(outFile)) {
074: break;
075: }
076:
077: }
078: } finally {
079: if (refReader != null)
080: refReader.close();
081: if (outReader != null)
082: outReader.close();
083: }
084: // they were equals if both files ended at the same time
085: boolean equal = refLine == null && outFile == null;
086: if (equal)
087: pw.print("File contents are equal");
088: else
089: pw.println("Differences found\nref: " + refLine + "\nout: "
090: + outFile);
091: pw.println();
092: pw.println();
093:
094: return equal;
095: }
096:
097: public static boolean dotFilesEqual(PrintWriter pw, String dotPath,
098: String refPath) throws IOException {
099: pw.println("Performing diff:\nout:" + dotPath + "\nref:"
100: + refPath);
101: DotDiff differ = new DotDiff(new File(dotPath), new File(
102: refPath));
103: boolean equal = differ.graphEquals();
104: if (equal) {
105: pw.println("File contents are structurally equal");
106: } else {
107: pw.println("File contents are structurally not equal");
108: printList(pw, "# Lines in out but not in ref", differ
109: .getExtraLines1());
110: printList(pw, "# Lines in ref but not in out", differ
111: .getExtraLines2());
112: printList(pw, "# Nodes in out but not in ref", differ
113: .getNodes1());
114: printList(pw, "# Nodes in ref but not in out", differ
115: .getNodes2());
116: printList(pw, "# Arcs in out but not in ref", differ
117: .getArcs1());
118: printList(pw, "# Arcs in ref but not in out", differ
119: .getArcs2());
120: }
121: pw.println("\n\n");
122: return equal;
123: }
124:
125: public static void printList(PrintWriter pw, String message,
126: List extraOut) {
127: if (extraOut.size() > 0) {
128: pw.println(message);
129: for (Object o : extraOut) {
130: pw.println(o);
131: }
132: }
133: }
134:
135: /**
136: * Deletes the content of the folder, eventually in a recursive way (but
137: * avoids deleting eventual .cvsignore files and CVS folders)
138: */
139: public static void cleanFolder(File folder, boolean recurse) {
140: for (File f : folder.listFiles()) {
141: if (f.isDirectory() && !f.getName().equals("CVS")) {
142: if (recurse) {
143: cleanFolder(f, true);
144: if (f.list().length == 0)
145: f.delete();
146: }
147: } else if (!f.getName().equals(".cvsignore")) {
148: f.delete();
149: }
150:
151: }
152:
153: }
154:
155: }
|