|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| TokenizeTagHandler.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 junit.framework.Assert;
|
|
| 7 |
|
|
| 8 |
import org.w3c.dom.Element;
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
/**
|
|
| 12 |
* Return the tokens of a string one at a time.
|
|
| 13 |
*
|
|
| 14 |
* <H2>Title:</H2>
|
|
| 15 |
* TokenizeTagHandler
|
|
| 16 |
* <H2>Description:</H2>
|
|
| 17 |
*
|
|
| 18 |
* The TokenizeTagHandler provides a mechanism for breaking
|
|
| 19 |
* up a string into pieces. The refid points to the string
|
|
| 20 |
* that will be broken up, each call to tokenize returns
|
|
| 21 |
* the token, and the remainder of the token string that
|
|
| 22 |
* can be used again in a subsequent call.
|
|
| 23 |
*
|
|
| 24 |
* <H2>Attributes:</H2>
|
|
| 25 |
* <pre>
|
|
| 26 |
* id - the string token to be returned [required]
|
|
| 27 |
* refid - the string of tokens to separate [required]
|
|
| 28 |
* delimiter - the token separator [required]
|
|
| 29 |
* </pre>
|
|
| 30 |
*
|
|
| 31 |
*
|
|
| 32 |
* <H2>Example:</H2>
|
|
| 33 |
* If array contained "File>New" and your delimiter was ">".
|
|
| 34 |
* As in:
|
|
| 35 |
* <pre>
|
|
| 36 |
* <code>
|
|
| 37 |
* <property name="array" value="File>New"/>
|
|
| 38 |
* <tokenize id="token" refid="array" delimiter=">"/>
|
|
| 39 |
* <!-- Now array="New" token="File" -->
|
|
| 40 |
* <tokenize id="token" refid="array" delimiter=">"/>
|
|
| 41 |
* <!-- Now array="" token="New" -->
|
|
| 42 |
* </code>
|
|
| 43 |
* </pre>
|
|
| 44 |
* The first call would return "File" in the token property
|
|
| 45 |
* and update the array to "New".
|
|
| 46 |
* The second call would return "New" and update the
|
|
| 47 |
array to: ""
|
|
| 48 |
* Any subsequent call would return ""<br>
|
|
| 49 |
*
|
|
| 50 |
* Another example is to return the values of various fields of
|
|
| 51 |
* which some may be null.
|
|
| 52 |
* <code>
|
|
| 53 |
* <property name="array" value="userid:*:Guest:::"/>
|
|
| 54 |
* <tokenize id="token" refid="array" delimiter=":"/>
|
|
| 55 |
* <!-- Now array="*:Guest:::" token="userid" -->
|
|
| 56 |
* <tokenize id="token" refid="array" delimiter=":"/>
|
|
| 57 |
* <!-- Now array="Guest:::" token="*" -->
|
|
| 58 |
* <tokenize id="token" refid="array" delimiter=":"/>
|
|
| 59 |
* <!-- Now array="::" token="Guest" -->
|
|
| 60 |
* <tokenize id="token" refid="array" delimiter=":"/>
|
|
| 61 |
* <!-- Now array=":" token="" -->
|
|
| 62 |
* <tokenize id="token" refid="array" delimiter=":"/>
|
|
| 63 |
* <!-- Now array="" token="" -->
|
|
| 64 |
* </code>
|
|
| 65 |
* <p>Copyright: Copyright (c) 2004</p>
|
|
| 66 |
* <p>Company: JFCUnit Sourceforge project</p>
|
|
| 67 |
*
|
|
| 68 |
*/
|
|
| 69 |
public class TokenizeTagHandler extends AbstractTagHandler { |
|
| 70 |
/**
|
|
| 71 |
* Constructor.
|
|
| 72 |
* @param element Element to be created.
|
|
| 73 |
* @param testcase IXMLTestCase parent test case.
|
|
| 74 |
*/
|
|
| 75 | 2 |
public TokenizeTagHandler(final Element element, final IXMLTestCase testcase) {
|
| 76 | 2 |
super(element, testcase);
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
/**
|
|
| 80 |
* Handle the XML processing of the tag.
|
|
| 81 |
* @throws XMLException may be thrown.
|
|
| 82 |
*/
|
|
| 83 | 7 |
public void processElement() throws XMLException { |
| 84 | 7 |
validateElement(); |
| 85 |
|
|
| 86 | 7 |
String tokenlist = (String) getXMLTestCase().getProperty( |
| 87 |
getString(REFID)); |
|
| 88 |
|
|
| 89 | 7 |
if (getXMLTestCase().getDebug()) {
|
| 90 | 3 |
System.out.println("tokenlist:" + tokenlist);
|
| 91 |
} |
|
| 92 |
|
|
| 93 | 7 |
String delimiter = getString(DELIMITER); |
| 94 |
|
|
| 95 | 7 |
Assert.assertNotNull("Error: Unable to obtain delimiter : ", delimiter);
|
| 96 |
|
|
| 97 | 7 |
int index = tokenlist.indexOf(delimiter);
|
| 98 |
|
|
| 99 |
// Last token.
|
|
| 100 | 7 |
if (index == -1) {
|
| 101 |
// set the token value to the ID property
|
|
| 102 | 2 |
((IXMLTestCase) getTestCase()).addProperty( |
| 103 |
getString(ID), |
|
| 104 |
tokenlist); |
|
| 105 |
|
|
| 106 |
// clear the refid (remove the property to make null)
|
|
| 107 | 2 |
((IXMLTestCase) getTestCase()).removeProperty(getString(REFID)); |
| 108 |
} else {
|
|
| 109 |
// More tokens so break off the first.
|
|
| 110 |
// set the token value to the ID property
|
|
| 111 | 5 |
String first = tokenlist.substring(0, index); |
| 112 | 5 |
String others = tokenlist.substring(index + delimiter.length()); |
| 113 | 5 |
((IXMLTestCase) getTestCase()).addProperty( |
| 114 |
getString(ID), |
|
| 115 |
first); |
|
| 116 |
|
|
| 117 |
// update the refid with the remainder of the tokenizer
|
|
| 118 | 5 |
((IXMLTestCase) getTestCase()).addProperty( |
| 119 |
getString(REFID), |
|
| 120 |
others); |
|
| 121 |
} |
|
| 122 |
} |
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* Make sure the appropriate tag and attributes are
|
|
| 126 |
* used.
|
|
| 127 |
* @throws XMLException upon failure to validate.
|
|
| 128 |
*/
|
|
| 129 | 7 |
public void validateElement() throws XMLException { |
| 130 | 7 |
super.validateElement();
|
| 131 |
|
|
| 132 |
// check the element tag name
|
|
| 133 |
// checkElementTagName("selectMenuOnToolbar");
|
|
| 134 | 7 |
checkRequiredAttribute(ID); |
| 135 |
|
|
| 136 | 7 |
checkRequiredAttribute(DELIMITER); |
| 137 |
|
|
| 138 | 7 |
checkRequiredAttribute(REFID); |
| 139 |
} |
|
| 140 |
} |
|
| 141 |
|
|
||||||||||