|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| EchoTagHandler.java | 92.3% | 98.6% | 100% | 97.2% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Created on Oct 24, 2003
|
|
| 3 |
*
|
|
| 4 |
* To change the template for this generated file go to
|
|
| 5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
|
| 6 |
*/
|
|
| 7 |
package junit.extensions.xml.elements;
|
|
| 8 |
|
|
| 9 |
import junit.extensions.jfcunit.xml.JFCXMLTestCase;
|
|
| 10 |
|
|
| 11 |
import junit.extensions.xml.IXMLTestCase;
|
|
| 12 |
import junit.extensions.xml.XMLException;
|
|
| 13 |
|
|
| 14 |
import junit.framework.Assert;
|
|
| 15 |
|
|
| 16 |
import org.w3c.dom.Element;
|
|
| 17 |
|
|
| 18 |
import java.awt.event.ActionEvent;
|
|
| 19 |
import java.awt.event.ActionListener;
|
|
| 20 |
|
|
| 21 |
import javax.swing.Box;
|
|
| 22 |
import javax.swing.BoxLayout;
|
|
| 23 |
import javax.swing.JButton;
|
|
| 24 |
import javax.swing.JDialog;
|
|
| 25 |
import javax.swing.JLabel;
|
|
| 26 |
import javax.swing.JOptionPane;
|
|
| 27 |
import javax.swing.JPanel;
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
/**
|
|
| 31 |
* Provide a mechanism for sending debug messages to the
|
|
| 32 |
* test case developer.
|
|
| 33 |
*
|
|
| 34 |
* <H3>Tag Name:</H3>
|
|
| 35 |
* echo
|
|
| 36 |
* <H3>Attributes:</H3>
|
|
| 37 |
* <pre>
|
|
| 38 |
* message - the message to output [required]
|
|
| 39 |
* mode - what is the output mechanism [optional]
|
|
| 40 |
* 'stdout' is default, sends output to stdout
|
|
| 41 |
* 'dialog' sends the output to a JOptionPane
|
|
| 42 |
* 'confirm' sends the output to a JOptionPane
|
|
| 43 |
* confirmation dialog.
|
|
| 44 |
* 'stderr' sends the output to stderr
|
|
| 45 |
* block - Block is a valid attribute when the mode is
|
|
| 46 |
* dialog or confirm. It controlls the modality
|
|
| 47 |
* of the dialog. Default value is true.
|
|
| 48 |
* </pre>
|
|
| 49 |
* <H3>Examples:</H3>
|
|
| 50 |
* <pre>
|
|
| 51 |
* <echo message="hello world"/>
|
|
| 52 |
* <echo mode='dialog' message="hello world"/>
|
|
| 53 |
* <echo mode='dialog' block="false" message="hello world"/>
|
|
| 54 |
* <echo mode='stderr' message="hello world"/>
|
|
| 55 |
* </pre>
|
|
| 56 |
* @author JFCUnit contributor
|
|
| 57 |
*/
|
|
| 58 |
public class EchoTagHandler extends AbstractTagHandler { |
|
| 59 |
/**
|
|
| 60 |
* Result of confirm dialog when YES button is pressed.
|
|
| 61 |
*/
|
|
| 62 |
private static final int YES_OPTION = JOptionPane.YES_OPTION; |
|
| 63 |
|
|
| 64 |
/**
|
|
| 65 |
* Result of confirm dialog when NO or CANCEL button is pressed.
|
|
| 66 |
*/
|
|
| 67 |
private static final int NO_OPTION = JOptionPane.NO_OPTION; |
|
| 68 |
|
|
| 69 |
/**
|
|
| 70 |
* Window close result.
|
|
| 71 |
*/
|
|
| 72 |
private boolean m_result = false; |
|
| 73 |
|
|
| 74 |
/**
|
|
| 75 |
* Constructor.
|
|
| 76 |
* @param element Element to be processed by the tag handler.
|
|
| 77 |
* @param testcase parent test case.
|
|
| 78 |
*/
|
|
| 79 | 14 |
public EchoTagHandler(final Element element, final IXMLTestCase testcase) {
|
| 80 | 14 |
super(element, testcase);
|
| 81 |
} |
|
| 82 |
|
|
| 83 |
/**
|
|
| 84 |
* Handle the XML processing of the tag 'echo'.
|
|
| 85 |
* @throws XMLException upon failure of processing.
|
|
| 86 |
*/
|
|
| 87 | 13 |
public void processElement() throws XMLException { |
| 88 | 13 |
validateElement(); |
| 89 |
|
|
| 90 | 13 |
final String msg = getString(MESSAGE); |
| 91 |
|
|
| 92 | 13 |
String mode = getString(MODE); |
| 93 |
|
|
| 94 | 13 |
if (mode == null) { |
| 95 | 5 |
mode = STDOUT; |
| 96 |
} |
|
| 97 |
|
|
| 98 | 13 |
boolean block = getBoolean("block", true); |
| 99 |
|
|
| 100 | 13 |
if (DIALOG.equals(mode)) {
|
| 101 | 2 |
m_result = false;
|
| 102 |
|
|
| 103 | 2 |
if (block) {
|
| 104 | 1 |
JOptionPane.showMessageDialog(null, msg);
|
| 105 |
} else {
|
|
| 106 | 1 |
showDialog(msg); |
| 107 |
} |
|
| 108 |
|
|
| 109 | 2 |
System.err.println("[echo] " + msg);
|
| 110 | 11 |
} else if (CONFIRM.equalsIgnoreCase(mode)) { |
| 111 | 4 |
System.err.println("[echo] " + msg);
|
| 112 |
|
|
| 113 | 4 |
if (block) {
|
| 114 | 2 |
m_result = JOptionPane.showConfirmDialog(null, msg, "Confirm", |
| 115 |
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; |
|
| 116 |
} else {
|
|
| 117 | 2 |
m_result = (showConfirm(msg) == JOptionPane.YES_OPTION); |
| 118 |
} |
|
| 119 |
|
|
| 120 | 4 |
if (!m_result) {
|
| 121 | 2 |
Assert.fail(msg); |
| 122 |
} |
|
| 123 | 7 |
} else if (STDERR.equalsIgnoreCase(mode)) { |
| 124 | 1 |
System.err.println("[echo] " + msg);
|
| 125 | 6 |
} else if (STDOUT.equalsIgnoreCase(mode)) { |
| 126 | 5 |
System.out.println("[echo] " + msg);
|
| 127 |
} else {
|
|
| 128 | 1 |
throw new XMLException("Invalid mode", null, |
| 129 |
getElement(), |
|
| 130 |
getXMLTestCase().getPropertyCache()); |
|
| 131 |
} |
|
| 132 |
} |
|
| 133 |
|
|
| 134 |
/**
|
|
| 135 |
* Make sure the appropriate tag and attributes are used.
|
|
| 136 |
* @throws XMLException when validation fails.
|
|
| 137 |
*/
|
|
| 138 | 13 |
public void validateElement() throws XMLException { |
| 139 | 13 |
super.validateElement();
|
| 140 |
|
|
| 141 |
// message is a required attribute
|
|
| 142 | 13 |
checkRequiredAttribute(MESSAGE); |
| 143 |
} |
|
| 144 |
|
|
| 145 |
/**
|
|
| 146 |
* Show a confirm dialog.
|
|
| 147 |
* @param msg Message to be displayed.
|
|
| 148 |
* @return YES_OPTION or NO_OPTION
|
|
| 149 |
*/
|
|
| 150 | 2 |
int showConfirm(final String msg) {
|
| 151 | 2 |
return showDialog(msg, true); |
| 152 |
} |
|
| 153 |
|
|
| 154 |
/**
|
|
| 155 |
* Show a Dialog.
|
|
| 156 |
* @param msg Message to be displayed.
|
|
| 157 |
*/
|
|
| 158 | 1 |
void showDialog(final String msg) {
|
| 159 | 1 |
showDialog(msg, false);
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* Show the dialog with the message given.
|
|
| 164 |
* @param msg message to be displayed.
|
|
| 165 |
* @param confirm if true then use YES/NO buttons.
|
|
| 166 |
* @return YES_OPTION or NO_OPTION.
|
|
| 167 |
*/
|
|
| 168 | 3 |
int showDialog(final String msg, final boolean confirm) { |
| 169 | 3 |
EchoDialog ed = new EchoDialog(msg, confirm);
|
| 170 | 3 |
ed.setVisible(true);
|
| 171 |
|
|
| 172 | 3 |
IXMLTestCase tc = getXMLTestCase(); |
| 173 |
|
|
| 174 | 3 |
while (!ed.getDialogClosed()) {
|
| 175 | 4 |
if (tc instanceof JFCXMLTestCase) { |
| 176 | 0 |
((JFCXMLTestCase) tc).sleep(1000); |
| 177 |
} else {
|
|
| 178 | 4 |
try {
|
| 179 | 4 |
Thread.currentThread().sleep(1000); |
| 180 |
} catch (InterruptedException ie) {
|
|
| 181 |
; |
|
| 182 |
} |
|
| 183 |
} |
|
| 184 |
} |
|
| 185 |
|
|
| 186 | 3 |
return ed.getResult();
|
| 187 |
} |
|
| 188 |
|
|
| 189 |
/**
|
|
| 190 |
* <p>Title: EchoDialog non-modal dialog for echos</p>
|
|
| 191 |
* <p>Description: Non modal dialog for echo/confirm.
|
|
| 192 |
* This dialog allows Application to be exercized.
|
|
| 193 |
* <p>Copyright: Copyright (c) 2003</p>
|
|
| 194 |
* <p>Company: JFCUnit Project</p>
|
|
| 195 |
* @author Kevin Wilson
|
|
| 196 |
* @version 1.0
|
|
| 197 |
*/
|
|
| 198 |
private class EchoDialog extends JDialog implements ActionListener { |
|
| 199 |
/**
|
|
| 200 |
* NO Button.
|
|
| 201 |
*/
|
|
| 202 |
private JButton m_no;
|
|
| 203 |
|
|
| 204 |
/**
|
|
| 205 |
* Yes Button.
|
|
| 206 |
*/
|
|
| 207 |
private JButton m_ok;
|
|
| 208 |
|
|
| 209 |
/**
|
|
| 210 |
* Private result of the dialog.
|
|
| 211 |
*/
|
|
| 212 |
private volatile boolean m_dislogClosed = false; |
|
| 213 |
|
|
| 214 |
/**
|
|
| 215 |
* Result of the dialog.
|
|
| 216 |
*/
|
|
| 217 |
private int m_result = JOptionPane.DEFAULT_OPTION; |
|
| 218 |
|
|
| 219 |
/**
|
|
| 220 |
* Constructor.
|
|
| 221 |
* @param message Message to be displayed.
|
|
| 222 |
* @param confirm true if YES/NO buttons false for OK button.
|
|
| 223 |
*/
|
|
| 224 | 3 |
public EchoDialog(final String message, final boolean confirm) { |
| 225 | 3 |
JLabel msg = new JLabel(message);
|
| 226 |
|
|
| 227 | 3 |
m_ok = new JButton("OK"); |
| 228 |
|
|
| 229 | 3 |
if (confirm) {
|
| 230 | 2 |
m_ok.setText("Yes");
|
| 231 |
} |
|
| 232 |
|
|
| 233 | 3 |
m_ok.addActionListener(this);
|
| 234 | 3 |
m_no = new JButton("No"); |
| 235 | 3 |
m_no.addActionListener(this);
|
| 236 | 3 |
m_no.setVisible(confirm); |
| 237 |
|
|
| 238 | 3 |
JPanel buttonPanel = new JPanel();
|
| 239 | 3 |
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
|
| 240 | 3 |
buttonPanel.add(Box.createHorizontalGlue()); |
| 241 | 3 |
buttonPanel.add(m_ok); |
| 242 | 3 |
buttonPanel.add(m_no); |
| 243 | 3 |
buttonPanel.add(Box.createHorizontalGlue()); |
| 244 |
|
|
| 245 | 3 |
JPanel panel = new JPanel();
|
| 246 | 3 |
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
| 247 | 3 |
panel.add(msg); |
| 248 | 3 |
panel.add(buttonPanel); |
| 249 | 3 |
setContentPane(panel); |
| 250 | 3 |
this.setModal(false); |
| 251 | 3 |
pack(); |
| 252 |
} |
|
| 253 |
|
|
| 254 |
/**
|
|
| 255 |
* Get the dialog closed state.
|
|
| 256 |
* @return true if the dialog was closed.
|
|
| 257 |
*/
|
|
| 258 | 7 |
public boolean getDialogClosed() { |
| 259 | 7 |
return m_dislogClosed;
|
| 260 |
} |
|
| 261 |
|
|
| 262 |
/**
|
|
| 263 |
* Get the result of the dialog.
|
|
| 264 |
* @return the result of the dialog YES_OPTION, NO_OPTION.
|
|
| 265 |
*/
|
|
| 266 | 3 |
public int getResult() { |
| 267 | 3 |
return m_result;
|
| 268 |
} |
|
| 269 |
|
|
| 270 |
/**
|
|
| 271 |
* Action handler for this dialog.
|
|
| 272 |
* @param ae ActionEvent to be consumed.
|
|
| 273 |
*/
|
|
| 274 | 3 |
public void actionPerformed(final ActionEvent ae) { |
| 275 | 3 |
Object source = ae.getSource(); |
| 276 |
|
|
| 277 | 3 |
if (source == m_ok) {
|
| 278 | 2 |
m_result = YES_OPTION; |
| 279 | 1 |
} else if (source == m_no) { |
| 280 | 1 |
m_result = NO_OPTION; |
| 281 |
} |
|
| 282 |
|
|
| 283 | 3 |
this.setVisible(false); |
| 284 | 3 |
this.dispose();
|
| 285 | 3 |
m_ok = null;
|
| 286 | 3 |
m_no = null;
|
| 287 | 3 |
m_dislogClosed = true;
|
| 288 |
} |
|
| 289 |
} |
|
| 290 |
} |
|
| 291 |
|
|
||||||||||