Commit d1003962 authored by Romain Bioteau's avatar Romain Bioteau
Browse files

BS-11266

Fix preference store loading issue
remove duplicated code
add unit test
parent 0afd80dc
......@@ -3,5 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src-test/java"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -19,7 +19,10 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.ide;bundle-version="3.7.0",
org.bonitasoft.studio.connectors.model.edit;bundle-version="1.0.0",
org.eclipse.core.net;bundle-version="1.2.100",
org.eclipse.ui.net;bundle-version="1.2.100"
org.eclipse.ui.net;bundle-version="1.2.100",
org.junit;bundle-version="4.11.0";resolution:=optional,
org.mockito;bundle-version="1.9.5";resolution:=optional,
assertj-core;bundle-version="1.5.0";resolution:=optional
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Bundle-Localization: plugin
......
......@@ -5,3 +5,4 @@ bin.includes = META-INF/,\
plugin.xml,\
messages.properties,\
plugin.properties
src.excludes = src-test/java/
/**
* Copyright (C) 2014 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
* 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/>.
*/
package org.bonitasoft.studio.preferences.pages;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bonitasoft.studio.preferences.extension.IPreferenceFieldEditorContribution;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
* @author Romain Bioteau
*
*/
@RunWith(MockitoJUnitRunner.class)
public class AbstractBonitaPreferencePageTest {
@Mock
private AbstractBonitaPreferencePage preferencePage;
@Mock
private IWorkbench workbench;
@Mock
private FieldEditor aFieldEditor;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_createPreferenceEditorContributions_call_createFieldEditors_for_matching_ids() throws Exception {
final List<IPreferenceFieldEditorContribution> contributions = listOfContribWithIds("myContribId");
when(preferencePage.getFieldEditorContibutions()).thenReturn(contributions);
doCallRealMethod().when(preferencePage).createPreferenceEditorContributions(anyString());
preferencePage.createPreferenceEditorContributions("myContribId");
verify(contributions.get(0)).createFieldEditors(any(Composite.class));
}
@Test
public void should_createPreferenceEditorContributions_do_not_call_createFieldEditors_for_not_matching_ids() throws Exception {
final List<IPreferenceFieldEditorContribution> contributions = listOfContribWithIds("myContribId");
when(preferencePage.getFieldEditorContibutions()).thenReturn(contributions);
doCallRealMethod().when(preferencePage).createPreferenceEditorContributions(anyString());
preferencePage.createPreferenceEditorContributions("myContribId2");
verify(contributions.get(0), never()).createFieldEditors(any(Composite.class));
}
@Test
public void should_init_initialize_all_contributions() throws Exception {
final List<IPreferenceFieldEditorContribution> contributions = listOfContribWithIds("myContribId");
when(preferencePage.getContributions()).thenReturn(contributions);
doCallRealMethod().when(preferencePage).init(workbench);
preferencePage.init(workbench);
verify(contributions.get(0)).init(workbench);
}
@Test
public void should_initialize_load_editor_preference_store() throws Exception {
final Map<FieldEditor, IPreferenceStore> contributedEditor = mapOfContributedEditors(aFieldEditor);
when(preferencePage.getContributedEditors()).thenReturn(contributedEditor);
doCallRealMethod().when(preferencePage).initialize();
preferencePage.initialize();
verify(contributedEditor.keySet().iterator().next()).setPreferenceStore(contributedEditor.values().iterator().next());
verify(contributedEditor.keySet().iterator().next()).load();
}
private Map<FieldEditor, IPreferenceStore> mapOfContributedEditors(final FieldEditor... editors) {
final Map<FieldEditor, IPreferenceStore> contribuedEditors = new HashMap<FieldEditor, IPreferenceStore>();
for (final FieldEditor editor : editors) {
contribuedEditors.put(editor, mock(IPreferenceStore.class));
}
return contribuedEditors;
}
private List<IPreferenceFieldEditorContribution> listOfContribWithIds(final String... ids) {
final List<IPreferenceFieldEditorContribution> contribList = new ArrayList<IPreferenceFieldEditorContribution>();
for (final String id : ids) {
final IPreferenceFieldEditorContribution filedEditorContribMock = mock(IPreferenceFieldEditorContribution.class);
when(filedEditorContribMock.appliesTo(id)).thenReturn(true);
contribList.add(filedEditorContribMock);
}
return contribList;
}
}
......@@ -17,53 +17,144 @@
*/
package org.bonitasoft.studio.preferences.pages;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bonitasoft.studio.common.extension.BonitaStudioExtensionRegistryManager;
import org.bonitasoft.studio.common.jface.BonitaStudioFontRegistry;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.preferences.BonitaStudioPreferencesPlugin;
import org.bonitasoft.studio.preferences.extension.IPreferenceFieldEditorContribution;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
public abstract class AbstractBonitaPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
private static final String FIELD_EDITOR_CONTRIBUTION_ID = "org.bonitasoft.studio.preferences.prefrenceFieldEditorContribution";
private final List<IPreferenceFieldEditorContribution> contributions = new ArrayList<IPreferenceFieldEditorContribution>();
private final Map<FieldEditor, IPreferenceStore> contributedEditors = new HashMap<FieldEditor, IPreferenceStore>();
public AbstractBonitaPreferencePage() {
super();
}
public AbstractBonitaPreferencePage(final int style) {
super(style);
}
public AbstractBonitaPreferencePage(final String title, final int style) {
super(title, style);
}
public AbstractBonitaPreferencePage(final String title, final ImageDescriptor image,
final int style) {
super(title, image, style);
}
protected void createTitleBar(final String titleLabel, final Image image, final boolean useSeparator) {
if (useSeparator) {
new Label(getFieldEditorParent(), SWT.NONE);
final Label separator = new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL);
separator.setLayoutData(GridDataFactory.fillDefaults().span(3, 1).grab(true, false).create());
}
final Composite composite = new Composite(getFieldEditorParent(), SWT.NONE);
composite.setLayout(new GridLayout(2, false));
composite.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(3, 1).create());
final Label imageLabel = new Label(composite, SWT.NONE);
imageLabel.setImage(image);
public abstract class AbstractBonitaPreferencePage extends FieldEditorPreferencePage {
final Label title = new Label(composite, SWT.NONE);
title.setText(titleLabel);
title.setFont(BonitaStudioFontRegistry.getPreferenceTitleFont());
}
public AbstractBonitaPreferencePage() {
super();
}
protected void createPreferenceEditorContributions(final String contributorId) {
final List<IPreferenceFieldEditorContribution> prefEditorContributions = getFieldEditorContibutions();
for (final IPreferenceFieldEditorContribution prefEditorContrib : prefEditorContributions) {
if (prefEditorContrib.appliesTo(contributorId)) {
addContribution(prefEditorContrib);
for (final FieldEditor fe : prefEditorContrib.createFieldEditors(getFieldEditorParent())) {
addField(fe);
contributedEditors.put(fe, fe.getPreferenceStore());
}
}
}
}
public AbstractBonitaPreferencePage(int style) {
super(style);
}
protected void addContribution(final IPreferenceFieldEditorContribution prefEditorContrib) {
contributions.add(prefEditorContrib);
}
public AbstractBonitaPreferencePage(String title, int style) {
super(title, style);
}
protected List<IPreferenceFieldEditorContribution> getFieldEditorContibutions() {
final List<IPreferenceFieldEditorContribution> result = new ArrayList<IPreferenceFieldEditorContribution>();
final IConfigurationElement[] elems = BonitaStudioExtensionRegistryManager.getInstance().getConfigurationElements(
FIELD_EDITOR_CONTRIBUTION_ID);
for (final IConfigurationElement elem : elems) {
try {
final IPreferenceFieldEditorContribution prefEditorContrib = (IPreferenceFieldEditorContribution) elem.createExecutableExtension("class");
result.add(prefEditorContrib);
} catch (final CoreException e) {
BonitaStudioLog.error(e, BonitaStudioPreferencesPlugin.PLUGIN_ID);
}
}
return result;
}
public AbstractBonitaPreferencePage(String title, ImageDescriptor image,
int style) {
super(title, image, style);
}
protected List<IPreferenceFieldEditorContribution> getContributions() {
return contributions;
}
protected void createTitleBar(String titleLabel, Image image,boolean useSeparator) {
protected Map<FieldEditor, IPreferenceStore> getContributedEditors() {
return contributedEditors;
}
if(useSeparator){
new Label(getFieldEditorParent(), SWT.NONE) ;
Label separator = new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL);
separator.setLayoutData(GridDataFactory.fillDefaults().span(3, 1).grab(true, false).create()) ;
}
Composite composite = new Composite(getFieldEditorParent(), SWT.NONE) ;
composite.setLayout(new GridLayout(2,false)) ;
composite.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(3, 1).create()) ;
Label imageLabel = new Label(composite, SWT.NONE) ;
imageLabel.setImage(image) ;
/*
* (non-Javadoc)
* @see
* org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
@Override
public void init(final IWorkbench workbench) {
for (final IPreferenceFieldEditorContribution contrib : getContributions()) {
contrib.init(workbench);
}
}
Label title = new Label(composite, SWT.NONE) ;
title.setText(titleLabel) ;
title.setFont(BonitaStudioFontRegistry.getPreferenceTitleFont()) ;
@Override
protected void initialize() {
super.initialize();
for (final Entry<FieldEditor, IPreferenceStore> pe : getContributedEditors().entrySet()) {
if (pe.getValue() != null) {
pe.getKey().setPreferenceStore(pe.getValue());
pe.getKey().load();
}
}
}
}
@Override
public boolean performOk() {
for (final IPreferenceFieldEditorContribution contrib : getContributions()) {
if (!contrib.performOk()) {
return false;
}
}
return super.performOk();
}
}
\ No newline at end of file
......@@ -14,46 +14,34 @@
*/
package org.bonitasoft.studio.preferences.pages;
import java.util.ArrayList;
import java.util.List;
import org.bonitasoft.studio.common.extension.BonitaStudioExtensionRegistryManager;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.connector.model.definition.provider.ConnectorEditPlugin;
import org.bonitasoft.studio.connector.model.definition.wizard.AbstractDefinitionWizard;
import org.bonitasoft.studio.pics.Pics;
import org.bonitasoft.studio.pics.PicsConstants;
import org.bonitasoft.studio.preferences.BonitaPreferenceConstants;
import org.bonitasoft.studio.preferences.BonitaStudioPreferencesPlugin;
import org.bonitasoft.studio.preferences.extension.IPreferenceFieldEditorContribution;
import org.bonitasoft.studio.preferences.i18n.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
/**
* @author Romain Bioteau
*
*
*/
public class BonitaAdvancedPreferencePage extends AbstractBonitaPreferencePage implements IWorkbenchPreferencePage {
private BooleanFieldEditor askSaveDiagramAfterFirstSave;
private static final String ADVANCED_CONTRIBUTOR_ID = "Advanced";
private List<IPreferenceFieldEditorContribution> contributions;
private BooleanFieldEditor askSaveDiagramAfterFirstSave;
private BooleanFieldEditor showConnectorEditionConfirmation;
public BonitaAdvancedPreferencePage() {
super(GRID);
setPreferenceStore(BonitaStudioPreferencesPlugin.getDefault().getPreferenceStore());
contributions = new ArrayList<IPreferenceFieldEditorContribution>();
}
/**
......@@ -61,6 +49,7 @@ public class BonitaAdvancedPreferencePage extends AbstractBonitaPreferencePage i
* GUI blocks needed to manipulate various types of preferences. Each field
* editor knows how to save and restore itself.
*/
@Override
public void createFieldEditors() {
createTitleBar(Messages.BonitaPreferenceDialog_Advanced, Pics.getImage(PicsConstants.preferenceAdvanced), false);
......@@ -73,63 +62,17 @@ public class BonitaAdvancedPreferencePage extends AbstractBonitaPreferencePage i
Messages.doNotDisplayConnectorDefConfirmationMessage, getFieldEditorParent());
addField(showConnectorEditionConfirmation);
IConfigurationElement[] elems = BonitaStudioExtensionRegistryManager.getInstance().getConfigurationElements(
"org.bonitasoft.studio.preferences.prefrenceFieldEditorContribution"); //$NON-NLS-1$
IPreferenceFieldEditorContribution prefEditorContrib = null;
for (IConfigurationElement elem : elems) {
try {
prefEditorContrib = (IPreferenceFieldEditorContribution) elem.createExecutableExtension("class"); //$NON-NLS-1$
} catch (CoreException e) {
BonitaStudioLog.error(e);
}
if (prefEditorContrib.appliesTo("Advanced")) {
new Label(getFieldEditorParent(), SWT.NONE);
new Label(getFieldEditorParent(), SWT.NONE);
Label separator = new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL);
separator.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(2, 1).create());
new Label(getFieldEditorParent(), SWT.NONE);
new Label(getFieldEditorParent(), SWT.NONE);
contributions.add(prefEditorContrib);
for (FieldEditor fe : prefEditorContrib.createFieldEditors(getFieldEditorParent())) {
addField(fe);
}
}
}
new Label(getFieldEditorParent(), SWT.NONE);
new Label(getFieldEditorParent(), SWT.NONE);
}
final Label separator = new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL);
separator.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(2, 1).create());
@Override
public boolean performOk() {
boolean ok = true;
for (IPreferenceFieldEditorContribution contrib : contributions) {
if (ok) {
ok = ok && contrib.performOk();
}
}
return super.performOk() && ok;
}
new Label(getFieldEditorParent(), SWT.NONE);
new Label(getFieldEditorParent(), SWT.NONE);
/*
* (non-Javadoc)
* @see
* org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
for (IPreferenceFieldEditorContribution contrib : contributions) {
contrib.init(workbench);
}
createPreferenceEditorContributions(ADVANCED_CONTRIBUTOR_ID);
}
@Override
protected void initialize() {
super.initialize();
if (showConnectorEditionConfirmation != null) {
showConnectorEditionConfirmation.setPreferenceStore(ConnectorEditPlugin.getPlugin().getPreferenceStore());
showConnectorEditionConfirmation.load();
}
}
}
/*
* Copyright (C) 2009-2011 BonitaSoft S.A.
* BonitaSoft, 31 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
* 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/>.
*/
package org.bonitasoft.studio.preferences.pages;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bonitasoft.studio.common.extension.BonitaStudioExtensionRegistryManager;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.pics.Pics;
import org.bonitasoft.studio.pics.PicsConstants;
import org.bonitasoft.studio.preferences.BonitaCoolBarPreferenceConstant;
import org.bonitasoft.studio.preferences.BonitaStudioPreferencesPlugin;
import org.bonitasoft.studio.preferences.extension.IPreferenceFieldEditorContribution;
import org.bonitasoft.studio.preferences.i18n.Messages;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
/**
* @author Romain Bioteau
* @author Romain Bioteau
*
*/
public class BonitaAppearancePreferencePage extends AbstractBonitaPreferencePage implements IWorkbenchPreferencePage {
private RadioGroupFieldEditor radioFiled;
private List<IPreferenceFieldEditorContribution> contributions;
private List<FieldEditor> fieldEditors;
public BonitaAppearancePreferencePage() {
super(GRID);
setPreferenceStore(BonitaStudioPreferencesPlugin.getDefault().getPreferenceStore());
contributions = new ArrayList<IPreferenceFieldEditorContribution>();
fieldEditors = new ArrayList<FieldEditor>() ;
}
/**
* Creates the field editors. Field editors are abstractions of the common
* GUI blocks needed to manipulate various types of preferences. Each field
* editor knows how to save and restore itself.
*/
public void createFieldEditors() {
createTitleBar(Messages.BonitaPreferenceDialog_appearance,Pics.getImage(PicsConstants.preferenceAppearance),false);