Skip to content

Commit

Permalink
added default GUI if there are no parameters (with Jon Dickinson)
Browse files Browse the repository at this point in the history
  • Loading branch information
skizz committed Jun 11, 2003
1 parent 905f2b1 commit 1cc27d6
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 27 deletions.
14 changes: 14 additions & 0 deletions src/java/biz/skizz/testdox/Generator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package biz.skizz.testdox;

import java.io.File;

/**
* Created by IntelliJ IDEA.
* User: stevcc
* Date: 11-Jun-2003
* Time: 20:17:50
* To change this template use Options | File Templates.
*/
public interface Generator {
void generate(File file);
}
100 changes: 100 additions & 0 deletions src/java/biz/skizz/testdox/Gui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package biz.skizz.testdox;

import javax.swing.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

/**
* Created by IntelliJ IDEA.
* User: stevcc
* Date: 11-Jun-2003
* Time: 19:08:11
* To change this template use Options | File Templates.
*/
public class Gui extends JFrame {

JButton browseButton;
JButton goButton;
JFileChooser fileChooser;
JTextField path;
Generator gen;

private DocumentListener pathChangeListener = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
doPathChange();
}

public void removeUpdate(DocumentEvent e) {
doPathChange();
}

public void changedUpdate(DocumentEvent e) {
doPathChange();
}
};

private void doPathChange() {
File f = new File(path.getText());
goButton.setEnabled(f.exists());
if (goButton.isEnabled())
getRootPane().setDefaultButton(goButton);
else
getRootPane().setDefaultButton(browseButton);

}

public Gui(String title, Generator gen) throws HeadlessException {
super(title);
this.gen = gen;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setup();
}

private ActionListener goActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
gen.generate(new File(path.getText()));
}
};

private ActionListener browseActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
doBrowseForFile();
}
};

private void setup() {
path = new JTextField(50);
path.getDocument().addDocumentListener(pathChangeListener);
browseButton = new JButton("Browse");
browseButton.addActionListener(browseActionListener);
goButton = new JButton("Go");
goButton.setEnabled(false);
goButton.addActionListener(goActionListener);

Container pane = getContentPane();
pane.setLayout(new FlowLayout());
pane.add(path);
pane.add(browseButton);
pane.add(goButton);
getRootPane().setDefaultButton(browseButton);
pack();
}

private void doBrowseForFile() {
if (fileChooser == null) {
fileChooser = new JFileChooser();
}
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fileChooser.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
path.setText(selectedFile.getPath());
}
}
}

36 changes: 25 additions & 11 deletions src/java/biz/skizz/testdox/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaSource;

import javax.swing.*;

