Rework SingleFeatureTest
[odlparent.git] / features-test-plugin / src / main / java / org / opendaylight / odlparent / features / test / plugin / PaxExamExecution.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.odlparent.features.test.plugin;
9
10 import static java.util.Objects.requireNonNull;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.io.PrintStream;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.ops4j.pax.exam.ExamSystem;
19 import org.ops4j.pax.exam.TestContainer;
20
21 final class PaxExamExecution {
22     private final TestContainer[] containers;
23     private final ExamSystem examSystem;
24     private final String localRepository;
25
26     PaxExamExecution(final String localRepository, final ExamSystem examSystem, final TestContainer ... containers) {
27         this.localRepository = requireNonNull(localRepository);
28         this.containers = containers;
29         this.examSystem = examSystem;
30     }
31
32     @SuppressWarnings({"IllegalCatch", "RegexpSinglelineJava"})
33     @SuppressFBWarnings("DM_DEFAULT_ENCODING")
34     void execute() throws MojoExecutionException {
35
36         // Use the same repository for Pax Exam as is used for Maven
37         System.setProperty("org.ops4j.pax.url.mvn.localRepository", localRepository);
38
39         for (var container : containers) {
40             // disable karaf stdout output to maven log
41             final var stdout = System.out;
42             System.setOut(new PrintStream(OutputStream.nullOutputStream()));
43
44             final var containerStarted = new AtomicBoolean(false);
45             try {
46                 container.start();
47                 containerStarted.set(true);
48
49                 // build probe
50                 final var probeBuilder = examSystem.createProbe();
51                 final var address = probeBuilder.addTest(TestProbe.class, "testFeature");
52                 probeBuilder.addTest(TestProbe.CheckResult.class);
53
54                 // install probe bundle
55                 container.install(probeBuilder.build().getStream());
56                 // execute probe testMethod
57                 container.call(address);
58
59             } catch (RuntimeException | IOException e) {
60                 throw new MojoExecutionException(e);
61             } finally {
62                 if (containerStarted.get()) {
63                     container.stop();
64                 }
65                 // restore stdout
66                 System.setOut(stdout);
67             }
68         }
69     }
70 }