From 67dbafa8ce62d3a379badf460a02c8ede053beff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Mo=CC=88ser?= Date: Wed, 17 Dec 2014 18:43:38 +0100 Subject: [PATCH 1/3] Extract model validators from legacy validators. --- .../framework/validation/LegacyValidator.java | 66 ---------------- .../framework/validation/ModelValidator.xtend | 77 +++++++++++++++++++ .../validation/ValidatorHelpers.java | 2 +- 3 files changed, 78 insertions(+), 67 deletions(-) diff --git a/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/LegacyValidator.java b/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/LegacyValidator.java index f0afbce6..e32b6728 100644 --- a/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/LegacyValidator.java +++ b/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/LegacyValidator.java @@ -19,9 +19,6 @@ import de.wwu.md2.framework.mD2.AbstractViewGUIElementRef; import de.wwu.md2.framework.mD2.AlternativesPane; -import de.wwu.md2.framework.mD2.Attribute; -import de.wwu.md2.framework.mD2.AttributeType; -import de.wwu.md2.framework.mD2.AttributeTypeParam; import de.wwu.md2.framework.mD2.AutoGeneratedContentElement; import de.wwu.md2.framework.mD2.ContainerElement; import de.wwu.md2.framework.mD2.ContainerElementReference; @@ -45,7 +42,6 @@ import de.wwu.md2.framework.mD2.MD2Package; import de.wwu.md2.framework.mD2.Main; import de.wwu.md2.framework.mD2.Model; -import de.wwu.md2.framework.mD2.ModelElement; import de.wwu.md2.framework.mD2.ReferencedModelType; import de.wwu.md2.framework.mD2.SimpleDataType; import de.wwu.md2.framework.mD2.SimpleType; @@ -509,68 +505,6 @@ public void checkFilterMultiplicity(ContentProvider contentProvider) { } - ///////////////////////////////////////////////////////// - /// Model layer - ///////////////////////////////////////////////////////// - - /** - * Enforce conventions: - * Warn the user if the defined entity or enum does not start with an upper case letter - * - * @param modelElement - */ - @Check - public void checkEntitiesStartsWithCapital(ModelElement modelElement) { - if(!Character.isUpperCase(modelElement.getName().charAt(0))) { - warning("Entity and Enum identifiers should start with an upper case letter", MD2Package.eINSTANCE.getModelElement_Name()); - } - } - - /** - * Enforce conventions: - * Warn the user if the feature name of an entity does not start with a lower case letter - * - * @param feature - */ - @Check - public void checkFeaturesStartsWithCapital(Attribute attribute) { - if(!Character.isLowerCase(attribute.getName().charAt(0))) { - warning("Entities should start with a lower case letter", MD2Package.eINSTANCE.getAttribute_Name()); - } - } - - /** - * Prevent from defining the same parameter multiple times for the entity attribute constraint - * - * @param attribute - */ - @Check - public void checkRepeatedParams(AttributeType attributeType) { - helper.repeatedParamsError(attributeType, null, this, - "AttrIsOptional", "optional", - "AttrIdentifier", "identifier", - "AttrIntMax", "max", "AttrIntMin", "min", - "AttrFloatMax", "max", "AttrFloatMin", "min", - "AttrStringMax", "maxLength", "AttrStringMin", "minLength", - "AttrDateMax", "max", "AttrDateMin", "min", - "AttrTimeMax", "max", "AttrTimeMin", "min", - "AttrDateTimeMax", "max", "AttrDateTimeMin", "min"); - } - - /** - * Inform the user about unsupported language features. - * @param attributeTypeParam - */ - @Check - public void checkAttributeTypeParam(AttributeTypeParam attributeTypeParam) { - Collection unsupportedParamTypes = Sets.newHashSet(MD2Package.eINSTANCE.getAttrDateMax(), MD2Package.eINSTANCE.getAttrDateMin(), MD2Package.eINSTANCE.getAttrTimeMax(), MD2Package.eINSTANCE.getAttrTimeMin(), MD2Package.eINSTANCE.getAttrDateTimeMax(), MD2Package.eINSTANCE.getAttrDateTimeMin()); - if (unsupportedParamTypes.contains(attributeTypeParam.eClass())) { - // attributeTypeParam.eClass().getEAllStructuralFeatures().get(0)); - warning("Unsupported language feature: "+attributeTypeParam.eClass().getName()+". Using this parameter will have no effect.", MD2Package.eINSTANCE.getAttributeTypeParam().getEIDAttribute()); - } - } - - ///////////////////////////////////////////////////////// /// Private helpers ///////////////////////////////////////////////////////// diff --git a/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ModelValidator.xtend b/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ModelValidator.xtend index 6112d2cc..e4dc8f54 100644 --- a/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ModelValidator.xtend +++ b/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ModelValidator.xtend @@ -6,6 +6,12 @@ import de.wwu.md2.framework.mD2.Entity import de.wwu.md2.framework.mD2.ReferencedType import org.eclipse.xtext.validation.Check import org.eclipse.xtext.validation.EValidatorRegistrar +import de.wwu.md2.framework.mD2.ModelElement +import de.wwu.md2.framework.mD2.MD2Package +import de.wwu.md2.framework.mD2.Attribute +import de.wwu.md2.framework.mD2.AttributeType +import de.wwu.md2.framework.mD2.AttributeTypeParam +import com.google.common.collect.Sets /** * Valaidators for all model elements of MD2. @@ -17,7 +23,14 @@ class ModelValidator extends AbstractMD2JavaValidator { // nothing to do } + @Inject + private ValidatorHelpers helper; + public static final String DEFAULTREFERENCEVALUE = "defaultReferenceValue" + public static final String ENTITYENUMUPPERCASE = "entityEnumUppercase" + public static final String ATTRIBUTELOWERCASE = "attributeLowercase" + public static final String REPEATEDPARAMS = "repeatedParams" + public static final String UNSUPPORTEDPARAMTYPE = "unsupportedParamType" ///////////////////////////////////////////////////////// /// Validators @@ -39,5 +52,69 @@ class ModelValidator extends AbstractMD2JavaValidator { } } + /** + * Enforce conventions: + * Warn the user if the defined entity or enum does not start with an upper case letter + * + * @param modelElement + */ + @Check + def checkEntityStartsWithCapital(ModelElement modelElement) { + if(!Character.isUpperCase(modelElement.name.charAt(0))) { + warning("Entity and Enum identifiers should start with an upper case letter", MD2Package.eINSTANCE.modelElement_Name, -1, ENTITYENUMUPPERCASE); + } + } + + /** + * Enforce conventions: + * Warn the user if the attribute name of an entity does not start with a lower case letter + * + * @param feature + */ + @Check + def checkAttributeStartsWithCapital(Attribute attribute) { + if(!Character.isLowerCase(attribute.name.charAt(0))) { + warning("Attribute should start with a lower case letter", MD2Package.eINSTANCE.attribute_Name, -1, ATTRIBUTELOWERCASE); + } + } + + /** + * Prevent from defining the same parameter multiple times for the entity attribute constraint + * + * @param attribute + */ + @Check + def checkRepeatedParams(AttributeType attributeType) { + helper.repeatedParamsError(attributeType, null, this, + "AttrIsOptional", "optional", + "AttrIdentifier", "identifier", + "AttrIntMax", "max", "AttrIntMin", "min", + "AttrFloatMax", "max", "AttrFloatMin", "min", + "AttrStringMax", "maxLength", "AttrStringMin", "minLength", + "AttrDateMax", "max", "AttrDateMin", "min", + "AttrTimeMax", "max", "AttrTimeMin", "min", + "AttrDateTimeMax", "max", "AttrDateTimeMin", "min"); + } + /** + * Inform the user about unsupported language features. + * @param attributeTypeParam + */ + @Check + def checkAttributeTypeParam(AttributeTypeParam attributeTypeParam) { + var unsupportedParamTypes = Sets.newHashSet( + MD2Package.eINSTANCE.attrDateMax, + MD2Package.eINSTANCE.attrDateMin, + MD2Package.eINSTANCE.attrTimeMax, + MD2Package.eINSTANCE.attrTimeMin, + MD2Package.eINSTANCE.attrDateTimeMax, + MD2Package.eINSTANCE.attrDateTimeMin + ); + + if (unsupportedParamTypes.contains(attributeTypeParam.eClass)) { + warning("Unsupported language feature: " + attributeTypeParam.eClass.name + ". Using this parameter will have no effect.", + MD2Package.eINSTANCE.attributeTypeParam.EIDAttribute, -1, UNSUPPORTEDPARAMTYPE + ); + } + } } diff --git a/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ValidatorHelpers.java b/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ValidatorHelpers.java index 1e382b40..79cdc7fb 100644 --- a/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ValidatorHelpers.java +++ b/de.wwu.md2.framework/src/de/wwu/md2/framework/validation/ValidatorHelpers.java @@ -102,7 +102,7 @@ public void repeatedParamsError(EObject element, EStructuralFeature literal, Val String name = ((EObject)param).eClass().getName(); if(set.contains(name)) { acceptor.acceptError("Parameter \"" + eClassToNameMapping.get(name) - + "\" has been defined multiple times", element, literal, -1, null); + + "\" has been defined multiple times", element, literal, -1, ModelValidator.REPEATEDPARAMS); break; } else { set.add(name); From 14f05e2595f0c0ee5e5722b9acdc902fa59af245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Mo=CC=88ser?= Date: Wed, 17 Dec 2014 18:44:07 +0100 Subject: [PATCH 2/3] Add test cases for model validators. --- .../model/validator/LowercaseAttribute.md2 | 5 ++ .../tests/dsl/model/validator/Model.md2 | 2 +- .../model/validator/RepeatedParameters.md2 | 5 ++ .../model/validator/UnsupportedFeatures.md2 | 5 ++ .../dsl/model/validator/UppercaseEntity.md2 | 5 ++ .../validator/Validator_Model_Test.xtend | 52 ++++++++++++++++--- .../framework/tests/utils/ModelProvider.java | 7 +++ 7 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/LowercaseAttribute.md2 create mode 100644 de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/RepeatedParameters.md2 create mode 100644 de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UnsupportedFeatures.md2 create mode 100644 de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UppercaseEntity.md2 diff --git a/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/LowercaseAttribute.md2 b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/LowercaseAttribute.md2 new file mode 100644 index 00000000..51523134 --- /dev/null +++ b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/LowercaseAttribute.md2 @@ -0,0 +1,5 @@ +package TestProject.models + +entity LowerCaseTest { + Test2: string +} diff --git a/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/Model.md2 b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/Model.md2 index 7494b66c..1c6d02e1 100644 --- a/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/Model.md2 +++ b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/Model.md2 @@ -15,4 +15,4 @@ enum DrugStatus { entity Pharmacy { drug: Drug (default "hallo") -} \ No newline at end of file +} diff --git a/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/RepeatedParameters.md2 b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/RepeatedParameters.md2 new file mode 100644 index 00000000..517ba07a --- /dev/null +++ b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/RepeatedParameters.md2 @@ -0,0 +1,5 @@ +package TestProject.models + +entity RepeatedParams { + test3: integer (optional, optional) +} diff --git a/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UnsupportedFeatures.md2 b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UnsupportedFeatures.md2 new file mode 100644 index 00000000..66c84dbf --- /dev/null +++ b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UnsupportedFeatures.md2 @@ -0,0 +1,5 @@ +package TestProject.models + +entity UnsupportedFeatures { + testDate: date (max 2014-10-12) +} diff --git a/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UppercaseEntity.md2 b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UppercaseEntity.md2 new file mode 100644 index 00000000..ed39c41f --- /dev/null +++ b/de.wwu.md2.framework.tests/models/de/wwu/md2/framework/tests/dsl/model/validator/UppercaseEntity.md2 @@ -0,0 +1,5 @@ +package TestProject.models + +entity upperCaseTest { + test1: string +} diff --git a/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend b/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend index c1f82abb..c66b59cc 100644 --- a/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend +++ b/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend @@ -21,21 +21,59 @@ class Validator_Model_Test { @Inject extension ParseHelper @Inject extension ValidationTestHelper - MD2Model model_Testmodel; + MD2Model defaultReferenceValue; + MD2Model entityEnumUppercase; + MD2Model attributeLowercase; + MD2Model repeatedParameter; + MD2Model unsupportedFeatures; ResourceSet rs; - //Loads the different Elements of the Model into the corresponding variables @Before def void setUp() { rs = new ResourceSetImpl(); - model_Testmodel = VALIDATOR_MODEL_M.load.parse(rs); - + defaultReferenceValue = VALIDATOR_MODEL_M.load.parse(rs) + entityEnumUppercase = MODEL_VALIDATOR_UPPERCASE_ENTITY.load.parse + attributeLowercase = MODEL_VALIDATOR_LOWERCASE_ATTRIBUTE.load.parse + repeatedParameter = MODEL_VALIDATOR_REPEATED_PARAMETERS.load.parse + unsupportedFeatures = MODEL_VALIDATOR_UNSUPPORTED_FEATURES.load.parse + } + + @Test + def defaultValueForReferenceTest() { + defaultReferenceValue.assertError(MD2Package::eINSTANCE.attrEnumDefault, ModelValidator::DEFAULTREFERENCEVALUE) + } + + /** + * Checks whether the error for a non-capitalized entity is thrown. + */ + @Test + def checkEntitiesStartsWithCapitalTest() { + entityEnumUppercase.assertWarning(MD2Package::eINSTANCE.modelElement, ModelValidator::ENTITYENUMUPPERCASE) + } + + /** + * Checks whether the error for capitalized attributes is thrown. + */ + @Test + def checkAttributeStartsWithCapitalTest() { + attributeLowercase.assertWarning(MD2Package::eINSTANCE.attribute, ModelValidator::ATTRIBUTELOWERCASE) } + /** + * Checks whether the error for repeated declarations of parameters is thrown. + */ @Test - def testDefaultValueForReference() { - model_Testmodel.assertError(MD2Package::eINSTANCE.attrEnumDefault,ModelValidator::DEFAULTREFERENCEVALUE) + def checkRepeatedParamsTest() { + //repeatedParameter.assertNoErrors + repeatedParameter.assertError(MD2Package::eINSTANCE.attributeType, ModelValidator::REPEATEDPARAMS) } -} \ No newline at end of file + /** + * Checks whether the warning for unsupported language features is thrown. + */ + @Test + def checkAttributeTypeParam() { + unsupportedFeatures.assertWarning(MD2Package::eINSTANCE.attributeTypeParam, ModelValidator::UNSUPPORTEDPARAMTYPE) + } +} diff --git a/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/utils/ModelProvider.java b/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/utils/ModelProvider.java index 7f5816f1..2a8bb679 100644 --- a/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/utils/ModelProvider.java +++ b/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/utils/ModelProvider.java @@ -52,6 +52,13 @@ public class ModelProvider { public static final String FILTER_MULTIPLIZITY_C = "dsl/controller/contentProvider/validator/FilterMultiplizity.md2"; + // Model Validators + + public static final String MODEL_VALIDATOR_LOWERCASE_ATTRIBUTE = "dsl/model/validator/LowercaseAttribute.md2"; + public static final String MODEL_VALIDATOR_REPEATED_PARAMETERS = "dsl/model/validator/RepeatedParameters.md2"; + public static final String MODEL_VALIDATOR_UNSUPPORTED_FEATURES = "dsl/model/validator/UnsupportedFeatures.md2"; + public static final String MODEL_VALIDATOR_UPPERCASE_ENTITY = "dsl/model/validator/UppercaseEntity.md2"; + /** * Load the model from file * @param modelUri the model URIs are listed in the ModelProvider as static Strings. From 7437ac9974813898a4ef99e285d79b4c0eb56da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Mo=CC=88ser?= Date: Fri, 19 Dec 2014 12:22:53 +0100 Subject: [PATCH 3/3] Add comment for defaultValueForReferenceTest. --- .../tests/dsl/model/validator/Validator_Model_Test.xtend | 3 +++ 1 file changed, 3 insertions(+) diff --git a/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend b/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend index c66b59cc..d6832413 100644 --- a/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend +++ b/de.wwu.md2.framework.tests/src/de/wwu/md2/framework/tests/dsl/model/validator/Validator_Model_Test.xtend @@ -39,6 +39,9 @@ class Validator_Model_Test { unsupportedFeatures = MODEL_VALIDATOR_UNSUPPORTED_FEATURES.load.parse } + /** + * Checks whether the error for defaults assigned to entities is thrown. + */ @Test def defaultValueForReferenceTest() { defaultReferenceValue.assertError(MD2Package::eINSTANCE.attrEnumDefault, ModelValidator::DEFAULTREFERENCEVALUE)