2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.odlparent.features.test.plugin;
10 import static java.util.Objects.requireNonNull;
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;
21 final class PaxExamExecution {
22 private final TestContainer[] containers;
23 private final ExamSystem examSystem;
24 private final String localRepository;
26 PaxExamExecution(final String localRepository, final ExamSystem examSystem, final TestContainer ... containers) {
27 this.localRepository = requireNonNull(localRepository);
28 this.containers = containers;
29 this.examSystem = examSystem;
32 @SuppressWarnings({"IllegalCatch", "RegexpSinglelineJava"})
33 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
34 void execute() throws MojoExecutionException {
36 // Use the same repository for Pax Exam as is used for Maven
37 System.setProperty("org.ops4j.pax.url.mvn.localRepository", localRepository);
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()));
44 final var containerStarted = new AtomicBoolean(false);
47 containerStarted.set(true);
50 final var probeBuilder = examSystem.createProbe();
51 final var address = probeBuilder.addTest(TestProbe.class, "testFeature");
52 probeBuilder.addTest(TestProbe.CheckResult.class);
54 // install probe bundle
55 container.install(probeBuilder.build().getStream());
56 // execute probe testMethod
57 container.call(address);
59 } catch (RuntimeException | IOException e) {
60 throw new MojoExecutionException(e);
62 if (containerStarted.get()) {
66 System.setOut(stdout);