Commit 9c7c784e authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub

fix(h2Console) improve logs when opening h2 console (#290)


fixes BS-16214
parent 67390399
......@@ -48,7 +48,11 @@ Require-Bundle: org.bonitasoft.studio.engine;bundle-version="1.0.0",
org.eclipse.jdt.launching,
org.eclipse.e4.ui.model.workbench;bundle-version="1.1.0.v20140512-1820",
org.eclipse.e4.core.di;bundle-version="1.4.0",
org.eclipse.e4.core.di.extensions;bundle-version="0.12.0"
org.eclipse.e4.core.di.extensions;bundle-version="0.12.0",
org.eclipse.core.filesystem,
org.eclipse.debug.core,
org.eclipse.core.externaltools;bundle-version="1.0.300",
org.eclipse.debug.ui
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Import-Package: org.bonitasoft.engine.api,
......
......@@ -15,17 +15,25 @@
package org.bonitasoft.studio.businessobject.ui.handler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.eclipse.core.externaltools.internal.IExternalToolConstants;
import org.eclipse.core.resources.IContainer;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
......@@ -41,19 +49,31 @@ public class OpenH2ConsoleHandlerTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Mock
private Runtime runtime;
@Mock
private RepositoryAccessor repositoryAccessor;
@Mock
private Process process;
private ILaunchManager launchManager;
@Spy
private OpenH2ConsoleHandler openH2ConsoleHandler;
@Mock
private ILaunchConfigurationType lanchConfigurationType;
@Mock
private ILaunchConfigurationWorkingCopy workingCopy;
@Before
public void setUp() throws Exception {
doReturn("/test/h2_db").when(openH2ConsoleHandler).pathToDBFolder(repositoryAccessor);
doReturn(rootFile()).when(openH2ConsoleHandler).rootFile(repositoryAccessor);
doReturn(launchManager).when(openH2ConsoleHandler).getLaunchManager();
doReturn("/usr/bin/java").when(openH2ConsoleHandler).javaBinaryLocation();
when(launchManager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE))
.thenReturn(lanchConfigurationType);
when(lanchConfigurationType.newInstance(any(IContainer.class), notNull(String.class))).thenReturn(workingCopy);
}
@Test
......@@ -69,40 +89,30 @@ public class OpenH2ConsoleHandlerTest {
@Test
public void should_throw_FileNotFoundException_if_h2_jar_is_missing() throws Exception {
doReturn(new File(OpenH2ConsoleHandlerTest.class.getResource("/workspaceWithoutH2").toURI().toURL().getFile())).when(openH2ConsoleHandler)
doReturn(new File(OpenH2ConsoleHandlerTest.class.getResource("/workspaceWithoutH2").toURI().toURL().getFile()))
.when(openH2ConsoleHandler)
.rootFile(repositoryAccessor);
expectedException.expect(FileNotFoundException.class);
openH2ConsoleHandler.locateH2jar(repositoryAccessor);
}
@Test
public void should_destroy_processes_on_shutdown() throws Exception {
doReturn(runtime).when(openH2ConsoleHandler).getRuntime();
doReturn(process).when(runtime).exec(anyString());
doReturn(rootFile()).when(openH2ConsoleHandler).rootFile(repositoryAccessor);
doReturn("h2.jar").when(openH2ConsoleHandler).locateH2jar(repositoryAccessor);
openH2ConsoleHandler.execute(repositoryAccessor);
verify(runtime).addShutdownHook(notNull(Thread.class));
}
@Test
public void should_execute_java_command() throws Exception {
doReturn(runtime).when(openH2ConsoleHandler).getRuntime();
public void should_build_java_command() throws Exception {
final File logFile = new File("");
doReturn(logFile).when(openH2ConsoleHandler).logFile();
doReturn(rootFile()).when(openH2ConsoleHandler).rootFile(repositoryAccessor);
doReturn("h2.jar").when(openH2ConsoleHandler).locateH2jar(repositoryAccessor);
openH2ConsoleHandler.execute(repositoryAccessor);
final ArgumentCaptor<String[]> argument = ArgumentCaptor.forClass(String[].class);
verify(runtime).exec(argument.capture());
assertThat(argument.getValue()).contains("-webPort", "java", "-jar", "h2.jar", "-browser", "-tcp", "-user", "sa",
"-url", "jdbc:h2:file:/test/h2_db/business_data.db;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;IGNORECASE=TRUE;AUTO_SERVER=TRUE;", "-driver",
"org.h2.Driver");
verify(workingCopy).setAttribute(IExternalToolConstants.ATTR_LOCATION, "/usr/bin/java");
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(workingCopy).setAttribute(eq(IExternalToolConstants.ATTR_TOOL_ARGUMENTS), captor.capture());
assertThat(captor.getValue()).contains("-jar h2.jar -browser -webPort",
"-tcp -user sa -url jdbc:h2:file:/test/h2_db/business_data.db;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;IGNORECASE=TRUE;AUTO_SERVER=TRUE; -driver org.h2.Driver");
verify(workingCopy).launch("run", Repository.NULL_PROGRESS_MONITOR);
}
}
......@@ -22,66 +22,88 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.externaltools.internal.IExternalToolConstants;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.SocketUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicate;
public class OpenH2ConsoleHandler{
public class OpenH2ConsoleHandler {
private static final String URL = "jdbc:h2:file:%s/business_data.db;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;IGNORECASE=TRUE;AUTO_SERVER=TRUE;";
private static final String DRIVER = "org.h2.Driver";
private static final String USER = "sa";
private static final int PORT = SocketUtil.findFreePort();
@Execute
public void execute(final RepositoryAccessor repositoryAccessor) throws ExecutionException {
final ILaunchManager manager = getLaunchManager();
final ILaunchConfigurationType ltype = manager
.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE);
try {
final String h2JarPath = locateH2jar(repositoryAccessor);
final Process process = getRuntime()
.exec(new String[] {
"java",
"-jar",
h2JarPath,
"-browser",
"-webPort",
String.valueOf(PORT),
"-tcp",
"-user",
USER,
"-url",
String.format(URL, pathToDBFolder(repositoryAccessor)),
"-driver",
DRIVER });
getRuntime().addShutdownHook(exitProcessHook(process));
} catch (final IOException e) {
throw new ExecutionException("Failed to locate h2 jar", e);
final ILaunchConfigurationWorkingCopy workingCopy = ltype.newInstance(null, "Open h2 web console");
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, javaBinaryLocation());
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS,
Joiner.on(" ").join(buildCommand(repositoryAccessor)));
workingCopy.setAttribute(IDebugUIConstants.ATTR_APPEND_TO_FILE, true);
workingCopy.setAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_FILE, logFile().getAbsolutePath());
workingCopy.launch("run", Repository.NULL_PROGRESS_MONITOR);
} catch (final CoreException | IOException e) {
BonitaStudioLog.error("Failed to run h2 console", e);
}
}
protected String pathToDBFolder(final RepositoryAccessor repositoryAccessor) {
return repositoryAccessor.getCurrentRepository().getDatabaseHandler().getDBLocation().getAbsolutePath();
protected String javaBinaryLocation() {
return Paths.get(JavaRuntime.getDefaultVMInstall().getInstallLocation().getAbsolutePath(), "bin", "java")
.toString();
}
private Thread exitProcessHook(final Process process) {
return new Thread() {
protected ILaunchManager getLaunchManager() {
return DebugPlugin.getDefault().getLaunchManager();
}
@Override
public void run() {
if (process != null) {
process.destroy();
}
}
};
protected List<String> buildCommand(final RepositoryAccessor repositoryAccessor) throws IOException {
return Arrays.asList(
"-jar",
locateH2jar(repositoryAccessor),
"-browser",
"-webPort",
String.valueOf(PORT),
"-tcp",
"-user",
USER,
"-url",
String.format(URL, pathToDBFolder(repositoryAccessor)),
"-driver",
DRIVER);
}
protected File logFile() throws CoreException {
final IFileStore fileStore = EFS.getLocalFileSystem().getStore(Platform.getLogFileLocation());
return fileStore.toLocalFile(EFS.NONE, Repository.NULL_PROGRESS_MONITOR);
}
protected Runtime getRuntime() {
return Runtime.getRuntime();
protected String pathToDBFolder(final RepositoryAccessor repositoryAccessor) {
return repositoryAccessor.getCurrentRepository().getDatabaseHandler().getDBLocation().getAbsolutePath();
}
protected String locateH2jar(RepositoryAccessor repositoryAccessor) throws IOException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment