1 package de.fhdw.wtf.core.integration.testutil;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5
6 import java.util.Collection;
7 import java.util.Iterator;
8
9 import de.fhdw.wtf.common.ast.Attribute;
10 import de.fhdw.wtf.common.ast.Group;
11 import de.fhdw.wtf.common.ast.GroupElement;
12 import de.fhdw.wtf.common.ast.Model;
13 import de.fhdw.wtf.common.ast.Operation;
14 import de.fhdw.wtf.common.ast.type.AtomicType;
15 import de.fhdw.wtf.common.ast.type.ByReferenceState;
16 import de.fhdw.wtf.common.ast.type.ClassType;
17 import de.fhdw.wtf.common.ast.type.CompositeType;
18 import de.fhdw.wtf.common.ast.type.ListType;
19 import de.fhdw.wtf.common.ast.type.MapType;
20 import de.fhdw.wtf.common.ast.type.ProductElementType;
21 import de.fhdw.wtf.common.ast.type.ProductType;
22 import de.fhdw.wtf.common.ast.type.SumType;
23 import de.fhdw.wtf.common.ast.type.ThrownType;
24 import de.fhdw.wtf.common.ast.type.Type;
25 import de.fhdw.wtf.common.ast.type.TypeProxy;
26 import de.fhdw.wtf.common.ast.visitor.CompositeTypeVisitor;
27 import de.fhdw.wtf.common.ast.visitor.GroupElementVisitor;
28 import de.fhdw.wtf.common.ast.visitor.TypeVisitor;
29 import de.fhdw.wtf.walker.walker.HelperUtils;
30
31 @SuppressWarnings("javadoc")
32 public class AssertOperations {
33
34 public void assertAllReferencesByType(final Model model) {
35 final Iterator<Group> modelGroups = model.getGroups().iterator();
36 while (modelGroups.hasNext()) {
37 final Group current = modelGroups.next();
38 this.assertByTypeInGroup(current);
39 }
40 }
41
42 private void assertByTypeInGroup(final Group group) {
43 final Iterator<GroupElement> elements = group.getGroupElements().iterator();
44 while (elements.hasNext()) {
45 final GroupElement current = elements.next();
46 current.accept(new GroupElementVisitor() {
47 @Override
48 public void handle(final ClassType clss) {
49 AssertOperations.this.assertByTypeInClass(clss);
50 }
51
52 @Override
53 public void handle(final Group group) {
54 AssertOperations.this.assertByTypeInGroup(group);
55 }
56 });
57 }
58 }
59
60 private void assertByTypeInClass(final ClassType clazz) {
61 this.assertByTypeSuperTypes(clazz.getSuperTypes());
62 this.assertByTypeAttributes(clazz.getAttributes());
63 this.assertByTypeOperations(clazz.getOperations());
64 }
65
66 private void assertByTypeSuperTypes(final Collection<Type> supertypes) {
67 for (final Type supertype : supertypes) {
68 try {
69 HelperUtils.getReferencedType(supertype);
70 } catch (final Error e) {
71 fail("The State is no ByReferenceState");
72 }
73 }
74 }
75
76 private void assertByTypeAttributes(final Collection<Attribute> attributes) {
77 for (final Attribute attribute : attributes) {
78 final Type current = attribute.getAttrType();
79 this.assertByTypeType(current);
80 }
81 }
82
83 private void assertByTypeOperations(final Collection<Operation> operations) {
84 for (final Operation operation : operations) {
85 final Type returnValue = operation.getReturnType();
86 this.assertByTypeType(returnValue);
87 final ProductType params = operation.getSignature().getParameters();
88 this.assertByTypeType(params);
89 }
90 }
91
92 private void assertByTypeType(final Type type) {
93 type.accept(new TypeVisitor() {
94
95 @Override
96 public void handle(final TypeProxy s) {
97 assertEquals(true, s.getState() instanceof ByReferenceState);
98 }
99
100 @Override
101 public void handle(final CompositeType c) {
102 c.accept(new CompositeTypeVisitor() {
103
104 @Override
105 public void handle(final SumType sum) {
106 for (final Type sumElement : sum.getElements()) {
107 AssertOperations.this.assertByTypeType(sumElement);
108 }
109 }
110
111 @Override
112 public void handle(final ProductType product) {
113 for (final ProductElementType productElement : product.getElements()) {
114 AssertOperations.this.assertByTypeType(productElement.getType());
115 }
116 }
117
118 @Override
119 public void handle(final MapType map) {
120 AssertOperations.this.assertByTypeType(map.getKey());
121 AssertOperations.this.assertByTypeType(map.getOf());
122 }
123
124 @Override
125 public void handle(final ListType list) {
126 AssertOperations.this.assertByTypeType(list.getOf());
127 }
128
129 @Override
130 public void handle(final ThrownType thrownType) {
131 AssertOperations.this.assertByTypeType(thrownType.getUnderlyingType());
132 }
133 });
134 }
135
136 @Override
137 public void handle(final AtomicType s) {
138 fail();
139
140 }
141 });
142 }
143
144 }