Skip to content

Commit

Permalink
FELIX-6736 Emit properties in alphabetical order
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Nov 15, 2024
1 parent 958c28d commit bf51840
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.felix.cm.json.io.ConfigurationResource;
import org.apache.felix.cm.json.io.ConfigurationWriter;
Expand Down Expand Up @@ -85,9 +88,24 @@ public void writeConfiguration(final Dictionary<String, Object> properties) thro
}
}

/**
* Returns an alphabetically sorted enumeration of the given enumeration.
*
* @param enumeration the enumeration to sort
* @return the sorted enumeration
*/
private static Enumeration<String> sortedEnumeration(Enumeration<String> enumeration) {
SortedSet<String> sortedSet = new TreeSet<>();
while (enumeration.hasMoreElements())
{
sortedSet.add(enumeration.nextElement());
}
return Collections.enumeration(sortedSet);
}

private void writeConfigurationInternal(final Dictionary<String, Object> properties) throws IOException {
generator.writeStartObject();
final Enumeration<String> e = properties.keys();
final Enumeration<String> e = sortedEnumeration(properties.keys());
while (e.hasMoreElements()) {
final String name = e.nextElement();
final Object value = properties.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
@Version("1.0.0")
@Version("1.1.0")
package org.apache.felix.cm.json.io;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/
package org.apache.felix.cm.json.io.impl;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Dictionary;

import org.junit.Test;

public class ConfigurationWriterImplTest {

@Test
public void testWriteInAlphabeticalOrder() throws IOException {
final ConfigurationWriterImpl cfgWriter = new ConfigurationWriterImpl();
StringWriter writer = new StringWriter();
Dictionary<String, Object> properties = new OrderedDictionary();
properties.put("Z", "Z");
properties.put("A", Integer.valueOf(1));
properties.put("b", Long.valueOf(2000l));
cfgWriter.build(writer).writeConfiguration(properties);
assertEquals("{\n"
+ " \"A:Integer\":1,\n"
+ " \"Z\":\"Z\",\n"
+ " \"b\":2000\n"
+ "}", writer.toString());
}

}

0 comments on commit bf51840

Please sign in to comment.