|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StopWatchTagHandler.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 |
package junit.extensions.xml.elements;
|
|
| 2 |
|
|
| 3 |
import junit.extensions.xml.IXMLTestCase;
|
|
| 4 |
import junit.extensions.xml.XMLException;
|
|
| 5 |
|
|
| 6 |
import org.w3c.dom.Element;
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
/**
|
|
| 10 |
* This is tag handler can measure the milliseconds within a
|
|
| 11 |
* test case.
|
|
| 12 |
*
|
|
| 13 |
* <h3>Description</h3>
|
|
| 14 |
* <p>
|
|
| 15 |
* Mark the start of the process to be timed with a mark action.
|
|
| 16 |
* <pre>
|
|
| 17 |
* <stopwatch id="myid" action="mark"/>
|
|
| 18 |
* </pre>
|
|
| 19 |
*
|
|
| 20 |
* Then afterward assert that the duration in milliseconds.
|
|
| 21 |
* <pre>
|
|
| 22 |
* <stopwatch refid="myid" action="lessthan"
|
|
| 23 |
* value="8000"/>
|
|
| 24 |
* </pre>
|
|
| 25 |
* Or to simply write the duration to stdout.
|
|
| 26 |
* <pre>
|
|
| 27 |
* <stopwatch refid="myid" action="log"/>
|
|
| 28 |
* </pre>
|
|
| 29 |
* @see junit.extensions.jfcunit.eventdata.JComboBoxMouseEventData
|
|
| 30 |
* @author Kevin Wilson
|
|
| 31 |
*/
|
|
| 32 |
public class StopWatchTagHandler extends AbstractTagHandler { |
|
| 33 |
/**
|
|
| 34 |
* constructor.
|
|
| 35 |
* @param element Element to be processed.
|
|
| 36 |
* @param testCase containing test case.
|
|
| 37 |
*/
|
|
| 38 | 8 |
public StopWatchTagHandler(final Element element,
|
| 39 |
final IXMLTestCase testCase) {
|
|
| 40 | 8 |
super(element, testCase);
|
| 41 |
} |
|
| 42 |
|
|
| 43 |
/**
|
|
| 44 |
* Get the action attribute from the element.
|
|
| 45 |
* @return String The value of the action attribute.
|
|
| 46 |
*/
|
|
| 47 | 13 |
public String getAction() {
|
| 48 | 13 |
return getString(ACTION);
|
| 49 |
} |
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* Get the value of the id attribute.
|
|
| 53 |
* @return String value of the id attribute.
|
|
| 54 |
*/
|
|
| 55 | 3 |
public String getId() {
|
| 56 | 3 |
return getString(ID);
|
| 57 |
} |
|
| 58 |
|
|
| 59 |
/**
|
|
| 60 |
* Get the value of the refid attribute.
|
|
| 61 |
* @return String value of the refid attribute.
|
|
| 62 |
*/
|
|
| 63 | 4 |
public String getRefid() {
|
| 64 | 4 |
return getString(REFID);
|
| 65 |
} |
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* Process the element.
|
|
| 69 |
* @throws XMLException may be thrown.
|
|
| 70 |
*/
|
|
| 71 | 7 |
public void processElement() throws XMLException { |
| 72 | 7 |
long newMark = System.currentTimeMillis();
|
| 73 | 7 |
validateElement(); |
| 74 |
|
|
| 75 | 6 |
String action = getAction(); |
| 76 |
|
|
| 77 | 6 |
if (MARK.equals(action)) {
|
| 78 | 3 |
String id = getId(); |
| 79 | 3 |
Long mark = new Long(newMark);
|
| 80 | 3 |
getXMLTestCase().addProperty(id, mark); |
| 81 |
} else {
|
|
| 82 |
//Get the stored objects
|
|
| 83 | 3 |
String refid = getRefid(); |
| 84 | 3 |
long mark = ((Long) getXMLTestCase().getProperty(refid))
|
| 85 |
.longValue(); |
|
| 86 |
|
|
| 87 | 3 |
long duration = newMark - mark;
|
| 88 |
|
|
| 89 | 3 |
if (action.equals(LOG)) {
|
| 90 | 1 |
String name = this.getTestCase().getName();
|
| 91 | 1 |
System.out.println("Duration of test(" + name + "/" |
| 92 |
+ getRefid() + ") = " + duration);
|
|
| 93 |
} |
|
| 94 |
|
|
| 95 | 3 |
if (action.equals(LESSTHAN)) {
|
| 96 | 2 |
long value = getLong(VALUE, 0);
|
| 97 | 2 |
getTestCase().assertTrue("Duration exceeded.", duration < value);
|
| 98 |
} |
|
| 99 |
} |
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
/**
|
|
| 103 |
* Validate that the element is properly configured.
|
|
| 104 |
* @throws XMLException Exception may be thrown if there
|
|
| 105 |
* are missing elements.
|
|
| 106 |
*/
|
|
| 107 | 7 |
public void validateElement() throws XMLException { |
| 108 |
// check the element tag name
|
|
| 109 | 7 |
checkElementTagName(STOPWATCH); |
| 110 |
|
|
| 111 |
// action is a required attribute
|
|
| 112 | 7 |
checkRequiredAttribute(ACTION); |
| 113 |
|
|
| 114 | 7 |
String action = getAction(); |
| 115 |
|
|
| 116 | 7 |
if (LESSTHAN.equals(action)) {
|
| 117 |
// if action is not a mark, then a value is required.
|
|
| 118 | 2 |
checkRequiredAttribute( |
| 119 |
getElement(), |
|
| 120 |
VALUE); |
|
| 121 | 2 |
checkRequiredAttribute( |
| 122 |
getElement(), |
|
| 123 |
REFID); |
|
| 124 | 5 |
} else if (LOG.equals(action)) { |
| 125 | 1 |
checkRequiredAttribute( |
| 126 |
getElement(), |
|
| 127 |
REFID); |
|
| 128 | 4 |
} else if (MARK.equals(action)) { |
| 129 | 3 |
checkRequiredAttribute( |
| 130 |
getElement(), |
|
| 131 |
ID); |
|
| 132 |
} else {
|
|
| 133 | 1 |
throw new XMLException("Invalid action", null, |
| 134 |
getElement(), |
|
| 135 |
getTest().getPropertyCache()); |
|
| 136 |
} |
|
| 137 |
} |
|
| 138 |
} |
|
| 139 |
|
|
||||||||||