Commit 0278e671 authored by Adrien's avatar Adrien Committed by Romain Bioteau
Browse files

tr(tests) remove file leaks in tmp folder (#306)

parent d29b91f9
......@@ -4,6 +4,5 @@
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src-test/java"/>
<classpathentry exported="true" kind="lib" path="lib/mvel2-2.2.1.Final.jar" sourcepath="C:/Users/Romain/.m2/repository/org/mvel/mvel2/2.2.1.Final/mvel2-2.2.1.Final-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -23,7 +23,9 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.9.0",
org.bonitasoft.studio.expression.editor;bundle-version="6.4.0",
org.codehaus.groovy.eclipse.ui;bundle-version="2.8.0",
org.eclipse.ui.ide,
org.eclipse.e4.ui.workbench;bundle-version="1.2.1",
org.eclipse.jdt.groovy.core,
org.bonitasoft.studio.businessobject,
org.codehaus.groovy.eclipse.codeassist.completion;bundle-version="2.8.0",
org.eclipse.jdt;bundle-version="3.9.0",
org.codehaus.groovy,
......@@ -68,5 +70,4 @@ Import-Package: org.bonitasoft.engine.bdm,
org.bonitasoft.engine.bpm.process.impl,
org.bonitasoft.engine.expression,
org.bonitasoft.engine.operation
Bundle-ClassPath: lib/mvel2-2.2.1.Final.jar,
.
Bundle-ClassPath: .
......@@ -5,6 +5,5 @@ bin.includes = META-INF/,\
plugin.xml,\
plugin*.properties,\
messages*.properties,\
icons/,\
lib/
icons/
src.excludes = src-test/java/
......@@ -51,7 +51,7 @@ public class AddBpmnBarResourceRunnable implements RunnableWithResult<List<BarRe
private final BusinessArchiveBuilder builder;
private final AbstractProcess process;
final List<BarResource> res = new ArrayList<BarResource>();
final List<BarResource> res = new ArrayList<>();
public AddBpmnBarResourceRunnable(final BusinessArchiveBuilder builder, final AbstractProcess process) {
this.builder = builder;
......@@ -109,7 +109,8 @@ public class AddBpmnBarResourceRunnable implements RunnableWithResult<List<BarRe
protected MainProcessEditPart createMainEditPart(Diagram diagramFor, final Resource eResourceuseless) {
final ResourceSet rSet = new ResourceSetImpl();
final TransactionalEditingDomain editingDomain = CustomDiagramEditingDomainFactory.getInstance().createEditingDomain(rSet);
final TransactionalEditingDomain editingDomain = CustomDiagramEditingDomainFactory.getInstance()
.createEditingDomain(rSet);
final Resource resource = rSet.createResource(diagramFor.eResource().getURI());
try {
resource.load(rSet.getLoadOptions());
......
......@@ -5,14 +5,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.importer.bar.processor;
......@@ -27,41 +25,45 @@ import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
* @author Aurelien Pupier
*
*/
public class BarReaderUtilTest {
@Test
public void findCustomConnectorClassNameTestWithEmptyFileDontThrowException() throws ZipException, IOException{
final File emptyFile = File.createTempFile("testEmptyFile", "");
BarReaderUtil.findCustomConnectorClassName(emptyFile);
emptyFile.delete();
}
@Test
public void findSeveralConnectorClassName() throws IOException {
final File zipFile = getZipFileToTest();
List<String> foundCustomConnectorClassName = BarReaderUtil.findCustomConnectorClassName(zipFile);
assertThat(foundCustomConnectorClassName).containsExactly("name1", "name2");
}
private File getZipFileToTest() throws IOException, FileNotFoundException {
InputStream inputStream = BarReaderUtilTest.class.getClassLoader().getResourceAsStream("zipWithSeveralPropertyFile.zip");
final File zipFile = File.createTempFile("zipWithSeveralPropertyFile", "zip");
OutputStream outputStream = new FileOutputStream(zipFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
return zipFile;
}
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void findCustomConnectorClassNameTestWithEmptyFileDontThrowException() throws ZipException, IOException {
final File emptyFile = tempFolder.newFile("testEmptyFile");
BarReaderUtil.findCustomConnectorClassName(emptyFile);
}
@Test
public void findSeveralConnectorClassName() throws IOException {
final File zipFile = getZipFileToTest();
final List<String> foundCustomConnectorClassName = BarReaderUtil.findCustomConnectorClassName(zipFile);
assertThat(foundCustomConnectorClassName).containsExactly("name1", "name2");
}
private File getZipFileToTest() throws IOException, FileNotFoundException {
final InputStream inputStream = BarReaderUtilTest.class.getClassLoader()
.getResourceAsStream("zipWithSeveralPropertyFile.zip");
final File zipFile = tempFolder.newFile("zipWithSeveralPropertyFile.zip");
final OutputStream outputStream = new FileOutputStream(zipFile);
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
return zipFile;
}
}
......@@ -28,7 +28,9 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
......@@ -43,6 +45,9 @@ public class BosArchiveProcessorTest {
@Mock
private ImportBosArchiveOperation ibo;
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
@Before
public void init() throws Exception {
Mockito.doCallRealMethod().when(bap).getStatus();
......@@ -55,7 +60,7 @@ public class BosArchiveProcessorTest {
public void testGetStatusOkBasic() throws MalformedURLException, IOException, Exception {
mockStatus(IStatus.OK);
bap.createDiagram(File.createTempFile("test", "test").toURI().toURL(), new NullProgressMonitor());
bap.createDiagram(tmpFolder.newFile("test").toURI().toURL(), new NullProgressMonitor());
assertThat(bap.getStatus().isOK()).isTrue();
}
......@@ -64,7 +69,7 @@ public class BosArchiveProcessorTest {
public void testGetStatusErrorForValidationOKForRun() throws MalformedURLException, IOException, Exception {
mockStatus(IStatus.ERROR);
bap.createDiagram(File.createTempFile("test", "test").toURI().toURL(), new NullProgressMonitor());
bap.createDiagram(tmpFolder.newFile("test").toURI().toURL(), new NullProgressMonitor());
assertThat(bap.getStatus().isOK()).isFalse();
}
......@@ -73,7 +78,7 @@ public class BosArchiveProcessorTest {
public void testGetStatusErrorForValidationErrorForRun() throws MalformedURLException, IOException, Exception {
mockStatus(IStatus.ERROR);
bap.createDiagram(File.createTempFile("test", "test").toURI().toURL(), new NullProgressMonitor());
bap.createDiagram(tmpFolder.newFile("test").toURI().toURL(), new NullProgressMonitor());
assertThat(bap.getStatus().isOK()).isFalse();
}
......@@ -82,7 +87,7 @@ public class BosArchiveProcessorTest {
public void testGetStatusOkForValidationErrorForRun() throws MalformedURLException, IOException, Exception {
mockStatus(IStatus.ERROR);
bap.createDiagram(File.createTempFile("test", "test").toURI().toURL(), new NullProgressMonitor());
bap.createDiagram(tmpFolder.newFile("test").toURI().toURL(), new NullProgressMonitor());
assertThat(bap.getStatus().isOK()).isFalse();
}
......
......@@ -160,6 +160,7 @@ public class JBPM3ToProc extends ToProcProcessor {
builder = new ProcBuilder(progressMonitor);
result = File.createTempFile(diagramName, ".proc") ;
result.deleteOnExit();
builder.createDiagram(diagramName, diagramName, "1.0", result);
try {
importFromJBPM(processDef);
......
......@@ -5,14 +5,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.properties.sections.forms;
......@@ -69,7 +67,6 @@ import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
/**
*
* @author Charles Souillard
* @author Aurelien Pupier
* @author Baptiste Mesta
......@@ -77,13 +74,14 @@ import org.eclipse.ui.PlatformUI;
public class FormsUtils {
public static enum WidgetEnum {
TEXT, TEXT_AREA, COMBO, CHECKBOX,CHECKBOX_LIST, DATE, LIST, PASSWORD, RADIO, SELECT, FILE
TEXT, TEXT_AREA, COMBO, CHECKBOX, CHECKBOX_LIST, DATE, LIST, PASSWORD, RADIO, SELECT, FILE
};
public static void duplicateForm(final Element pageFlow, final TransactionalEditingDomain editingDomain, final EStructuralFeature feature, final Form baseForm, final String id, final String formDesc) {
public static void duplicateForm(final Element pageFlow, final TransactionalEditingDomain editingDomain,
final EStructuralFeature feature, final Form baseForm, final String id, final String formDesc) {
try {
OperationHistoryFactory.getOperationHistory().execute(new DuplicateFormCommand(pageFlow, feature, baseForm, id, formDesc, editingDomain),
OperationHistoryFactory.getOperationHistory().execute(
new DuplicateFormCommand(pageFlow, feature, baseForm, id, formDesc, editingDomain),
Repository.NULL_PROGRESS_MONITOR, null);
} catch (final ExecutionException e) {
BonitaStudioLog.error(e);
......@@ -94,16 +92,18 @@ public class FormsUtils {
* create the diagram of the form and put it in the same resource file
*
* @param form
* create the diagram corresponding to this form
* create the diagram corresponding to this form
* @return created diagram
*/
public static void createFormDiagram(final Form form, final TransactionalEditingDomain editingDomain) {
final AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, "", null) { //$NON-NLS-1$
@Override
protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
final Diagram diagram = ViewService.createDiagram(form, FormEditPart.MODEL_ID, FormDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
final Resource resource = getEditingDomain().getResourceSet().getResource(form.eResource().getURI(), false);
protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, final IAdaptable info)
throws ExecutionException {
final Diagram diagram = ViewService.createDiagram(form, FormEditPart.MODEL_ID,
FormDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
final Resource resource = getEditingDomain().getResourceSet().getResource(form.eResource().getURI(), false);
resource.getContents().add(diagram);
diagram.persist();
diagram.setElement(form);
......@@ -119,17 +119,16 @@ public class FormsUtils {
}
}
/**
* open the diagram corresponding to the form
*
* @param form
* the form to open
* the form to open
*/
public static DiagramEditor openDiagram(final Form form,final EditingDomain domain) {
public static DiagramEditor openDiagram(final Form form, final EditingDomain domain) {
/* get the Diagram element related to the form in the resource */
final Diagram diag = ModelHelper.getDiagramFor(form,domain);
final Diagram diag = ModelHelper.getDiagramFor(form, domain);
/*
* need to get the URI after save because the name can change as it is
......@@ -138,7 +137,8 @@ public class FormsUtils {
final URI uri = EcoreUtil.getURI(diag);
/* open the form editor */
final FormDiagramEditor formEditor = (FormDiagramEditor) EditorService.getInstance().openEditor(new URIEditorInput(uri, form.getName()));
final FormDiagramEditor formEditor = (FormDiagramEditor) EditorService.getInstance()
.openEditor(new URIEditorInput(uri, form.getName()));
formEditor.getDiagramGraphicalViewer().select(formEditor.getDiagramEditPart());
if (form != null && formEditor != null) {
final MainProcess diagram = ModelHelper.getMainProcess(form);
......@@ -171,7 +171,8 @@ public class FormsUtils {
}
}
protected static void ensureAdaptersForDeletionOrRenamingWellSetted(final Form form, final FormDiagramEditor formEditor) {
protected static void ensureAdaptersForDeletionOrRenamingWellSetted(final Form form,
final FormDiagramEditor formEditor) {
final EObject parent = form.eContainer();
if (parent instanceof PageFlow) {
parent.eAdapters().add(new FormRemovedAdapter(form, formEditor));
......@@ -196,15 +197,14 @@ public class FormsUtils {
* @param name
*/
public static void changeIdInTemplate(final Form form, final String oldId, final String newId) {
final HtmlTemplateGenerator generator = (HtmlTemplateGenerator) ExporterService.getInstance().getExporterService(SERVICE_TYPE.HtmlTemplateGenerator);
final HtmlTemplateGenerator generator = (HtmlTemplateGenerator) ExporterService.getInstance()
.getExporterService(SERVICE_TYPE.HtmlTemplateGenerator);
final File file = WebTemplatesUtil.getFile(form.getHtmlTemplate().getPath());
FileInputStream fis;
try {
fis = new FileInputStream(file);
final File tempFile = File.createTempFile("tempForm", ".html");
File tempFile = null;
try (FileInputStream fis = new FileInputStream(file)) {
tempFile = File.createTempFile("tempForm", ".html");
final FileWriter fileWriter = new FileWriter(tempFile);
generator.changeDivId(oldId, newId, fis, fileWriter);
fis.close();
fileWriter.close();
FileUtil.copy(tempFile, file);
WebTemplatesUtil.refreshFile(form.getHtmlTemplate().getPath());
......@@ -214,12 +214,15 @@ public class FormsUtils {
@Override
public void run() {
new BonitaErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.Error, "Unexpected error", e).open();
new BonitaErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.Error,
"Unexpected error", e).open();
}
});
} finally {
if (tempFile != null) {
tempFile.delete();
}
}
}
}
......@@ -5,14 +5,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.properties.sections.forms.adapters;
......@@ -43,6 +41,7 @@ import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
public class WidgetAddedOrRemoved extends AdapterImpl {
private final Form form;
private final DefaultElementNameProvider elementNameProvider;
......@@ -69,12 +68,15 @@ public class WidgetAddedOrRemoved extends AdapterImpl {
}
private void handleRemoveFormWidgetNotification() {
if (form.getHtmlTemplate() != null && form.getHtmlTemplate().getPath() != null && !form.getHtmlTemplate().getPath().isEmpty()) {
if (form.getHtmlTemplate() != null && form.getHtmlTemplate().getPath() != null
&& !form.getHtmlTemplate().getPath().isEmpty()) {
// there is a template
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.widgetRemovedWarning_title, Messages.widgetRemovedWarning_msg);
MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
Messages.widgetRemovedWarning_title, Messages.widgetRemovedWarning_msg);
}
});
}
......@@ -85,18 +87,18 @@ public class WidgetAddedOrRemoved extends AdapterImpl {
if (ModelHelper.formIsCustomized(form)) {
BonitaStudioLog.info("Updating Custom Form Template. Adding widget: " + widget.getName(), Activator.PLUGIN_ID);
final File file = WebTemplatesUtil.getFile(form.getHtmlTemplate().getPath());
FileInputStream fis;
try {
fis = new FileInputStream(file);
final File tempFile = File.createTempFile("tempForm", ".html");
File tempFile = null;
try (FileInputStream fis = new FileInputStream(file)) {
tempFile = File.createTempFile("tempForm", ".html");
final FileWriter fileWriter = new FileWriter(tempFile);
addDivForAddedWidget(widget, fis, fileWriter);
fis.close();
fileWriter.close();
FileUtil.copy(tempFile, file);
WebTemplatesUtil.refreshFile(form.getHtmlTemplate().getPath());
} catch (final Exception e) {
handleExceptionWhileAddingWidgetInTemplate(e);
} finally {
tempFile.delete();
}
}
}
......@@ -104,15 +106,19 @@ public class WidgetAddedOrRemoved extends AdapterImpl {
private void handleExceptionWhileAddingWidgetInTemplate(final Exception e) {
BonitaStudioLog.error(e);
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
new BonitaErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.Error, "Unexpected error", e).open();
new BonitaErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.Error,
"Unexpected error", e).open();
}
});
}
private void addDivForAddedWidget(final Widget widget, final FileInputStream fis, final FileWriter fileWriter) throws IOException {
final HtmlTemplateGenerator generator = (HtmlTemplateGenerator) ExporterService.getInstance().getExporterService(SERVICE_TYPE.HtmlTemplateGenerator);
private void addDivForAddedWidget(final Widget widget, final FileInputStream fis, final FileWriter fileWriter)
throws IOException {
final HtmlTemplateGenerator generator = (HtmlTemplateGenerator) ExporterService.getInstance()
.getExporterService(SERVICE_TYPE.HtmlTemplateGenerator);
String label = elementNameProvider.getNameFor(widget);
label = NamingUtils.convertToId(label);
int number = NamingUtils.getMaxElements(form, label);
......@@ -120,4 +126,4 @@ public class WidgetAddedOrRemoved extends AdapterImpl {
label += number;
generator.addDivInTemplate(label, fis, fileWriter);
}
}
\ No newline at end of file
}
/**
* Copyright (C) 2012 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.actors.tests.SWTbot;
......@@ -29,14 +26,12 @@ import org.bonitasoft.studio.connector.model.definition.Input;
import org.bonitasoft.studio.connector.model.i18n.Messages;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swtbot.eclipse.gef.finder.SWTGefBot;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.keyboard.Keyboard;
import org.eclipse.swtbot.swt.finder.keyboard.KeyboardFactory;
import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.waits.ICondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.junit.Before;
import org.junit.Test;
......@@ -44,14 +39,13 @@ import org.junit.runner.RunWith;
/**
* @author Aur�lie Zara
*
*/
@RunWith(SWTBotJunit4ClassRunner.class)
public class ActorFilterDefinitionTest {
public class ActorFilterDefinitionTest {
private SWTGefBot bot = new SWTGefBot();
@Before
public void activateActorFilterDefinitonShell() {
SWTBotActorFilterUtil.activateActorFilterDefinitionShell(bot);
......@@ -101,23 +95,7 @@ public class ActorFilterDefinitionTest {
activateActorFilterDefinitonShell();
testButtonDisabled(id, textLabel);
bot.textWithLabel(textLabelVersion).setText(version);
bot.waitUntil(new ICondition() {
@Override
public boolean test() throws Exception {
return bot.button(IDialogConstants.FINISH_LABEL).isEnabled();
}
@Override
public void init(SWTBot bot) {
}
@Override
public String getFailureMessage() {
return "changing version should enable finish button";
}
}, 5000);
bot.waitUntil(Conditions.widgetIsEnabled(bot.button(IDialogConstants.FINISH_LABEL)), 5000);
bot.button(IDialogConstants.CANCEL_LABEL).click();
......@@ -149,8 +127,7 @@ public class ActorFilterDefinitionTest {
bot.treeWithLabel(Messages.categoryLabel).select(categoryId);
bot.button(IDialogConstants.FINISH_LABEL).click();
ActorFilterDefRepositoryStore store = (ActorFilterDefRepositoryStore) RepositoryManager
.getInstance().getRepositoryStore(
ActorFilterDefRepositoryStore.class);