Skip to content

Commit

Permalink
FORGE-440: Fixed property sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 2, 2016
1 parent 795e398 commit 9c4d79d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.ActivationOS;
Expand Down Expand Up @@ -117,20 +117,20 @@ public MavenJDOMWriter()
* @param name
* @param parent
*/
protected Element findAndReplaceProperties(Counter counter, Element parent, String name, Map props)
protected Element findAndReplaceProperties(Counter counter, Element parent, String name, Properties props)
{
boolean shouldExist = props != null && !props.isEmpty();
Element element = updateElement(counter, parent, name, shouldExist);
if (shouldExist)
{
Counter innerCounter = new Counter(counter.getDepth() + 1);
// while ( it.hasNext() )
for (Map.Entry<String, String> entry : ((Map<String, String>) props).entrySet())
List lst = new ArrayList(props.keySet());
for (Object key : lst)
{
String key = entry.getKey();
findAndReplaceSimpleElement(innerCounter, element, key, entry.getValue(), null);
String strKey = key.toString();
findAndReplaceSimpleElement(innerCounter, element, strKey, props.getProperty(strKey), null);
}
List lst = new ArrayList(props.keySet());
Iterator it = element.getChildren().iterator();
while (it.hasNext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.jboss.forge.addon.maven.projects.facets;

import java.io.InputStream;
import java.nio.file.Paths;
import java.util.List;

import org.apache.maven.model.Model;
Expand All @@ -21,6 +23,7 @@
import org.jboss.forge.arquillian.archive.AddonArchive;
import org.jboss.forge.furnace.container.simple.Service;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;
import org.jboss.forge.furnace.util.Streams;
import org.jboss.forge.parser.xml.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
Expand All @@ -47,6 +50,7 @@ public static AddonArchive getDeployment()
{
AddonArchive archive = ShrinkWrap
.create(AddonArchive.class)
.addAsResource(Paths.get("src/test/resources/pom-template.xml").toFile(), "templates/pom-template.xml")
.addAsServiceProvider(Service.class, MavenFacetTest.class);

return archive;
Expand Down Expand Up @@ -85,4 +89,22 @@ public void testSortedProperties() throws Exception
Assert.assertEquals("D", propEntries.get(3).getName());
Assert.assertEquals("E", propEntries.get(4).getName());
}

@Test
public void testPreservePOMFormat() throws Exception
{
String pom;
try (InputStream is = getClass().getClassLoader().getResourceAsStream("templates/pom-template.xml"))
{
pom = Streams.toString(is);
}
Project project = projectFactory.createTempProject();
MavenFacet facet = project.getFacet(MavenFacet.class);
MavenModelResource modelResource = facet.getModelResource();
modelResource.setContents(pom);
Model model = modelResource.getCurrentModel();
model.setArtifactId("mytest");
facet.setModel(model);
Assert.assertEquals(pom.replace("myartifactid", "mytest").trim(), modelResource.getContents().trim());
}
}
22 changes: 15 additions & 7 deletions maven/tests/src/test/resources/pom-template.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<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">
<?xml version="1.0" encoding="UTF8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<artifactId>projects-impl</artifactId>
<groupId>org.example</groupId>
<artifactId>myartifactid</artifactId>
<name>Forge - Projects Impl</name>

<!-- Project dependencies go here -->
<dependencies>
</dependencies>

<!-- This is where the build goes -->
<build>
<defaultGoal>clean</defaultGoal>
</build>
<!-- There are not many contributors -->
<contributors>
<contributor>
<name>George</name>
<email>ggastald@redhat.com</email>
</contributor>
</contributors>
</project>

0 comments on commit 9c4d79d

Please sign in to comment.