View Javadoc
1   package de.fhdw.wtf.tooling.test;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.fail;
5   
6   import java.net.URI;
7   import java.util.Collection;
8   import java.util.Iterator;
9   
10  import org.junit.Test;
11  
12  import de.fhdw.wtf.common.ast.Group;
13  import de.fhdw.wtf.common.ast.GroupElement;
14  import de.fhdw.wtf.common.ast.Model;
15  import de.fhdw.wtf.common.ast.type.ClassType;
16  import de.fhdw.wtf.common.ast.visitor.GroupElementVisitor;
17  import de.fhdw.wtf.common.exception.editor.CheckException;
18  import de.fhdw.wtf.common.exception.editor.MarkableCheckException;
19  import de.fhdw.wtf.common.exception.editor.MultipleCheckExceptions;
20  import de.fhdw.wtf.common.exception.parser.NoColonException;
21  import de.fhdw.wtf.common.exception.parser.NoPlusSymbolException;
22  import de.fhdw.wtf.common.exception.referencer.InvalidTypeReferenceException;
23  import de.fhdw.wtf.common.exception.walker.AbstractOperationsException;
24  import de.fhdw.wtf.common.exception.walker.BaseTypeInheritanceException;
25  import de.fhdw.wtf.common.exception.walker.DoubleGroupcomponentException;
26  import de.fhdw.wtf.testutil.TestUtil;
27  import de.fhdw.wtf.tooling.SyntaxCheck;
28  
29  /**
30   * Test {@link SyntaxCheck} - scanning, parsing and referencing and walker checks - in one integration test.
31   */
32  public class SyntaxCheckTest {
33  	
34  	/**
35  	 * Path to the file directory with the test resources.
36  	 */
37  	private static final String PATH_TO_TEST_FILE_DIR = "src/test/java/de/fhdw/wtf/tooling/test/files/";
38  	
39  	/**
40  	 * Suffix for model files.
41  	 */
42  	private static final String MODEL_SUFFIX = "model";
43  	
44  	/**
45  	 * Tests the scanning, parsing, referencing and checks of a model-string.
46  	 * 
47  	 * @throws Exception
48  	 *             {@link java.io.FileNotFoundException}, {@link java.io.IOException}, {@link MultipleCheckExceptions},
49  	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicDependencyException}, {@link InterruptedException},
50  	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicPartDefinitionException},
51  	 *             {@link java.util.concurrent.ExecutionException}
52  	 */
53  	@Test
54  	public void testSimpleModelFromString() throws Exception {
55  		final String input = TestUtil.readFileToString(PATH_TO_TEST_FILE_DIR + "Person" + '.' + MODEL_SUFFIX);
56  		final SyntaxCheck instance = SyntaxCheck.getInstance();
57  		final Model model = instance.getModelFromString(input);
58  		
59  		// -- should be one group --//
60  		final Collection<Group> groups = model.getGroups();
61  		assertEquals(1, groups.size());
62  		
63  		// -- should be three groupElements --//
64  		final Group group1 = groups.iterator().next();
65  		final Collection<GroupElement> groupElements = group1.getGroupElements();
66  		assertEquals(3, groupElements.size());
67  		final Iterator<GroupElement> iterator = groupElements.iterator();
68  		
69  		// -- first groupElement should have one attribute --//
70  		iterator.next().accept(new GroupElementVisitor() {
71  			
72  			@Override
73  			public void handle(final Group group) {
74  				fail();
75  			}
76  			
77  			@Override
78  			public void handle(final ClassType clss) {
79  				assertEquals(1, clss.getAttributes().size());
80  				assertEquals(0, clss.getOperations().size());
81  				assertEquals(1, clss.getModifiers().size());
82  			}
83  		});
84  		
85  		// -- second groupElement should have no attribute --//
86  		iterator.next().accept(new GroupElementVisitor() {
87  			
88  			@Override
89  			public void handle(final Group group) {
90  				fail();
91  			}
92  			
93  			@Override
94  			public void handle(final ClassType clss) {
95  				assertEquals(0, clss.getAttributes().size());
96  				assertEquals(0, clss.getOperations().size());
97  				assertEquals(0, clss.getModifiers().size());
98  			}
99  		});
100 		
101 		// -- third groupElement should have one attribute --//
102 		iterator.next().accept(new GroupElementVisitor() {
103 			
104 			@Override
105 			public void handle(final Group group) {
106 				fail();
107 			}
108 			
109 			@Override
110 			public void handle(final ClassType clss) {
111 				assertEquals(1, clss.getAttributes().size());
112 				assertEquals(0, clss.getOperations().size());
113 				assertEquals(0, clss.getModifiers().size());
114 			}
115 		});
116 	}
117 	
118 	/**
119 	 * Tests the scanning, parsing, referencing and checks of a model-file.
120 	 * 
121 	 * @throws Exception
122 	 *             {@link java.io.FileNotFoundException}, {@link java.io.IOException}, {@link MultipleCheckExceptions},
123 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicDependencyException}, {@link InterruptedException},
124 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicPartDefinitionException},
125 	 *             {@link java.util.concurrent.ExecutionException}
126 	 */
127 	@Test
128 	public void testSimpleModelFromFile() throws Exception {
129 		final SyntaxCheck instance = SyntaxCheck.getInstance();
130 		final URI uri = new URI(PATH_TO_TEST_FILE_DIR + "Person" + '.' + MODEL_SUFFIX);
131 		final Model model = instance.getModelFromFile(uri);
132 		
133 		// -- should be one group --//
134 		final Collection<Group> groups = model.getGroups();
135 		assertEquals(1, groups.size());
136 		
137 		// -- should be three groupElements --//
138 		final Group group1 = groups.iterator().next();
139 		final Collection<GroupElement> groupElements = group1.getGroupElements();
140 		assertEquals(3, groupElements.size());
141 		final Iterator<GroupElement> iterator = groupElements.iterator();
142 		
143 		// -- first groupElement should have one attribute --//
144 		iterator.next().accept(new GroupElementVisitor() {
145 			
146 			@Override
147 			public void handle(final Group group) {
148 				fail();
149 			}
150 			
151 			@Override
152 			public void handle(final ClassType clss) {
153 				assertEquals(1, clss.getAttributes().size());
154 				assertEquals(0, clss.getOperations().size());
155 				assertEquals(1, clss.getModifiers().size());
156 			}
157 		});
158 		
159 		// -- second groupElement should have no attribute --//
160 		iterator.next().accept(new GroupElementVisitor() {
161 			
162 			@Override
163 			public void handle(final Group group) {
164 				fail();
165 			}
166 			
167 			@Override
168 			public void handle(final ClassType clss) {
169 				assertEquals(0, clss.getAttributes().size());
170 				assertEquals(0, clss.getOperations().size());
171 				assertEquals(0, clss.getModifiers().size());
172 			}
173 		});
174 		
175 		// -- third groupElement should have one attribute --//
176 		iterator.next().accept(new GroupElementVisitor() {
177 			
178 			@Override
179 			public void handle(final Group group) {
180 				fail();
181 			}
182 			
183 			@Override
184 			public void handle(final ClassType clss) {
185 				assertEquals(1, clss.getAttributes().size());
186 				assertEquals(0, clss.getOperations().size());
187 				assertEquals(0, clss.getModifiers().size());
188 			}
189 		});
190 	}
191 	
192 	/**
193 	 * Tests the scanning, parsing, referencing and checks of a model-file with some parser exceptions.
194 	 * 
195 	 * @throws Exception
196 	 *             {@link java.io.FileNotFoundException}, {@link java.io.IOException}, {@link MultipleCheckExceptions},
197 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicDependencyException}, {@link InterruptedException},
198 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicPartDefinitionException},
199 	 *             {@link java.util.concurrent.ExecutionException}
200 	 */
201 	@Test
202 	public void testInvalidModelFileWithParserExceptions() throws Exception {
203 		final SyntaxCheck instance = SyntaxCheck.getInstance();
204 		final URI uri = new URI(PATH_TO_TEST_FILE_DIR + "InvalidPerson" + '.' + MODEL_SUFFIX);
205 		try {
206 			instance.getModelFromFile(uri);
207 			fail();
208 		} catch (final MultipleCheckExceptions e) {
209 			assertEquals(3, e.getExeptionCount());
210 			final Iterator<CheckException> iterator = e.iterator();
211 			final MarkableCheckException noColonException = (MarkableCheckException) iterator.next();
212 			assertEquals(NoColonException.class, noColonException.getCause().getClass());
213 			final MarkableCheckException noSemicolonException = (MarkableCheckException) iterator.next();
214 			assertEquals(NoPlusSymbolException.class, noSemicolonException.getCause().getClass());
215 		}
216 	}
217 	
218 	/**
219 	 * Tests the scanning, parsing, referencing and checks of a model-file with some walker exceptions.
220 	 * 
221 	 * @throws Exception
222 	 *             {@link java.io.FileNotFoundException}, {@link java.io.IOException}, {@link MultipleCheckExceptions},
223 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicDependencyException}, {@link InterruptedException},
224 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicPartDefinitionException},
225 	 *             {@link java.util.concurrent.ExecutionException}
226 	 */
227 	@Test
228 	public void testInvalidModelFileWithWalkerExceptions() throws Exception {
229 		final SyntaxCheck instance = SyntaxCheck.getInstance();
230 		final URI uri = new URI(PATH_TO_TEST_FILE_DIR + "ProblemModel" + '.' + MODEL_SUFFIX);
231 		try {
232 			instance.getModelFromFile(uri);
233 			fail();
234 		} catch (final MultipleCheckExceptions e) {
235 			assertEquals(2, e.getExeptionCount());
236 			final Iterator<CheckException> iterator = e.iterator();
237 			assertEquals(DoubleGroupcomponentException.class, iterator.next().getCause().getClass());
238 			assertEquals(AbstractOperationsException.class, iterator.next().getCause().getClass());
239 		}
240 	}
241 	
242 	/**
243 	 * Tests the scanning, parsing, referencing and checks of a model-file with a referencer exceptions.
244 	 * 
245 	 * @throws Exception
246 	 *             {@link java.io.FileNotFoundException}, {@link java.io.IOException}, {@link MultipleCheckExceptions},
247 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicDependencyException}, {@link InterruptedException},
248 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicPartDefinitionException},
249 	 *             {@link java.util.concurrent.ExecutionException}
250 	 */
251 	@Test
252 	public void testInvalidModelFileWithReferencerException() throws Exception {
253 		final SyntaxCheck instance = SyntaxCheck.getInstance();
254 		final URI uri = new URI(PATH_TO_TEST_FILE_DIR + "ProblemModel2" + '.' + MODEL_SUFFIX);
255 		try {
256 			instance.getModelFromFile(uri);
257 			fail();
258 		} catch (final MultipleCheckExceptions e) {
259 			assertEquals(1, e.getExeptionCount());
260 			final Iterator<CheckException> iterator = e.iterator();
261 			assertEquals(InvalidTypeReferenceException.class, iterator.next().getCause().getClass());
262 		}
263 	}
264 	
265 	/**
266 	 * @throws Exception
267 	 *             {@link java.io.FileNotFoundException}, {@link java.io.IOException}, {@link MultipleCheckExceptions},
268 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicDependencyException}, {@link InterruptedException},
269 	 *             {@link de.fhdw.wtf.common.exception.walker.CyclicPartDefinitionException},
270 	 *             {@link java.util.concurrent.ExecutionException}
271 	 */
272 	@Test
273 	public void testInheritanceFromBaseType() throws Exception {
274 		final String input = "personGroup:group=[Person:class=String + {name:String*;};];";
275 		final SyntaxCheck instance = SyntaxCheck.getInstance();
276 		
277 		try {
278 			instance.getModelFromString(input);
279 			fail();
280 		} catch (final MultipleCheckExceptions e) {
281 			assertEquals(1, e.getExeptionCount());
282 			final Iterator<CheckException> iterator = e.iterator();
283 			assertEquals(BaseTypeInheritanceException.class, iterator.next().getCause().getClass());
284 		}
285 	}
286 }