Chapter 4. Testing XML rules
After you have created an XML rule, you should create a test rule to ensure that it works.
4.1. Creating a test rule
Test rules are created using a process similar to the process for creating an XML rule, with the following differences:
-
Test rules should be placed in a
tests/
directory beneath the rule to be tested. -
Any data, such as test classes, should be placed in a
data/
directory beneath thetests/
directory. -
Test rules should use the
.windup.test.xml
extension. - These rules use the structure defined in the Test XML Rule Structure.
In addition, it is recommended to create a test rule that follows the name of the rule it tests. For instance, if a rule were created with a filename of proprietary-rule.mta.xml
, the test rule should be called proprietary-rule.windup.test.xml
.
4.1.1. Test XML rule structure
All test XML rules are defined as elements within ruletests
which contain one or more rulesets
. For more details, see the MTA XML rule schema.
A ruletest is a group of one or more tests that targets a specific area of migration. This is the basic structure of the <ruletest>
element.
<ruletest id="<RULE_TOPIC>-test">
: Defines this as a unique MTA ruletest and gives it a unique ruletest id.-
<testDataPath>
: Defines the path to any data, such as classes or files, used for testing. -
<sourceMode>
: Indicates if the passed in data only contains source files. If an archive, such as an EAR, WAR, or JAR, is in use, then this should be set tofalse
. Defaults totrue
. -
<rulePath>
: The path to the rule to be tested. This should end in the name of the rule to test. -
<ruleset>
: Rulesets containing the logic of the test cases. These are identical to the ones defined in Rulesets.
-
4.1.2. Test XML rule syntax
In addition to the tags in the standard XML rule syntax, the following when
conditions are commonly used for creating test rules:
-
<not>
-
<iterable-filter>
-
<classification-exists>
-
<hint-exists>
In addition to the tags in the standard perform action
syntax, the following perform
conditions are commonly used as actions in test rules:
-
<fail>
4.1.2.1. <not> syntax
Summary
The <not>
element is the standard logical not operator, and is commonly used to perform a <fail>
if the condition is not met.
The following is an example of a test rule that fails if only a specific message exists at the end of the analysis.
<ruletest xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="proprietary-servlet-test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd"> <testDataPath>data/</testDataPath> <rulePath>../proprietary-servlet.windup.xml</rulePath> <ruleset> <rules> <rule id="proprietary-servlet-01000-test"> <when> <!-- The `<not>` will perform a logical _not_ operator on the elements within. --> <not> <!-- The defined `<iterable-filter>` has a size of `1`. This rule will only match on a single instance of the defined hint. --> <iterable-filter size="1"> <hint-exists message="Replace the proprietary @ProprietaryServlet annotation with the Java EE 7 standard @WebServlet annotation*" /> </iterable-filter> </not> </when> <!-- This `<perform>` element is only executed if the previous `<when>` condition is false. This ensures that it only executes if there is not a single instance of the defined hint. --> <perform> <fail message="Hint for @ProprietaryServlet was not found!" /> </perform> </rule> </rules> </ruleset> </ruletest>
The <not>
element has no unique attributes or child elements.
4.1.2.2. <iterable-filter> syntax
Summary
The <iterable-filter>
element counts the number of times a condition is verified. For additional information, see the IterableFilter class.
The following is an example that looks for four instances of the specified message.
<ruletest xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="proprietary-servlet-test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd"> <testDataPath>data/</testDataPath> <rulePath>../proprietary-servlet.mta.xml</rulePath> <ruleset> <rules> <rule id="proprietary-servlet-03000-test"> <when> <!-- The `<not>` will perform a logical _not_ operator on the elements within. --> <not> <!-- The defined `<iterable-filter>` has a size of `4`. This rule will only match on four instances of the defined hint. --> <iterable-filter size="4"> <hint-exists message="Replace the proprietary @ProprietaryInitParam annotation with the Java EE 7 standard @WebInitParam annotation*" /> </iterable-filter> </not> </when> <!-- This `<perform>` element is only executed if the previous `<when>` condition is false. In this configuration, it only executes if there are not four instances of the defined hint. --> <perform> <fail message="Hint for @ProprietaryInitParam was not found!" /> </perform> </rule> </rules> </ruleset> </ruletest>
The <iterable-filter>
element has no unique child elements.
<iterable-filter> element attributes
Attribute Name | Type | Description |
---|---|---|
size | integer | The number of times to be verified. |
4.1.2.3. <classification-exists> syntax
The <classification-exists>
element determines if a specific classification title has been included in the analysis. For additional information, see the ClassificationExists class.
When testing for a message that contains special characters, such as [
or '
, you must escape each special character with a backslash (\
) to correctly match.
The following is an example that searches for a specific classification title.
<ruletest xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="proprietary-servlet-test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd"> <testDataPath>data/</testDataPath> <rulePath>../weblogic.mta.xml</rulePath> <ruleset> <rules> <rule id="weblogic-01000-test"> <when> <!-- The `<not>` will perform a logical _not_ operator on the elements within. --> <not> <!-- The defined `<classification-exists>` is attempting to match on the defined title. This classification would have been generated by a matching `<classification title="WebLogic scheduled job" .../>` rule. --> <classification-exists classification="WebLogic scheduled job" /> </not> </when> <!-- This `<perform>` element is only executed if the previous `<when>` condition is false. In this configuration, it only executes if there is not a matching classification. --> <perform> <fail message="Triggerable not found" /> </perform> </rule> </rules> </ruleset> </ruletest>
The <classification-exists>
has no unique child elements.
<classification-exists> element attributes
Attribute Name | Type | Description |
---|---|---|
classification | String |
The |
in | String | An optional argument that restricts matching to files that contain the defined filename. |
4.1.2.4. <hint-exists> syntax
The <hint-exists>
element determines if a specific hint has been included in the analysis. It searches for any instances of the defined message, and is typically used to search for the beginning or a specific class inside of a <message>
element. For additional information, see the HintExists class.
When testing for a message that contains special characters, such as [
or '
, you must escape each special character with a backslash (\
) to correctly match.
The following is an example that searches for a specific hint.
<ruletest xmlns="http://windup.jboss.org/schema/jboss-ruleset" id="proprietary-servlet-test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd"> <testDataPath>data/</testDataPath> <rulePath>../weblogic.windup.xml</rulePath> <ruleset> <rules> <rule id="weblogic-eap7-05000-test"> <when> <!-- The `<not>` will perform a logical _not_ operator on the elements within. --> <not> <!-- The defined `<hint-exists>` is attempting to match on the defined message. This message would have been generated by a matching `<message>` element on the `<hint>` condition. --> <hint-exists message="Replace with the Java EE standard method .*javax\.transaction\.TransactionManager\.resume\(Transaction tx\).*" /> </not> </when> <!-- This `<perform>` element is only executed if the previous `<when>` condition is false. In this configuration, it only executes if there is not a matching hint. --> <perform> <fail message="Note to replace with standard TransactionManager.resume is missing!" /> </perform> </rule> </rules> </ruleset> </ruletest>
The <hint-exists>
element has no unique child elements.
<hint-exists> element attributes
Attribute Name | Type | Description |
---|---|---|
message | String |
The |
in | String |
An optional argument that restricts matching to |
4.1.2.5. <fail> syntax
The <fail>
element reports the execution as a failure and displays the associated message. It is commonly used in conjunction with the <not>
condition to display a message only if the conditions are not met.
The <fail>
element has no unique child elements.
<fail> element attributes
Attribute Name | Type | Description |
---|---|---|
message | String | The message to be displayed. |
4.2. Manually testing an XML rule
You can run an XML rule against your application file to test it:
$ <MTA_HOME>/bin/windup-cli [--sourceMode] --input <INPUT_ARCHIVE_OR_FOLDER> --output <OUTPUT_REPORT_DIRECTORY> --target <TARGET_TECHNOLOGY> --packages <PACKAGE_1> <PACKAGE_2> <PACKAGE_N>
You should see the following result:
Report created: <OUTPUT_REPORT_DIRECTORY>/index.html Access it at this URL: file:///<OUTPUT_REPORT_DIRECTORY>/index.html
More examples of how to run MTA are located in the Migration Toolkit for Applications CLI Guide.
4.3. Testing the rules by using JUnit
Once a test rule has been created, it can be analyzed as part of a JUnit test to confirm that the rule meets all criteria for execution. The WindupRulesMultipleTests
class in the MTA rules repository is designed to test multiple rules simultaneously, and provides feedback on any missing requirements.
Prerequisites
- Fork and clone the MTA XML rules. The location of this repository will be referred to as <RULESETS_REPO>.
- Create a test XML rule.
Creating the JUnit test configuration
The following instructions detail creating a JUnit test using Eclipse. When using a different IDE, it is recommended to consult your IDE’s documentation for instructions on creating a JUnit test.
- Import the MTA rulesets repository into your IDE.
Copy the custom rules, along with the corresponding tests and data, into
</path/to/RULESETS_REPO>/rules-reviewed/<RULE_NAME>/
. This should create the following directory structure.Directory structure
├── *rules-reviewed/* _(Root directory of the rules found within the project)_ │ ├── *<RULE_NAME>/* _(Directory to contain the newly developed rule and tests)_ │ │ ├── *<RULE_NAME>.windup.xml* _(Custom rule)_ │ │ ├── *tests/* _(Directory that contains any test rules and data)_ │ │ │ ├── *<RULE_NAME>.windup.test.xml* _(Test rule)_ │ │ │ └── *data/* _(Optional directory to contain test rule data)_
- Select Run from the top menu bar.
- Select Run Configurations… from the drop down that appears.
- Right-click JUnit from the options on the left side and select New.
Enter the following:
-
Name: A name for your JUnit test, such as
WindupRulesMultipleTests
. -
Project: Ensure this is set to
windup-rulesets
. Test class: Set this to
org.jboss.windup.rules.tests.WindupRulesMultipleTests
.
-
Name: A name for your JUnit test, such as
Select the Arguments tab, and add the
-DrunTestsMatching=<RULE_NAME>
VM argument. For instance, if your rule name wascommunity-rules
, then you would add-DrunTestsMatching=community-rules
as seen in the following image.Click Run in the bottom right corner to begin the test.
When the execution completes, the results are available for analysis. If all tests passed, then the test rule is correctly formatted. If all tests did not pass, it is recommended to address each of the issues raised in the test failures.
4.4. About validation reports
Validation reports provide details about test rules and failures and contain the following sections:
Summary
This section contains the total number of tests run and reports the number of errors and failures. It displays the total success rate and the time taken, in seconds, for the report to be generated.
Package List
This section contains the number of tests executed for each package and reports the number of errors and failures. It displays the success rate and the time taken, in seconds, for each package to be analyzed.
A single package named
org.jboss.windup.rules.tests
is displayed unless additional test cases have been defined.Test Cases
This section describes the test cases. Each failure includes a Details section that can be expanded to show the stack trace for the assertion, including a human-readable line indicating the source of the error.
4.4.1. Creating a validation report
You can create a validation report for your custom rules.
Prerequisites
- You must fork and clone the MTA XML rules.
- You must have one or more test XML rules to validate.
Procedure
-
Navigate to the local
windup-rulesets
repository. -
Create a directory for your custom rules and tests:
windup-rulesets/rules-reviewed/myTests
. -
Copy your custom rules and tests to the
windup-rulesets/rules-reviewed/<myTests>
directory. Run the following command from the root directory of the
windup-rulesets
repository:$ mvn -Dtest=WindupRulesMultipleTests -DrunTestsMatching=<myTests> clean <myReport>:report 1 2
The validation report is created in the
windup-rulesets/target/site/
repository.
4.4.2. Validation report error messages
Validation reports contain errors encountered while running the rules and tests.
The following table contains error messages and how to resolve the errors.
Error message | Description | Resolution |
---|---|---|
No test file matching rule | This error occurs when a rule file exists without a corresponding test file. | Create a test file for the existing rule. |
Test rule Ids <RULE_NAME> not found! | This error is thrown when a rule exists without a corresponding ruletest. | Create a test for the existing rule. |
XML parse fail on file <FILE_NAME> | The syntax in the XML file is invalid, and unable to be parsed successfully by the rule validator. | Correct the invalid syntax. |
Test file path from |
No files are found in the path defined in the |
Create the path defined in the |
The rule with id="<RULE_ID>" has not been executed. | The rule with the provided id has not been executed during this validation. | Ensure that a test data file exists that matches the conditions defined in the specified rule. |