blob: 1e85e84e5dd611ff1c645053a5620027fcf3599a [file] [log] [blame]
package org.eclipse.epp.logging.aeri.ide.manual;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.List;
import java.util.Map.Entry;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.epp.logging.aeri.tests.scenarios.Scenario;
import org.eclipse.epp.logging.aeri.tests.scenarios.ScenarioParser;
import org.eclipse.epp.logging.aeri.tests.util.StackTraceParser;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import com.google.common.base.Strings;
import com.google.common.io.Files;
public class TestScenariosContribution extends org.eclipse.ui.menus.WorkbenchWindowControlContribution {
private ILog log = Platform.getLog(FrameworkUtil.getBundle(getClass()));
public static final String PROP_SCENARIOS_DIR = "aeri.test.scenarios.dir";
public TestScenariosContribution() {
}
public TestScenariosContribution(String id) {
super(id);
}
@Override
protected Control createControl(Composite parent) {
ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
toolItem.setText("Log...");
toolItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Rectangle rect = ((ToolItem) e.getSource()).getBounds();
Point pt = new Point(rect.x, rect.y + rect.height);
pt = toolBar.toDisplay(pt);
Menu menu = new Menu(toolBar);
hookFillMenu(menu);
menu.setLocation(pt.x, pt.y);
menu.setVisible(true);
}
});
return toolBar;
}
private void hookFillMenu(Menu menu) {
File scenariosDir = getScenariosDir();
try {
hookDirectoryScenarios(menu, scenariosDir);
} catch (IOException e) {
e.printStackTrace();
}
new MenuItem(menu, SWT.SEPARATOR);
MenuItem menuItemCategory = new MenuItem(menu, SWT.CASCADE);
menuItemCategory.setText("Open scenarios directory\u2026");
menuItemCategory.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Program.launch(scenariosDir.getAbsolutePath());
}
});
new MenuItem(menu, SWT.SEPARATOR);
hookPredefinedScenarios(menu);
}
private File getScenariosDir() {
String scenariosDirProperty = System.getProperty(PROP_SCENARIOS_DIR);
File scenariosDir = null;
if (scenariosDirProperty != null) {
scenariosDir = new File(scenariosDirProperty);
if (!scenariosDir.exists() && !scenariosDir.mkdirs()) {
throw new IllegalStateException("Could not create test scenarios directory " + scenariosDir);
}
}
if (scenariosDir == null || !scenariosDir.isDirectory()) {
Bundle bundle = FrameworkUtil.getBundle(getClass());
scenariosDir = Platform.getStateLocation(bundle).toFile();
}
return scenariosDir;
}
private void hookDirectoryScenarios(Menu menu, File scenarioDirectory) throws IOException {
if (isEmptyPluginStateLocation(scenarioDirectory)) {
copyTestResourcesScenarios(scenarioDirectory);
}
ScenarioParser parser = new ScenarioParser();
for (File f : scenarioDirectory.listFiles()) {
List<Scenario> scenarios = parser.parseScenarios(f);
if (scenarios.isEmpty()) {
continue;
}
MenuItem menuItemCategory = new MenuItem(menu, SWT.CASCADE);
menuItemCategory.setText(f.getName());
Menu menuScenarios = new Menu(menu);
for (Scenario scenario : scenarios) {
addScenarioMenuItem(scenario, menuScenarios);
}
menuItemCategory.setMenu(menuScenarios);
}
}
private boolean isEmptyPluginStateLocation(File dir) {
return System.getProperty(PROP_SCENARIOS_DIR) == null && dir.listFiles().length == 0;
}
private void hookPredefinedScenarios(Menu menu) {
MenuItem defaultScenarios = new MenuItem(menu, SWT.CASCADE);
defaultScenarios.setText("Hardcoded");
Menu scenarios = new Menu(menu);
for (Entry<String, Runnable> entry : TestScenarios.TEST_SCENARIOS.entrySet()) {
MenuItem menuItem = new MenuItem(scenarios, SWT.NONE);
menuItem.setText(entry.getKey());
menuItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ensureBuildIdIsSet();
entry.getValue().run();
}
});
}
defaultScenarios.setMenu(scenarios);
}
private void copyTestResourcesScenarios(File toDirectory) {
Bundle bundle = Platform.getBundle(org.eclipse.epp.logging.aeri.tests.Constants.BUNDLE_ID);
Enumeration<String> entryPaths = bundle.getEntryPaths("/resources/");
while (entryPaths.hasMoreElements()) {
String entryPath = entryPaths.nextElement();
URL entryUrl = bundle.getEntry(entryPath);
try {
URL fileUrl = FileLocator.toFileURL(entryUrl);
URI resolved = new URI(fileUrl.getProtocol(), fileUrl.getPath(), null);
File file = new File(resolved);
Files.copy(file, new File(toDirectory, file.getName()));
} catch (URISyntaxException e) {
e.printStackTrace();
continue;
} catch (IOException e) {
e.printStackTrace();
continue;
}
}
}
private void addScenarioMenuItem(Scenario scenario, Menu menuScenarios) {
String name = scenario.getName();
if (name.equals("---")) {
new MenuItem(menuScenarios, SWT.SEPARATOR);
} else if (name.startsWith("-")) {
MenuItem menuItem = new MenuItem(menuScenarios, SWT.NONE);
menuItem.setText(name);
menuItem.setEnabled(false);
} else {
MenuItem menuItemScenario = new MenuItem(menuScenarios, SWT.NONE);
menuItemScenario.setText(name);
String description = Strings.emptyToNull(scenario.getDescription());
if (description != null) {
menuItemScenario.setToolTipText(description);
}
menuItemScenario.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
executeScenario(scenario);
}
});
}
}
private void executeScenario(Scenario scenario) {
ensureBuildIdIsSet();
new Job("log error") {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
Throwable throwable = StackTraceParser.parse(scenario.getStackTrace());
Status status = new Status(IStatus.ERROR, scenario.getLoggingPlugin(), scenario.getName(), throwable);
log.log(status);
} catch (Exception e) {
Display.getDefault().asyncExec(() -> MessageDialog.openError(null, "Scenario failed",
MessageFormat.format("Scenario \u2018{0}\u2019 failed with exception: {1}", scenario.getName(), e)));
}
return Status.OK_STATUS;
}
}.schedule();
}
/**
* logged statuses are filtered if no build id is set (e.g. in a runtime Eclipse)
*/
private void ensureBuildIdIsSet() {
if (System.getProperty("eclipse.buildId") == null) {
System.setProperty("eclipse.buildId", "unit-tests");
}
}
}