[MNG-6801] MavenXpp3Writer doesn't retain order of properties (#811)

This commit is contained in:
Guillaume Nodet 2022-10-10 22:22:26 +02:00 committed by GitHub
parent ae655e0e83
commit 8b57a3abc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 4 deletions

View File

@ -41,6 +41,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -273,7 +274,7 @@ public class ${className}
${classLcapName}.${field.name}( ${field.name} );
break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new HashMap<>();
Map<String, String> ${field.name} = new LinkedHashMap<>();
locations = new HashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG )
{

View File

@ -41,6 +41,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -297,7 +298,7 @@ public class ${className}
${classLcapName}.${field.name}( ${field.name} );
break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new HashMap<>();
Map<String, String> ${field.name} = new LinkedHashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG )
{
String key = parser.getName();

View File

@ -0,0 +1,69 @@
package org.apache.maven.model.v4;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.maven.api.model.Model;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ModelXmlTest
{
@Test
public void testXmlRoundtripWithProperties() throws Exception
{
Map<String, String> props = new LinkedHashMap<>();
props.put( "javax.version", "3.1.0" );
props.put( "mockito.version", "1.10.19" );
props.put( "hamcret.version", "2.1" );
props.put( "lombok.version", "1.18.6" );
props.put( "junit.version", "4.12" );
Model model = Model.newBuilder( true )
.properties( props )
.build();
String xml = toXml( model );
for ( int i = 0; i < 10; i++ )
{
String newStr = toXml( fromXml( xml ) );
assertEquals( newStr, xml );
}
}
String toXml( Model model ) throws IOException
{
StringWriter sw = new StringWriter();
new MavenXpp3Writer().write( sw, model );
return sw.toString();
}
Model fromXml( String xml ) throws IOException, XmlPullParserException
{
return new MavenXpp3Reader().read( new StringReader( xml ) );
}
}

View File

@ -41,6 +41,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -771,7 +772,7 @@ public class ${className}
${classLcapName}.${field.name}( ${field.name} );
break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new HashMap<>();
Map<String, String> ${field.name} = new LinkedHashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG )
{
String key = parser.getName();

View File

@ -41,6 +41,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -771,7 +772,7 @@ public class ${className}
${classLcapName}.${field.name}( ${field.name} );
break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new HashMap<>();
Map<String, String> ${field.name} = new LinkedHashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG )
{
String key = parser.getName();