Skip to content

Commit

Permalink
Fixing case where configuration is first element
Browse files Browse the repository at this point in the history
  • Loading branch information
Don Brown authored and Don Brown committed Mar 30, 2009
1 parent d831a7d commit 8541aee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private void validateTargetContents(String yamlText) throws InvalidFormatExcepti
}
catch (RuntimeException ex)
{
System.out.println("yaml:"+yamlText);
throw new InvalidFormatException("Invalid YAML", yamlText, ex);
}

Expand Down Expand Up @@ -75,13 +76,13 @@ private void convert(Element element, String tabs, boolean isInList, Writer yaml
{
if (isConfigurationNotYamlSafe(element, tabs, tab))
{
yamlWriter.write(tabs + "configuration : |\n");
yamlWriter.write(tabs + prefix + "configuration : |\n");
StringWriter blockWriter = new StringWriter();
for (Iterator i = element.elementIterator(); i.hasNext(); )
{
blockWriter.append(elementToBlockString((Element) i.next()));
}
yamlWriter.write(indent(blockWriter.toString(), tabs + tab));
yamlWriter.write(indent(blockWriter.toString(), (isInList ? " " : "") + tabs + tab));
return;
}
}
Expand Down Expand Up @@ -132,7 +133,7 @@ else if (list.elements().isEmpty())
yamlWriter.write(tabs + prefix + name + ":\n");
for (Iterator i = element.elementIterator(); i.hasNext(); )
{
convert((Element) i.next(), tabs + tab, false, yamlWriter, tab);
convert((Element) i.next(), (isInList ? " " : "") + tabs + tab, false, yamlWriter, tab);
}
}
}
Expand Down
42 changes: 41 additions & 1 deletion src/test/java/org/twdata/maven/yamlpom/XmlToYamlConvertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,53 @@ public void testConvertConfigAsYamlWithSingleAttribute() throws Exception
assertTrue(config.toString().contains("implementation=\"org.apache"));
}

public void testConvertConfigAsYamlWithConfigAsFirstElement() throws Exception
{
Map<String, Object> data = buildYaml("/pom.config.xml");

Map plugin = (Map) ((List)((Map)data.get("build")).get("plugins")).get(3);
Object exec = ((List<Object>)plugin.get("executions")).get(0);
assertNotNull(exec);
assertTrue(exec instanceof Map);
//System.out.println(config);
Object config = ((Map<String,Object>)exec).get("configuration");
assertNotNull(config);
assertTrue(config instanceof Map);
assertEquals("true", ((Map<String,String>)config).get("createDependencyReducedPom"));

Object goals = ((Map<String,Object>)exec).get("goals");
assertNotNull(goals);
assertEquals(1, ((List<String>)goals).size());
assertEquals("go", ((List<String>)goals).get(0));
}

public void testConvertConfigAsStringWithConfigAsFirstElement() throws Exception
{
Map<String, Object> data = buildYaml("/pom.config.xml");

Map plugin = (Map) ((List)((Map)data.get("build")).get("plugins")).get(4);
Object exec = ((List<Object>)plugin.get("executions")).get(0);
assertNotNull(exec);
assertTrue(exec instanceof Map);
//System.out.println(config);
Object config = ((Map<String,Object>)exec).get("configuration");
assertNotNull(config);
assertTrue(config instanceof String);
assertTrue(config.toString().contains("implementation=\"org.apache"));

Object goals = ((Map<String,Object>)exec).get("goals");
assertNotNull(goals);
assertEquals(1, ((List<String>)goals).size());
assertEquals("go", ((List<String>)goals).get(0));
}

private Map<String, Object> buildYaml(String path) throws Exception
{
ConverterOptions opt = new ConverterOptions().indent(" ");
String yamlText = new XmlToYamlConverter().convert(pathToReader(path), opt);

Yaml yaml = YamlUtils.buildYaml();
//System.out.println(yamlText);
System.out.println(yamlText);
Map<String,Object> data = (Map<String,Object>) yaml.load(yamlText);
return data;
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/resources/pom.config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
</configuration>
<goals>
<goal>go</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<artifactSet>
<includes>
<include>SnakeYAML:SnakeYAML</include>
</includes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer" />
</transformers>
</configuration>
<goals>
<goal>go</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>

0 comments on commit 8541aee

Please sign in to comment.