Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change KarafTestContainer to rely directly on options and KarafPropertiesFile #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public void writeToFile(File featuresXmlFile) throws IOException {
public void adaptDistributionToStartExam(File karafHome, File featuresXmlFile) throws IOException {
KarafPropertiesFile karafPropertiesFile = new KarafPropertiesFile(karafHome, Constants.FEATURES_CFG_LOCATION);
karafPropertiesFile.load();
String finalFilePath = ",file:" + featuresXmlFile.toString().replaceAll("\\\\", "/").replaceAll(" ", "%20");
String finalFilePath = "file:" + featuresXmlFile.toString().replaceAll("\\\\", "/").replaceAll(" ", "%20");
karafPropertiesFile.extend("featuresRepositories", finalFilePath);
karafPropertiesFile.extend("featuresBoot", ",exam");
karafPropertiesFile.extend("featuresBoot", "exam");
karafPropertiesFile.store();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFileExtendOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFileOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFilePutOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFileReplacementOption;

public class KarafPropertiesFile {

Expand Down Expand Up @@ -50,12 +54,16 @@ public void put(String key, String value) {
properties.put(key, value);
}

public void extend(String key, String value) {
public void extend(String key, String separator, String value) {
if (properties.get(key) == null) {
properties.put(key, value);
return;
}
properties.put(key, properties.get(key) + value);
properties.put(key, properties.get(key) + separator + value);
}

public void extend(String key, String value) {
extend(key, ",", value);
}

public String get(String key) {
Expand All @@ -75,4 +83,19 @@ public void replace(File source) {
}
}

/**
* Method used for delegating handling of the options to karaf property file.
*
* @param option Option to be applied for given configuration file.
*/
public void handle(KarafDistributionConfigurationFileOption option) {
if (option instanceof KarafDistributionConfigurationFilePutOption) {
put(option.getKey(), option.getValue());
} else if (option instanceof KarafDistributionConfigurationFileExtendOption) {
extend(option.getKey(), option.getSeparator(), option.getValue());
} else if (option instanceof KarafDistributionConfigurationFileReplacementOption) {
replace(((KarafDistributionConfigurationFileReplacementOption) option).getSource());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,22 @@
import org.ops4j.pax.exam.karaf.container.internal.runner.Runner;
import org.ops4j.pax.exam.karaf.options.DoNotModifyLogOption;
import org.ops4j.pax.exam.karaf.options.ExamBundlesStartLevel;
import org.ops4j.pax.exam.karaf.options.KarafBootFeatureOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionBaseConfigurationOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationConsoleOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFileExtendOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFileOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFilePutOption;
import org.ops4j.pax.exam.karaf.options.KarafDistributionConfigurationFileReplacementOption;
import org.ops4j.pax.exam.karaf.options.KarafExamSystemConfigurationOption;
import org.ops4j.pax.exam.karaf.options.KarafFeatureRepositoryOption;
import org.ops4j.pax.exam.karaf.options.KarafFeaturesOption;
import org.ops4j.pax.exam.karaf.options.KeepRuntimeFolderOption;
import org.ops4j.pax.exam.karaf.options.LogCategoryLevelOption;
import org.ops4j.pax.exam.karaf.options.LogLevelOption;
import org.ops4j.pax.exam.karaf.options.configs.CustomProperties;
import org.ops4j.pax.exam.karaf.options.configs.FeaturesCfg;
import org.ops4j.pax.exam.karaf.options.configs.LoggingCfg;
import org.ops4j.pax.exam.options.BootClasspathLibraryOption;
import org.ops4j.pax.exam.options.BootDelegationOption;
import org.ops4j.pax.exam.options.ProvisionOption;
Expand Down Expand Up @@ -189,6 +193,7 @@ public synchronized TestContainer start() {
String[] fileEndings = new String[] { "jar", "war", "zip", "kar", "xml" };

updateLogProperties(karafHome, subsystem);
updateLogCategoryProperties(karafHome, subsystem);
updateUserSetProperties(karafHome, subsystem);
setupExamProperties(karafHome, subsystem);
makeScriptsInBinExec(karafBin);
Expand Down Expand Up @@ -423,7 +428,7 @@ else if (optionToApply instanceof KarafDistributionConfigurationFileReplacementO
}
else {
karafPropertiesFile
.extend(optionToApply.getKey(), optionToApply.getValue());
.extend(optionToApply.getKey(), optionToApply.getSeparator(), optionToApply.getValue());
}
}
if (!store) {
Expand Down Expand Up @@ -470,18 +475,14 @@ private Collection<? extends KarafDistributionConfigurationFileOption> configure
private Collection<? extends KarafDistributionConfigurationFileOption> extractFileOptionsBasedOnFeaturesScannerOptions(
ExamSystem subsystem) {
ArrayList<KarafDistributionConfigurationFileOption> retVal = Lists.newArrayList();
KarafFeaturesOption[] featuresOptions = subsystem
.getOptions(KarafFeaturesOption.class);
KarafFeaturesOption[] featuresOptions = subsystem.getOptions(KarafFeaturesOption.class);

for (KarafFeaturesOption featuresOption : featuresOptions) {
String url = featuresOption.getURL();
retVal.add(new KarafDistributionConfigurationFileExtendOption(FeaturesCfg.REPOSITORIES,
"," + url));
StringBuilder buffer = new StringBuilder();
retVal.add(new KarafFeatureRepositoryOption(featuresOption.getURL()));

for (String feature : featuresOption.getFeatures()) {
buffer.append(",");
buffer.append(feature);
retVal.add(new KarafBootFeatureOption(feature));
}
retVal.add(new KarafDistributionConfigurationFileExtendOption(FeaturesCfg.BOOT, buffer.toString()));
}
return retVal;
}
Expand All @@ -503,18 +504,31 @@ private void updateLogProperties(File karafHome, ExamSystem _system) throws IOEx
LOGGER.info("Log file should not be modified by the test framework");
return;
}
String realLogLevel = retrieveRealLogLevel(_system);
File customPropertiesFile = new File(karafHome + "/etc/org.ops4j.pax.logging.cfg");
Properties karafPropertyFile = new Properties();
karafPropertyFile.load(new FileInputStream(customPropertiesFile));
karafPropertyFile.put("log4j.rootLogger", realLogLevel + ", out, stdout, osgi:*");
karafPropertyFile.store(new FileOutputStream(customPropertiesFile), "updated by pax-exam");
}

private String retrieveRealLogLevel(ExamSystem _system) {
LogLevelOption[] logLevelOptions = _system.getOptions(LogLevelOption.class);
return logLevelOptions != null && logLevelOptions.length != 0 ? logLevelOptions[0]
.getLogLevel().toString() : "WARN";
if (logLevelOptions != null && logLevelOptions.length == 1) {
KarafPropertiesFile karafPropertyFile = new KarafPropertiesFile(karafHome, LoggingCfg.FILE_PATH);
karafPropertyFile.load();
karafPropertyFile.handle(logLevelOptions[0]);
karafPropertyFile.store();
}
}

private void updateLogCategoryProperties(File karafHome, ExamSystem _system) throws IOException {
DoNotModifyLogOption[] modifyLog = _system.getOptions(DoNotModifyLogOption.class);
if (modifyLog != null && modifyLog.length != 0) {
LOGGER.info("Log file should not be modified by the test framework");
return;
}

KarafPropertiesFile karafPropertyFile = new KarafPropertiesFile(karafHome, LoggingCfg.FILE_PATH);
karafPropertyFile.load();

for (LogCategoryLevelOption option : _system.getOptions(LogCategoryLevelOption.class)) {
karafPropertyFile.handle(option);
}

karafPropertyFile.store();
}

private String[] buildKarafClasspath(File karafHome) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.ops4j.pax.exam.karaf.options;

import org.ops4j.pax.exam.karaf.options.configs.FeaturesCfg;

public class KarafBootFeatureOption extends KarafDistributionConfigurationFileExtendOption {

public KarafBootFeatureOption(String name) {
super(FeaturesCfg.BOOT, name);
}

public KarafBootFeatureOption(String name, String version) {
super(FeaturesCfg.BOOT, name + ";version=" + version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ public abstract class KarafDistributionConfigurationFileOption implements Option
private String configurationFilePath;
private String key;
private String value;
private String separator;

public KarafDistributionConfigurationFileOption(ConfigurationPointer pointer, String value) {
this(pointer.getConfigurationFilePath(), pointer.getKey(), value);
this(pointer.getConfigurationFilePath(), pointer.getKey(), value, null);
}

public KarafDistributionConfigurationFileOption(ConfigurationPointer pointer, String value, String separator) {
this(pointer.getConfigurationFilePath(), pointer.getKey(), value, separator);
}

public KarafDistributionConfigurationFileOption(String configurationFilePath, String key, String value) {
this(configurationFilePath, key, value, null);
}

public KarafDistributionConfigurationFileOption(String configurationFilePath, String key, String value, String separator) {
this.configurationFilePath = configurationFilePath;
this.key = key;
this.value = value;
this.separator = separator;
}

public KarafDistributionConfigurationFileOption(String configurationFilePath) {
Expand All @@ -54,4 +64,8 @@ public String getValue() {
return value;
}

public String getSeparator() {
return separator;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,19 @@ public static Option logLevel(LogLevel logLevel) {
return new LogLevelOption(logLevel);
}

/**
* Specify given log level for category.
*/
public static Option logCategoryLevel(String category, LogLevel logLevel) {
return new LogCategoryLevelOption(category, logLevel);
}

/**
* A very simple and convinient method to set a specific log level without the need of configure
* the specific option itself.
*/
public static LogLevelOption logLevel() {
return new LogLevelOption();
return new LogLevelOption(LogLevel.WARN);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.ops4j.pax.exam.karaf.options;

import org.ops4j.pax.exam.karaf.options.configs.FeaturesCfg;

public class KarafFeatureRepositoryOption extends KarafDistributionConfigurationFileExtendOption {

public KarafFeatureRepositoryOption(String url) {
super(FeaturesCfg.REPOSITORIES, url);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.ops4j.pax.exam.karaf.options;

import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
import org.ops4j.pax.exam.karaf.options.configs.LoggingCfg;

public class LogCategoryLevelOption extends KarafDistributionConfigurationFilePutOption {

public LogCategoryLevelOption(String category, LogLevel level) {
super(LoggingCfg.FILE_PATH, "log4j.logger." + category, level.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@
*/
package org.ops4j.pax.exam.karaf.options;

import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.karaf.options.configs.LoggingCfg;

/**
* While the log-level could also be configured using the config options in the configuration files we also provide a
* more easy option here.
*/
public class LogLevelOption implements Option {
public class LogLevelOption extends KarafDistributionConfigurationFilePutOption {

public static enum LogLevel {
TRACE, DEBUG, INFO, WARN, ERROR
}

public final static String DEFAULT_APPENDERS = "out, stdout, osgi:*";

private LogLevel logLevel;

public LogLevelOption() {
public LogLevelOption(LogLevel level) {
this(level, DEFAULT_APPENDERS);
}

public LogLevelOption(LogLevel logLevel) {
this.logLevel = logLevel;
public LogLevelOption(LogLevel level, String appenders) {
super(LoggingCfg.ROOT_LOGGER, level + "," + appenders);
}

public LogLevelOption logLevel(LogLevel _logLevel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

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.logCategoryLevel;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;

import java.io.File;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
import org.osgi.framework.BundleContext;

@RunWith(PaxExam.class)
public class KarafOptionsTest {

@Inject
private BundleContext bc;

@Configuration
public Option[] config() {
return new Option[] {
karafDistributionConfiguration()
.frameworkUrl(maven("org.apache.karaf", "apache-karaf", "2.3.2").type("zip"))
.karafVersion("2.3.2").useDeployFolder(false).unpackDirectory(new File("target/karaf")),
configureConsole().ignoreLocalConsole().startRemoteShell(),
logLevel(LogLevel.INFO),
logCategoryLevel("org.apache.aries", LogLevel.ERROR),
logCategoryLevel("org.apache.felix", LogLevel.ERROR),
};
}

@Test
public void checkKarafSystemService() throws Exception {
assertThat(bc, is(notNullValue()));
}

}
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@
<name>OPS4J Pax Exam (Reactor POM)</name>

<profiles>
<profile>
<id>core</id>

<modules>
<module>build/pax-exam-checkstyle-rules</module>
<module>pom</module>
<module>core/pax-exam</module>
<module>core/pax-exam-spi</module>
<module>core/pax-exam-extender-service</module>
<module>core/pax-exam-inject</module>
<module>core/pax-exam-invoker-junit</module>
<module>core/pax-exam-invoker-servlet</module>
<module>core/pax-exam-servlet-bridge</module>
<module>core/pax-exam-cdi</module>
<module>core/pax-exam-spring</module>
<module>core/pax-exam-cm</module>
<module>core/pax-exam-link-mvn</module>
<module>core/pax-exam-link-assembly</module>
<module>core/pax-exam-container-rbc</module>
<module>core/pax-exam-container-rbc-client</module>
</modules>

</profile>


<profile>
<id>default</id>
<activation>
Expand Down