Commit e92a04f3 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Tests for FbValidator

parent 6ebb387b
......@@ -12,5 +12,7 @@ Export-Package: tdt4250.fb.tests
Require-Bundle: org.eclipse.core.runtime,
tdt4250.facebook;visibility:=reexport,
org.eclipse.emf.ecore.xmi;visibility:=reexport,
org.junit;visibility:=reexport
org.junit;visibility:=reexport,
org.eclipse.acceleo.query;bundle-version="7.0.0",
org.antlr.runtime;bundle-version="4.7.2"
Bundle-ActivationPolicy: lazy
package tdt4250.fb.tests;
import java.util.List;
import org.eclipse.acceleo.query.delegates.AQLValidationDelegate;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EValidator.ValidationDelegate;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.Diagnostician;
import junit.framework.TestCase;
import tdt4250.fb.Facebook;
import tdt4250.fb.FbFactory;
import tdt4250.fb.FbPackage;
import tdt4250.fb.Like;
import tdt4250.fb.Post;
import tdt4250.fb.Share;
import tdt4250.fb.User;
import tdt4250.fb.util.FbResourceFactoryImpl;
public class FbValidatorTest extends TestCase {
// private Resource testInstance;
// private Diagnostic diagnostics;
protected Resource loadFbResource(String name) {
// A container of Resource, whether loaded explicitly or on-demand (automatically)
ResourceSet resSet = new ResourceSetImpl();
// relate the FbPackage identifier used in XMI files to the FbPackage instance (EPackage meta-object)
resSet.getPackageRegistry().put(FbPackage.eNS_URI, FbPackage.eINSTANCE);
// map the 'fb' file extension to our custom Resource.Factory implementation
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("fb", new FbResourceFactoryImpl());
return resSet.getResource(URI.createURI(FbValidatorTest.class.getResource(name + ".fb").toString()), true);
}
protected void setUp() throws Exception {
// register AQL (an OCL implementation) constraint support, so we can test OCL constraints, too
ValidationDelegate.Registry.INSTANCE.put("http://www.eclipse.org/acceleo/query/1.0", new AQLValidationDelegate());
}
public void testConstraint_userNotAuthor1() {
// create post with a like from other user
User user1 = FbFactory.eINSTANCE.createUser();
User user2 = FbFactory.eINSTANCE.createUser();
Post post = FbFactory.eINSTANCE.createPost();
post.setAuthor(user1);
Like like = FbFactory.eINSTANCE.createLike();
post.getLikes().add(like);
like.setUser(user2);
// run validation and check result
Diagnostic diagnostics = Diagnostician.INSTANCE.validate(like);
assertTrue(diagnostics.getSeverity() == Diagnostic.OK);
// make like user and post author the same
like.setUser(user1);
// re-run validation and check result
diagnostics = Diagnostician.INSTANCE.validate(like);
assertTrue(diagnostics.getSeverity() == Diagnostic.ERROR);
}
// test constraints with instances loaded from pre-made resource
public void testConstraint_userNotAuthor2() {
// load test resource
Resource resource = loadFbResource("testConstraint_userNotAuthor2");
// navigate to likes
List<Like> likes = ((Facebook) resource.getContents().get(0)).getUsers().get(1).getPosts().get(0).getLikes();
// run validation one first like and check result
Diagnostic diagnostics = Diagnostician.INSTANCE.validate(likes.get(0));
assertTrue(diagnostics.getSeverity() == Diagnostic.OK);
// run validation one second like and check result
diagnostics = Diagnostician.INSTANCE.validate(likes.get(1));
assertTrue(diagnostics.getSeverity() == Diagnostic.ERROR);
}
public void testConstraint_shareAuthorNotPostAuthor1() {
// create post and share with different authors
User user1 = FbFactory.eINSTANCE.createUser();
User user2 = FbFactory.eINSTANCE.createUser();
Post post = FbFactory.eINSTANCE.createPost();
post.setAuthor(user1);
Share share = FbFactory.eINSTANCE.createShare();
share.setOriginal(post);
share.setAuthor(user2);
// run validation and check result
Diagnostic diagnostics = Diagnostician.INSTANCE.validate(share);
assertTrue(diagnostics.getSeverity() == Diagnostic.OK);
// make authors the same
share.setAuthor(user1);
// re-run validation and check result
diagnostics = Diagnostician.INSTANCE.validate(share);
assertTrue(diagnostics.getSeverity() == Diagnostic.ERROR);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<fb:Facebook
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:fb="platform:/plugin/tdt4250.facebook/model/fb.ecore"
>
<users givenName="Hallvard"
familyName="Trætteberg"/>
<users givenName="Steinar"
familyName="Lien">
<posts author="//@users.1">
<likes
user="//@users.0"/>
<likes
user="//@users.1"/>
<body
contents=""/>
</posts>
</users>
</fb:Facebook>
......@@ -14,19 +14,19 @@
<users givenName="Steinar"
familyName="Lien">
<posts author="//@users.1">
<body
contents="Topp tur med gubbeklubben Ulenth!&#xA;Ryggjehø, Nautgardsoksle, Nautgardstinden, Austre Nautgardstinden og Stornubben. Og gitarspill, sang, øl, vin, mat (bl.a. Bessheims berømte viltbuffet!) og meninger. Sterke meninger...."/>
<likes
user="//@users.2"/>
<body
contents="Topp tur med gubbeklubben Ulenth!&#xA;Ryggjehø, Nautgardsoksle, Nautgardstinden, Austre Nautgardstinden og Stornubben. Og gitarspill, sang, øl, vin, mat (bl.a. Bessheims berømte viltbuffet!) og meninger. Sterke meninger...."/>
<comments
author="//@users.2">
<body
contents="Men hvor er &quot;broren &quot;da ??"/>
<reply
<replies
author="//@users.1">
<body
contents="Han var ikke tilstede da disse bildene ble tatt!"/>
</reply>
</replies>
</comments>
<comments
author="//@users.3">
......
......@@ -28,13 +28,13 @@
<eStructuralFeatures xsi:type="ecore:EAttribute" name="timestamp" eType="#//ELocalDateTime"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Post" eSuperTypes="#//AbstractContent">
<eStructuralFeatures xsi:type="ecore:EReference" name="body" eType="#//PostBody"
<eStructuralFeatures xsi:type="ecore:EReference" name="body" lowerBound="1" eType="#//PostBody"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="comments" upperBound="-1"
eType="#//Comment" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Comment" eSuperTypes="#//AbstractContent">
<eStructuralFeatures xsi:type="ecore:EReference" name="body" eType="#//CommentBody"
<eStructuralFeatures xsi:type="ecore:EReference" name="body" lowerBound="1" eType="#//CommentBody"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="replies" upperBound="-1"
eType="#//Comment" containment="true"/>
......
......@@ -10,7 +10,11 @@
<genEnums typeSafeEnumCompatible="false" ecoreEnum="fb.ecore#//LikeKind">
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/like"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/heart"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/compasion"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/compassion"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/funny"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/wow"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/sad"/>
<genEnumLiterals ecoreEnumLiteral="fb.ecore#//LikeKind/angry"/>
</genEnums>
<genDataTypes ecoreDataType="fb.ecore#//ELocalDateTime"/>
<genDataTypes ecoreDataType="fb.ecore#//ELocalDate"/>
......
......@@ -84,7 +84,7 @@ public abstract class AbstractContentImpl extends MinimalEObjectImpl.Container i
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @generated NOT
*/
protected AbstractContentImpl() {
super();
......
......@@ -143,7 +143,7 @@ public class FbFactoryImpl extends EFactoryImpl implements FbFactory {
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @generated NOT
*/
public Post createPost() {
PostImpl post = new PostImpl();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment