Skip to content

Commit 22aaff6

Browse files
authored
Web.xml source validation and quick assist (#1488)
* source validation and quick assist * change partition type * update configuration * check for app engine standard facet, add tests * create app engine project with facets * address review
1 parent a32457d commit 22aaff6

13 files changed

Lines changed: 446 additions & 67 deletions

File tree

plugins/com.google.cloud.tools.eclipse.appengine.validation.test/src/com/google/cloud/tools/eclipse/appengine/validation/ApplicationQuickAssistProcessorTest.java renamed to plugins/com.google.cloud.tools.eclipse.appengine.validation.test/src/com/google/cloud/tools/eclipse/appengine/validation/AbstractQuickAssistProcessorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
3636
import com.google.cloud.tools.eclipse.ui.util.WorkbenchUtil;
3737

38-
public class ApplicationQuickAssistProcessorTest {
38+
public class AbstractQuickAssistProcessorTest {
3939

4040
@Rule public TestProjectCreator projectCreator = new TestProjectCreator();
4141

@@ -55,7 +55,7 @@ public void testComputeQuickAssistProposals() throws CoreException {
5555
model.addAnnotation(new Annotation("type", false, annotationMessage), new Position(1));
5656

5757
TextInvocationContext context = new TextInvocationContext(viewer, 1, 1);
58-
ApplicationQuickAssistProcessor processor = new ApplicationQuickAssistProcessor();
58+
AbstractQuickAssistProcessor processor = new ApplicationQuickAssistProcessor();
5959
ICompletionProposal[] proposals = processor.computeQuickAssistProposals(context);
6060
assertEquals(1, proposals.length);
6161
}

plugins/com.google.cloud.tools.eclipse.appengine.validation.test/src/com/google/cloud/tools/eclipse/appengine/validation/AbstractXmlSourceValidatorTest.java

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,29 @@
1717
package com.google.cloud.tools.eclipse.appengine.validation;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
2022

2123
import org.eclipse.core.resources.IFile;
2224
import org.eclipse.core.resources.IProject;
2325
import org.eclipse.core.runtime.CoreException;
26+
import org.eclipse.core.runtime.IPath;
2427
import org.eclipse.jface.text.IDocument;
28+
import org.eclipse.jst.common.project.facet.core.JavaFacet;
29+
import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils;
30+
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
31+
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
32+
import org.eclipse.wst.sse.ui.internal.reconcile.validator.IncrementalHelper;
2533
import org.eclipse.wst.sse.ui.internal.reconcile.validator.IncrementalReporter;
34+
import org.eclipse.wst.validation.internal.core.ValidationException;
2635
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
36+
import org.eclipse.wst.validation.internal.provisional.core.IValidationContext;
2737
import org.junit.Rule;
2838
import org.junit.Test;
39+
40+
import com.google.cloud.tools.eclipse.appengine.facets.AppEngineStandardFacet;
2941
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
42+
import com.google.common.collect.Lists;
3043

3144
public class AbstractXmlSourceValidatorTest {
3245

@@ -35,13 +48,63 @@ public class AbstractXmlSourceValidatorTest {
3548
+ "<application>"
3649
+ "</application>"
3750
+ "</appengine-web-app>";
51+
private static final IProjectFacetVersion APPENGINE_STANDARD_FACET_VERSION_1 =
52+
ProjectFacetsManager.getProjectFacet(AppEngineStandardFacet.ID).getVersion("1");
53+
54+
@Rule public TestProjectCreator dynamicWebProject =
55+
new TestProjectCreator().withFacetVersions(Lists.newArrayList(JavaFacet.VERSION_1_7,
56+
WebFacetUtils.WEB_25));
57+
@Rule
58+
public TestProjectCreator appEngineStandardProject =
59+
new TestProjectCreator().withFacetVersions(Lists.newArrayList(JavaFacet.VERSION_1_7,
60+
WebFacetUtils.WEB_25, APPENGINE_STANDARD_FACET_VERSION_1));
3861

39-
@Rule public TestProjectCreator projectCreator = new TestProjectCreator();
62+
@Test
63+
public void testValidate_appEngineStandardFacet() throws CoreException, ValidationException {
64+
IProject project = appEngineStandardProject.getProject();
65+
IFile file = project.getFile("testdata.xml");
66+
file.create(ValidationTestUtils.stringToInputStream(
67+
APPLICATION_XML), 0, null);
68+
69+
IDocument document = ValidationTestUtils.getDocument(file);
70+
71+
// Adds the URI of the file to be validated to the IncrementalHelper.
72+
IncrementalHelper helper = new IncrementalHelper(document, project);
73+
IPath path = file.getFullPath();
74+
helper.setURI(path.toString());
75+
76+
AbstractXmlSourceValidator validator = new AppEngineWebXmlSourceValidator();
77+
validator.connect(document);
78+
IncrementalReporter reporter = new IncrementalReporter(null);
79+
validator.validate(helper, reporter);
80+
assertEquals(1, reporter.getMessages().size());
81+
}
82+
83+
@Test
84+
public void testValidate_dynamicWebProject() throws CoreException, ValidationException {
85+
IProject project = dynamicWebProject.getProject();
86+
IFile file = project.getFile("testdata.xml");
87+
file.create(ValidationTestUtils.stringToInputStream(
88+
APPLICATION_XML), 0, null);
89+
90+
IDocument document = ValidationTestUtils.getDocument(file);
91+
92+
// Adds the URI of the file to be validated to the IncrementalHelper.
93+
IncrementalHelper helper = new IncrementalHelper(document, project);
94+
IPath path = file.getFullPath();
95+
helper.setURI(path.toString());
96+
97+
AbstractXmlSourceValidator validator = new AppEngineWebXmlSourceValidator();
98+
validator.connect(document);
99+
IncrementalReporter reporter = new IncrementalReporter(null);
100+
validator.validate(helper, reporter);
101+
assertEquals(0, reporter.getMessages().size());
102+
}
40103

41104
@Test
42105
public void getDocumentEncodingTest() throws CoreException {
43106

44-
IProject project = projectCreator.getProject();
107+
IProject project = appEngineStandardProject.getProject();
45108
IFile file = project.getFile("testdata.xml");
46109
file.create(ValidationTestUtils.stringToInputStream(
47110
APPLICATION_XML), IFile.FORCE, null);
@@ -59,4 +122,38 @@ public void testCreateMessage() throws CoreException {
59122
assertEquals(1, reporter.getMessages().size());
60123
}
61124

125+
@Test
126+
public void testGetFile() throws CoreException {
127+
IProject project = appEngineStandardProject.getProject();
128+
IFile file = project.getFile("testdata.xml");
129+
file.create(ValidationTestUtils.stringToInputStream(
130+
APPLICATION_XML), 0, null);
131+
132+
assertTrue(file.exists());
133+
134+
IPath path = file.getFullPath();
135+
IFile testFile = AbstractXmlSourceValidator.getFile(path.toString());
136+
137+
assertNotNull(testFile);
138+
assertEquals(file, testFile);
139+
}
140+
141+
@Test
142+
public void testGetProject() throws CoreException {
143+
IProject project = appEngineStandardProject.getProject();
144+
IFile file = project.getFile("testdata.xml");
145+
file.create(ValidationTestUtils.stringToInputStream(
146+
APPLICATION_XML), 0, null);
147+
148+
IDocument document = ValidationTestUtils.getDocument(file);
149+
150+
IncrementalHelper helper = new IncrementalHelper(document, project);
151+
IPath path = file.getFullPath();
152+
helper.setURI(path.toString());
153+
154+
IProject testProject = AbstractXmlSourceValidator.getProject((IValidationContext) helper);
155+
assertNotNull(testProject);
156+
assertEquals(project, testProject);
157+
}
158+
62159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.tools.eclipse.appengine.validation;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
import javax.xml.parsers.ParserConfigurationException;
25+
import org.eclipse.core.runtime.CoreException;
26+
import org.eclipse.wst.sse.ui.internal.reconcile.validator.IncrementalReporter;
27+
import org.junit.Test;
28+
29+
public class WebXmlSourceValidatorTest {
30+
31+
private static final byte[] XML_NO_BANNED_ELEMENTS =
32+
"<test></test>".getBytes(StandardCharsets.UTF_8);
33+
private static final String XML = "<web-app xmlns='http://xmlns.jcp.org/xml/ns/javaee'"
34+
+ " version='3.1'></web-app>";
35+
private static final byte[] XML_BYTES = XML.getBytes(StandardCharsets.UTF_8);
36+
private IncrementalReporter reporter = new IncrementalReporter(null /*progress monitor*/);
37+
38+
@Test
39+
public void testValidate_noBannedElements() throws CoreException, IOException, ParserConfigurationException {
40+
WebXmlSourceValidator validator = new WebXmlSourceValidator();
41+
validator.validate(reporter, XML_NO_BANNED_ELEMENTS);
42+
assertTrue(reporter.getMessages().isEmpty());
43+
}
44+
45+
@Test
46+
public void testValidate() throws CoreException, IOException, ParserConfigurationException {
47+
WebXmlSourceValidator validator = new WebXmlSourceValidator();
48+
validator.validate(reporter, XML_BYTES);
49+
assertEquals(1, reporter.getMessages().size());
50+
reporter.removeAllMessages(validator);
51+
assertTrue(reporter.getMessages().isEmpty());
52+
}
53+
}

plugins/com.google.cloud.tools.eclipse.appengine.validation.test/src/com/google/cloud/tools/eclipse/appengine/validation/XsltSourceQuickFixTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertTrue;
2222

23+
import com.google.cloud.tools.eclipse.appengine.facets.AppEngineStandardFacet;
2324
import com.google.cloud.tools.eclipse.test.util.project.ProjectUtils;
2425
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
2526
import com.google.cloud.tools.eclipse.ui.util.WorkbenchUtil;
26-
import java.util.Arrays;
27+
import com.google.common.collect.Lists;
28+
2729
import org.eclipse.core.resources.IFile;
2830
import org.eclipse.core.resources.IProject;
2931
import org.eclipse.core.resources.IResource;
@@ -36,6 +38,8 @@
3638
import org.eclipse.ui.IEditorPart;
3739
import org.eclipse.ui.IWorkbench;
3840
import org.eclipse.ui.PlatformUI;
41+
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
42+
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
3943
import org.junit.Rule;
4044
import org.junit.Test;
4145

@@ -49,14 +53,18 @@ public class XsltSourceQuickFixTest {
4953
+ "<application>"
5054
+ "</application>"
5155
+ "</appengine-web-app>";
52-
53-
@Rule public TestProjectCreator projectCreator = new TestProjectCreator().withFacetVersions(
54-
Arrays.asList(JavaFacet.VERSION_1_7, WebFacetUtils.WEB_25));
56+
private static final IProjectFacetVersion APPENGINE_STANDARD_FACET_VERSION_1 =
57+
ProjectFacetsManager.getProjectFacet(AppEngineStandardFacet.ID).getVersion("1");
58+
59+
@Rule
60+
public TestProjectCreator appEngineStandardProject =
61+
new TestProjectCreator().withFacetVersions(Lists.newArrayList(JavaFacet.VERSION_1_7,
62+
WebFacetUtils.WEB_25, APPENGINE_STANDARD_FACET_VERSION_1));
5563

5664
@Test
5765
public void testApply() throws CoreException {
5866

59-
IProject project = projectCreator.getProject();
67+
IProject project = appEngineStandardProject.getProject();
6068
IFile file = project.getFile("appengine-web.xml");
6169
file.create(ValidationTestUtils.stringToInputStream(APPLICATION_XML), IFile.FORCE, null);
6270

plugins/com.google.cloud.tools.eclipse.appengine.validation/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Bundle-Version: 0.1.0.qualifier
77
Bundle-Vendor: Google Inc.
88
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
99
Bundle-Localization: plugin
10+
Bundle-ActivationPolicy: lazy
1011
Require-Bundle: org.eclipse.core.resources,
1112
org.eclipse.jdt.core
1213
Import-Package: com.google.cloud.tools.eclipse.appengine.facets,
@@ -17,14 +18,14 @@ Import-Package: com.google.cloud.tools.eclipse.appengine.facets,
1718
com.google.common.io;version="[20.0.0,21.0.0)",
1819
org.eclipse.core.databinding.validation,
1920
org.eclipse.core.runtime,
20-
org.eclipse.core.runtime.jobs,
2121
org.eclipse.jface.text,
2222
org.eclipse.jface.text.contentassist,
2323
org.eclipse.jface.text.quickassist,
2424
org.eclipse.jface.text.source,
2525
org.eclipse.swt.graphics,
2626
org.eclipse.ui,
2727
org.eclipse.ui.ide,
28+
org.eclipse.wst.common.project.facet.core,
2829
org.eclipse.wst.sse.core.internal.encoding,
2930
org.eclipse.wst.sse.core.internal.text,
3031
org.eclipse.wst.sse.ui.internal.reconcile.validator,

plugins/com.google.cloud.tools.eclipse.appengine.validation/plugin.xml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,39 @@
118118
scope="total"
119119
class="com.google.cloud.tools.eclipse.appengine.validation.AppEngineWebXmlSourceValidator"
120120
id="appEngineWebXmlSourceValidator">
121-
<contentTypeIdentifier id="appengine-web.xml">
121+
<contentTypeIdentifier id="com.google.appengine.configuration.xml">
122122
<partitionType id="org.eclipse.wst.xml.XML_DEFAULT"/>
123123
</contentTypeIdentifier>
124124
</validator>
125125
</extension>
126126

127+
<extension point="org.eclipse.wst.sse.ui.sourcevalidation">
128+
<validator
129+
scope="total"
130+
class="com.google.cloud.tools.eclipse.appengine.validation.WebXmlSourceValidator"
131+
id="webXmlSourceValidator">
132+
<contentTypeIdentifier id="org.eclipse.jst.jee.ee5webDD">
133+
<partitionType id="org.eclipse.wst.xml.XML_DEFAULT"/>
134+
</contentTypeIdentifier>
135+
</validator>
136+
</extension>
137+
127138
<extension point="org.eclipse.core.contenttype.contentTypes">
128139
<content-type
129140
name="appengine-web.xml"
130-
id="appengine-web.xml"
141+
id="com.google.appengine.configuration.xml"
131142
base-type="org.eclipse.core.runtime.xml"
132143
file-names="appengine-web.xml">
133-
</content-type>
144+
</content-type>
134145
</extension>
135146

136147
<extension point="org.eclipse.wst.sse.ui.quickFixProcessor">
137148
<quickFixProcessor
138149
class="com.google.cloud.tools.eclipse.appengine.validation.ApplicationQuickAssistProcessor">
139150
</quickFixProcessor>
151+
<quickFixProcessor
152+
class="com.google.cloud.tools.eclipse.appengine.validation.ServletQuickAssistProcessor">
153+
</quickFixProcessor>
140154
</extension>
141155

142156
<extension point="org.eclipse.wst.sse.ui.editorConfiguration">
@@ -145,6 +159,11 @@
145159
target="org.eclipse.wst.xml.XML_DEFAULT"
146160
type="org.eclipse.jface.text.quickassist.IQuickAssistProcessor">
147161
</provisionalConfiguration>
162+
<provisionalConfiguration
163+
class="com.google.cloud.tools.eclipse.appengine.validation.ServletQuickAssistProcessor"
164+
target="org.eclipse.wst.xml.XML_DEFAULT"
165+
type="org.eclipse.jface.text.quickassist.IQuickAssistProcessor">
166+
</provisionalConfiguration>
148167
</extension>
149168

150169
</plugin>

0 commit comments

Comments
 (0)