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
|
/*******************************************************************************
* Copyright (c) 2004, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jst.jsp.core.tests.contenttypeidentifier.contentspecific;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import junit.framework.TestCase;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter;
import org.eclipse.jst.jsp.core.internal.provisional.contenttype.ContentTypeIdForJSP;
import org.eclipse.jst.jsp.core.tests.taglibindex.BundleResourceUtil;
import org.eclipse.wst.css.core.internal.provisional.contenttype.ContentTypeIdForCSS;
import org.eclipse.wst.html.core.internal.provisional.contenttype.ContentTypeIdForHTML;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.ltk.modelhandler.EmbeddedTypeHandler;
import org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler;
import org.eclipse.wst.sse.core.internal.modelhandler.ModelHandlerRegistry;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.provisional.contenttype.ContentTypeIdForXML;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.Document;
public class TestModelHandlers extends TestCase {
private static ModelHandlerRegistry getModelHandlerRegistry() {
ModelHandlerRegistry registry = ModelHandlerRegistry.getInstance();
return registry;
}
public TestModelHandlers() {
super();
}
public void testCreation() {
ModelHandlerRegistry registry = getModelHandlerRegistry();
assertTrue("model handler registry must exist", registry != null);
}
public void testCSSExists() {
String id = ContentTypeIdForCSS.ContentTypeID_CSS;
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerForContentTypeId(id);
assertTrue("model handler registry does not have CSS type ", handler != null && handler.getAssociatedContentTypeId().equals(id));
}
public void testCSSExistsFromFilename() throws IOException {
String filename = "test.css";
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerFor(filename, null);
assertTrue("model handler registry does not have CSS type ", handler != null && handler.getAssociatedContentTypeId().equals(ContentTypeIdForCSS.ContentTypeID_CSS));
}
public void testDTDExists() {
String id = "org.eclipse.wst.dtd.core.dtdsource";
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerForContentTypeId(id);
assertTrue("model handler registry does not have DTD type ", handler != null && handler.getAssociatedContentTypeId().equals(id));
}
public void testDTDExistsFromFilename() throws IOException {
String filename = "test.dtd";
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerFor(filename, null);
assertTrue("model handler registry does not have DTD type ", handler != null && handler.getAssociatedContentTypeId().equals("org.eclipse.wst.dtd.core.dtdsource"));
}
public void testHTMLExists() {
String id = ContentTypeIdForHTML.ContentTypeID_HTML;
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerForContentTypeId(id);
assertTrue("model handler registry does not have HTML type ", handler != null && handler.getAssociatedContentTypeId().equals(id));
}
public void testHTMLExistsFromFilename() throws IOException {
String filename = "test.html";
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerFor(filename, null);
assertTrue("model handler registry does not have HTML type ", handler != null && handler.getAssociatedContentTypeId().equals(ContentTypeIdForHTML.ContentTypeID_HTML));
}
public void testJSPExists() {
String id = ContentTypeIdForJSP.ContentTypeID_JSP;
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerForContentTypeId(id);
assertTrue("model handler registry does not have JSP type ", handler != null && handler.getAssociatedContentTypeId().equals(id));
}
public void testJSPExistsFromFilename() throws IOException {
String filename = "test.jsp";
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerFor(filename, null);
assertTrue("model handler registry does not have JSP type ", handler != null && handler.getAssociatedContentTypeId().equals(ContentTypeIdForJSP.ContentTypeID_JSP));
}
public void testXMLExists() {
String id = ContentTypeIdForXML.ContentTypeID_XML;
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerForContentTypeId(id);
assertEquals("model handler registry does not have XML type ", id, handler.getAssociatedContentTypeId());
}
public void testXMLExistsFromFilename() throws IOException {
String filename = "test.xml";
ModelHandlerRegistry registry = getModelHandlerRegistry();
IModelHandler handler = registry.getHandlerFor(filename, null);
assertEquals("model handler registry does not have XML type ", ContentTypeIdForXML.ContentTypeID_XML, handler.getAssociatedContentTypeId());
}
public void testDirtyStateForEmbeddedContentTypeTextHTML() throws Exception {
String name = "bug243243";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(name, null, null);
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug243243", "/bug243243");
}
IFile testFile = project.getFile("html.jsp");
IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
model.releaseFromRead();
project.delete(true, null);
}
public void testDirtyStateForEmbeddedContentTypeTextCSS() throws Exception {
String name = "bug243243";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(name, null, null);
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug243243", "/bug243243");
}
IFile testFile = project.getFile("css.jsp");
IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
model.releaseFromRead();
project.delete(true, null);
}
public void testDirtyStateForEmbeddedContentTypeTextXML() throws Exception {
String name = "bug243243";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(name, null, null);
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug243243", "/bug243243");
}
IFile testFile = project.getFile("xml.jsp");
IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
model.releaseFromRead();
project.delete(true, null);
}
public void testDirtyStateForMisspelledEmbeddedCharset() throws Exception {
String projectName = "TestModelHandlers." + getName();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(projectName, null, null);
}
IFile testFile = project.getFile("charaset.jsp");
String contents = "<%@ page language=\"java\" contentType=\"text/xml; charaset=UTF-8\" pageEncoding=\"UTF-8\"%>\n" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Contents>\n" +
"<Error state=\"fatal\" code=\"\">\n" +
"</Error>\n" +
"</Contents>";
if (!testFile.exists()) {
testFile.create(new ByteArrayInputStream(contents.getBytes("utf8")), IResource.FORCE, null);
}
else {
testFile.setContents(new ByteArrayInputStream(contents.getBytes("utf8")), IResource.FORCE, null);
}
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getModelForRead(testFile);
Document doc = ((IDOMModel) model).getDocument();
PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) ((INodeNotifier) doc).getAdapterFor(PageDirectiveAdapter.class);
EmbeddedTypeHandler embeddedHandler = pageDirectiveAdapter.getEmbeddedType();
assertFalse("newly opened model was dirty, embedded handler changed? current family:" + embeddedHandler.getFamilyId(), model.isDirty());
}
finally {
if (model != null)
model.releaseFromRead();
}
project.delete(true, null);
}
public void testDirtyStateForEmbeddedCharsetWithoutContentType() throws Exception {
String projectName = "TestModelHandlers." + getName();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(projectName, null, null);
}
IFile testFile = project.getFile("charaset.jsp");
String contents = "<%@ page language=\"java\" contentType=\"charset=UTF-8\" pageEncoding=\"UTF-8\"%>\n" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Contents>\n" +
"<Error state=\"fatal\" code=\"\">\n" +
"</Error>\n" +
"</Contents>";
if (!testFile.exists()) {
testFile.create(new ByteArrayInputStream(contents.getBytes("utf8")), IResource.FORCE, null);
}
else {
testFile.setContents(new ByteArrayInputStream(contents.getBytes("utf8")), IResource.FORCE, null);
}
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getModelForRead(testFile);
Document doc = ((IDOMModel) model).getDocument();
PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) ((INodeNotifier) doc).getAdapterFor(PageDirectiveAdapter.class);
EmbeddedTypeHandler embeddedHandler = pageDirectiveAdapter.getEmbeddedType();
assertFalse("newly opened model was dirty, embedded handler changed? current family:" + embeddedHandler.getFamilyId(), model.isDirty());
}
finally {
if (model != null)
model.releaseFromRead();
}
project.delete(true, null);
}
public void testDirtyStateForUnsupportedEmbeddedContentType() throws Exception {
String projectName = "TestModelHandlers." + getName();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(projectName, null, null);
}
IFile testFile = project.getFile("unsupported.jsp");
String contents = "<%@ page language=\"java\" contentType=\"image/gif\"%>\n" +
"out.write(\"GIF89a\"";
if (!testFile.exists()) {
testFile.create(new ByteArrayInputStream(contents.getBytes("utf8")), IResource.FORCE, null);
}
else {
testFile.setContents(new ByteArrayInputStream(contents.getBytes("utf8")), IResource.FORCE, null);
}
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
}
finally {
if (model != null)
model.releaseFromRead();
}
project.delete(true, null);
}
public void testDirtyStateForEmbeddedContentTypeSubXML() throws Exception {
String name = "bug243243";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(name, null, null);
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug243243", "/bug243243");
}
IFile testFile = project.getFile("rdf.jsp");
IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
model.releaseFromRead();
project.delete(true, null);
}
public void testDirtyStateForDefaultEmbeddedContentType() throws Exception {
String name = "bug243243";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(name, null, null);
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug243243", "/bug243243");
}
IFile testFile = project.getFile("default.jsp");
IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
model.releaseFromRead();
project.delete(true, null);
}
public void testDirtyStateWithNoPageDirective() throws Exception {
String name = "bug243243";
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!project.isAccessible()) {
project = BundleResourceUtil.createSimpleProject(name, null, null);
BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/bug243243", "/bug243243");
}
IFile testFile = project.getFile("nodirective.jsp");
IStructuredModel model = StructuredModelManager.getModelManager().getModelForRead(testFile);
assertFalse("newly opened model was dirty " + testFile.getName(), model.isDirty());
model.releaseFromRead();
project.delete(true, null);
}
}
|