-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better indentation when adding GCP libraries to pom.xml #3511
Open
lak-proddev
wants to merge
6
commits into
GoogleCloudPlatform:master
Choose a base branch
from
lak-proddev:#3381
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d028af
Better indentation when adding GCP libraries to pom.xml #3381
lak-proddev 5f56823
#3381 - test case added
lak-proddev 8194366
#3381 - test case updated
lak-proddev 45c771d
#3381 - test case updated
lak-proddev 38d59ea
#3381 - test case updated
lak-proddev 741ded9
Typo
lak-proddev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
....libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/PomFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.eclipse.appengine.libraries; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
import com.google.cloud.tools.eclipse.appengine.libraries.model.Library; | ||
import com.google.cloud.tools.eclipse.appengine.libraries.model.LibraryFile; | ||
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator; | ||
import com.google.cloud.tools.eclipse.util.ArtifactRetriever; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.FileLocator; | ||
import org.eclipse.core.runtime.Platform; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.osgi.framework.Bundle; | ||
import org.xml.sax.SAXException; | ||
|
||
public class PomFormatTest { | ||
|
||
@Rule | ||
public final TestProjectCreator projectCreator = new TestProjectCreator(); | ||
private Pom pom; | ||
private IFile pomFile; | ||
|
||
@Before | ||
public void setUp() throws SAXException, IOException, CoreException { | ||
IProject project = projectCreator.getProject(); | ||
pomFile = project.getFile("pom.xml"); | ||
try (InputStream in = | ||
Files.newInputStream(Paths.get("testdata/testPomEmpty.xml").toAbsolutePath())) { | ||
pomFile.create(in, IFile.FORCE, null); | ||
pom = Pom.parse(pomFile); | ||
} | ||
|
||
Logger logger = Logger.getLogger(ArtifactRetriever.class.getName()); | ||
logger.setLevel(Level.OFF); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
Logger logger = Logger.getLogger(ArtifactRetriever.class.getName()); | ||
logger.setLevel(null); | ||
} | ||
|
||
@Test | ||
public void testAddDependencies() throws Exception { | ||
Library library0 = PomTest.newLibrary("id0", | ||
new LibraryFile(PomTest.coordinates("com.example.group0", "artifact0", "1.2.3"))); | ||
Library library1 = PomTest.newLibrary("id1", | ||
new LibraryFile(PomTest.coordinates("com.example.group1", "artifact1"))); | ||
Library library2 = PomTest.newLibrary("id2", | ||
new LibraryFile(PomTest.coordinates("com.example.group2", "artifact2")), | ||
new LibraryFile(PomTest.coordinates("com.example.group3", "artifact3"))); | ||
|
||
List<Library> libraries = Arrays.asList(library0, library1, library2); | ||
pom.addDependencies(libraries); | ||
|
||
Bundle bundle = Platform.getBundle("com.google.cloud.tools.eclipse.appengine.libraries.test"); | ||
URL fileUrl = bundle.getEntry("/testdata/formatAdd.xml"); | ||
File expected = new File(FileLocator.resolve(fileUrl).toURI()); | ||
assertFileContentsEqual(expected, pomFile.getLocation().toFile()); | ||
} | ||
|
||
@Test | ||
public void testRemoveUnusedDependencies() throws Exception { | ||
LibraryFile file1 = new LibraryFile(PomTest.coordinates("com.example.group1", "artifact1")); | ||
LibraryFile file2 = new LibraryFile(PomTest.coordinates("com.example.group2", "artifact2")); | ||
Library library1 = PomTest.newLibrary("id1", file1); | ||
Library library2 = PomTest.newLibrary("id2", file1, file2); | ||
|
||
pom.addDependencies(Arrays.asList(library1, library2)); | ||
|
||
Bundle bundle = Platform.getBundle("com.google.cloud.tools.eclipse.appengine.libraries.test"); | ||
URL fileUrl = bundle.getEntry("/testdata/formatRemove.xml"); | ||
File expected = new File(FileLocator.resolve(fileUrl).toURI()); | ||
assertFileContentsEqual(expected, pomFile.getLocation().toFile()); | ||
} | ||
|
||
private static void assertFileContentsEqual(File expected, File actual) throws IOException { | ||
assertArrayEquals(Files.readAllBytes(expected.toPath()), Files.readAllBytes(actual.toPath())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/testdata/formatAdd.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-bom</artifactId> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
<version>0.99.0-alpha</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.example.group0</groupId> | ||
<artifactId>artifact0</artifactId> | ||
<version>1.2.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.example.group1</groupId> | ||
<artifactId>artifact1</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.example.group2</groupId> | ||
<artifactId>artifact2</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.example.group3</groupId> | ||
<artifactId>artifact3</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
24 changes: 24 additions & 0 deletions
24
plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/testdata/formatRemove.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-bom</artifactId> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
<version>0.99.0-alpha</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.example.group1</groupId> | ||
<artifactId>artifact1</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.example.group2</groupId> | ||
<artifactId>artifact2</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
6 changes: 6 additions & 0 deletions
6
plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/testdata/testPomEmpty.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,7 +234,7 @@ void updateDependencies(Collection<Library> selectedLibraries, | |
dependency.appendChild(artifactIdElement); | ||
|
||
if (testComment == null) { | ||
dependencies.appendChild(dependency); | ||
addChild(dependencies, dependency); | ||
} else { | ||
dependencies.insertBefore(dependency, testComment); | ||
} | ||
|
@@ -279,7 +279,7 @@ private void handleDependencyManaged(LibraryFile artifact, Element dependency) { | |
} | ||
} else { | ||
if (versionNode != null) { | ||
dependency.removeChild(versionNode); | ||
removeChild(dependency, versionNode); | ||
} | ||
} | ||
} | ||
|
@@ -305,8 +305,8 @@ private void createBOMIfNeeded(XPath xpath) throws CoreException { | |
dependencyManagement = document.createElementNS("http://maven.apache.org/POM/4.0.0", | ||
"dependencyManagement"); | ||
} | ||
dependencyManagement.appendChild(dependencies); | ||
document.getDocumentElement().appendChild(dependencyManagement); | ||
addChild(dependencyManagement, dependencies); | ||
addChild(document.getDocumentElement(), dependencyManagement); | ||
} | ||
|
||
Element dependency = | ||
|
@@ -321,7 +321,7 @@ private void createBOMIfNeeded(XPath xpath) throws CoreException { | |
if (bom != null) { | ||
boms.add(bom); | ||
} | ||
dependencies.appendChild(dependency); | ||
addChild(dependencies, dependency); | ||
} | ||
} catch (XPathExpressionException ex) { | ||
IStatus status = StatusUtil.error(Pom.class, ex.getMessage(), ex); | ||
|
@@ -444,7 +444,7 @@ static void removeUnusedDependencies(Element dependencies, | |
} | ||
|
||
for (Node node : nodesToRemove) { | ||
dependencies.removeChild(node); | ||
removeChild(dependencies, node); | ||
} | ||
} | ||
|
||
|
@@ -482,10 +482,29 @@ private static String getValue(Element dependency, String childName) { | |
private void writeDocument() throws CoreException, TransformerException { | ||
Transformer transformer = transformerFactory.newTransformer(); | ||
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | ||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2"); //$NON-NLS-1$ //$NON-NLS-2$ | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
transformer.transform(new DOMSource(document), new StreamResult(out)); | ||
InputStream in = new ByteArrayInputStream(out.toByteArray()); | ||
|
||
pomFile.setContents(in, true, true, null); | ||
} | ||
|
||
private void addChild(Node parent, Node newChild) { | ||
parent.appendChild(newChild); | ||
removeWhiteSpaceBefore(newChild);// for child indentation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: space before // |
||
} | ||
|
||
private static void removeChild(Node parent, Node oldChild) { | ||
removeWhiteSpaceBefore(oldChild); | ||
parent.removeChild(oldChild); | ||
} | ||
|
||
private static void removeWhiteSpaceBefore(Node node) { | ||
Node prevElement = node.getPreviousSibling(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previousElement |
||
if (prevElement != null && prevElement.getNodeType() == Node.TEXT_NODE | ||
&& prevElement.getNodeValue().trim().length() == 0) { | ||
node.getParentNode().removeChild(prevElement); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core fix. Other changes are for removing extra spaces and blank line when add or removing libaries.