io.fabric8.runtime.itests.support.CommandSupport Maven / Gradle / Ivy
/**
* Copyright 2005-2014 Red Hat, Inc.
*
* Red Hat 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 io.fabric8.runtime.itests.support;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.felix.gogo.commands.basic.AbstractCommand;
import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Function;
import org.jboss.gravia.runtime.ModuleContext;
import org.jboss.gravia.runtime.RuntimeLocator;
import org.jboss.gravia.runtime.RuntimeType;
import org.junit.Assert;
/**
* Test helper utility
*
* @since 03-Feb-2014
*/
public final class CommandSupport {
// Hide ctor
private CommandSupport() {
}
public static String executeCommands(String... commands) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
CommandSession commandSession = getCommandSession(printStream);
for (String cmdstr : commands) {
System.out.println(cmdstr);
executeCommand(cmdstr, commandSession);
}
printStream.flush();
String result = baos.toString();
System.out.println(result);
return result;
}
public static String executeCommand(String cmdstr) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
System.out.println(cmdstr);
CommandSession commandSession = getCommandSession(printStream);
executeCommand(cmdstr, commandSession);
printStream.flush();
String result = baos.toString();
System.out.println(result);
return result;
}
private static CommandSession getCommandSession(PrintStream printStream) {
CommandSession commandSession;
if (RuntimeType.getRuntimeType() == RuntimeType.KARAF) {
ModuleContext moduleContext = RuntimeLocator.getRequiredRuntime().getModuleContext();
CommandProcessor commandProcessor = ServiceLocator.awaitService(moduleContext, CommandProcessor.class);
commandSession = commandProcessor.createSession(System.in, printStream, printStream);
commandSession.put("APPLICATION", System.getProperty("karaf.name"));
commandSession.put("USER", "karaf");
} else {
commandSession = new SessionSupport(System.in, printStream) {
@Override
public Object execute(CharSequence cmdstr) throws Exception {
List tokens = Arrays.asList(cmdstr.toString().split("\\s"));
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy