ReplaceInStringTagHandler.java replaces Strings.
It works with regular expressions.
Benifit:
- Reacting to feedback from GUIs
- Changing files (in combination with other tag handlers)
There is a detailed example in the javadoc of the class.
If you think the classe is usefull for you or other users please intergrate it into the official version.
We hope this small contibution will help to spread and develop JFC-Unit.
Thanks a lot.
Tom Wiedenhoeft
P.S. Please note to modify the files XMLConstants.java and TagMapping.properties as shown below.
/**
* This tag handler finds and replaces Strings or
* parts in Strings.
* <h3>Parameters</h3>
* <table border="1" cellpadding="2" cellspacing="0">
* <tr>
* <td valign="top"><b>Attribute</b></td>
* <td valign="top"><b>Description</b></td>
* <td align="center" valign="top"><b>Required</b></td>
* <td valign="top"><b>Default</b></td>
* <td valign="top"><b>Values</b></td>
* </tr>
* <tr>
* <td valign="top">id</td>
* <td valign="top">Future reference to replaced String</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">N/A</td>
* </tr>
* <tr>
* <td valign="top">string</td>
* <td valign="top">The string in which the replacement takes place</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">String value</td>
* </tr>
* <tr>
* <td valign="top">replace</td>
* <td valign="top">The part in the string that should be replaced</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">regular expression</td>
* </tr>
* <tr>
* <td valign="top">with</td>
* <td valign="top">String that will be inserted</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">String value</td>
* </tr>
* </table>
* <h3>Example</h3>
* <blockquote><pre>
* <replace
* id="myReplacedString"
* string="Oktoberfest will be"
* replace="will be"
* with="is over"
* />
* </pre></blockquote>
* <p>
* The above replaces "Oktoberfest will be" by "Oktoberfest is over".
* </p>
* <h3>Example</h3>
* <blockquote><pre>
* <replace
* id="myReplacedString"
* string="Oktoberfest will be"
* replace="will.*"
* with="is over"
* />
* </pre></blockquote>
* <p>
* The above replaces "Oktoberfest will be" by "Oktoberfest is over".
* </p>
* @author tom@smart-tail.com (Tom Wiedenhoeft, DV-Ratio acting under contract with Siemens L&A PA)
*/
public class ReplaceInStringTagHandler extends AbstractTagHandler {
/**
* constructor.
* @param element Element to be processed.
* @param testCase containing test case.
*/
public ReplaceInStringTagHandler(final Element element,
final IXMLTestCase testCase) {
super(element, testCase);
}
/**
* Get the value of the id attribute.
* @return String value of the id attribute.
*/
public String getId() {
return getString(ID);
}
/**
* Get the directory attribute string the element.
* @return String The value of the string attribute.
*/
public String getString() {
return getString(STRING_TO_REPLACE);
}
/**
* Get the value of the replace attribute.
* @return String value of the replace attribute.
*/
public String getReplace() {
return getString(REPLACE);
}
/**
* Get the value of the with attribute.
* @return String value of the with attribute.
*/
public String getWith() {
return getString(WITH);
}
/**
* Process the element.
* Stores an object of type java.io.File for later usage in the cache.
* This object is later accessable via the "id".
* @throws XMLException may be thrown.
*/
public void processElement() throws XMLException {
validateElement();
String replacedString = "";
try {
replacedString = this.replaceString();
} catch(Exception e) {
throw new XMLException(
"Failed to replace String.",
e,
getElement(),
getTest().getPropertyCache());
}
// Store result string
String id = this.getId();
getXMLTestCase().addProperty(id, replacedString);
}
/**
* Validate that the element is properly configured.
* @throws XMLException Exception may be thrown if there
* are missing elements.
*/
public void validateElement() throws XMLException {
// check the element tag name
checkElementTagName(REPLACE);
Hallo, again!
There is another class that might interest you.
ReplaceInStringTagHandler.java replaces Strings.
It works with regular expressions.
Benifit:
- Reacting to feedback from GUIs
- Changing files (in combination with other tag handlers)
There is a detailed example in the javadoc of the class.
If you think the classe is usefull for you or other users please intergrate it into the official version.
We hope this small contibution will help to spread and develop JFC-Unit.
Thanks a lot.
Tom Wiedenhoeft
P.S. Please note to modify the files XMLConstants.java and TagMapping.properties as shown below.
---schnipp -- ReplaceInStringTagHandler.java ---
package junit.extensions.xml.elements;
import junit.extensions.xml.IXMLTestCase;
import junit.extensions.xml.XMLException;
import org.w3c.dom.Element;
import org.apache.regexp.RE;
/**
* This tag handler finds and replaces Strings or
* parts in Strings.
* <h3>Parameters</h3>
* <table border="1" cellpadding="2" cellspacing="0">
* <tr>
* <td valign="top"><b>Attribute</b></td>
* <td valign="top"><b>Description</b></td>
* <td align="center" valign="top"><b>Required</b></td>
* <td valign="top"><b>Default</b></td>
* <td valign="top"><b>Values</b></td>
* </tr>
* <tr>
* <td valign="top">id</td>
* <td valign="top">Future reference to replaced String</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">N/A</td>
* </tr>
* <tr>
* <td valign="top">string</td>
* <td valign="top">The string in which the replacement takes place</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">String value</td>
* </tr>
* <tr>
* <td valign="top">replace</td>
* <td valign="top">The part in the string that should be replaced</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">regular expression</td>
* </tr>
* <tr>
* <td valign="top">with</td>
* <td valign="top">String that will be inserted</td>
* <td valign="top" align="center">Yes</td>
* <td valign="top">N/A</td>
* <td valign="top">String value</td>
* </tr>
* </table>
* <h3>Example</h3>
* <blockquote><pre>
* <replace
* id="myReplacedString"
* string="Oktoberfest will be"
* replace="will be"
* with="is over"
* />
* </pre></blockquote>
* <p>
* The above replaces "Oktoberfest will be" by "Oktoberfest is over".
* </p>
* <h3>Example</h3>
* <blockquote><pre>
* <replace
* id="myReplacedString"
* string="Oktoberfest will be"
* replace="will.*"
* with="is over"
* />
* </pre></blockquote>
* <p>
* The above replaces "Oktoberfest will be" by "Oktoberfest is over".
* </p>
* @author tom@smart-tail.com (Tom Wiedenhoeft, DV-Ratio acting under contract with Siemens L&A PA)
*/
public class ReplaceInStringTagHandler extends AbstractTagHandler {
/**
* constructor.
* @param element Element to be processed.
* @param testCase containing test case.
*/
public ReplaceInStringTagHandler(final Element element,
final IXMLTestCase testCase) {
super(element, testCase);
}
/**
* Get the value of the id attribute.
* @return String value of the id attribute.
*/
public String getId() {
return getString(ID);
}
/**
* Get the directory attribute string the element.
* @return String The value of the string attribute.
*/
public String getString() {
return getString(STRING_TO_REPLACE);
}
/**
* Get the value of the replace attribute.
* @return String value of the replace attribute.
*/
public String getReplace() {
return getString(REPLACE);
}
/**
* Get the value of the with attribute.
* @return String value of the with attribute.
*/
public String getWith() {
return getString(WITH);
}
/**
* Process the element.
* Stores an object of type java.io.File for later usage in the cache.
* This object is later accessable via the "id".
* @throws XMLException may be thrown.
*/
public void processElement() throws XMLException {
validateElement();
String replacedString = "";
try {
replacedString = this.replaceString();
} catch(Exception e) {
throw new XMLException(
"Failed to replace String.",
e,
getElement(),
getTest().getPropertyCache());
}
// Store result string
String id = this.getId();
getXMLTestCase().addProperty(id, replacedString);
}
/**
* Validate that the element is properly configured.
* @throws XMLException Exception may be thrown if there
* are missing elements.
*/
public void validateElement() throws XMLException {
// check the element tag name
checkElementTagName(REPLACE);
// check attributes
checkRequiredAttribute(ID);
checkRequiredAttribute(STRING_TO_REPLACE);
checkRequiredAttribute(REPLACE);
checkRequiredAttribute(WITH);
}
private String replaceString()
{
String pattern = this.getReplace();
String s = this.getString();
String with = this.getWith();
RE r = new RE(pattern);
String replacedString = r.subst(s, with, RE.REPLACE_ALL);
return replacedString;
}
}
---schnapp -- ReplaceInStringTagHandler.java ---
Please add in TagMapping.properties
---schnipp -- TagMapping.properties ---
replace = junit.extensions.xml.elements.ReplaceInStringTagHandler
---schnapp -- TagMapping.properties ---
Please add in XMLConstants.java
---schnipp -- XMLConstants.java ---
/** replace taghandler and attribute */
String REPLACE = "replace";
/** string */
String STRING_TO_REPLACE = "string";
/** with */
String WITH = "with";
---schnapp -- XMLConstants.java ---
Great addition.
Thanks.