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: package org.umlgraph.test;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import java.util.ArrayList;
026: import java.util.Arrays;
027: import java.util.List;
028:
029: /**
030: * UmlGraphDoc doclet regression tests
031: * @author wolf
032: *
033: */
034: public class UmlDocTest {
035:
036: static final String testSourceFolder = "testdata/umldoc-src";
037:
038: static final String testDestFolder = "testdata/umldoc-out";
039:
040: static final String testRefFolder = "testdata/umldoc-ref";
041: static final String doclet = "org.umlgraph.doclet.UmlGraphDoc";
042:
043: static PrintWriter pw = new PrintWriter(System.out);
044:
045: public static void main(String[] args) throws IOException {
046: List<String> differences = new ArrayList<String>();
047:
048: File outFolder = new File(testDestFolder);
049: if (!outFolder.exists())
050: outFolder.mkdirs();
051:
052: TestUtils.cleanFolder(outFolder, true);
053:
054: // don't use windows specific fonts
055: System.setProperty("os.name", "generic");
056:
057: // run tests
058: runTest(differences);
059: if (differences.size() > 0) {
060: pw
061: .println("ERROR, some files are not structurally equal or some files are missing:");
062: for (String className : differences) {
063: pw.println(className);
064: }
065: } else {
066: pw.println("GOOD, all files are structurally equal");
067: }
068: pw.println();
069: pw.println();
070: pw.flush();
071: }
072:
073: private static void runTest(List<String> differences)
074: throws IOException {
075: File outFolder = new File(testDestFolder);
076: String[] options = new String[] { "-docletpath", "build",
077: "-private", "-d", outFolder.getAbsolutePath(),
078: "-sourcepath", testSourceFolder, "-compact",
079: "-subpackages", "gr.spinellis", "-inferrel",
080: "-inferdep", "-qualify", "-postfixpackage",
081: "-collpackages", "java.util.*" };
082: runDoclet(options);
083:
084: compareDocletOutputs(differences, new File(testRefFolder),
085: new File(testDestFolder));
086: }
087:
088: /**
089: * Ensures that reference and output have the same contents in terms of:
090: * <ul>
091: * <li> html files </li>
092: * <li> dot files </li>
093: * <li> folders </li>
094: * </ul>
095: * @throws IOException
096: */
097: private static void compareDocletOutputs(List<String> differences,
098: File refFolder, File outFolder) throws IOException {
099: if (refFolder.getName().equals("CVS"))
100: return;
101:
102: if (!refFolder.exists() || !refFolder.isDirectory())
103: throw new IllegalArgumentException(
104: "Reference does not exists or is not a folder: "
105: + refFolder.getAbsolutePath());
106: if (!outFolder.exists() || !outFolder.isDirectory())
107: throw new IllegalArgumentException(
108: "Output does not exists or is not a folder: "
109: + outFolder.getAbsolutePath());
110:
111: // get elements and sort
112: String[] refFiles = refFolder.list();
113: String[] outFiles = refFolder.list();
114: Arrays.sort(refFiles);
115: Arrays.sort(outFiles);
116:
117: // parallel scan (mergesort inspired)
118: int i = 0, j = 0;
119: while (i < refFiles.length && j < outFiles.length) {
120: File ref = new File(refFolder, refFiles[i]);
121: File out = new File(outFolder, outFiles[j]);
122: int compare = refFiles[i].compareTo(outFiles[i]);
123: if (compare == 0) {
124: String refName = ref.getName().toLowerCase();
125: if (ref.isDirectory()) {
126: compareDocletOutputs(differences, ref, out);
127: } else if (refName.endsWith(".dot")) {
128: if (!TestUtils.dotFilesEqual(pw, ref
129: .getAbsolutePath(), out.getAbsolutePath()))
130: differences.add(out.getName()
131: + " is different from the reference");
132:
133: } else {
134: if (!TestUtils.textFilesEquals(pw, ref, out))
135: differences.add(out.getName()
136: + " is different from the reference");
137: }
138: i++;
139: j++;
140: } else if (compare < 0) {
141: differences
142: .add("Reference file/folder not found in output: "
143: + ref.getAbsolutePath());
144: i++;
145: } else {
146: j++;
147: }
148: }
149:
150: // all ref files remaining are missing ones
151: while (i < refFiles.length) {
152: File ref = new File(refFolder, refFiles[i]);
153: differences
154: .add("Reference file/folder not found in output: "
155: + ref.getAbsolutePath());
156: i++;
157: }
158: }
159:
160: /**
161: * Runs the UmlGraphDoc doclet
162: * @param options
163: */
164: private static void runDoclet(String[] options) {
165: pw.print("Run javadoc -doclet " + doclet);
166: for (String o : options)
167: pw.print(o + " ");
168: pw.println();
169: com.sun.tools.javadoc.Main.execute("UMLDoc test", pw, pw, pw,
170: doclet, options);
171: }
172:
173: }
|