Added testing for umldoc, and moved shared methods with the umlgraph tests in an utility class

This commit is contained in:
Andrea Aime 2006-06-04 13:55:00 +00:00
parent 6bbf7d2f71
commit d827bdf1f5
6 changed files with 658 additions and 124 deletions

View File

@ -27,6 +27,11 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* UmlGraph regression tests
* @author wolf
*
*/
public class BasicTest {
static String testSourceFolder = "testdata/java";
@ -38,38 +43,41 @@ public class BasicTest {
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
List<String> different = new ArrayList<String>();
List<String> differences = new ArrayList<String>();
File outFolder = new File(testDestFolder);
if (!outFolder.exists())
outFolder.mkdirs();
deleteFiles(outFolder, ".dot");
deleteFiles(outFolder, ".png");
System.setProperty("os.name", "generic"); // don't use windows specific fonts
performBasicTests(different);
performViewTests(different, outFolder);
TestUtils.cleanFolder(outFolder, true);
if (different.size() > 0) {
// don't use windows specific fonts
System.setProperty("os.name", "generic");
// run tests
performBasicTests(differences);
performViewTests(differences, outFolder);
if (differences.size() > 0) {
pw.println("ERROR, some files are not structurally equal or some files are missing:");
for (String className : different) {
for (String className : differences) {
pw.println(className);
}
} else {
pw.println("GOOD, all files are structurally equal");
}
pw.println();
pw.println();
pw.flush();
}
private static void performViewTests(List<String> differences, File outFolder)
throws IOException {
String[] options = new String[] { "-docletpath", "build", "-private", "-d",
outFolder.getAbsolutePath(), "-sourcepath", "testdata/java", "-compact", "-subpackages",
"gr.spinellis", "-views" };
outFolder.getAbsolutePath(), "-sourcepath", "testdata/java", "-compact",
"-subpackages", "gr.spinellis", "-views" };
runDoclet(options);
List<String> viewFiles = new ArrayList<String>();
List<String> viewFiles = new ArrayList<String>();
viewFiles.addAll(getViewList(new File(testSourceFolder, "gr/spinellis/basic/views")));
viewFiles.addAll(getViewList(new File(testSourceFolder, "gr/spinellis/context/views")));
viewFiles.addAll(getViewList(new File(testSourceFolder, "gr/spinellis/iface/views")));
@ -91,26 +99,16 @@ public class BasicTest {
}
private static List<String> getViewList(File viewFolder) {
if(!viewFolder.exists())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath() + " does not exists.");
else if(!viewFolder.isDirectory())
if (!viewFolder.exists())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath()
+ " does not exists.");
else if (!viewFolder.isDirectory())
throw new RuntimeException(viewFolder.getAbsolutePath() + " is not a folder!.");
else if(!viewFolder.canRead())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath() + " cannot be read.");
return Arrays.asList(viewFolder.list(new SimpleFileFilter(".java")));
}
else if (!viewFolder.canRead())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath()
+ " cannot be read.");
private static void compare(List<String> differences, File dotFile, File refFile) throws IOException {
if(!dotFile.exists()) {
pw.println("Error, output file " + dotFile + " has not been generated");
differences.add(dotFile.getName() + " has not been generated");
} else if(!refFile.exists()) {
pw.println("Error, reference file " + refFile + " is not available");
differences.add(refFile.getName() + " reference is not available");
} else if (!dotFilesEqual(dotFile.getAbsolutePath(), refFile.getAbsolutePath())) {
differences.add(dotFile.getName() + " is different from the reference");
}
return Arrays.asList(viewFolder.list(new SimpleFileFilter(".java")));
}
private static boolean performBasicTests(List<String> differences) throws IOException {
@ -123,8 +121,8 @@ public class BasicTest {
dotFile.delete();
File refFile = new File(testRefFolder, outFileName);
String javaPath = new File(testSourceFolder, javaFiles[i]).getAbsolutePath();
String[] options = new String[] { "-docletpath", "build", "-hide", "Hidden", "-compact",
"-private", "-d", testDestFolder, "-output", outFileName, javaPath };
String[] options = new String[] { "-docletpath", "build", "-hide", "Hidden",
"-compact", "-private", "-d", testDestFolder, "-output", outFileName, javaPath };
runDoclet(options);
compare(differences, dotFile, refFile);
@ -137,37 +135,18 @@ public class BasicTest {
"gr.spinellis.umlgraph.doclet.UmlGraph", options);
}
private static boolean dotFilesEqual(String dotPath, String refPath) throws IOException {
pw.println("Performing diff:\nout:" + dotPath + "\nref:" + refPath);
DotDiff differ = new DotDiff(new File(dotPath), new File(refPath));
boolean equal = differ.graphEquals();
if (equal) {
pw.println("File contents are structurally equal");
} else {
pw.println("File contents are structurally not equal");
printList("# Lines in out but not in ref", differ.getExtraLines1());
printList("# Lines in ref but not in out", differ.getExtraLines2());
printList("# Nodes in out but not in ref", differ.getNodes1());
printList("# Nodes in ref but not in out", differ.getNodes2());
printList("# Arcs in out but not in ref", differ.getArcs1());
printList("# Arcs in ref but not in out", differ.getArcs2());
private static void compare(List<String> differences, File dotFile, File refFile)
throws IOException {
if (!dotFile.exists()) {
pw.println("Error, output file " + dotFile + " has not been generated");
differences.add(dotFile.getName() + " has not been generated");
} else if (!refFile.exists()) {
pw.println("Error, reference file " + refFile + " is not available");
differences.add(refFile.getName() + " reference is not available");
} else if (!TestUtils.dotFilesEqual(pw, dotFile.getAbsolutePath(), refFile
.getAbsolutePath())) {
differences.add(dotFile.getName() + " is different from the reference");
}
pw.println("\n\n");
return equal;
}
private static void printList(String message, List extraOut) {
if (extraOut.size() > 0) {
pw.println(message);
for (Object o : extraOut) {
pw.println(o);
}
}
}
private static void deleteFiles(File folder, String extension) {
File[] files = folder.listFiles(new SimpleFileFilter(extension));
for(File f: files)
f.delete();
}
}

View File

@ -0,0 +1,136 @@
/*
* UmlGraph class diagram testing framework
*
* Contibuted by Andrea Aime
* (C) Copyright 2005 Diomidis Spinellis
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
*
*/
package gr.spinellis.umlgraph.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
* Collection of utility methods used by the test classes
* @author wolf
*
*/
public class TestUtils {
/**
* Simple text file diffing: will tell you if two text files are line by
* line equals, and will stop at the first difference found.
* @throws IOException
*/
public static boolean textFilesEquals(PrintWriter pw, File refTextFile, File outTextFile)
throws IOException {
BufferedReader refReader = null, outReader = null;
String refLine = null, outFile = null;
try {
pw.println("Performing diff:");
pw.println("out: " + outTextFile.getAbsolutePath());
pw.println("ref: " + refTextFile.getAbsolutePath());
refReader = new BufferedReader(new FileReader(refTextFile));
outReader = new BufferedReader(new FileReader(outTextFile));
// line by line scan, exit when one file ends or lines are not
// equal (and pass over the "Generated by javadoc ..." comments)
for (;;) {
refLine = refReader.readLine();
outFile = outReader.readLine();
if (refLine == null || outFile == null) {
break;
} else if (refLine.startsWith("<!-- Generated by javadoc ")) {
if (!outFile.startsWith("<!-- Generated by javadoc "))
break;
} else if (!refLine.equals(outFile)) {
break;
}
}
} finally {
if (refReader != null)
refReader.close();
if (outReader != null)
outReader.close();
}
// they were equals if both files ended at the same time
boolean equal = refLine == null && outFile == null;
if (equal)
pw.print("File contents are equal");
else
pw.println("Differences found\nref: " + refLine + "\nout: " + outFile);
pw.println();
pw.println();
return equal;
}
public static boolean dotFilesEqual(PrintWriter pw, String dotPath, String refPath)
throws IOException {
pw.println("Performing diff:\nout:" + dotPath + "\nref:" + refPath);
DotDiff differ = new DotDiff(new File(dotPath), new File(refPath));
boolean equal = differ.graphEquals();
if (equal) {
pw.println("File contents are structurally equal");
} else {
pw.println("File contents are structurally not equal");
printList(pw, "# Lines in out but not in ref", differ.getExtraLines1());
printList(pw, "# Lines in ref but not in out", differ.getExtraLines2());
printList(pw, "# Nodes in out but not in ref", differ.getNodes1());
printList(pw, "# Nodes in ref but not in out", differ.getNodes2());
printList(pw, "# Arcs in out but not in ref", differ.getArcs1());
printList(pw, "# Arcs in ref but not in out", differ.getArcs2());
}
pw.println("\n\n");
return equal;
}
public static void printList(PrintWriter pw, String message, List extraOut) {
if (extraOut.size() > 0) {
pw.println(message);
for (Object o : extraOut) {
pw.println(o);
}
}
}
/**
* Deletes the content of the folder, eventually in a recursive way (but
* avoids deleting eventual .cvsignore files)
*/
public static void cleanFolder(File folder, boolean recurse) {
for (File f : folder.listFiles()) {
if (f.isDirectory()) {
if (recurse) {
cleanFolder(f, true);
if (f.list().length == 0)
f.delete();
}
} else if (!f.getName().equals(".cvsignore")) {
f.delete();
}
}
}
}

View File

@ -0,0 +1,152 @@
/*
* UmlGraph class diagram testing framework
*
* Contibuted by Andrea Aime
* (C) Copyright 2005 Diomidis Spinellis
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
*
*/
package gr.spinellis.umlgraph.test;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* UmlDoc doclet regression tests
* @author wolf
*
*/
public class UmlDocTest {
static String testSourceFolder = "testdata/umldoc-src";
static String testDestFolder = "testdata/umldoc-out";
static String testRefFolder = "testdata/umldoc-ref";
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
List<String> differences = new ArrayList<String>();
File outFolder = new File(testDestFolder);
if (!outFolder.exists())
outFolder.mkdirs();
TestUtils.cleanFolder(outFolder, true);
// don't use windows specific fonts
System.setProperty("os.name", "generic");
// run tests
runTest(differences);
if (differences.size() > 0) {
pw.println("ERROR, some files are not structurally equal or some files are missing:");
for (String className : differences) {
pw.println(className);
}
} else {
pw.println("GOOD, all files are structurally equal");
}
pw.println();
pw.println();
pw.flush();
}
private static void runTest(List<String> differences) throws IOException {
File outFolder = new File(testDestFolder);
String[] options = new String[] { "-docletpath", "build", "-private", "-d",
outFolder.getAbsolutePath(), "-sourcepath", testSourceFolder, "-compact",
"-subpackages", "gr.spinellis", "-inferrel", "-inferdep", "-qualify",
"-postfixpackage", "-collpackages", "java.util.*" };
runDoclet(options);
compareDocletOutputs(differences, new File(testRefFolder), new File(testDestFolder));
}
/**
* Ensures that reference and output have the same contents in terms of:
* <ul>
* <li> html files </li>
* <li> dot files </li>
* <li> folders </li>
* </ul>
* @throws IOException
*/
private static void compareDocletOutputs(List<String> differences, File refFolder,
File outFolder) throws IOException {
if (!refFolder.exists() || !refFolder.isDirectory())
throw new IllegalArgumentException("Reference does not exists or is not a folder: "
+ refFolder.getAbsolutePath());
if (!outFolder.exists() || !outFolder.isDirectory())
throw new IllegalArgumentException("Output does not exists or is not a folder: "
+ refFolder.getAbsolutePath());
// get elements and sort
String[] refFiles = refFolder.list();
String[] outFiles = refFolder.list();
Arrays.sort(refFiles);
Arrays.sort(outFiles);
// parallel scan (mergesort inspired)
int i = 0, j = 0;
while (i < refFiles.length && j < outFiles.length) {
File ref = new File(refFolder, refFiles[i]);
File out = new File(outFolder, outFiles[j]);
int compare = refFiles[i].compareTo(outFiles[i]);
if (compare == 0) {
String refName = ref.getName().toLowerCase();
if (ref.isDirectory()) {
compareDocletOutputs(differences, ref, out);
} else if (refName.endsWith(".dot")) {
if (!TestUtils.dotFilesEqual(pw, ref.getAbsolutePath(), out.getAbsolutePath()))
differences.add(out.getName() + " is different from the reference");
} else {
if (!TestUtils.textFilesEquals(pw, ref, out))
differences.add(out.getName() + " is different from the reference");
}
i++;
j++;
} else if (compare < 0) {
differences.add("Reference file/folder not found in output: "
+ ref.getAbsolutePath());
i++;
} else {
j++;
}
}
// all ref files remaining are missing ones
while (i < refFiles.length) {
File ref = new File(refFolder, refFiles[i]);
differences.add("Reference file/folder not found in output: " + ref.getAbsolutePath());
i++;
}
}
/**
* Runs the UmlDoc doclet
* @param options
*/
private static void runDoclet(String[] options) {
com.sun.tools.javadoc.Main.execute("UMLDoc test", pw, pw, pw,
"gr.spinellis.umlgraph.doclet.UmlDoc", options);
}
}

View File

@ -27,6 +27,11 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* UmlGraph regression tests
* @author wolf
*
*/
public class BasicTest {
static String testSourceFolder = "testdata/java";
@ -38,38 +43,41 @@ public class BasicTest {
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
List<String> different = new ArrayList<String>();
List<String> differences = new ArrayList<String>();
File outFolder = new File(testDestFolder);
if (!outFolder.exists())
outFolder.mkdirs();
deleteFiles(outFolder, ".dot");
deleteFiles(outFolder, ".png");
System.setProperty("os.name", "generic"); // don't use windows specific fonts
performBasicTests(different);
performViewTests(different, outFolder);
TestUtils.cleanFolder(outFolder, true);
if (different.size() > 0) {
// don't use windows specific fonts
System.setProperty("os.name", "generic");
// run tests
performBasicTests(differences);
performViewTests(differences, outFolder);
if (differences.size() > 0) {
pw.println("ERROR, some files are not structurally equal or some files are missing:");
for (String className : different) {
for (String className : differences) {
pw.println(className);
}
} else {
pw.println("GOOD, all files are structurally equal");
}
pw.println();
pw.println();
pw.flush();
}
private static void performViewTests(List<String> differences, File outFolder)
throws IOException {
String[] options = new String[] { "-docletpath", "build", "-private", "-d",
outFolder.getAbsolutePath(), "-sourcepath", "testdata/java", "-compact", "-subpackages",
"gr.spinellis", "-views" };
outFolder.getAbsolutePath(), "-sourcepath", "testdata/java", "-compact",
"-subpackages", "gr.spinellis", "-views" };
runDoclet(options);
List<String> viewFiles = new ArrayList<String>();
List<String> viewFiles = new ArrayList<String>();
viewFiles.addAll(getViewList(new File(testSourceFolder, "gr/spinellis/basic/views")));
viewFiles.addAll(getViewList(new File(testSourceFolder, "gr/spinellis/context/views")));
viewFiles.addAll(getViewList(new File(testSourceFolder, "gr/spinellis/iface/views")));
@ -91,26 +99,16 @@ public class BasicTest {
}
private static List<String> getViewList(File viewFolder) {
if(!viewFolder.exists())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath() + " does not exists.");
else if(!viewFolder.isDirectory())
if (!viewFolder.exists())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath()
+ " does not exists.");
else if (!viewFolder.isDirectory())
throw new RuntimeException(viewFolder.getAbsolutePath() + " is not a folder!.");
else if(!viewFolder.canRead())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath() + " cannot be read.");
return Arrays.asList(viewFolder.list(new SimpleFileFilter(".java")));
}
else if (!viewFolder.canRead())
throw new RuntimeException("The folder " + viewFolder.getAbsolutePath()
+ " cannot be read.");
private static void compare(List<String> differences, File dotFile, File refFile) throws IOException {
if(!dotFile.exists()) {
pw.println("Error, output file " + dotFile + " has not been generated");
differences.add(dotFile.getName() + " has not been generated");
} else if(!refFile.exists()) {
pw.println("Error, reference file " + refFile + " is not available");
differences.add(refFile.getName() + " reference is not available");
} else if (!dotFilesEqual(dotFile.getAbsolutePath(), refFile.getAbsolutePath())) {
differences.add(dotFile.getName() + " is different from the reference");
}
return Arrays.asList(viewFolder.list(new SimpleFileFilter(".java")));
}
private static boolean performBasicTests(List<String> differences) throws IOException {
@ -123,8 +121,8 @@ public class BasicTest {
dotFile.delete();
File refFile = new File(testRefFolder, outFileName);
String javaPath = new File(testSourceFolder, javaFiles[i]).getAbsolutePath();
String[] options = new String[] { "-docletpath", "build", "-hide", "Hidden", "-compact",
"-private", "-d", testDestFolder, "-output", outFileName, javaPath };
String[] options = new String[] { "-docletpath", "build", "-hide", "Hidden",
"-compact", "-private", "-d", testDestFolder, "-output", outFileName, javaPath };
runDoclet(options);
compare(differences, dotFile, refFile);
@ -137,37 +135,18 @@ public class BasicTest {
"gr.spinellis.umlgraph.doclet.UmlGraph", options);
}
private static boolean dotFilesEqual(String dotPath, String refPath) throws IOException {
pw.println("Performing diff:\nout:" + dotPath + "\nref:" + refPath);
DotDiff differ = new DotDiff(new File(dotPath), new File(refPath));
boolean equal = differ.graphEquals();
if (equal) {
pw.println("File contents are structurally equal");
} else {
pw.println("File contents are structurally not equal");
printList("# Lines in out but not in ref", differ.getExtraLines1());
printList("# Lines in ref but not in out", differ.getExtraLines2());
printList("# Nodes in out but not in ref", differ.getNodes1());
printList("# Nodes in ref but not in out", differ.getNodes2());
printList("# Arcs in out but not in ref", differ.getArcs1());
printList("# Arcs in ref but not in out", differ.getArcs2());
private static void compare(List<String> differences, File dotFile, File refFile)
throws IOException {
if (!dotFile.exists()) {
pw.println("Error, output file " + dotFile + " has not been generated");
differences.add(dotFile.getName() + " has not been generated");
} else if (!refFile.exists()) {
pw.println("Error, reference file " + refFile + " is not available");
differences.add(refFile.getName() + " reference is not available");
} else if (!TestUtils.dotFilesEqual(pw, dotFile.getAbsolutePath(), refFile
.getAbsolutePath())) {
differences.add(dotFile.getName() + " is different from the reference");
}
pw.println("\n\n");
return equal;
}
private static void printList(String message, List extraOut) {
if (extraOut.size() > 0) {
pw.println(message);
for (Object o : extraOut) {
pw.println(o);
}
}
}
private static void deleteFiles(File folder, String extension) {
File[] files = folder.listFiles(new SimpleFileFilter(extension));
for(File f: files)
f.delete();
}
}

View File

@ -0,0 +1,136 @@
/*
* UmlGraph class diagram testing framework
*
* Contibuted by Andrea Aime
* (C) Copyright 2005 Diomidis Spinellis
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
*
*/
package gr.spinellis.umlgraph.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
* Collection of utility methods used by the test classes
* @author wolf
*
*/
public class TestUtils {
/**
* Simple text file diffing: will tell you if two text files are line by
* line equals, and will stop at the first difference found.
* @throws IOException
*/
public static boolean textFilesEquals(PrintWriter pw, File refTextFile, File outTextFile)
throws IOException {
BufferedReader refReader = null, outReader = null;
String refLine = null, outFile = null;
try {
pw.println("Performing diff:");
pw.println("out: " + outTextFile.getAbsolutePath());
pw.println("ref: " + refTextFile.getAbsolutePath());
refReader = new BufferedReader(new FileReader(refTextFile));
outReader = new BufferedReader(new FileReader(outTextFile));
// line by line scan, exit when one file ends or lines are not
// equal (and pass over the "Generated by javadoc ..." comments)
for (;;) {
refLine = refReader.readLine();
outFile = outReader.readLine();
if (refLine == null || outFile == null) {
break;
} else if (refLine.startsWith("<!-- Generated by javadoc ")) {
if (!outFile.startsWith("<!-- Generated by javadoc "))
break;
} else if (!refLine.equals(outFile)) {
break;
}
}
} finally {
if (refReader != null)
refReader.close();
if (outReader != null)
outReader.close();
}
// they were equals if both files ended at the same time
boolean equal = refLine == null && outFile == null;
if (equal)
pw.print("File contents are equal");
else
pw.println("Differences found\nref: " + refLine + "\nout: " + outFile);
pw.println();
pw.println();
return equal;
}
public static boolean dotFilesEqual(PrintWriter pw, String dotPath, String refPath)
throws IOException {
pw.println("Performing diff:\nout:" + dotPath + "\nref:" + refPath);
DotDiff differ = new DotDiff(new File(dotPath), new File(refPath));
boolean equal = differ.graphEquals();
if (equal) {
pw.println("File contents are structurally equal");
} else {
pw.println("File contents are structurally not equal");
printList(pw, "# Lines in out but not in ref", differ.getExtraLines1());
printList(pw, "# Lines in ref but not in out", differ.getExtraLines2());
printList(pw, "# Nodes in out but not in ref", differ.getNodes1());
printList(pw, "# Nodes in ref but not in out", differ.getNodes2());
printList(pw, "# Arcs in out but not in ref", differ.getArcs1());
printList(pw, "# Arcs in ref but not in out", differ.getArcs2());
}
pw.println("\n\n");
return equal;
}
public static void printList(PrintWriter pw, String message, List extraOut) {
if (extraOut.size() > 0) {
pw.println(message);
for (Object o : extraOut) {
pw.println(o);
}
}
}
/**
* Deletes the content of the folder, eventually in a recursive way (but
* avoids deleting eventual .cvsignore files)
*/
public static void cleanFolder(File folder, boolean recurse) {
for (File f : folder.listFiles()) {
if (f.isDirectory()) {
if (recurse) {
cleanFolder(f, true);
if (f.list().length == 0)
f.delete();
}
} else if (!f.getName().equals(".cvsignore")) {
f.delete();
}
}
}
}

View File

@ -0,0 +1,152 @@
/*
* UmlGraph class diagram testing framework
*
* Contibuted by Andrea Aime
* (C) Copyright 2005 Diomidis Spinellis
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id$
*
*/
package gr.spinellis.umlgraph.test;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* UmlDoc doclet regression tests
* @author wolf
*
*/
public class UmlDocTest {
static String testSourceFolder = "testdata/umldoc-src";
static String testDestFolder = "testdata/umldoc-out";
static String testRefFolder = "testdata/umldoc-ref";
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
List<String> differences = new ArrayList<String>();
File outFolder = new File(testDestFolder);
if (!outFolder.exists())
outFolder.mkdirs();
TestUtils.cleanFolder(outFolder, true);
// don't use windows specific fonts
System.setProperty("os.name", "generic");
// run tests
runTest(differences);
if (differences.size() > 0) {
pw.println("ERROR, some files are not structurally equal or some files are missing:");
for (String className : differences) {
pw.println(className);
}
} else {
pw.println("GOOD, all files are structurally equal");
}
pw.println();
pw.println();
pw.flush();
}
private static void runTest(List<String> differences) throws IOException {
File outFolder = new File(testDestFolder);
String[] options = new String[] { "-docletpath", "build", "-private", "-d",
outFolder.getAbsolutePath(), "-sourcepath", testSourceFolder, "-compact",
"-subpackages", "gr.spinellis", "-inferrel", "-inferdep", "-qualify",
"-postfixpackage", "-collpackages", "java.util.*" };
runDoclet(options);
compareDocletOutputs(differences, new File(testRefFolder), new File(testDestFolder));
}
/**
* Ensures that reference and output have the same contents in terms of:
* <ul>
* <li> html files </li>
* <li> dot files </li>
* <li> folders </li>
* </ul>
* @throws IOException
*/
private static void compareDocletOutputs(List<String> differences, File refFolder,
File outFolder) throws IOException {
if (!refFolder.exists() || !refFolder.isDirectory())
throw new IllegalArgumentException("Reference does not exists or is not a folder: "
+ refFolder.getAbsolutePath());
if (!outFolder.exists() || !outFolder.isDirectory())
throw new IllegalArgumentException("Output does not exists or is not a folder: "
+ refFolder.getAbsolutePath());
// get elements and sort
String[] refFiles = refFolder.list();
String[] outFiles = refFolder.list();
Arrays.sort(refFiles);
Arrays.sort(outFiles);
// parallel scan (mergesort inspired)
int i = 0, j = 0;
while (i < refFiles.length && j < outFiles.length) {
File ref = new File(refFolder, refFiles[i]);
File out = new File(outFolder, outFiles[j]);
int compare = refFiles[i].compareTo(outFiles[i]);
if (compare == 0) {
String refName = ref.getName().toLowerCase();
if (ref.isDirectory()) {
compareDocletOutputs(differences, ref, out);
} else if (refName.endsWith(".dot")) {
if (!TestUtils.dotFilesEqual(pw, ref.getAbsolutePath(), out.getAbsolutePath()))
differences.add(out.getName() + " is different from the reference");
} else {
if (!TestUtils.textFilesEquals(pw, ref, out))
differences.add(out.getName() + " is different from the reference");
}
i++;
j++;
} else if (compare < 0) {
differences.add("Reference file/folder not found in output: "
+ ref.getAbsolutePath());
i++;
} else {
j++;
}
}
// all ref files remaining are missing ones
while (i < refFiles.length) {
File ref = new File(refFolder, refFiles[i]);
differences.add("Reference file/folder not found in output: " + ref.getAbsolutePath());
i++;
}
}
/**
* Runs the UmlDoc doclet
* @param options
*/
private static void runDoclet(String[] options) {
com.sun.tools.javadoc.Main.execute("UMLDoc test", pw, pw, pw,
"gr.spinellis.umlgraph.doclet.UmlDoc", options);
}
}