summaryrefslogtreecommitdiffstats
blob: fec9e498170b68b9cb10ff75b92461fd5763ddde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package org.eclipse.jet.tests.compiler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.codegen.jet.JETException;
import org.eclipse.jet.compiler.Problem;
import org.eclipse.jet.internal.compiler.CommentElementDelegate;
import org.eclipse.jet.internal.compiler.DeclarationElementDelegate;
import org.eclipse.jet.internal.compiler.XMLElementDelegate;
import org.eclipse.jet.internal.parser.JETCoreElement;
import org.eclipse.jet.internal.parser.JETMark;
import org.eclipse.jet.internal.parser.JETParseEventListener2;
import org.eclipse.jet.internal.parser.JETParser;
import org.eclipse.jet.internal.parser.JETReader;
import org.eclipse.jet.internal.parser.ParseProblemSeverity;

public class TestJETParser extends TestCase {
	
	
	private static class SavedProblem {

		private final ParseProblemSeverity severity;
		private final int problemId;
		private final String message;
		private final Object[] msgArgs;
		private final int start;
		private final int end;
		private final int line;

		public SavedProblem(ParseProblemSeverity severity, int problemId, String message, Object[] msgArgs, int start, int end, int line) {
			this.severity = severity;
			this.problemId = problemId;
			this.message = message;
			this.msgArgs = msgArgs;
			this.start = start;
			this.end = end;
			this.line = line;
		}

		/**
		 * @return Returns the end.
		 */
		public final int getEnd() {
			return end;
		}

		/**
		 * @return Returns the line.
		 */
		public final int getLine() {
			return line;
		}

		/**
		 * @return Returns the message.
		 */
		public final String getMessage() {
			return message;
		}

		/**
		 * @return Returns the msgArgs.
		 */
		public final Object[] getMsgArgs() {
			return msgArgs;
		}

		/**
		 * @return Returns the problemId.
		 */
		public final int getProblemId() {
			return problemId;
		}

		/**
		 * @return Returns the severity.
		 */
		public final ParseProblemSeverity getSeverity() {
			return severity;
		}

		/**
		 * @return Returns the start.
		 */
		public final int getStart() {
			return start;
		}
		
	}
	
	private static class AllJET2SyntaxListener implements JETParseEventListener2 {

		private final StringBuffer buffer = new StringBuffer();
		private int indent = 0;
		private final String NL = System.getProperty("line.separator");
		private final JETReader reader;
		private final List problems = new ArrayList();
		
	
		public AllJET2SyntaxListener(JETReader reader) {
			this.reader = reader;
		}

		public void beginPageProcessing() throws JETException {
			doIndent();
			buffer.append("<jet2>").append(NL);
			indent++;
		}

		private void doIndent() {
			for(int i = 0; i < indent; i++) {
				buffer.append("    ");
			}
		}

		public void endPageProcessing() throws JETException {
			doIndent();
			buffer.append("</jet2>").append(NL);
		}

		public String getLog() {
			return buffer.toString();
		}

		public void handleCharData(char[] chars) throws JETException {
			doIndent();
			buffer.append("<data>").append(chars).append("</data>").append(NL);
		}

