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: BasicTest.java,v 1.18 2007/11/27 09:04:22 dds Exp $
018: *
019: */
020:
021: package org.umlgraph.test;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.List;
029:
030: /**
031: * UmlGraph regression tests
032: * @author wolf
033: *
034: */
035: public class BasicTest {
036:
037: static String testSourceFolder = "testdata/java";
038:
039: static String testDestFolder = "testdata/dot-out";
040:
041: static String testRefFolder = "testdata/dot-ref";
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: performBasicTests(differences);
059: performViewTests(differences, outFolder);
060: if (differences.size() > 0) {
061: pw
062: .println("ERROR, some files are not structurally equal or some files are missing:");
063: for (String className : differences) {
064: pw.println(className);
065: }
066: } else {
067: pw.println("GOOD, all files are structurally equal");
068: }
069: pw.println();
070: pw.println();
071: pw.flush();
072: }
073:
074: private static void performViewTests(List<String> differences,
075: File outFolder) throws IOException {
076: String[] options = new String[] { "-docletpath", "build",
077: "-private", "-d", outFolder.getAbsolutePath(),
078: "-sourcepath", "testdata/java", "-compact",
079: "-subpackages", "gr.spinellis", "-views" };
080: runDoclet(options);
081:
082: List<String> viewFiles = new ArrayList<String>();
083: viewFiles.addAll(getViewList(new File(testSourceFolder,
084: "gr/spinellis/basic/views")));
085: viewFiles.addAll(getViewList(new File(testSourceFolder,
086: "gr/spinellis/context/views")));
087: viewFiles.addAll(getViewList(new File(testSourceFolder,
088: "gr/spinellis/iface/views")));
089: viewFiles.addAll(getViewList(new File(testSourceFolder,
090: "gr/spinellis/subclass/views")));
091: for (String fileName : viewFiles) {
092: String viewName = fileName.substring(0,
093: fileName.length() - 5);
094: File dotFile = new File(testDestFolder, viewName + ".dot");
095: File refFile = new File(testRefFolder, viewName + ".dot");
096: if (viewName.contains("Abstract")) {
097: // make sure abstract views are not generated
098: if (dotFile.exists()) {
099: pw.println("Error, abstract view " + viewName
100: + " has been generated");
101: differences.add(dotFile.getName()
102: + " should not be there");
103: }
104: } else {
105: compare(differences, dotFile, refFile);
106: }
107: }
108: }
109:
110: private static List<String> getViewList(File viewFolder) {
111: if (!viewFolder.exists())
112: throw new RuntimeException("The folder "
113: + viewFolder.getAbsolutePath()
114: + " does not exists.");
115: else if (!viewFolder.isDirectory())
116: throw new RuntimeException(viewFolder.getAbsolutePath()
117: + " is not a folder!.");
118: else if (!viewFolder.canRead())
119: throw new RuntimeException("The folder "
120: + viewFolder.getAbsolutePath() + " cannot be read.");
121:
122: return Arrays.asList(viewFolder.list(new SimpleFileFilter(
123: ".java")));
124: }
125:
126: private static boolean performBasicTests(List<String> differences)
127: throws IOException {
128: String[] javaFiles = new File(testSourceFolder)
129: .list(new SimpleFileFilter(".java"));
130: boolean equal = true;
131: for (int i = 0; i < javaFiles.length; i++) {
132: String javaFileName = javaFiles[i].substring(0,
133: javaFiles[i].length() - 5);
134: String outFileName = javaFileName + ".dot";
135: File dotFile = new File(testDestFolder, outFileName);
136: dotFile.delete();
137: File refFile = new File(testRefFolder, outFileName);
138: String javaPath = new File(testSourceFolder, javaFiles[i])
139: .getAbsolutePath();
140: String[] options = new String[] { "-docletpath", "build",
141: "-hide", "Hidden", "-compact", "-private", "-d",
142: testDestFolder, "-output", outFileName, javaPath };
143:
144: runDoclet(options);
145: compare(differences, dotFile, refFile);
146: }
147: return equal;
148: }
149:
150: private static void runDoclet(String[] options) {
151: com.sun.tools.javadoc.Main.execute("UMLGraph test", pw, pw, pw,
152: "org.umlgraph.doclet.UmlGraph", options);
153: }
154:
155: private static void compare(List<String> differences, File dotFile,
156: File refFile) throws IOException {
157: if (!dotFile.exists()) {
158: pw.println("Error, output file " + dotFile
159: + " has not been generated");
160: differences.add(dotFile.getName()
161: + " has not been generated");
162: } else if (!refFile.exists()) {
163: pw.println("Error, reference file " + refFile
164: + " is not available");
165: differences.add(refFile.getName()
166: + " reference is not available");
167: } else if (!TestUtils.dotFilesEqual(pw, dotFile
168: .getAbsolutePath(), refFile.getAbsolutePath())) {
169: differences.add(dotFile.getName()
170: + " is different from the reference");
171: }
172: }
173:
174: }
|