You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavadocCompareClassMode.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import java.io.*;
  2. import java.util.*;
  3. import common.OutputComparator;
  4. //import org.aspectj.testing.Tester;
  5. public class JavadocCompareClassMode {
  6. /** wait a minimum (of 1 second) for processes to complete */
  7. static final int MIN_SECS = 1;
  8. /** wait a maximum (of 4 hours) for processes to complete */
  9. static final int MAX_SECS = 4*60*60;
  10. static final String INPUT_FILES = "input/applesJava/*.java";
  11. static final String FILE_1 = "Apple.html";
  12. static final String FILE_2 = "AppleCrate.html";
  13. static final String OUTPUT_DIR = "output";
  14. static final String AJDOC_DIR = OUTPUT_DIR + File.separator + "ajdoc";
  15. static final String JAVADOC_DIR = OUTPUT_DIR + File.separator + "javadoc";
  16. static final String AJDOC_CALL = "java org.aspectj.tools.ajdoc.Main -d " + AJDOC_DIR + " " + INPUT_FILES;
  17. static final String JAVADOC_CALL = "javadoc -package -d " + JAVADOC_DIR + " " + INPUT_FILES;
  18. public static void main(String[] args) { test(System.out); }
  19. public static boolean ensureDir(String dirPath, StringBuffer errSink) {
  20. boolean result = false;
  21. if (dirPath != null) {
  22. try {
  23. File dir = new File(dirPath);
  24. if (!dir.exists()) {
  25. dir.mkdir();
  26. }
  27. result = (dir.exists() && dir.isDirectory());
  28. } catch (SecurityException e) {
  29. if (null != errSink) {
  30. errSink.append(e.getClass().getName());
  31. errSink.append(" ensuring directory ");
  32. errSink.append(dirPath);
  33. errSink.append(": ");
  34. errSink.append(e.getMessage());
  35. }
  36. }
  37. }
  38. return result;
  39. } // ensureDir
  40. /**
  41. * This implements a basic three-step test:
  42. * <UL>
  43. * <LI>step 1: exec ajdoc as a command on INPUT_FILES
  44. * <LI>step 2: exec javadoc in the same way
  45. * <LI>step 3: find differences in files FILE_1 and FILE_2
  46. * </UL>
  47. */
  48. public static void test(PrintStream sink) {
  49. OutputComparator outputComparator = new OutputComparator();
  50. sink.println("> Setup directories");
  51. StringBuffer errSink = new StringBuffer();
  52. if (! ensureDir(OUTPUT_DIR, errSink)) {
  53. sink.println("Error: " + errSink.toString());
  54. return;
  55. }
  56. if (! ensureDir(AJDOC_DIR, errSink)) {
  57. sink.println("Error: " + errSink.toString());
  58. return;
  59. }
  60. if (! ensureDir(JAVADOC_DIR, errSink)) {
  61. sink.println("Error: " + errSink.toString());
  62. return;
  63. }
  64. String toolName = "> ajdoc";
  65. sink.println(toolName + " running ");
  66. int result = runCommand(AJDOC_CALL);
  67. sink.println(toolName + " result " + result);
  68. toolName = "> javadoc";
  69. sink.println(toolName + " running ");
  70. result = runCommand(JAVADOC_CALL);
  71. sink.println(toolName + " result " + result);
  72. toolName = "> compare";
  73. sink.println(toolName + " running ");
  74. String[] files = new String[] { FILE_1, FILE_2 };
  75. Vector diffs = null;
  76. result = -2;
  77. for (int i = 0; i < files.length; i++) {
  78. String file = files[i];
  79. String ajdocFile = AJDOC_DIR + "/" + file;
  80. String javadocFile = JAVADOC_DIR + "/" + file;
  81. try {
  82. diffs = outputComparator.compareFilesByLine(ajdocFile, javadocFile);
  83. if (diffs == null) {
  84. sink.println("No differences in file " + file);
  85. result = 0;
  86. } else {
  87. result = diffs.size();
  88. sink.println("Start of Differences in file " + FILE_1);
  89. sink.println(diffs.toString());
  90. sink.println("end of Differences in file " + FILE_1);
  91. }
  92. } catch (IOException e) {
  93. sink.println("Exception comparing: " + file);
  94. e.printStackTrace(sink);
  95. result = -1;
  96. }
  97. }
  98. sink.println(toolName + " result " + result);
  99. }
  100. /** write in to out */
  101. static void writeStream(InputStream in, PrintStream out) {
  102. if ((null == in) || (null == out)) {
  103. return;
  104. }
  105. try {
  106. BufferedReader lines = new BufferedReader(new InputStreamReader(in));
  107. String line;
  108. while (null != (line = lines.readLine())) {
  109. out.println(line);
  110. }
  111. } catch (IOException e) {
  112. e.printStackTrace(out);
  113. }
  114. }
  115. /**
  116. * Complete a running process, handling timeout and streams appropriately.
  117. * @param process the Process to run
  118. * @param secsToWait an int for the number of seconds to wait before timing out
  119. * - use Integer.MAXVALUE to mean no timeout (otherwise,
  120. * IllegalArgumentException unless (MIN_SECS <= secsToWait <= MAX_SECS))
  121. * @param outSink the PrintStream sink for the process output stream
  122. * (use null to ignore process output stream).
  123. * @param errSink the PrintStream sink for the process error stream
  124. * (use null to ignore process error stream).
  125. * @returns Integer.MIN_VALUE if interrupted while waiting for process to complete,
  126. * Integer.MAX_VALUE if timed out,
  127. * or the int returned by <code>Process.waitFor()</code> otherwise.
  128. * @throws IllegalArgumentException if any parms are null or invalid
  129. */
  130. public static int completeProcess(final Process process, int secsToWait,
  131. PrintStream outSink,
  132. PrintStream errSink) {
  133. if (null == process) throw new IllegalArgumentException("null process");
  134. if ((Integer.MAX_VALUE != secsToWait)
  135. && ((MIN_SECS > secsToWait) || ((MAX_SECS < secsToWait)))) {
  136. throw new IllegalArgumentException("invalid time: " + secsToWait);
  137. }
  138. // setup timeout
  139. TimerTask task = null;
  140. if (Integer.MAX_VALUE != secsToWait) {
  141. Timer t = new Timer(true);
  142. task = new TimerTask() {
  143. public void run() {
  144. process.destroy();
  145. }
  146. };
  147. t.schedule(task, secsToWait*1000l);
  148. }
  149. // try to wait for the process
  150. int status = Integer.MAX_VALUE;
  151. try {
  152. status = process.waitFor();
  153. } catch (InterruptedException ie) {
  154. status = Integer.MIN_VALUE; // ignore
  155. }
  156. finally {
  157. if (null != task) task.cancel();
  158. if (errSink != null) writeStream(process.getErrorStream(), errSink);
  159. // misnamed API: the "input" stream is our input from the process output
  160. if (outSink != null) writeStream(process.getInputStream(), outSink);
  161. }
  162. return status;
  163. } // completeProcess
  164. /**
  165. * Run command, delegating process handling to runProcess.
  166. * @param command the String passed to Runtime.exec
  167. * @return the int returned from process.waitFor();
  168. */
  169. public static int runCommand(String command) {
  170. int result = -1;
  171. try {
  172. System.out.println("Running " + command);
  173. Process process = Runtime.getRuntime().exec(command);
  174. System.out.println("waiting for Result.." );
  175. final int seconds = 60;
  176. result = completeProcess(process, seconds, System.out, System.err);
  177. System.out.println("Result: " + result + " for " + command);
  178. } catch (Exception e) {
  179. throw new RuntimeException("could not execute: " + command +
  180. ", " + e.getMessage() );
  181. }
  182. return result;
  183. }
  184. }