/**
* Simple usage:
*
*
*/
public class Main {
public class Main implements Generator {

private static String usage =
"Generate simple test documentation from JUnit source files\n" + "Usage: {0} <source-directory>\n" + "\t<source-directory>: A source directory containing JUnit tests" + "\n" + "TestDox will generate documentation for test cases of the form *Test.java, *TestCase.java\n";
"Generate simple test documentation from JUnit source files\n" +
"Usage: {0} <source-directory>\n" +
"\t<source-directory>: A source directory containing JUnit tests" +
"\n" +
"TestDox will generate documentation for test cases of the form *Test.java, *TestCase.java\n";
String directory;
private MultiplexingGenerator gen = new MultiplexingGenerator();
private NamePrettifier prettifier = new NamePrettifier();
public static JFrame gui;

public Main() {
gen.addGenerator( new ConsoleGenerator() );
Expand Down Expand Up @@ -75,17 +82,24 @@ private void doMethods(JavaMethod[] methods) {
}

public static void main(String[] args) {
if ( args.length == 0 ) {
String message = MessageFormat.format(usage, new String[] {Main.class.getName()} );
System.err.println(message);
return;
}


Generator main = new Main();
if ( args.length == 0 ) {
gui = new Gui("Test Docs", main);
gui.show();
return;
// String message = MessageFormat.format(usage, new String[] {Main.class.getName()} );
// System.err.println(message);
// return;
}
File file = new File(args[0]);
Main main = new Main();
main.generate(file);
}

public void generate(File file) {
try {
main.setTestDirectory(file.getCanonicalPath());
main.parse();
setTestDirectory(file.getCanonicalPath());
parse();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
Expand Down
134 changes: 134 additions & 0 deletions src/test/biz/skizz/testdox/GuiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package biz.skizz.testdox;

import junit.framework.TestCase;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
* Created by IntelliJ IDEA.
* User: stevcc
* Date: 11-Jun-2003
* Time: 19:09:15
* To change this template use Options | File Templates.
*/
public class GuiTest extends TestCase {
private Gui gui;
private static JFileChooser selectSrcChooser = new JFileChooser() {
public File getSelectedFile() {
return selectedFile;
}

public int showOpenDialog(Component parent) throws HeadlessException {
return JFileChooser.APPROVE_OPTION;
}
};
private static File selectedFile = new File("src");
private TestGenerator gen;

protected void setUp() throws Exception {
gen = new TestGenerator();
}

protected void tearDown() throws Exception {
super.tearDown();
}

public void testShowsFileChooserIfBrowseIsClicked() {
final boolean[] wasShown = new boolean[]{false};

JFileChooser chooser = new JFileChooser() {
public int showOpenDialog(Component parent) throws HeadlessException {
wasShown[0] = true;
return JFileChooser.CANCEL_OPTION;
}
};

gui = new Gui("foo", gen);
gui.fileChooser = chooser;
gui.browseButton.doClick();
assertNotNull(gui.fileChooser);
assertTrue(gui.fileChooser.isDirectorySelectionEnabled());
assertTrue(wasShown[0]);
}

public void testSomeGuiDefaults() {
gui = new Gui("foo", gen);
assertEquals(JFrame.EXIT_ON_CLOSE, gui.getDefaultCloseOperation());
}

public void testSelectedFileIsShownInTextField() {

gui = new Gui("foo", gen);
gui.fileChooser = selectSrcChooser;
gui.browseButton.doClick();

assertEquals("src", gui.path.getText());
}

public void testSelectedFileIsNotShownIfUserClickedCancel() {
JFileChooser chooser = new JFileChooser() {
public File getSelectedFile() {
return null;
}

public int showOpenDialog(Component parent) throws HeadlessException {
return JFileChooser.CANCEL_OPTION;
}
};

gui = new Gui("foo", gen);
gui.fileChooser = chooser;
gui.browseButton.doClick();

assertEquals("", gui.path.getText());
}

public void testGoButtonEnableUponUserFileSelection() {
gui = new Gui("foo", gen);
assertFalse(gui.goButton.isEnabled());
gui.fileChooser = selectSrcChooser;
gui.browseButton.doClick();
assertTrue(gui.goButton.isEnabled());
}

public static class TestGenerator implements Generator {
private File file;

public void generate(File file) {
this.file = file;
}

public File getFile() {
return file;
}
};

public void testClickGoButtonRunsIt() {

gui = new Gui("foo", gen);
gui.path.setText("src");
gui.goButton.setEnabled(true);
gui.goButton.doClick();

assertEquals(selectedFile, gen.getFile());
}


public void testGoButtonDisabledIfFileDoesNotExist() {
gui = new Gui("foo", gen);
gui.path.setText("non-existent-file");
assertFalse(gui.goButton.isEnabled());
}

public void testEnteringPathFreehandEnablesGoButtonAndMakesItDefault() throws IOException, InterruptedException {
gui = new Gui("foo", gen);
gui.path.setText(selectedFile.getCanonicalPath());
assertTrue(gui.goButton.isEnabled());
assertTrue(gui.goButton.isDefaultButton());

}

}
41 changes: 25 additions & 16 deletions src/test/biz/skizz/testdox/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import junit.framework.TestCase;

import javax.swing.*;

public class MainTest extends TestCase {
private MockDocumentGenerator gen;
private Main main;
Expand Down Expand Up @@ -62,22 +64,29 @@ public void testIgnoreSetUpMethod() {
assertFalse(descriptions.contains("p"));
}

public void testMainShowsUsageIfNoParameters() {
PrintStream oldErr = System.err;
String result = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setErr(new PrintStream(out));
Main.main(new String[]{});
result = out.toString();
}
finally {
System.setErr(oldErr);
}
assertNotNull(result);
assertTrue(result.indexOf(Main.class.getName()) >= 0 );


// public void testMainShowsUsageIfNoParameters() {
// PrintStream oldErr = System.err;
// String result = null;
// try {
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// System.setErr(new PrintStream(out));
// Main.main(new String[]{});
// result = out.toString();
// }
// finally {
// System.setErr(oldErr);
// }
// assertNotNull(result);
// assertTrue(result.indexOf(Main.class.getName()) >= 0 );
// }

public void testIfNoArgumentsShowGui() {
Main.main(new String[] {});
Gui frame = (Gui) Main.gui;
assertNotNull( frame );
assertTrue(frame.isVisible());
assertNotNull(frame.gen);
}


}

0 comments on commit 1cc27d6

Please sign in to comment.