		public void handleComment(JETMark start, JETMark stop) {
			doIndent();
			buffer.append("<comment start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">");
			buffer.append(reader.getChars(start,stop));
			buffer.append("</comment>").append(NL);
			
		}

		public void handleDeclaration(JETMark start, JETMark stop) {
			doIndent();
			buffer.append("<declaration start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">");
			buffer.append(reader.getChars(start,stop));
			buffer.append("</declaration>").append(NL);
			
		}

		public void handleDirective(String directive, JETMark start,
				JETMark stop, Map attributes) throws JETException {
			doIndent();
			buffer.append("<directive name=\"" + directive + "\" start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">").append(NL);
			indent++;
			for (Iterator i = attributes.entrySet().iterator(); i.hasNext();) {
				Map.Entry entry = (Map.Entry) i.next();
				doIndent();
				buffer.append("<attribute name=\"" + entry.getKey() + "\">");
				buffer.append(entry.getValue()).append("</attribute>").append(NL);
				
			}
			indent--;
			doIndent();
			buffer.append("</directive>").append(NL);
		}

		public void handleExpression(JETMark start, JETMark stop, Map attributes)
				throws JETException {
			doIndent();
			buffer.append("<expression start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">");
			buffer.append(reader.getChars(start,stop));
			buffer.append("</expression>").append(NL);
		}

		public void handleScriptlet(JETMark start, JETMark stop, Map attributes)
				throws JETException {
			doIndent();
			buffer.append("<scriptlet start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">");
			buffer.append(reader.getChars(start,stop));
			buffer.append("</scriptlet>").append(NL);
		}

		public void handleXMLEmptyTag(String tagName, JETMark start, JETMark stop, Map attributeMap) {
			doIndent();
			buffer.append("<tag name=\"" + tagName + "\" start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">").append(NL);
			indent++;
			for (Iterator i = attributeMap.entrySet().iterator(); i.hasNext();) {
				Map.Entry entry = (Map.Entry) i.next();
				doIndent();
				buffer.append("<attribute name=\"" + entry.getKey() + "\">" + entry.getValue() + "</attribute>").append(NL);
			}
			indent--;
			doIndent();
			buffer.append("</tag>").append(NL);
		}

		public void handleXMLEndTag(String tagName, JETMark start, JETMark stop) {
			indent--;
			doIndent();
			buffer.append("</content>").append(NL);
			indent--;
			doIndent();
			buffer.append("</tag>").append(NL);
		}

		public void handleXMLStartTag(String tagName, JETMark start, JETMark stop, Map attributeMap) {
			doIndent();
			buffer.append("<tag name=\"" + tagName + "\" start=\"" + start.toShortString() + "\" stop=\"" + stop.toShortString() + "\">").append(NL);
			indent++;
			for (Iterator i = attributeMap.entrySet().iterator(); i.hasNext();) {
				Map.Entry entry = (Map.Entry) i.next();
				doIndent();
				buffer.append("<attribute name=\"" + entry.getKey() + "\">" + entry.getValue() + "</attribute>").append(NL);
			}
			doIndent();
			buffer.append("<content>").append(NL);
			indent++;
		}

		public boolean isKnownTag(String tagName) {
			return tagName.startsWith("test:");
		}

		/**
		 * @return Returns the problems.
		 */
		public final List getProblems() {
			return problems;
		}

		public void recordProblem(ParseProblemSeverity severity, int problemId, String message, Object[] msgArgs, int start, int end, int line) {
			problems.add(new SavedProblem(severity, problemId, message, msgArgs, start, end, line));
			
		}
	}

	private AllJET2SyntaxListener doJET2Parse(String urlString) throws IOException, MalformedURLException, FileNotFoundException, JETException {
		String fileName = Platform.resolve(new URL(urlString)).getFile();
		
		FileInputStream inputStream = new FileInputStream(new File(fileName));
		JETReader reader = new JETReader(urlString, inputStream, "UTF8");
		AllJET2SyntaxListener listener = new AllJET2SyntaxListener(reader);
		JETParser.Directive directive = new JETParser.Directive();
		directive.getDirectives().add("jet");
		
		JETCoreElement[] coreElements = new JETCoreElement[] {
				directive,
				new JETParser.Expression(),
				new CommentElementDelegate(),
				new DeclarationElementDelegate(),
				new JETParser.Scriptlet(),
				new XMLElementDelegate(),
		};
		JETParser parser = new JETParser(reader, listener, coreElements);
		
		parser.parse();
		return listener;
	}

	private void doUnclosedElementTest(String urlString, String openChars, int expectedProblemId) throws IOException, MalformedURLException, FileNotFoundException, JETException {
		final AllJET2SyntaxListener listener = doJET2Parse(urlString);
		final List problems = listener.getProblems();
		assertEquals(1, problems.size());
		SavedProblem problem = (SavedProblem) problems.get(0);
		assertEquals(ParseProblemSeverity.ERROR, problem.getSeverity());
		assertEquals(expectedProblemId, problem.getProblemId());
	}

	private String getFileContents(String urlString) throws Exception {
		String fileName = Platform.resolve(new URL(urlString)).getFile();
		FileInputStream inputStream = new FileInputStream(new File(fileName));
		FileChannel channel = inputStream.getChannel();
		MappedByteBuffer mapBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
		Charset utf8Decoder = Charset.forName("UTF-8");
		CharBuffer charBuffer = utf8Decoder.decode(mapBuffer);
		String contents = charBuffer.toString();
		channel.close();
		inputStream.close();
		return contents;
	}

	public void testAllJET2Syntax() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/alljet2.jet";
		AllJET2SyntaxListener listener = doJET2Parse(urlString);
		
		assertEquals(getFileContents(urlString + ".expected"), listener.getLog());
	}
	
	public void testUnclosedComment() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedComment.jet";
		String openChars = "<%--";

		try {
			doUnclosedElementTest(urlString, openChars, 0);
			fail();
		} catch(JETException e) {
			// success.
		}
		
		
	}
	
	public void testUnclosedDeclaration() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedDeclaration.jet";
		String openChars = "<%!";

		try {
			doUnclosedElementTest(urlString, openChars, 0);
			fail();
		} catch(JETException e) {
			// success.
		}
	}
	
	public void testUnclosedDirective() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedDirective.jet";
		String openChars = "<%@";

		try {
			doUnclosedElementTest(urlString, openChars, 0);
			fail();
		} catch(JETException e) {
			// success.
		}
	}

	public void testUnclosedExpression() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedExpression.jet";
		String openChars = "<%=";

		try {
			doUnclosedElementTest(urlString, openChars, 0);
			fail();
		} catch(JETException e) {
			// success.
		}
	}
	
	public void testUnclosedScriptlet() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedScriplet.jet";
		String openChars = "<%";

		try {
			doUnclosedElementTest(urlString, openChars, 0);
			fail();
		} catch(JETException e) {
			// success.
		}
	}
	
	public void testUnclosedXMLStart() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedXMLStart.jet";
		String openChars = "<";

		doUnclosedElementTest(urlString, openChars, Problem.UnterminatedXMLTag);
	}
	
	public void testUnclosedXMLEnd() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/unclosedXMLEnd.jet";
		String openChars = "</";

		doUnclosedElementTest(urlString, openChars, Problem.UnterminatedXMLTag);
	}
	
	public void testXMLEndWithAttributes() throws Exception {
		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/xmlEndWithAttributes.jet";
		String openChars = "</";

		doUnclosedElementTest(urlString, openChars, Problem.UnterminatedXMLTag);
	}
	
	// this error is not detected by the parser, but by post parse processing.
//	public void testXMLStartWithoutEnd() throws Exception {
//		String urlString = "platform:/plugin/org.eclipse.jet.tests/data/xmlStartWithoutEnd.jet";
//		String openChars = "<test:start>";
//
//		doUnclosedElementTest(urlString, openChars, Problem.MissingXmlEndTag);
//	}
}