2727import org .xml .sax .SAXException ;
2828
2929import org .eclipse .debug .core .ILaunch ;
30- import org .eclipse .unittest .internal .junitXmlReport .TestRunHandler ;
3130import org .eclipse .unittest .launcher .ITestRunnerClient ;
32- import org .eclipse .unittest .model .ITestCaseElement ;
33- import org .eclipse .unittest .model .ITestElement ;
34- import org .eclipse .unittest .model .ITestElement .FailureTrace ;
35- import org .eclipse .unittest .model .ITestElement .Result ;
3631import org .eclipse .unittest .model .ITestRunSession ;
3732import org .eclipse .unittest .model .ITestSuiteElement ;
3833
3934import org .apache .maven .execution .ExecutionEvent .Type ;
4035
4136import org .eclipse .m2e .internal .launch .MavenBuildProjectDataConnection ;
37+ import org .eclipse .m2e .internal .launch .testing .copied .TestRunHandler ;
4238import org .eclipse .m2e .internal .maven .listener .MavenBuildListener ;
4339import org .eclipse .m2e .internal .maven .listener .MavenProjectBuildData ;
4440import org .eclipse .m2e .internal .maven .listener .MavenTestEvent ;
@@ -63,6 +59,7 @@ public MavenTestRunnerClient(ITestRunSession session) {
6359 }
6460
6561 public void startMonitoring () {
62+ System .out .println ("---- start monitoring ---" );
6663 MavenBuildProjectDataConnection .getConnection (session .getLaunch ())
6764 .ifPresent (con -> con .addMavenBuildListener (this ));
6865
@@ -78,6 +75,7 @@ public void stopTest() {
7875 }
7976
8077 public void stopMonitoring () {
78+ System .out .println ("---- stop Monitoring ----\r \n " );
8179 MavenBuildProjectDataConnection .getConnection (session .getLaunch ())
8280 .ifPresent (con -> con .removeMavenBuildListener (this ));
8381 }
@@ -93,74 +91,58 @@ public void projectStarted(MavenProjectBuildData data) {
9391 this .projectData .set (data );
9492 }
9593
94+ boolean started ;
95+
9696 public void onTestEvent (MavenTestEvent mavenTestEvent ) {
97+ System .out .println ("MavenTestRunnerClient.onTestEvent()" );
9798 if (mavenTestEvent .getType () == Type .MojoSucceeded || mavenTestEvent .getType () == Type .MojoFailed ) {
99+ MavenProjectBuildData buildData = projectData .get ();
100+ // Display.getDefault().execute(() -> {
101+
98102 //in any case look for the tests...
99103 Path reportDirectory = mavenTestEvent .getReportDirectory ();
100104 if (Files .isDirectory (reportDirectory )) {
101105 SAXParser parser = getParser ();
102106 if (parser == null ) {
103107 return ;
104108 }
109+ ensureStarted ();
110+ ITestSuiteElement projectSuite = getProjectSuite (buildData );
105111 try (Stream <Path > list = Files .list (reportDirectory )) {
106112 Iterator <Path > iterator = list .iterator ();
107113 while (iterator .hasNext ()) {
108114 Path path = iterator .next ();
109115 System .out .println ("Scan result file " + path );
110- ITestRunSession importedSession = parseFile (path , parser );
111- if (importedSession != null ) {
112- ITestSuiteElement project = getProjectSuite ();
113- ITestSuiteElement file = session .newTestSuite (path .toString (), path .getFileName ().toString (), null ,
114- project , path .getFileName ().toString (), null );
115- for (ITestElement element : importedSession .getChildren ()) {
116- importTestElement (element , file );
117- }
118- }
116+ parseFile (path , parser , projectSuite );
119117 }
120118 } catch (IOException ex ) {
121119 }
122120 }
121+ // });
123122 }
124123 }
125124
126- /**
127- * @param element
128- * @param file
129- */
130- private void importTestElement (ITestElement element , ITestSuiteElement parent ) {
131- if (element instanceof ITestCaseElement testcase ) {
132- ITestCaseElement importedTestCase = session .newTestCase (parent .getId () + "." + testcase .getId (),
133- testcase .getTestName (), parent , testcase .getDisplayName (), testcase .getData ());
134- session .notifyTestStarted (importedTestCase );
135- FailureTrace failureTrace = testcase .getFailureTrace ();
136- if (failureTrace == null ) {
137- session .notifyTestEnded (importedTestCase , testcase .isIgnored ());
138- } else {
139- session .notifyTestFailed (importedTestCase , Result .ERROR /*TODO how do we know?*/ , false /*TODO how do we know?*/ ,
140- failureTrace );
141- }
142- } else if (element instanceof ITestSuiteElement suite ) {
143- ITestSuiteElement importedTestSuite = session .newTestSuite (parent .getId () + "." + suite .getId (),
144- suite .getTestName (), null , parent , suite .getDisplayName (), suite .getData ());
145- session .notifyTestStarted (importedTestSuite );
146- for (ITestElement child : suite .getChildren ()) {
147- importTestElement (child , importedTestSuite );
148- }
149- session .notifyTestEnded (importedTestSuite , false );
125+ private synchronized void ensureStarted () {
126+ if (!started ) {
127+ session .notifyTestSessionStarted (null );
128+ started = true ;
150129 }
151130 }
152131
153132 /**
154133 * @return
155134 */
156- private ITestSuiteElement getProjectSuite () {
157- return projectElementMap .computeIfAbsent (projectData . get () , data -> {
135+ private ITestSuiteElement getProjectSuite (MavenProjectBuildData buildData ) {
136+ return projectElementMap .computeIfAbsent (buildData , data -> {
158137 Path basedir = data .projectBasedir ;
159- return session .newTestSuite (basedir .toString (), basedir .getFileName ().toString (), null , null ,
160- data .groupId + ":" + data .artifactId , null );
138+ ITestSuiteElement suite = session .newTestSuite (System .currentTimeMillis () + basedir .toString (),
139+ basedir .getFileName ().toString (), null , null , data .groupId + ":" + data .artifactId , null );
140+ session .notifyTestStarted (suite );
141+ return suite ;
161142 });
162143 }
163144
145+ @ SuppressWarnings ("restriction" )
164146 private SAXParser getParser () {
165147 try {
166148 return org .eclipse .core .internal .runtime .XmlProcessorFactory .createSAXParserWithErrorOnDOCTYPE ();
@@ -170,20 +152,19 @@ private SAXParser getParser() {
170152 return null ;
171153 }
172154
173- private ITestRunSession parseFile (Path path , SAXParser parser ) {
174- //TODO Currently NOT working as this is internal API that is not exported!
175- final TestRunHandler handler = new TestRunHandler ();
155+ private void parseFile (Path path , SAXParser parser , ITestSuiteElement parent ) {
156+ final TestRunHandler handler = new TestRunHandler (session , parent );
176157 try {
177158 parser .parse (Files .newInputStream (path ), handler );
178- return handler .getTestRunSession ();
179159 } catch (SAXException | IOException ex ) {
180160 //can't read then...
181- return null ;
182161 }
183162 }
184163
185164 public void close () {
186- // nothing to do yet...
165+ if (started ) {
166+ session .notifyTestSessionCompleted (null );
167+ }
187168 }
188169
189170}
0 commit comments