Bug 522137: Remove macro recorder toolbar button allows to remove obsolete code from codeFactory Change-Id: I9ecd3c74cf0153f8f82f29475f136066cd25ca6f
diff --git a/plugins/org.eclipse.ease.ui.scripts/plugin.xml b/plugins/org.eclipse.ease.ui.scripts/plugin.xml index e138a0d..93faf3a 100644 --- a/plugins/org.eclipse.ease.ui.scripts/plugin.xml +++ b/plugins/org.eclipse.ease.ui.scripts/plugin.xml
@@ -63,16 +63,6 @@ </command> <command categoryId="org.eclipse.ease.commands.category.script" - defaultHandler="org.eclipse.ease.ui.scripts.handler.ToggleScriptRecording" - id="org.eclipse.ease.commands.script.toggleScriptRecording" - name="%toggleScriptRec"> - <state - class="org.eclipse.jface.commands.ToggleState" - id="STYLE"> - </state> - </command> - <command - categoryId="org.eclipse.ease.commands.category.script" defaultHandler="org.eclipse.ease.ui.scripts.handler.EditScript" id="org.eclipse.ease.commands.script.edit" name="%editScript"> @@ -128,15 +118,6 @@ </separator> </menuContribution> <menuContribution - locationURI="toolbar:org.eclipse.ease.ui.views.scriptShell?after=additions"> - <command - commandId="org.eclipse.ease.commands.script.toggleScriptRecording" - icon="icons/elcl16/start_script_recording.png" - label="%toggleScriptRec" - style="toggle"> - </command> - </menuContribution> - <menuContribution allPopups="false" locationURI="popup:org.eclipse.ease.scripts.properties.allKeywords?after=additions"> <command
diff --git a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/ToggleScriptRecording.java b/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/ToggleScriptRecording.java deleted file mode 100644 index 259d2e9..0000000 --- a/plugins/org.eclipse.ease.ui.scripts/src/org/eclipse/ease/ui/scripts/handler/ToggleScriptRecording.java +++ /dev/null
@@ -1,203 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 Christian Pontesegger and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License_Identifier: EPL-2.0 - * - * Contributors: - * Christian Pontesegger - initial API and implementation - *******************************************************************************/ -package org.eclipse.ease.ui.scripts.handler; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.IHandler; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.Path; -import org.eclipse.ease.IExecutionListener; -import org.eclipse.ease.IScriptEngine; -import org.eclipse.ease.IScriptEngineProvider; -import org.eclipse.ease.Logger; -import org.eclipse.ease.Script; -import org.eclipse.ease.service.EngineDescription; -import org.eclipse.ease.service.ScriptType; -import org.eclipse.ease.tools.StringTools; -import org.eclipse.ease.ui.scripts.Activator; -import org.eclipse.ease.ui.scripts.Messages; -import org.eclipse.ease.ui.scripts.ScriptEditorInput; -import org.eclipse.ease.ui.scripts.ScriptStorage; -import org.eclipse.ease.ui.scripts.dialogs.SelectScriptStorageDialog; -import org.eclipse.ease.ui.scripts.preferences.PreferencesHelper; -import org.eclipse.ease.ui.scripts.repository.IRepositoryService; -import org.eclipse.ease.ui.tools.ToggleHandler; -import org.eclipse.jface.dialogs.InputDialog; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jface.window.Window; -import org.eclipse.osgi.util.NLS; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.IEditorDescriptor; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.PartInitException; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.commands.IElementUpdater; -import org.eclipse.ui.handlers.HandlerUtil; -import org.eclipse.ui.ide.IDE; -import org.eclipse.ui.menus.UIElement; - -/** - * Start/stop script recording. - */ -public class ToggleScriptRecording extends ToggleHandler implements IHandler, IElementUpdater, IExecutionListener { - - private static final Map<IScriptEngine, StringBuffer> fRecordings = new HashMap<>(); - - private static StringBuffer addHeaderData(StringBuffer buffer, String scriptName, ScriptType scriptType) { - - final Map<String, String> keywords = new HashMap<>(); - keywords.put("name", new Path(scriptName).makeRelative().toString()); - keywords.put("description", "Script recorded by user."); - keywords.put("script-type", scriptType.getName()); - keywords.put("author", System.getProperty("user.name")); - keywords.put("date-recorded", new SimpleDateFormat("yyyy-MM-dd, HH:mm").format(new Date())); - final String keywordBlock = scriptType.getCodeFactory().createKeywordHeader(keywords, null); - - buffer.insert(0, StringTools.LINE_DELIMITER); - buffer.insert(0, scriptType.getCodeFactory().createCommentedString(keywordBlock, true)); - - return buffer; - } - - private static String askForScriptName(ExecutionEvent event, ScriptStorage storage) { - // ask for script name - final InputDialog dialog = new InputDialog(HandlerUtil.getActiveShell(event), Messages.ToggleScriptRecording_saveScript, - Messages.ToggleScriptRecording_enterUniqueName, "", name -> { - if ((storage != null) && (storage.exists(new Path(name).makeAbsolute().toString()))) - return NLS.bind(Messages.ToggleScriptRecording_nameAlreadyInUse, name); - - return null; - }); - - if (dialog.open() == Window.OK) - return dialog.getValue(); - - return null; - } - - private static ScriptStorage createOrGetStorage() { - // if no default storage is selected, ask the user for the correct location - if (PreferencesHelper.getUserScriptStorageLocation() == null) { - - // user did not select a storage yet, ask for location - final SelectScriptStorageDialog dialog = new SelectScriptStorageDialog(Display.getDefault().getActiveShell()); - if (dialog.open() == Window.OK) { - final IRepositoryService repositoryService = PlatformUI.getWorkbench().getService(IRepositoryService.class); - repositoryService.addLocation(dialog.getLocation(), true, true); - } - - else - return null; - } - - return ScriptStorage.createStorage(); - } - - private boolean fChecked = false; - - @Override - protected final void executeToggle(final ExecutionEvent event, final boolean checked) { - final IWorkbenchPart part = HandlerUtil.getActivePart(event); - - if (part instanceof IScriptEngineProvider) { - final IScriptEngine engine = ((IScriptEngineProvider) part).getScriptEngine(); - - if (engine != null) { - if (checked) { - // start recording, eventually overrides a running recording - // of the same provider - fRecordings.put(engine, new StringBuffer()); - engine.addExecutionListener(this); - - } else { - // stop recording - StringBuffer buffer = fRecordings.remove(engine); - - if (buffer.length() > 0) { - // script data is available - - final ScriptStorage storage = createOrGetStorage(); - final String scriptName = askForScriptName(event, storage); - final EngineDescription description = engine.getDescription(); - final ScriptType scriptType = description.getSupportedScriptTypes().iterator().next(); - - buffer = addHeaderData(buffer, scriptName, scriptType); - - if (storage != null) { - // store script - final String fileName = scriptName + "." + scriptType.getDefaultExtension(); - if (!storage.store(fileName, buffer.toString())) - // could not store script - MessageDialog.openError(HandlerUtil.getActiveShell(event), Messages.ToggleScriptRecording_saveError, - Messages.ToggleScriptRecording_couldNotStore); - - } else { - // we do not have a storage, open script in editor - // and let user decide what to do - final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); - try { - final IEditorDescriptor editor = IDE.getDefaultEditor( - ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("/sample/foo." + scriptType.getDefaultExtension()))); - final IEditorPart openEditor = IDE.openEditor(page, new ScriptEditorInput(scriptName, buffer.toString()), editor.getId()); - // the editor starts indicating it is not dirty, - // so ask the user to perform a save as action - openEditor.doSaveAs(); - - } catch (final PartInitException e) { - Logger.error(Activator.PLUGIN_ID, "Could not open editor for recorded script.", e); - } - } - } - } - } - } - - fChecked = checked; - } - - @Override - public final void updateElement(final UIElement element, @SuppressWarnings("rawtypes") final Map parameters) { - super.updateElement(element, parameters); - - if (fChecked) - element.setIcon(org.eclipse.ease.ui.Activator.getImageDescriptor(Activator.PLUGIN_ID, "icons/elcl16/stop_script_recording.png")); - - else - element.setIcon(org.eclipse.ease.ui.Activator.getImageDescriptor(Activator.PLUGIN_ID, "icons/elcl16/start_script_recording.png")); - } - - @Override - public void notify(final IScriptEngine engine, final Script script, final int status) { - if (IExecutionListener.SCRIPT_END == status) { - try { - final StringBuffer buffer = fRecordings.get(engine); - if (buffer != null) { - buffer.append(script.getCode()); - - if (!buffer.toString().endsWith(StringTools.LINE_DELIMITER)) - buffer.append(StringTools.LINE_DELIMITER); - } else - engine.removeExecutionListener(this); - } catch (final Exception e) { - // could not fetch code from script, gracefully fail - } - } - } -}
diff --git a/plugins/org.eclipse.ease/.classpath b/plugins/org.eclipse.ease/.classpath index cf36b56..1db08c6 100644 --- a/plugins/org.eclipse.ease/.classpath +++ b/plugins/org.eclipse.ease/.classpath
@@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> - <classpathentry kind="src" path="src/"/> + <classpathentry kind="src" path="src"/> <classpathentry kind="output" path="target/classes"/> </classpath>
diff --git a/plugins/org.eclipse.ease/.settings/org.eclipse.jdt.core.prefs b/plugins/org.eclipse.ease/.settings/org.eclipse.jdt.core.prefs index 64e8545..7004b85 100644 --- a/plugins/org.eclipse.ease/.settings/org.eclipse.jdt.core.prefs +++ b/plugins/org.eclipse.ease/.settings/org.eclipse.jdt.core.prefs
@@ -11,17 +11,21 @@ org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes= org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.methodParameters=generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.compliance=11 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false +org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 @@ -29,19 +33,22 @@ org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_assignment=0 -org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16 +org.eclipse.jdt.core.formatter.alignment_for_bitwise_operator=16 org.eclipse.jdt.core.formatter.alignment_for_compact_if=16 org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80 org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0 org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16 org.eclipse.jdt.core.formatter.alignment_for_expressions_in_for_loop_header=0 +org.eclipse.jdt.core.formatter.alignment_for_logical_operator=16 org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0 org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16 +org.eclipse.jdt.core.formatter.alignment_for_multiplicative_operator=16 org.eclipse.jdt.core.formatter.alignment_for_parameterized_type_references=0 org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80 org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16 +org.eclipse.jdt.core.formatter.alignment_for_string_concatenation=16 org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16 org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16 @@ -130,11 +137,12 @@ org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert +org.eclipse.jdt.core.formatter.insert_space_after_additive_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert -org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_bitwise_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=insert org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert @@ -165,6 +173,8 @@ org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow=insert +org.eclipse.jdt.core.formatter.insert_space_after_logical_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_multiplicative_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert @@ -189,13 +199,17 @@ org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_after_relational_operator=insert org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert +org.eclipse.jdt.core.formatter.insert_space_after_shift_operator=insert +org.eclipse.jdt.core.formatter.insert_space_after_string_concatenation=insert org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_additive_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert -org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_bitwise_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert @@ -243,6 +257,8 @@ org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert +org.eclipse.jdt.core.formatter.insert_space_before_logical_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_multiplicative_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert @@ -279,9 +295,12 @@ org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_relational_operator=insert org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert +org.eclipse.jdt.core.formatter.insert_space_before_shift_operator=insert +org.eclipse.jdt.core.formatter.insert_space_before_string_concatenation=insert org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert @@ -317,9 +336,13 @@ org.eclipse.jdt.core.formatter.tabulation.size=4 org.eclipse.jdt.core.formatter.use_on_off_tags=true org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false +org.eclipse.jdt.core.formatter.wrap_before_additive_operator=true org.eclipse.jdt.core.formatter.wrap_before_assignment_operator=false -org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true +org.eclipse.jdt.core.formatter.wrap_before_bitwise_operator=true org.eclipse.jdt.core.formatter.wrap_before_conditional_operator=true +org.eclipse.jdt.core.formatter.wrap_before_logical_operator=true +org.eclipse.jdt.core.formatter.wrap_before_multiplicative_operator=true org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true +org.eclipse.jdt.core.formatter.wrap_before_string_concatenation=true org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter
diff --git a/plugins/org.eclipse.ease/META-INF/MANIFEST.MF b/plugins/org.eclipse.ease/META-INF/MANIFEST.MF index 226cfa2..a988da4 100644 --- a/plugins/org.eclipse.ease/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.ease/META-INF/MANIFEST.MF
@@ -3,7 +3,7 @@ Bundle-SymbolicName: org.eclipse.ease;singleton:=true Bundle-Version: 0.9.0.qualifier Bundle-Vendor: Eclipse.org -Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Bundle-RequiredExecutionEnvironment: JavaSE-11 Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.7.0,4.0.0)";visibility:=reexport, org.eclipse.ui;bundle-version="[3.7.0,4.0.0)", org.eclipse.debug.core;bundle-version="[3.7.1,4.0.0)",
diff --git a/plugins/org.eclipse.ease/src/org/eclipse/ease/AbstractCodeFactory.java b/plugins/org.eclipse.ease/src/org/eclipse/ease/AbstractCodeFactory.java index 5c3da0b..9971fb7 100644 --- a/plugins/org.eclipse.ease/src/org/eclipse/ease/AbstractCodeFactory.java +++ b/plugins/org.eclipse.ease/src/org/eclipse/ease/AbstractCodeFactory.java
@@ -14,13 +14,11 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.Set; -import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,54 +39,6 @@ } @Override - public String createKeywordHeader(Map<String, String> keywords, String existingHeader) { - final StringBuilder header = new StringBuilder(); - - // copy existing text before keywords - if (existingHeader == null) - existingHeader = ""; - - final String[] existingLines = existingHeader.split("\\r?\\n"); - int index = 0; - for (; index < existingLines.length; index++) { - final Matcher matcher = AbstractCodeParser.PARAMETER_PATTERN.matcher(existingLines[index]); - if (!matcher.matches()) - header.append(existingLines[index]).append(LINE_DELIMITER); - else - break; - } - - // add line delimiter before keywords - if ((header.length() > 0) && (!keywords.isEmpty())) - header.append(LINE_DELIMITER); - - // add keywords - for (final Entry<String, String> entry : keywords.entrySet()) { - header.append(entry.getKey()); - header.append(new String(new char[Math.max(16 - entry.getKey().length(), 1)]).replace('\0', ' ')); - - header.append(": "); - header.append(entry.getValue()).append(LINE_DELIMITER); - } - - // copy existing text after keywords - boolean isFirstLineAfterKeywords = true; - for (; index < existingLines.length; index++) { - final Matcher matcher = AbstractCodeParser.PARAMETER_PATTERN.matcher(existingLines[index]); - if (!matcher.matches()) { - if (isFirstLineAfterKeywords) { - header.append(LINE_DELIMITER); - isFirstLineAfterKeywords = false; - } - - header.append(existingLines[index]).append(LINE_DELIMITER); - } - } - - return header.toString(); - } - - @Override public String getDefaultValue(final Parameter parameter) { final String defaultStringValue = parameter.getDefaultValue().replaceAll("\\r", "\\\\r").replaceAll("\\n", "\\\\n"); @@ -102,36 +52,50 @@ try { return Integer.toString(Integer.parseInt(defaultStringValue)); } catch (final NumberFormatException e1) { + throw new IllegalArgumentException(String.format("Cannot convert default value '%s' of parameter '%s' to %s", parameter.getDefaultValue(), + parameter.getName(), parameter.getClazz().getSimpleName()), e1); } } if ((Long.class.equals(clazz)) || (long.class.equals(clazz))) { try { return Long.toString(Long.parseLong(defaultStringValue)); } catch (final NumberFormatException e1) { + throw new IllegalArgumentException(String.format("Cannot convert default value '%s' of parameter '%s' to %s", parameter.getDefaultValue(), + parameter.getName(), parameter.getClazz().getSimpleName()), e1); } } if ((Byte.class.equals(clazz)) || (byte.class.equals(clazz))) { try { return Integer.toString(Integer.parseInt(defaultStringValue)); } catch (final NumberFormatException e1) { + throw new IllegalArgumentException(String.format("Cannot convert default value '%s' of parameter '%s' to %s", parameter.getDefaultValue(), + parameter.getName(), parameter.getClazz().getSimpleName()), e1); } } if ((Float.class.equals(clazz)) || (float.class.equals(clazz))) { try { return Float.toString(Float.parseFloat(defaultStringValue)); } catch (final NumberFormatException e1) { + throw new IllegalArgumentException(String.format("Cannot convert default value '%s' of parameter '%s' to %s", parameter.getDefaultValue(), + parameter.getName(), parameter.getClazz().getSimpleName()), e1); } } if ((Double.class.equals(clazz)) || (double.class.equals(clazz))) { try { return Double.toString(Double.parseDouble(defaultStringValue)); } catch (final NumberFormatException e1) { + throw new IllegalArgumentException(String.format("Cannot convert default value '%s' of parameter '%s' to %s", parameter.getDefaultValue(), + parameter.getName(), parameter.getClazz().getSimpleName()), e1); } } if ((Boolean.class.equals(clazz)) || (boolean.class.equals(clazz))) { return Boolean.parseBoolean(defaultStringValue) ? getTrueString() : getFalseString(); } if ((Character.class.equals(clazz)) || (char.class.equals(clazz))) { + if (defaultStringValue.isEmpty()) + throw new IllegalArgumentException( + String.format("Cannot convert empty default value of parameter '%s' to %s", parameter.getName(), parameter.getClazz().getSimpleName())); + return "'" + defaultStringValue.charAt(0) + "'"; } if (String.class.equals(clazz)) @@ -179,7 +143,7 @@ final WrapToScript wrapAnnotation = method.getAnnotation(WrapToScript.class); if (wrapAnnotation != null) { for (final String name : wrapAnnotation.alias().split(WrapToScript.DELIMITER)) - if (!name.trim().isEmpty()) + if (!name.isBlank()) methodAliases.add(name.trim()); } @@ -213,29 +177,21 @@ @Override public String createFunctionCall(final Method method, final Object... parameters) { - final StringBuilder code = new StringBuilder(); + final String parameterString = Arrays.asList(parameters).stream().map(p -> parameterToString(p)).collect(Collectors.joining(", ")); + return String.format("%s(%s);", method.getName(), parameterString); + } - code.append(method.getName()).append('('); - for (final Object parameter : parameters) { - if (parameter instanceof String) - code.append('"').append(((String) parameter).replace("\"", "\\\"").replace("\\", "\\\\")).append('"'); - else if (parameter == null) - code.append(getNullString()); - else if (parameter instanceof Boolean) - code.append(((Boolean) parameter) ? getTrueString() : getFalseString()); - else - code.append(parameter.toString()); + private String parameterToString(Object parameter) { + if (parameter instanceof String) + return String.format("\"%s\"", ((String) parameter).replace("\"", "\\\"").replace("\\", "\\\\")); - code.append(", "); - } + if (parameter == null) + return getNullString(); - // remove last comma separator - if (parameters.length > 0) - code.delete(code.length() - 2, code.length()); + if (parameter instanceof Boolean) + return ((Boolean) parameter) ? getTrueString() : getFalseString(); - code.append(");"); - - return code.toString(); + return parameter.toString(); } /**
diff --git a/plugins/org.eclipse.ease/src/org/eclipse/ease/ICodeFactory.java b/plugins/org.eclipse.ease/src/org/eclipse/ease/ICodeFactory.java index e50f313..4ace2ea 100644 --- a/plugins/org.eclipse.ease/src/org/eclipse/ease/ICodeFactory.java +++ b/plugins/org.eclipse.ease/src/org/eclipse/ease/ICodeFactory.java
@@ -13,7 +13,6 @@ package org.eclipse.ease; import java.lang.reflect.Method; -import java.util.Map; import org.eclipse.core.runtime.Platform; import org.eclipse.ease.modules.IEnvironment; @@ -128,17 +127,6 @@ String createCommentedString(String comment, boolean blockComment); /** - * Create a comment header for given keywords. Arbitrary content will be delimited by an empty line - * - * @param keywords - * key:value pairs to be stored - * @param existingHeader - * current header to copy plain text from - * @return String representation of header - */ - String createKeywordHeader(Map<String, String> keywords, String existingHeader); - - /** * Create script wrapper code for a given java instance. * * @param environment @@ -151,6 +139,7 @@ * whether to store methods to the global namespace or to create a custom object * @param engine * script engine + * @return create wrapped script code */ String createWrapper(IEnvironment environment, Object instance, String identifier, boolean customNamespace, IScriptEngine engine); }
diff --git a/releng/org.eclipse.ease.releng/pmd/pmd_rules_unittest.xml b/releng/org.eclipse.ease.releng/pmd/pmd_rules_unittest.xml index f0513ae..c594ff1 100644 --- a/releng/org.eclipse.ease.releng/pmd/pmd_rules_unittest.xml +++ b/releng/org.eclipse.ease.releng/pmd/pmd_rules_unittest.xml
@@ -25,7 +25,7 @@ <!-- More than 1 assert allowed in tests --> <exclude name="JUnitTestContainsTooManyAsserts" /> - <!-- JUnit 5 tests should be package private --> + <!-- Allow public test classes/methods for JUnit 5 --> <exclude name="JUnit5TestShouldBePackagePrivate" /> </rule> @@ -135,6 +135,9 @@ <!-- ignore locales when converting strings --> <exclude name="UseLocaleWithCaseConversions" /> + + <!-- ignore repeated string literals, eg for script code --> + <exclude name="AvoidDuplicateLiterals" /> </rule> <rule ref="category/java/errorprone.xml/EmptyCatchBlock">
diff --git a/tests/org.eclipse.ease.test/src/org/eclipse/ease/AbstractCodeFactoryTest.java b/tests/org.eclipse.ease.test/src/org/eclipse/ease/AbstractCodeFactoryTest.java index 9dcc9c9..9c8dda2 100644 --- a/tests/org.eclipse.ease.test/src/org/eclipse/ease/AbstractCodeFactoryTest.java +++ b/tests/org.eclipse.ease.test/src/org/eclipse/ease/AbstractCodeFactoryTest.java
@@ -13,27 +13,488 @@ package org.eclipse.ease; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import java.util.HashMap; +import java.io.File; +import java.util.Collection; +import org.eclipse.ease.ICodeFactory.Parameter; +import org.eclipse.ease.modules.WrapToScript; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; public class AbstractCodeFactoryTest { + private static final String SINGLELINE_HEADER = "This is a single line header."; + private static final String MULTILINE_HEADER = "This is a\n" + "multiline header."; + private static final String KEYWORDS = "keyword1 : value1\n" + "keyword2 : value2"; + + private AbstractCodeFactory fFactory; + + @BeforeEach + public void beforeEach() { + fFactory = mock(AbstractCodeFactory.class, Mockito.CALLS_REAL_METHODS); + } + @Test - public void createNewKeywordHeader() { - final AbstractCodeFactory factory = mock(AbstractCodeFactory.class, Mockito.CALLS_REAL_METHODS); + @DisplayName("getMethodNames() returns method name") + public void getMethodNames_returns_method_name() throws NoSuchMethodException { + final Collection<String> names = AbstractCodeFactory.getMethodNames(AbstractCodeFactoryTest.class.getMethod("methodWithoutAliases")); - final HashMap<String, String> keywords = new HashMap<>(); - keywords.put("first", "value"); - keywords.put("menu", "this is a menu entry"); + assertEquals(1, names.size()); + assertTrue(names.contains("methodWithoutAliases")); + } - final String header = factory.createKeywordHeader(keywords, null); + @Test + @DisplayName("getMethodNames() returns method name & aliases") + public void getMethodNames_returns_method_name_and_aliases() throws NoSuchMethodException { + final Collection<String> names = AbstractCodeFactory.getMethodNames(AbstractCodeFactoryTest.class.getMethod("methodWithAliases")); - assertTrue(header.contains("first : value")); - assertTrue(header.contains("menu : this is a menu entry")); + assertEquals(3, names.size()); + assertTrue(names.contains("methodWithAliases")); + assertTrue(names.contains("one")); + assertTrue(names.contains("two")); + } + + @Test + @DisplayName("getMethodNames() filters empty aliases") + public void getMethodNames_filters_empty_aliases() throws NoSuchMethodException { + final Collection<String> names = AbstractCodeFactory.getMethodNames(AbstractCodeFactoryTest.class.getMethod("methodWithEmptyAliases")); + + assertEquals(3, names.size()); + assertTrue(names.contains("methodWithEmptyAliases")); + assertTrue(names.contains("one")); + assertTrue(names.contains("two")); + } + + @Test + @DisplayName("getMethodAliases() is empty without annotation") + public void getMethodAliases_is_empty_without_annotation() throws NoSuchMethodException { + final Collection<String> names = AbstractCodeFactory.getMethodAliases(AbstractCodeFactoryTest.class.getMethod("methodWithoutAliases")); + + assertTrue(names.isEmpty()); + } + + @Test + @DisplayName("getMethodAliases() returns method aliases") + public void getMethodAliases_returns_method_aliases() throws NoSuchMethodException { + final Collection<String> names = AbstractCodeFactory.getMethodAliases(AbstractCodeFactoryTest.class.getMethod("methodWithAliases")); + + assertEquals(2, names.size()); + assertTrue(names.contains("one")); + assertTrue(names.contains("two")); + } + + @Test + @DisplayName("getMethodAliases() filters empty aliases") + public void getMethodAliases_filters_empty_aliases() throws NoSuchMethodException { + final Collection<String> names = AbstractCodeFactory.getMethodAliases(AbstractCodeFactoryTest.class.getMethod("methodWithEmptyAliases")); + + assertEquals(2, names.size()); + assertTrue(names.contains("one")); + assertTrue(names.contains("two")); + } + + @Test + @DisplayName("createFunctionCall() for foo()") + public void createFunctionCall_for_method_without_parameters() throws NoSuchMethodException { + assertEquals("foo();", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo"))); + } + + @Test + @DisplayName("createFunctionCall() for foo('test')") + public void createFunctionCall_for_method_with_String_parameter() throws NoSuchMethodException { + assertEquals("foo(\"test\");", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", String.class), "test")); + } + + @Test + @DisplayName("createFunctionCall() for foo(null)") + public void createFunctionCall_for_method_with_null_parameter() throws NoSuchMethodException { + when(fFactory.getNullString()).thenReturn("null"); + + assertEquals("foo(null);", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", String.class), (String) null)); + } + + @Test + @DisplayName("createFunctionCall() for foo(1)") + public void createFunctionCall_for_method_with_primitive_parameter() throws NoSuchMethodException { + assertEquals("foo(1);", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", int.class), 1)); + } + + @Test + @DisplayName("createFunctionCall() for foo(Object)") + public void createFunctionCall_for_method_with_Object_parameter() throws NoSuchMethodException { + assertEquals("foo(/);", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", File.class), new File("/"))); + } + + @Test + @DisplayName("createFunctionCall() for foo(true)") + public void createFunctionCall_for_method_with_true_parameter() throws NoSuchMethodException { + assertEquals("foo(true);", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", boolean.class), true)); + } + + @Test + @DisplayName("createFunctionCall() for foo(false)") + public void createFunctionCall_for_method_with_false_parameter() throws NoSuchMethodException { + assertEquals("foo(false);", fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", boolean.class), false)); + } + + @Test + @DisplayName("createFunctionCall() for foo('test', null, false)") + public void createFunctionCall_for_method_with_multiple_parameters() throws NoSuchMethodException { + when(fFactory.getNullString()).thenReturn("NULL"); + + assertEquals("foo(\"test\", NULL, true);", + fFactory.createFunctionCall(AbstractCodeFactoryTest.class.getMethod("foo", String.class, Object.class, boolean.class), "test", null, true)); + } + + @Test + @DisplayName("getDefaultValue(null) = null string") + public void getDefaultValue_for_null() throws NoSuchMethodException { + when(fFactory.getNullString()).thenReturn("NULL"); + + assertEquals("NULL", fFactory.getDefaultValue(new ICodeFactory.Parameter())); + } + + @Test + @DisplayName("getDefaultValue(int) = 1") + public void getDefaultValue_for_int() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(int.class); + parameter.setDefault("1"); + assertEquals("1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(int) throws for invalid default") + public void getDefaultValue_for_int_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(int.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Integer) = 1") + public void getDefaultValue_for_Integer() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Integer.class); + parameter.setDefault("1"); + assertEquals("1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Integer) throws for invalid default") + public void getDefaultValue_for_Integer_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Integer.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(long) = 1") + public void getDefaultValue_for_long() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(long.class); + parameter.setDefault("1"); + assertEquals("1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(long) throws for invalid default") + public void getDefaultValue_for_long_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(long.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Long) = 1") + public void getDefaultValue_for_Long() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Long.class); + parameter.setDefault("1"); + assertEquals("1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Long) throws for invalid default") + public void getDefaultValue_for_Long_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Long.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(byte) = 1") + public void getDefaultValue_for_byte() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(byte.class); + parameter.setDefault("1"); + assertEquals("1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(byte) throws for invalid default") + public void getDefaultValue_for_byte_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(byte.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Byte) = 1") + public void getDefaultValue_for_Byte() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Byte.class); + parameter.setDefault("1"); + assertEquals("1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Byte) throws for invalid default") + public void getDefaultValue_for_Byte_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Byte.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(float) = 1.1") + public void getDefaultValue_for_float() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(float.class); + parameter.setDefault("1.1"); + assertEquals("1.1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(float) throws for invalid default") + public void getDefaultValue_for_float_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(float.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Float) = 1.1") + public void getDefaultValue_for_Float() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Float.class); + parameter.setDefault("1.1"); + assertEquals("1.1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Float) throws for invalid default") + public void getDefaultValue_for_Float_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Float.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(double) = 1.1") + public void getDefaultValue_for_double() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(double.class); + parameter.setDefault("1.1"); + assertEquals("1.1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(double) throws for invalid default") + public void getDefaultValue_for_double_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(double.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Double) = 1.1") + public void getDefaultValue_for_Double() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Double.class); + parameter.setDefault("1.1"); + assertEquals("1.1", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Double) throws for invalid default") + public void getDefaultValue_for_Double_throws_for_invalid_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Double.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(boolean) = true") + public void getDefaultValue_for_boolean_is_true() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(boolean.class); + parameter.setDefault("true"); + assertEquals("true", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(boolean) = false") + public void getDefaultValue_for_boolean_is_false() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(boolean.class); + parameter.setDefault("false"); + assertEquals("false", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(boolean), invalid = false") + public void getDefaultValue_for_boolean_invalid_is_false() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(boolean.class); + parameter.setDefault(""); + assertEquals("false", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Boolean) = true") + public void getDefaultValue_for_Boolean_is_true() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Boolean.class); + parameter.setDefault("true"); + assertEquals("true", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Boolean) = false") + public void getDefaultValue_for_Boolean_is_false() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Boolean.class); + parameter.setDefault("false"); + assertEquals("false", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Boolean), invalid = false") + public void getDefaultValue_for_Boolean_invalid_is_false() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Boolean.class); + parameter.setDefault(""); + assertEquals("false", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(char) = 'a'") + public void getDefaultValue_for_char() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(char.class); + parameter.setDefault("a"); + assertEquals("'a'", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(char) takes first character") + public void getDefaultValue_for_char_takes_first_character() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(char.class); + parameter.setDefault("abc"); + assertEquals("'a'", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(char) throws on empty default") + public void getDefaultValue_for_char_throws_on_empty_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(char.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Character) = 'a'") + public void getDefaultValue_for_Character() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Character.class); + parameter.setDefault("a"); + assertEquals("'a'", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Character) takes first character") + public void getDefaultValue_for_Character_takes_first_character() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Character.class); + parameter.setDefault("abc"); + assertEquals("'a'", fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(Character) throws on empty default") + public void getDefaultValue_for_Character_throws_on_empty_default() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(Character.class); + parameter.setDefault(""); + assertThrows(IllegalArgumentException.class, () -> fFactory.getDefaultValue(parameter)); + } + + @Test + @DisplayName("getDefaultValue(String)") + public void getDefaultValue_for_String() throws NoSuchMethodException { + final Parameter parameter = new ICodeFactory.Parameter(); + parameter.setClass(String.class); + parameter.setDefault("abc"); + assertEquals("\"abc\"", fFactory.getDefaultValue(parameter)); + } + + @WrapToScript + public void methodWithoutAliases() { + // dummy method for alias tests; do not change the method name! + } + + @WrapToScript(alias = "one;two") + public void methodWithAliases() { + // dummy method for alias tests; do not change the method name! + } + + @WrapToScript(alias = " one ; ;; two;") + public void methodWithEmptyAliases() { + // dummy method for alias tests; do not change the method name! + } + + public void foo() { + // dummy method for createFunctionCall() tests + } + + public void foo(String bar) { + // dummy method for createFunctionCall() tests + } + + public void foo(String bar, Object another, boolean doIt) { + // dummy method for createFunctionCall() tests + } + + public void foo(int bar) { + // dummy method for createFunctionCall() tests + } + + public void foo(boolean bar) { + // dummy method for createFunctionCall() tests + } + + public void foo(File bar) { + // dummy method for createFunctionCall() tests } }
diff --git a/tests/org.eclipse.ease.test/src/org/eclipse/ease/ScriptResultTest.java b/tests/org.eclipse.ease.test/src/org/eclipse/ease/ScriptResultTest.java index f17283a..2b409e3 100644 --- a/tests/org.eclipse.ease.test/src/org/eclipse/ease/ScriptResultTest.java +++ b/tests/org.eclipse.ease.test/src/org/eclipse/ease/ScriptResultTest.java
@@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -24,13 +25,13 @@ private static final ScriptExecutionException EXCEPTION = new ScriptExecutionException("some error"); @Test - @DisplayName("isDone() == false on new ScriptResult()") + @DisplayName("isDone() = false on new ScriptResult()") public void isDone_false_on_new_ScriptResult() { assertFalse(new ScriptResult().isDone()); } @Test - @DisplayName("isDone() == true when result is ready") + @DisplayName("isDone() = true when result is ready") public void isDone_true_when_result_is_ready() { final ScriptResult result = new ScriptResult(); result.setResult(RESULT); @@ -39,7 +40,7 @@ } @Test - @DisplayName("isDone() == true when result is null") + @DisplayName("isDone() = true when result is null") public void isDone_true_when_result_is_null() { final ScriptResult result = new ScriptResult(); result.setResult(null); @@ -48,7 +49,7 @@ } @Test - @DisplayName("isDone() == true when exception was thrown") + @DisplayName("isDone() = true when exception was thrown") public void isDone_true_when_exception_was_thrown() { final ScriptResult result = new ScriptResult(); result.setException(EXCEPTION); @@ -210,4 +211,49 @@ result.setException(EXCEPTION); assertThrows(IllegalArgumentException.class, () -> result.setException(EXCEPTION)); } + + @Test + @DisplayName("cancel() = false") + public void cancel_returns_false() { + assertFalse(new ScriptResult().cancel(false)); + } + + @Test + @DisplayName("isCancelled() = false") + public void isCancelled_returns_false() { + assertFalse(new ScriptResult().isCancelled()); + } + + @Test + @DisplayName("toString() returns String for execution result") + public void toString_returns_String_for_execution_result() throws InterruptedException, ExecutionException { + final ScriptResult result = new ScriptResult(); + result.setResult(RESULT); + + assertNotNull(result.toString()); + } + + @Test + @DisplayName("toString() returns String for 'null' result") + public void toString_returns_String_for_null_result() throws InterruptedException, ExecutionException { + final ScriptResult result = new ScriptResult(); + result.setResult(null); + + assertNotNull(result.toString()); + } + + @Test + @DisplayName("toString() returns String for execution exception") + public void toString_returns_String_for_execution_exception() throws InterruptedException, ExecutionException { + final ScriptResult result = new ScriptResult(); + result.setException(EXCEPTION); + + assertNotNull(result.toString()); + } + + @Test + @DisplayName("VOID.toString() returns String") + public void void_toString_returns_String() throws InterruptedException, ExecutionException { + assertNotNull(ScriptResult.VOID.toString()); + } }