Skip to content

Commit 4aa7628

Browse files
authored
Old maven plugin warning (#1491)
* Old maven plugin warning * fix test * check groupId and artifactId * make scanner more clear (hopefully) * rename method * address review * rename namespace field
1 parent 9e475d8 commit 4aa7628

10 files changed

Lines changed: 515 additions & 4 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 java.util.Queue;
25+
import javax.xml.parsers.ParserConfigurationException;
26+
import org.junit.Test;
27+
import org.xml.sax.SAXException;
28+
29+
public class PomParserTest {
30+
31+
private static final String PROJECT_START_TAG = "<project xmlns='http://maven.apache.org/POM/4.0.0' "
32+
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
33+
+ "xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 "
34+
+ "http://maven.apache.org/xsd/maven-4.0.0.xsd'>";
35+
36+
@Test
37+
public void testReadXml_emptyXml()
38+
throws ParserConfigurationException, IOException, SAXException {
39+
String emptyXml = "";
40+
byte[] bytes = emptyXml.getBytes(StandardCharsets.UTF_8);
41+
assert(BlacklistSaxParser.readXml(bytes).getBlacklist().isEmpty());
42+
}
43+
44+
@Test
45+
public void testReadXml_xmlWithBannedElement()
46+
throws ParserConfigurationException, IOException, SAXException {
47+
String xml = PROJECT_START_TAG
48+
+ "<build><plugins><plugin><groupId>com.google.appengine</groupId>"
49+
+ "<artifactId>appengine-maven-plugin</artifactId></plugin></plugins></build></project>";
50+
byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);
51+
Queue<BannedElement> blacklist = PomParser.readXml(bytes).getBlacklist();
52+
String message = Messages.getString("maven.plugin");
53+
assertEquals(blacklist.poll().getMessage(), message);
54+
}
55+
56+
@Test
57+
public void testReadXml_differentOrder()
58+
throws ParserConfigurationException, IOException, SAXException {
59+
String xml = PROJECT_START_TAG
60+
+ "<build><plugins><plugin><artifactId>appengine-maven-plugin</artifactId>"
61+
+ "<groupId>com.google.appengine</groupId></plugin></plugins></build></project>";
62+
byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);
63+
Queue<BannedElement> blacklist = PomParser.readXml(bytes).getBlacklist();
64+
String message = Messages.getString("maven.plugin");
65+
assertEquals(blacklist.poll().getMessage(), message);
66+
}
67+
68+
@Test
69+
public void testReadXml_noBannedElements()
70+
throws ParserConfigurationException, IOException, SAXException {
71+
String xml = PROJECT_START_TAG
72+
+ "<build><plugins><plugin><groupId>com.google.tools</groupId>"
73+
+ "<artifactId>appengine-maven-plugin</artifactId></plugin></plugins></build></project>";
74+
byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);
75+
Queue<BannedElement> blacklist = PomParser.readXml(bytes).getBlacklist();
76+
assertTrue(blacklist.isEmpty());
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.xml.sax.SAXException;
26+
import org.xml.sax.ext.Locator2Impl;
27+
import org.xml.sax.helpers.AttributesImpl;
28+
29+
public class PomXmlScannerTest {
30+
31+
private static final String ELEMENT_MESSAGE = Messages.getString("maven.plugin");
32+
private PomXmlScanner scanner = new PomXmlScanner();
33+
34+
@Before
35+
public void setUp() throws SAXException {
36+
scanner.setDocumentLocator(new Locator2Impl());
37+
scanner.startDocument();
38+
}
39+
40+
@Test
41+
public void testStartElement_build() throws SAXException {
42+
scanner.startElement("", "plugin", "", new AttributesImpl());
43+
assertEquals(0, scanner.getBlacklist().size());
44+
assertTrue(scanner.getInsidePlugin());
45+
}
46+
47+
@Test
48+
public void testStartElement_groupId() throws SAXException {
49+
scanner.startElement("", "plugin", "", new AttributesImpl());
50+
scanner.startElement("", "groupId", "", new AttributesImpl());
51+
assertTrue(scanner.getSaveContents());
52+
}
53+
54+
@Test
55+
public void testStartElement_groupIdNoParent() throws SAXException {
56+
scanner.startElement("", "groupId", "", new AttributesImpl());
57+
assertFalse(scanner.getSaveContents());
58+
}
59+
60+
@Test
61+
public void testEndElement() throws SAXException {
62+
scanner.startElement("", "plugin", "", new AttributesImpl());
63+
scanner.startElement("", "groupId", "", new AttributesImpl());
64+
scanner.characters("com.google.appengine".toCharArray(), 0, 20);
65+
scanner.endElement("", "groupId", "");
66+
assertFalse(scanner.getSaveContents());
67+
scanner.startElement("", "artifactId", "", new AttributesImpl());
68+
scanner.characters("appengine-maven-plugin".toCharArray(), 0, 22);
69+
scanner.endElement("", "artifactId", "");
70+
assertEquals(1, scanner.getBlacklist().size());
71+
String message = scanner.getBlacklist().peek().getMessage();
72+
assertEquals(ELEMENT_MESSAGE, message);
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
import javax.xml.parsers.ParserConfigurationException;
25+
import org.eclipse.core.resources.IContainer;
26+
import org.eclipse.core.resources.IFile;
27+
import org.eclipse.core.resources.IFolder;
28+
import org.eclipse.core.resources.IMarker;
29+
import org.eclipse.core.resources.IProject;
30+
import org.eclipse.core.resources.IResource;
31+
import org.eclipse.core.runtime.CoreException;
32+
import org.eclipse.core.runtime.IPath;
33+
import org.eclipse.core.runtime.Path;
34+
import org.junit.After;
35+
import org.junit.BeforeClass;
36+
import org.junit.ClassRule;
37+
import org.junit.Test;
38+
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
39+
40+
public class PomXmlValidatorTest {
41+
42+
private static final String PROJECT_START_TAG = "<project xmlns='http://maven.apache.org/POM/4.0.0' "
43+
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
44+
+ "xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 "
45+
+ "http://maven.apache.org/xsd/maven-4.0.0.xsd'>";
46+
private static final String XML_NO_BANNED_ELEMENTS = PROJECT_START_TAG + "<test></test></project>";
47+
private static final String XML = PROJECT_START_TAG
48+
+ "<build><plugins><plugin><groupId>com.google.appengine</groupId>"
49+
+ "<artifactId>appengine-maven-plugin</artifactId></plugin></plugins></build></project>";
50+
private static final String PLUGIN_MARKER =
51+
"com.google.cloud.tools.eclipse.appengine.validation.mavenPluginMarker";
52+
private static IResource resource;
53+
private static IProject project;
54+
55+
@ClassRule public static TestProjectCreator projectCreator = new TestProjectCreator();
56+
57+
@BeforeClass
58+
public static void setUp() throws CoreException {
59+
project = projectCreator.getProject();
60+
createFolders(project, new Path("src/main/webapp/WEB-INF"));
61+
resource = project.getFile("src/main/webapp/WEB-INF/web.xml");
62+
((IFile) resource).create(new ByteArrayInputStream(new byte[0]), true, null);
63+
}
64+
65+
66+
@After
67+
public void tearDown() throws CoreException {
68+
if (resource != null) {
69+
resource.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO);
70+
}
71+
}
72+
73+
@Test
74+
public void testValidate_noBannedElements()
75+
throws IOException, CoreException, ParserConfigurationException {
76+
byte[] bytes = XML_NO_BANNED_ELEMENTS.getBytes(StandardCharsets.UTF_8);
77+
PomXmlValidator validator = new PomXmlValidator();
78+
validator.validate(resource, bytes);
79+
IMarker[] markers = resource.findMarkers(PLUGIN_MARKER, true, IResource.DEPTH_ZERO);
80+
assertEquals(0, markers.length);
81+
}
82+
83+
@Test
84+
public void testValidate_withBannedElements()
85+
throws IOException, CoreException, ParserConfigurationException {
86+
byte[] bytes = XML.getBytes(StandardCharsets.UTF_8);
87+
PomXmlValidator validator = new PomXmlValidator();
88+
validator.validate(resource, bytes);
89+
IMarker[] markers = resource.findMarkers(PLUGIN_MARKER, true, IResource.DEPTH_ZERO);
90+
assertEquals(1, markers.length);
91+
String message = Messages.getString("maven.plugin");
92+
assertEquals(message, (String) markers[0].getAttribute(IMarker.MESSAGE));
93+
assertEquals("line 1", markers[0].getAttribute(IMarker.LOCATION));
94+
}
95+
96+
private static void createFolders(IContainer parent, IPath path) throws CoreException {
97+
if (!path.isEmpty()) {
98+
IFolder folder = parent.getFolder(new Path(path.segment(0)));
99+
folder.create(true, true, null);
100+
createFolders(folder, path.removeFirstSegments(1));
101+
}
102+
}
103+
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ Bundle-Vendor: Google Inc.
88
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
99
Bundle-Localization: plugin
1010
Require-Bundle: org.eclipse.core.resources,
11-
org.eclipse.jdt.core,
12-
javax.servlet;bundle-version="3.1.0";visibility:=reexport,
13-
javax.servlet.jsp;bundle-version="2.2.0";visibility:=reexport
11+
org.eclipse.jdt.core
1412
Import-Package: com.google.cloud.tools.eclipse.appengine.facets,
1513
com.google.cloud.tools.eclipse.util,
1614
com.google.common.annotations;version="[20.0.0,21.0.0)",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extension.name=App Engine JRE Whitelist
22
appengine.web=App Engine Web XML Validator
3+
pom.validator=Pom Validator
34
appengine.blacklist.marker=App Engine Configuration Problem
45
web.xml=Web XML Validator
56
servlet.marker=Servlet Marker
7+
maven.plugin.marker=Obsolete Maven App Engine Plugin

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@
8787
markerType="com.google.cloud.tools.eclipse.appengine.validation.servletMarker">
8888
</markerResolutionGenerator>
8989
</extension>
90+
91+
<extension
92+
name="%pom.validator"
93+
point="org.eclipse.wst.validation.validatorV2"
94+
id="pomXmlValidator">
95+
<validator
96+
class="com.google.cloud.tools.eclipse.appengine.validation.PomXmlValidator"
97+
markerId="com.google.cloud.tools.eclipse.appengine.validation.mavenPluginMarker">
98+
<include>
99+
<rules>
100+
<file type="file" name="pom.xml"/>
101+
</rules>
102+
</include>
103+
</validator>
104+
</extension>
105+
106+
<extension
107+
id="mavenPluginMarker"
108+
name="%maven.plugin.marker"
109+
point="org.eclipse.core.resources.markers">
110+
<super type="org.eclipse.jdt.core.problem" />
111+
<super type="org.eclipse.core.resources.problemmarker" />
112+
<super type="org.eclipse.core.resources.textmarker" />
113+
<persistent value="true" />
114+
</extension>
90115

91116
<extension point="org.eclipse.wst.sse.ui.sourcevalidation">
92117
<validator
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 java.io.ByteArrayInputStream;
20+
import java.io.IOException;
21+
import javax.xml.parsers.ParserConfigurationException;
22+
import org.xml.sax.InputSource;
23+
import org.xml.sax.SAXException;
24+
import org.xml.sax.XMLReader;
25+
import org.xml.sax.helpers.XMLReaderFactory;
26+
27+
/**
28+
* Retrieves and returns the results of the SAX parser.
29+
*/
30+
class PomParser {
31+
32+
static SaxParserResults readXml(byte[] bytes)
33+
throws ParserConfigurationException, IOException, SAXException {
34+
if (bytes.length == 0) { //file is empty
35+
return new SaxParserResults();
36+
}
37+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
38+
InputSource is = new InputSource(bais);
39+
XMLReader reader = XMLReaderFactory.createXMLReader();
40+
PomXmlScanner handler = new PomXmlScanner();
41+
42+
reader.setContentHandler(handler);
43+
reader.setErrorHandler(handler);
44+
reader.parse(is);
45+
return handler.getParserResults();
46+
}
47+
48+
}

0 commit comments

Comments
 (0)