Skip to content

Commit cd4345c

Browse files
committed
add report formatter
1 parent a348d37 commit cd4345c

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@ An application to provide support for executing tests in the specific order and
99
<dependency>
1010
<groupId>com.hackerrank.applications</groupId>
1111
<artifactId>junit-ordered-test-runner</artifactId>
12-
<version>1.0.0</version>
12+
<version>1.0.1</version>
1313
</dependency>
1414
```
1515

1616
- [Gradle Groovy DSL](gradle.org)
1717
```
18-
compile 'com.hackerrank.applications:junit-ordered-test-runner:1.0.0'
18+
compile 'com.hackerrank.applications:junit-ordered-test-runner:1.0.1'
1919
```
2020

2121
- [Gradle Kotlin DSL](github.com/gradle/kotlin-dsl)
2222
```
23-
compile(group = "com.hackerrank.applications", name = "junit-ordered-test-runner", version = "1.0.0")
23+
compile(group = "com.hackerrank.applications", name = "junit-ordered-test-runner", version = "1.0.1")
2424
```
2525

2626
- [Scala SBT](scala-sbt.org)
2727
```
28-
libraryDependencies += "com.hackerrank.applications" % "junit-ordered-test-runner" % "1.0.0"
28+
libraryDependencies += "com.hackerrank.applications" % "junit-ordered-test-runner" % "1.0.1"
2929
```
3030

3131
- [Apache Ivy](ant.apache.org/ivy/)
3232
```
33-
<dependency org="com.hackerrank.applications" name="junit-ordered-test-runner" rev="1.0.0" />
33+
<dependency org="com.hackerrank.applications" name="junit-ordered-test-runner" rev="1.0.1" />
3434
```
3535

3636
- [Groovy Grape](groovy-lang.org/grape.html)
3737
```
3838
@Grapes(
39-
@Grab(group='com.hackerrank.applications', module='junit-ordered-test-runner', version='1.0.0')
39+
@Grab(group='com.hackerrank.applications', module='junit-ordered-test-runner', version='1.0.1')
4040
)
4141
```
4242

4343
- [Apache Builder](buildr.apache.org)
4444
```
45-
'com.hackerrank.applications:junit-ordered-test-runner:jar:1.0.0'
45+
'com.hackerrank.applications:junit-ordered-test-runner:jar:1.0.1'
4646
```
4747

4848
## Sample Usage
@@ -161,6 +161,26 @@ You can refer the given [test examples](src/test/java/com/hackerrank/test/utilit
161161
- The `TestWatcher` generates an XML report in the `target/hackerrank-report` directory. The filename is `TEST-{test-class-canonical-name}.xml`.
162162
- When running tests in a suite, suite report, as well as individual test reports, will be generated.
163163

164+
## Report Formatting
165+
166+
In test suite use `ReportFormatter.format(path)` to update the test name in canonical format, i.e., `{package}.{class_name}.{test_name}`. The path must be relative to the project directory. For example:
167+
```java
168+
package com.hackerrank.test;
169+
170+
@RunWith(Suite.class)
171+
@Suite.SuiteClasses({
172+
com.hackerrank.test.FirstTest.class,
173+
com.hackerrank.test.SecondTest.class
174+
})
175+
public class TestSuite {
176+
177+
@AfterClass
178+
public static void tearDownClass() throws Exception {
179+
ReportFormatter.format("target/surefire-reports/TEST-com.hackerrank.test.TestSuite.xml");
180+
}
181+
}
182+
```
183+
164184
## Building Project
165185

166186
- Use `mvn clean build` to build the project.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.hackerrank.applications</groupId>
66
<artifactId>junit-ordered-test-runner</artifactId>
7-
<version>1.0.0</version>
7+
<version>1.0.1</version>
88
<packaging>jar</packaging>
99

1010
<name>${project.groupId}:${project.artifactId}</name>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2018 HackerRank.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hackerrank.test.utility;
18+
19+
import java.io.File;
20+
import java.io.FileWriter;
21+
import java.io.IOException;
22+
import java.util.List;
23+
import org.jdom.Attribute;
24+
import org.jdom.Document;
25+
import org.jdom.Element;
26+
import org.jdom.JDOMException;
27+
import org.jdom.input.SAXBuilder;
28+
import org.jdom.output.Format;
29+
import org.jdom.output.XMLOutputter;
30+
31+
/**
32+
* @author Abhimanyu Singh
33+
34+
* @version 1.0.1
35+
* @since 1.0.1
36+
*/
37+
public class ReportFormatter {
38+
/**
39+
* @param reportPath XML report path relative to project root
40+
* @throws java.io.IOException no such file exception
41+
* @throws org.jdom.JDOMException XML document build exception
42+
*/
43+
public static void format(String reportPath) throws JDOMException, IOException {
44+
SAXBuilder builder = new SAXBuilder();
45+
File xmlFile = new File(reportPath);
46+
47+
Document report = (Document) builder.build(xmlFile);
48+
49+
List<Element> testcases = report.getRootElement().getChildren("testcase");
50+
51+
for (int i = 0; i < testcases.size(); i++) {
52+
Element testcase = testcases.get(i);
53+
54+
String testname = testcase.getAttributeValue("name");
55+
String testclass = testcase.getAttributeValue("classname");
56+
57+
Attribute test = new Attribute("name", testclass + "." + testname);
58+
59+
testcase.setAttribute(test);
60+
61+
System.out.println(testcase.getAttributeValue("name"));
62+
}
63+
64+
XMLOutputter outputter = new XMLOutputter();
65+
66+
outputter.setFormat(Format.getPrettyFormat());
67+
outputter.output(report, new FileWriter(xmlFile));
68+
}
69+
}

0 commit comments

Comments
 (0)