Commit d610f3c2 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Fix ordering of metadata entries"

See gh-26230
parent 3e34b0a6
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -160,21 +160,22 @@ class JsonConverter { ...@@ -160,21 +160,22 @@ class JsonConverter {
private static class ItemMetadataComparator implements Comparator<ItemMetadata> { private static class ItemMetadataComparator implements Comparator<ItemMetadata> {
private final Comparator<ItemMetadata> itemComparator = Comparator.comparing(this::isDeprecated) private static final Comparator<ItemMetadata> GROUP = Comparator.comparing(ItemMetadata::getName)
.thenComparing(ItemMetadata::getName).thenComparing(ItemMetadata::getSourceType); .thenComparing(ItemMetadata::getSourceType, Comparator.nullsFirst(Comparator.naturalOrder()));
private final Comparator<ItemMetadata> groupComparator = Comparator.comparing(ItemMetadata::getName) private static final Comparator<ItemMetadata> ITEM = Comparator.comparing(ItemMetadataComparator::isDeprecated)
.thenComparing(ItemMetadata::getSourceType); .thenComparing(ItemMetadata::getName)
.thenComparing(ItemMetadata::getSourceType, Comparator.nullsFirst(Comparator.naturalOrder()));
@Override @Override
public int compare(ItemMetadata o1, ItemMetadata o2) { public int compare(ItemMetadata o1, ItemMetadata o2) {
if (o1.isOfItemType(ItemType.GROUP)) { if (o1.isOfItemType(ItemType.GROUP)) {
return this.groupComparator.compare(o1, o2); return GROUP.compare(o1, o2);
} }
return this.itemComparator.compare(o1, o2); return ITEM.compare(o1, o2);
} }
private boolean isDeprecated(ItemMetadata item) { private static boolean isDeprecated(ItemMetadata item) {
return item.getDeprecation() != null; return item.getDeprecation() != null;
} }
......
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -139,4 +139,32 @@ class JsonMarshallerTests { ...@@ -139,4 +139,32 @@ class JsonMarshallerTests {
"com.example.Bar", "\"com.example.bravo.aaa\"", "com.example.Foo"); "com.example.Bar", "\"com.example.bravo.aaa\"", "com.example.Foo");
} }
@Test
void orderingForSameGroupWithNullSourceType() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newGroup("com.acme.alpha", null, "com.example.Foo", null));
metadata.add(ItemMetadata.newGroup("com.acme.alpha", null, null, null));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = outputStream.toString();
assertThat(json).containsSubsequence("\"groups\"", "\"name\": \"com.acme.alpha\"",
"\"name\": \"com.acme.alpha\"", "\"sourceType\": \"com.example.Foo\"");
}
@Test
void orderingForSamePropertyNamesWithNullSourceType() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", "java.lang.Boolean", null, null, null, null,
null));
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", "java.lang.Integer", "com.example.Bar", null,
null, null, null));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = outputStream.toString();
assertThat(json).containsSubsequence("\"groups\"", "\"properties\"", "\"com.example.bravo.aaa\"",
"\"java.lang.Boolean\"", "\"com.example.bravo.aaa\"", "\"java.lang.Integer\"", "\"com.example.Bar");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment