From d827bdf1f5f9f25151af211fd460e24881221774 Mon Sep 17 00:00:00 2001 From: Andrea Aime Date: Sun, 4 Jun 2006 13:55:00 +0000 Subject: [PATCH] Added testing for umldoc, and moved shared methods with the umlgraph tests in an utility class --- src/gr/spinellis/umlgraph/test/BasicTest.java | 103 +++++------- src/gr/spinellis/umlgraph/test/TestUtils.java | 136 ++++++++++++++++ .../spinellis/umlgraph/test/UmlDocTest.java | 152 ++++++++++++++++++ src/org/umlgraph/test/BasicTest.java | 103 +++++------- src/org/umlgraph/test/TestUtils.java | 136 ++++++++++++++++ src/org/umlgraph/test/UmlDocTest.java | 152 ++++++++++++++++++ 6 files changed, 658 insertions(+), 124 deletions(-) create mode 100644 src/gr/spinellis/umlgraph/test/TestUtils.java create mode 100644 src/gr/spinellis/umlgraph/test/UmlDocTest.java create mode 100644 src/org/umlgraph/test/TestUtils.java create mode 100644 src/org/umlgraph/test/UmlDocTest.java diff --git a/src/gr/spinellis/umlgraph/test/BasicTest.java b/src/gr/spinellis/umlgraph/test/BasicTest.java index b596c36..7206e01 100644 --- a/src/gr/spinellis/umlgraph/test/BasicTest.java +++ b/src/gr/spinellis/umlgraph/test/BasicTest.java @@ -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 different = new ArrayList(); + List differences = new ArrayList(); 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 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 viewFiles = new ArrayList(); + List viewFiles = new ArrayList(); 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 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 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 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 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(); - } } diff --git a/src/gr/spinellis/umlgraph/test/TestUtils.java b/src/gr/spinellis/umlgraph/test/TestUtils.java new file mode 100644 index 0000000..fc5283d --- /dev/null +++ b/src/gr/spinellis/umlgraph/test/TestUtils.java @@ -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("