Skip to content

Commit fdaf9c6

Browse files
committed
FELIX-6736 Emit properties in alphabetical order
1 parent 958c28d commit fdaf9c6

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

cm.json/src/main/java/org/apache/felix/cm/json/io/impl/ConfigurationWriterImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.Enumeration;
2727
import java.util.Hashtable;
2828
import java.util.Map;
29+
import java.util.SortedSet;
30+
import java.util.TreeSet;
2931

3032
import org.apache.felix.cm.json.io.ConfigurationResource;
3133
import org.apache.felix.cm.json.io.ConfigurationWriter;
@@ -85,9 +87,23 @@ public void writeConfiguration(final Dictionary<String, Object> properties) thro
8587
}
8688
}
8789

90+
/**
91+
* Returns an alphabetically sorted enumeration of the given enumeration.
92+
*
93+
* @param enumeration the enumeration to sort
94+
* @return the sorted enumeration
95+
*/
96+
private static Enumeration<String> sortedEnumeration(Enumeration<String> enumeration) {
97+
SortedSet<String> sortedSet = new TreeSet<>();
98+
while (enumeration.hasMoreElements()) {
99+
sortedSet.add(enumeration.nextElement());
100+
}
101+
return Collections.enumeration(sortedSet);
102+
}
103+
88104
private void writeConfigurationInternal(final Dictionary<String, Object> properties) throws IOException {
89105
generator.writeStartObject();
90-
final Enumeration<String> e = properties.keys();
106+
final Enumeration<String> e = sortedEnumeration(properties.keys());
91107
while (e.hasMoreElements()) {
92108
final String name = e.nextElement();
93109
final Object value = properties.get(name);

cm.json/src/main/java/org/apache/felix/cm/json/io/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
@Version("1.0.0")
19+
@Version("1.1.0")
2020
package org.apache.felix.cm.json.io;
2121

2222
import org.osgi.annotation.versioning.Version;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.felix.cm.json.io.impl;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
import java.io.IOException;
24+
import java.io.StringWriter;
25+
import java.util.Dictionary;
26+
27+
import org.junit.Test;
28+
29+
public class ConfigurationWriterImplTest {
30+
31+
@Test
32+
public void testWriteInAlphabeticalOrder() throws IOException {
33+
final ConfigurationWriterImpl cfgWriter = new ConfigurationWriterImpl();
34+
StringWriter writer = new StringWriter();
35+
Dictionary<String, Object> properties = new OrderedDictionary();
36+
properties.put("Z", "Z");
37+
properties.put("A", Integer.valueOf(1));
38+
properties.put("b", Long.valueOf(2000l));
39+
cfgWriter.build(writer).writeConfiguration(properties);
40+
assertEquals("{\n"
41+
+ " \"A:Integer\":1,\n"
42+
+ " \"Z\":\"Z\",\n"
43+
+ " \"b\":2000\n"
44+
+ "}", writer.toString());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)