Skip to content

Commit

Permalink
Merge pull request #8 from ps-md2/feature/model-validators
Browse files Browse the repository at this point in the history
Feature/model validators
  • Loading branch information
Julia65 committed Dec 19, 2014
2 parents a335f1b + 7437ac9 commit 9bc9c25
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package TestProject.models

entity LowerCaseTest {
Test2: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ enum DrugStatus {

entity Pharmacy {
drug: Drug (default "hallo")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package TestProject.models

entity RepeatedParams {
test3: integer (optional, optional)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package TestProject.models

entity UnsupportedFeatures {
testDate: date (max 2014-10-12)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package TestProject.models

entity upperCaseTest {
test1: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,62 @@ class Validator_Model_Test {

@Inject extension ParseHelper<MD2Model>
@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
}

/**
* Checks whether the error for defaults assigned to entities is thrown.
*/
@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)
}

}
/**
* Checks whether the warning for unsupported language features is thrown.
*/
@Test
def checkAttributeTypeParam() {
unsupportedFeatures.assertWarning(MD2Package::eINSTANCE.attributeTypeParam, ModelValidator::UNSUPPORTEDPARAMTYPE)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -461,68 +457,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<EClass> 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
/////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
* Validators for all model elements of MD2.
Expand All @@ -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
Expand All @@ -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
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9bc9c25

Please sign in to comment.