Skip to content

Commit

Permalink
#1091 Modernize Karaf test container
Browse files Browse the repository at this point in the history
* upgrade Karaf to 4.4.3
* streamline some integration tests
  • Loading branch information
oliverlietz committed Aug 18, 2023
1 parent 5003b48 commit 374265c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@

public class KarafTestContainer implements TestContainer {

private static final String[] configs = {
"config.properties",
"system.properties",
"org.apache.karaf.features.cfg",
"org.apache.karaf.management.cfg",
"org.ops4j.pax.logging.cfg"
};

private static final Logger LOGGER = LoggerFactory.getLogger(KarafTestContainer.class);

private static final String KARAF_TEST_CONTAINER = "KarafTestContainer.start";
Expand Down Expand Up @@ -222,23 +230,21 @@ public synchronized TestContainer start() {

private void backupConfigFiles() {
try {
File karafEtc = new File(karafBase, framework.getKarafEtc());
FileUtils.copyFile(new File(karafEtc, "config.properties"), new File(karafEtc, "config.properties.paxexam"));
FileUtils.copyFile(new File(karafEtc, "system.properties"), new File(karafEtc, "system.properties.paxexam"));
FileUtils.copyFile(new File(karafEtc, "org.apache.karaf.features.cfg"), new File(karafEtc, "org.apache.karaf.features.cfg.paxexam"));
FileUtils.copyFile(new File(karafEtc, "org.ops4j.pax.logging.cfg"), new File(karafEtc, "org.ops4j.pax.logging.cfg.paxexam"));
final File karafEtc = new File(karafBase, framework.getKarafEtc());
for (final String config : configs) {
FileUtils.copyFile(new File(karafEtc, config), new File(karafEtc, config.concat(".paxexam")));
}
} catch (Exception e) {
LOGGER.warn("Can't backup config files", e);
}
}

private void restoreConfigFiles() {
try {
File karafEtc = new File(karafBase, framework.getKarafEtc());
FileUtils.copyFile(new File(karafEtc, "config.properties.paxexam"), new File(karafEtc, "config.properties"));
FileUtils.copyFile(new File(karafEtc, "system.properties.paxexam"), new File(karafEtc, "system.properties"));
FileUtils.copyFile(new File(karafEtc, "org.apache.karaf.features.cfg.paxexam"), new File(karafEtc, "org.apache.karaf.features.cfg"));
FileUtils.copyFile(new File(karafEtc, "org.ops4j.pax.logging.cfg.paxexam"), new File(karafEtc, "org.ops4j.pax.logging.cfg"));
final File karafEtc = new File(karafBase, framework.getKarafEtc());
for (final String config : configs) {
FileUtils.copyFile(new File(karafEtc, config.concat(".paxexam")), new File(karafEtc, config));
}
} catch (Exception e) {
LOGGER.warn("Can't restore config files", e);
}
Expand Down Expand Up @@ -303,8 +309,7 @@ private void startKaraf(ExamSystem subsystem, File karafBase, File karafHome) {
+ subsystem.getTimeout());

if (subsystem.getOptions(ServerModeOption.class).length == 0) {
waitForState(org.ops4j.pax.exam.karaf.container.internal.Constants.SYSTEM_BUNDLE,
Bundle.ACTIVE, subsystem.getTimeout());
waitForState(Constants.SYSTEM_BUNDLE, Bundle.ACTIVE, subsystem.getTimeout());
}
else {
LOGGER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ops4j.pax.exam.karaf.container;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;

import java.io.File;

import javax.inject.Inject;
Expand All @@ -41,49 +32,67 @@
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;

@RunWith(PaxExam.class)
public abstract class AbstractKarafTestContainerIT {

private final MavenArtifactUrlReference KARAF_URL = maven("org.apache.karaf", "apache-karaf").type("zip");
protected static final File UNPACK_DIRECTORY = new File("target/paxexam/unpack/");

protected static final MavenArtifactUrlReference KARAF_URL = maven("org.apache.karaf", "apache-karaf").type("zip");

@Inject
private BundleContext bc;
private BundleContext bundleContext;

@Configuration
public Option[] config() {
String karafVersion = karafVersion();
return new Option[] {
karafDistributionConfiguration().frameworkUrl(KARAF_URL.version(karafVersion))
.karafVersion(karafVersion).useDeployFolder(false).unpackDirectory(new File("target/paxexam/unpack/")),
configureConsole().startLocalConsole().ignoreRemoteShell(), logLevel(LogLevel.INFO)
};
final String karafVersion = karafVersion();
return options(
karafDistributionConfiguration().
frameworkUrl(KARAF_URL.version(karafVersion)).
karafVersion(karafVersion).
useDeployFolder(false).
unpackDirectory(UNPACK_DIRECTORY),
configureConsole().
startLocalConsole().
ignoreRemoteShell(),
logLevel(LogLevel.DEBUG),
keepRuntimeFolder()
);
}

public String karafVersion() {
ConfigurationManager cm = new ConfigurationManager();
String karafVersion = cm.getProperty("pax.exam.karaf.version", getDefaultKarafVersion());
return karafVersion;
final ConfigurationManager cm = new ConfigurationManager();
return cm.getProperty("pax.exam.karaf.version", getDefaultKarafVersion());
}

protected abstract String getDefaultKarafVersion();
protected String getDefaultKarafVersion() {
return "4.4.3";
}

@Test
public void checkKarafSystemService() throws Exception {
assertThat(bc, is(notNullValue()));
ServiceReference<?> serviceRef = bc
.getServiceReference("org.apache.karaf.system.SystemService");
Object service = bc.getService(serviceRef);
public void checkKarafSystemService() {
assertThat(bundleContext, is(notNullValue()));
final ServiceReference<?> serviceRef = bundleContext.getServiceReference("org.apache.karaf.system.SystemService");
final Object service = bundleContext.getService(serviceRef);
assertThat(service, is(notNullValue()));
}
public Bundle findBundle(String symbolicName) {
Bundle[] bundles = bc.getBundles();
for (Bundle bundle : bundles) {

public Bundle findBundle(final String symbolicName) {
final Bundle[] bundles = bundleContext.getBundles();
for (final Bundle bundle : bundles) {
if (bundle.getSymbolicName().equals(symbolicName)) {
return bundle;
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@

import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.karaf.options.LogLevelOption;
import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;

import java.io.File;

import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;

public class Karaf4EmbeddedTestContainerIT extends AbstractKarafTestContainerIT {

private final MavenArtifactUrlReference KARAF_URL = maven("org.apache.karaf", "apache-karaf").type("zip");

@Override
protected String getDefaultKarafVersion() {
return "4.2.8";
}

@Configuration
public Option[] config() {
String karafVersion = karafVersion();
return new Option[] {
karafDistributionConfiguration().runEmbedded(true).frameworkUrl(KARAF_URL.version(karafVersion))
.karafVersion(karafVersion).useDeployFolder(false).unpackDirectory(new File("target/paxexam/unpack/")),
configureConsole().startLocalConsole().ignoreRemoteShell(), logLevel(LogLevelOption.LogLevel.INFO)
};
return options(
karafDistributionConfiguration().
runEmbedded(true).
frameworkUrl(KARAF_URL.version(karafVersion)).
karafVersion(karafVersion).
useDeployFolder(false).
unpackDirectory(UNPACK_DIRECTORY),
configureConsole().
startLocalConsole().
ignoreRemoteShell(),
logLevel(LogLevel.DEBUG),
keepRuntimeFolder()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ops4j.pax.exam.karaf.container;

public class Karaf4TestContainerIT extends AbstractKarafTestContainerIT {

@Override
protected String getDefaultKarafVersion() {
return "4.2.8";
}
}
2 changes: 1 addition & 1 deletion pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<dependency.deltaspike.version>1.5.1</dependency.deltaspike.version>
<dependency.glassfish.version>4.1.1</dependency.glassfish.version>
<dependency.httpcomponents.version>4.5.13</dependency.httpcomponents.version>
<dependency.karaf.version>4.2.8</dependency.karaf.version>
<dependency.karaf.version>4.4.3</dependency.karaf.version>

<dependency.logback.version>1.2.3</dependency.logback.version>

Expand Down

0 comments on commit 374265c

Please sign in to comment.