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

code quality

parent 2a936ce3
......@@ -48,8 +48,8 @@ import org.bonitasoft.engine.operation.LeftOperandBuilder;
import org.bonitasoft.engine.operation.OperationBuilder;
import org.bonitasoft.engine.operation.OperatorType;
import org.bonitasoft.studio.common.DataUtil;
import org.bonitasoft.studio.common.DateUtil;
import org.bonitasoft.studio.common.ExpressionConstants;
import org.bonitasoft.studio.common.TimerUtil;
import org.bonitasoft.studio.common.emf.tools.ModelHelper;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.engine.export.EngineExpressionUtil;
......@@ -552,7 +552,7 @@ public class FlowElementSwitch extends AbstractSwitch {
}
private TimerType getTimerType(final AbstractTimerEvent timer) {
if (TimerUtil.isDuration(timer)) {
if (isDuration(timer)) {
return TimerType.DURATION;
} else {
final String timerConditionReturnType = timer.getCondition().getReturnType();
......@@ -571,6 +571,14 @@ public class FlowElementSwitch extends AbstractSwitch {
return null;
}
private boolean isDuration(final AbstractTimerEvent timer) {
final Expression exp = timer.getCondition();
if (exp != null) {
return DateUtil.isDuration(exp.getContent());
}
return false;
}
@Override
public FlowElement caseEndEvent(final EndEvent endEvent) {
final EndEventDefinitionBuilder eventBuilder = builder.addEndEvent(endEvent.getName());
......
......@@ -16,9 +16,9 @@
*/
package org.bonitasoft.studio.importer.bar.custom.migration;
import org.bonitasoft.studio.common.TimerUtil;
import org.bonitasoft.studio.importer.bar.i18n.Messages;
import org.bonitasoft.studio.migration.migrator.ReportCustomMigration;
import org.bonitasoft.studio.migration.utils.LegacyTimerExpressionGenerator;
import org.bonitasoft.studio.model.process.ProcessFactory;
import org.bonitasoft.studio.model.process.StartTimerEvent;
import org.bonitasoft.studio.model.process.StartTimerScriptType;
......@@ -36,22 +36,22 @@ import org.eclipse.emf.edapt.migration.Model;
public class StartTimerCustomMigration extends ReportCustomMigration {
@Override
public void migrateAfter(Model model, Metamodel metamodel)
public void migrateAfter(final Model model, final Metamodel metamodel)
throws MigrationException {
for(Instance startTimer : model.getAllInstances("process.StartTimerEvent")){
EEnumLiteral scriptType = startTimer.get("scriptType");
StartTimerEvent event = ProcessFactory.eINSTANCE.createStartTimerEvent();
for(final Instance startTimer : model.getAllInstances("process.StartTimerEvent")){
final EEnumLiteral scriptType = startTimer.get("scriptType");
final StartTimerEvent event = ProcessFactory.eINSTANCE.createStartTimerEvent();
StartTimerScriptType type =null;
for(StartTimerScriptType t : StartTimerScriptType.values()){
for(final StartTimerScriptType t : StartTimerScriptType.values()){
if(t.getLiteral().equals(scriptType.getLiteral())){
type = t;
}
}
event.setScriptType(type);
if(TimerUtil.isCycle(event)){
if(LegacyTimerExpressionGenerator.isCycle(event)){
addReportChange((String) startTimer.get("name"), startTimer.getEClass().getName(), startTimer.getUuid(), Messages.startTimerCycleMigration, Messages.timerCondition, IStatus.WARNING);
}
}
}
}
......@@ -23,7 +23,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.bonitasoft.studio.connectors.model;bundle-version="6.2.0",
org.junit;bundle-version="4.11.0";resolution:=optional,
org.mockito;bundle-version="1.9.5";resolution:=optional,
org.fest-assert;bundle-version="1.4.2";resolution:=optional
org.bonitasoft.studio.tests-utils;bundle-version="6.4.0";resolution:=optional,
assertj-core;bundle-version="1.5.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.bonitasoft.studio.migration,org.bonitasoft.studio.
......
......@@ -16,7 +16,7 @@
*/
package org.bonitasoft.studio.migration.custom.migration;
import static org.fest.assertions.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import java.util.List;
......@@ -28,10 +28,7 @@ import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.edapt.migration.Instance;
import org.eclipse.emf.edapt.migration.Metamodel;
import org.eclipse.emf.edapt.migration.MigrationFactory;
import org.eclipse.emf.edapt.migration.MigrationPackage;
import org.eclipse.emf.edapt.migration.Model;
import org.eclipse.emf.edapt.migration.impl.MetamodelImpl;
import org.eclipse.emf.edapt.migration.impl.ModelImpl;
import org.junit.After;
......@@ -50,12 +47,12 @@ import org.mockito.runners.MockitoJUnitRunner;
public class BusinessObjectTypeCustomMigrationTest {
private BusinessObjectTypeCustomMigration migrationUnderTest;
@Spy
private ModelImpl model;
private ModelImpl model;
@Spy
private MetamodelImpl metamodel;
private MetamodelImpl metamodel;
/**
* @throws java.lang.Exception
......@@ -77,18 +74,18 @@ public class BusinessObjectTypeCustomMigrationTest {
@Test
public void shouldMigrateAfter_AddBusinessObjectType_InDiagram_IfNotExists() throws Exception {
doReturn(metamodel).when(model).getMetamodel();
EPackage ePackage = ProcessPackage.eINSTANCE.getMainProcess().getEPackage();
EList<EPackage> result = new BasicEList<EPackage>();
final EPackage ePackage = ProcessPackage.eINSTANCE.getMainProcess().getEPackage();
final EList<EPackage> result = new BasicEList<EPackage>();
result.add(ePackage);
doReturn(result).when(metamodel).getEPackages();
EList<Instance> emptyList = new BasicEList<Instance>();
final EList<Instance> emptyList = new BasicEList<Instance>();
doReturn(emptyList).when(model).getAllInstances("process.BusinessObjectType");
EList<Instance> diagramList = new BasicEList<Instance>();
Instance instance = model.newInstance("process.MainProcess");
final EList<Instance> diagramList = new BasicEList<Instance>();
final Instance instance = model.newInstance("process.MainProcess");
diagramList.add(instance);
doReturn(diagramList).when(model).getAllInstances("process.MainProcess");
migrationUnderTest.migrateAfter(model , metamodel);
assertThat((List<Instance>)instance.get("datatypes")).onProperty("name").containsOnly(NamingUtils.convertToId(DataTypeLabels.businessObjectType));
assertThat((List<Instance>) instance.get("datatypes")).extracting("name").containsOnly(NamingUtils.convertToId(DataTypeLabels.businessObjectType));
}
}
/**
* 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.migration.utils;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Date;
import org.bonitasoft.studio.model.expression.builders.ExpressionBuilder;
import org.bonitasoft.studio.model.process.StartTimerScriptType;
import org.bonitasoft.studio.model.process.builders.StartTimerEventBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Romain Bioteau
*
*/
public class LegacyTimerExpressionGeneratorTest {
private LegacyTimerExpressionGenerator legacyTimerExpressionGenerator;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
legacyTimerExpressionGenerator = new LegacyTimerExpressionGenerator();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_isCycle_return_false() throws Exception {
assertThat(LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.CONSTANT)
.build())).isFalse();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.GROOVY)
.build())).isFalse();
}
@Test
public void should_isCycle_return_true() throws Exception {
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.DAILY)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.HOURLY)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.MINUTELY)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.MONTHLY)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.YEARLY_DAY_OF_MONTH)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.YEARLY_DAY_OF_YEAR)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.MONTHLY_DAY_NUMBER)
.build())).isTrue();
assertThat(
LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.MONTHLY_DAY_OF_WEEK)
.build())).isTrue();
}
@Test(expected = IllegalArgumentException.class)
public void should_isCycle_throw_IllegalArgumentException() throws Exception {
assertThat(LegacyTimerExpressionGenerator.isCycle(StartTimerEventBuilder.createStartTimerEventBuilder().withScriptType(StartTimerScriptType.CUSTOM)
.build()));
}
@Test
public void should_generateConstant_return_a_script_with_new_Date() throws Exception {
final Date date = new Date();
final long time = date.getTime();
final String generateConstant = LegacyTimerExpressionGenerator.generateConstant(date);
assertThat(generateConstant).isEqualTo("return new Date(" + time + ");");
}
@Test
public void should_getTimerExpressionContent_return_expression_content_for_GROOVY_type() throws Exception {
final String content = "new Date()";
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.GROOVY).havingConditionExpression(ExpressionBuilder.create().withContent(content))
.build());
assertThat(expressionContent).isEqualTo(content);
}
@Test
public void should_getTimerExpressionContent_return_null_for_GROOVY_type() throws Exception {
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.GROOVY)
.build());
assertThat(expressionContent).isNull();
}
@Test
public void should_getTimerExpressionContent_return_cron_for_YEARLY_DAY_OF_MONTH_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.YEARLY_DAY_OF_MONTH).at(at).withDayNumber(2).withMonth(2)
.build());
assertThat(expressionContent).isEqualTo("0 30 9 2 3 ?");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_YEARLY_DAY_OF_YEAR_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.YEARLY_DAY_OF_YEAR).at(at).withDayNumber(2)
.build());
assertThat(expressionContent).isEqualTo("0 30 9 2 1 ?");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_MONTHLY_DAY_NUMBER_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.MONTHLY_DAY_NUMBER).at(at).withDayNumber(2)
.build());
assertThat(expressionContent).isEqualTo("0 30 9 2 * ?");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_MONTHLY_DAY_OF_WEEK_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.MONTHLY_DAY_OF_WEEK).at(at).withDayNumber(2).withDay(1)
.build());
assertThat(expressionContent).isEqualTo("0 30 9 ? * 1#2");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_WEEKLY_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.WEEKLY).at(at).withDay(2)
.build());
assertThat(expressionContent).isEqualTo("0 30 9 ? * 2");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_DAILY_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.DAILY).at(at).withDay(2)
.build());
assertThat(expressionContent).isEqualTo("0 30 9 * * ?");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_HOURLY_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.HOURLY).at(at).withHours(2)
.build());
assertThat(expressionContent).isEqualTo("0 * */2 * * ?");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_MINUTELY_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.MINUTELY).at(at).withMinutes(30)
.build());
assertThat(expressionContent).isEqualTo("0 */30 * * * ?");
}
@Test
public void should_getTimerExpressionContent_return_cron_for_CONSTANT_type() throws Exception {
final Date at = new Date(2014, 11, 2, 9, 30);
final long time = at.getTime();
final String expressionContent = legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.CONSTANT).at(at).withMinutes(30)
.build());
assertThat(expressionContent).isEqualTo("return new Date(" + time + ");");
}
@Test(expected = IllegalArgumentException.class)
public void should_getTimerExpressionContent_throw_IllegalArgumentException() throws Exception {
legacyTimerExpressionGenerator.getTimerExpressionContent(StartTimerEventBuilder.createStartTimerEventBuilder()
.withScriptType(StartTimerScriptType.CUSTOM)
.build());
}
}
......@@ -19,7 +19,7 @@ package org.bonitasoft.studio.migration.custom.migration.timer;
import java.util.Date;
import org.bonitasoft.studio.common.ExpressionConstants;
import org.bonitasoft.studio.common.TimerUtil;
import org.bonitasoft.studio.migration.utils.LegacyTimerExpressionGenerator;
import org.bonitasoft.studio.model.process.ProcessFactory;
import org.bonitasoft.studio.model.process.StartTimerEvent;
import org.bonitasoft.studio.model.process.StartTimerScriptType;
......@@ -36,20 +36,22 @@ import org.eclipse.emf.edapt.migration.Model;
*/
public class StartTimerConditionCustomMigration extends CustomMigration {
@Override
public void migrateAfter(Model model, Metamodel metamodel)
private LegacyTimerExpressionGenerator timerExpressionGenerator;
@Override
public void migrateAfter(final Model model, final Metamodel metamodel)
throws MigrationException {
for(Instance startTimer : model.getAllInstances("process.StartTimerEvent")){
EEnumLiteral scriptType = startTimer.get("scriptType");
Date from = startTimer.get("from");
Date at = startTimer.get("at");
int month = startTimer.get("month");
int day = startTimer.get("day");
int hours = startTimer.get("hours");
int dayNumber = startTimer.get("dayNumber");
int minutes = startTimer.get("minutes");
int seconds = startTimer.get("seconds");
StartTimerEvent event = ProcessFactory.eINSTANCE.createStartTimerEvent();
for(final Instance startTimer : model.getAllInstances("process.StartTimerEvent")){
final EEnumLiteral scriptType = startTimer.get("scriptType");
final Date from = startTimer.get("from");
final Date at = startTimer.get("at");
final int month = startTimer.get("month");
final int day = startTimer.get("day");
final int hours = startTimer.get("hours");
final int dayNumber = startTimer.get("dayNumber");
final int minutes = startTimer.get("minutes");
final int seconds = startTimer.get("seconds");
final StartTimerEvent event = ProcessFactory.eINSTANCE.createStartTimerEvent();
event.setAt(at);
event.setFrom(from);
event.setMonth(month);
......@@ -59,14 +61,15 @@ public class StartTimerConditionCustomMigration extends CustomMigration {
event.setMinutes(minutes);
event.setSeconds(seconds);
StartTimerScriptType type = null;
for(StartTimerScriptType t : StartTimerScriptType.values()){
for(final StartTimerScriptType t : StartTimerScriptType.values()){
if(t.getLiteral().equals(scriptType.getLiteral())){
type = t;
}
}
event.setScriptType(type);
if(TimerUtil.isCycle(event)){
String cron = TimerUtil.getTimerExpressionContent(event);
if(LegacyTimerExpressionGenerator.isCycle(event)){
timerExpressionGenerator = new LegacyTimerExpressionGenerator();
final String cron = timerExpressionGenerator.getTimerExpressionContent(event);
if(cron != null){
Instance condition = startTimer.get("condition");
if(condition != null){
......@@ -86,14 +89,14 @@ public class StartTimerConditionCustomMigration extends CustomMigration {
}
condition = model.newInstance("expression.Expression");
condition.set("name", "fixedDate");
condition.set("content", TimerUtil.generateConstant(at));
condition.set("content", LegacyTimerExpressionGenerator.generateConstant(at));
condition.set("returnType", Date.class.getName());
condition.set("type", ExpressionConstants.SCRIPT_TYPE);
condition.set("interpreter", ExpressionConstants.GROOVY);
startTimer.set("condition", condition);
}
}
}
}
/**
* Copyright (C) 2010 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.migration.utils;
import java.util.Calendar;
import java.util.Date;
import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.process.StartTimerEvent;
/**
* Utility that generate recusrion script for timers
*
* @author Baptiste Mesta
* @author Romain Bioteau
*/
public class LegacyTimerExpressionGenerator {
private static final String CRON_EVERY = "*/";
public static final String GROOVY_PREFIX = "${";//$NON-NLS-1$
public static final String GROOVY_SUFFIX = "}";//$NON-NLS-1$
private static final String CRON_WILDCARD = "*";
private static final String CRON_NOT_SET = "?";
private static final String CRON_DAY_OF_MONTH = "#";
public String getTimerExpressionContent(final StartTimerEvent item) {
switch (item.getScriptType()) {
case GROOVY:
return conditionExpressionContent(item);
case YEARLY_DAY_OF_MONTH:
return yearlyDayOfMonthCron(item);
case YEARLY_DAY_OF_YEAR:
return yearlyDayOfYearCron(item);
case MONTHLY_DAY_NUMBER:
return monthlyDayNumberCron(item);
case MONTHLY_DAY_OF_WEEK:
return monthlyDayOfWeekCron(item);
case WEEKLY:
return weeklyCron(item);
case DAILY:
return dailyCron(item);
case HOURLY:
return generateCronExpression(CRON_WILDCARD, CRON_EVERY + item.getHours(), CRON_WILDCARD, CRON_WILDCARD, CRON_NOT_SET);
case MINUTELY:
return generateCronExpression(CRON_EVERY + item.getMinutes(), CRON_WILDCARD, CRON_WILDCARD, CRON_WILDCARD, CRON_NOT_SET);
case CONSTANT:
return generateConstant(item.getAt());
default:
throw new IllegalArgumentException("Timer type" + item.getScriptType() + " is not supported.");
}
}
protected String dailyCron(final StartTimerEvent item) {
Calendar calendar;
calendar = Calendar.getInstance();
calendar.setTime(item.getAt());
return generateCronExpression(String.valueOf(calendar.get(Calendar.MINUTE)), String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), CRON_WILDCARD,
CRON_WILDCARD, CRON_NOT_SET);
}
protected String weeklyCron(final StartTimerEvent item) {
Calendar calendar;
calendar = Calendar.getInstance();
calendar.setTime(item.getAt());
return generateCronExpression(String.valueOf(calendar.get(Calendar.MINUTE)), String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), CRON_NOT_SET,
CRON_WILDCARD, String.valueOf(item.getDay()));