Skip to content

Commit

Permalink
Add 'sourceSortOrder' to decide how source files are iterated (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Vincent authored and joelittlejohn committed Apr 22, 2017
1 parent e775827 commit 609b5db
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.jsonschema2pojo.InclusionLevel;
import org.jsonschema2pojo.Jsonschema2Pojo;
import org.jsonschema2pojo.NoopAnnotator;
import org.jsonschema2pojo.SourceSortOrder;
import org.jsonschema2pojo.SourceType;
import org.jsonschema2pojo.URLProtocol;
import org.jsonschema2pojo.rules.RuleFactory;
Expand Down Expand Up @@ -154,6 +155,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private String refFragmentPathDelimiters = "#/.";

private SourceSortOrder sourceSortOrder = SourceSortOrder.OS;

/**
* Execute this task (it's expected that all relevant setters will have been
* called by Ant to provide task configuration <em>before</em> this method
Expand Down Expand Up @@ -729,7 +732,7 @@ public void setCustomDatePattern(String customDatePattern) {
/**
* Sets the 'customDateTimePattern' property of this class
*
* @param customDatePattern
* @param customDateTimePattern
* A custom pattern to use when formatting date-time fields during
* serialization. Requires support from your JSON binding
* library.
Expand All @@ -749,6 +752,16 @@ public void setRefFragmentPathDelimiters(String refFragmentPathDelimiters) {
this.refFragmentPathDelimiters = refFragmentPathDelimiters;
}

/**
* Sets the 'sourceSortOrder' property of this class
*
* @param sourceSortOrder Sets the sort order for the source files to be processed in. By default the OS can
* influence the processing order.
*/
public void setSourceSortOrder(SourceSortOrder sourceSortOrder) {
this.sourceSortOrder = sourceSortOrder;
}

@Override
public boolean isGenerateBuilders() {
return generateBuilders;
Expand Down Expand Up @@ -1010,4 +1023,9 @@ public String getCustomDateTimePattern() {
public String getRefFragmentPathDelimiters() {
return refFragmentPathDelimiters;
}

@Override
public SourceSortOrder getSourceSortOrder() {
return sourceSortOrder;
}
}
17 changes: 17 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,23 @@ <h3>Parameters</h3>
</td>
<td align="center" valign="top">No (default <code>#/.</code>)</td>
</tr>
<tr>
<td valign="top">sourceSortOrder</td>
<td valign="top">The sort order to be applied to the source files when being processed, by default the OS can
influence the ordering.
<ul>
<li><code>OS</code> (Let the OS influence the order that the source files are processed.)</li>
<li><code>FILES_FIRST</code> (Case sensitive sort, visit the files first. The source files are processed
in a breadth first sort order.)
</li>
<li><code>SUBDIRS_FIRST</code> (Case sensitive sort, visit the sub-directories before the files. The
source files are processed in a depth first sort order.)
</li>
</ul>
</td>
<td align="center" valign="top">No (default <code>OS</code>)</td>
</tr>

</table>

<h3>Examples</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.InclusionLevel;
import org.jsonschema2pojo.NoopAnnotator;
import org.jsonschema2pojo.SourceSortOrder;
import org.jsonschema2pojo.SourceType;
import org.jsonschema2pojo.rules.RuleFactory;

Expand Down Expand Up @@ -181,9 +182,13 @@ public class Arguments implements GenerationConfig {
@Parameter(names = {"-rpd", "--ref-fragment-path-delimiters"}, description = "A string containing any characters that should act as path delimiters when resolving $ref fragments. By default, #, / and . are used in an attempt to support JSON Pointer and JSON Path.")
private String refFragmentPathDelimiters = "#/.";

@Parameter(names = { "-sso", "--source-sort-order" }, description = "The sort order to be applied to the source files. Available options are: OS, FILES_FIRST or SUBDIRS_FIRST")
private SourceSortOrder sourceSortOrder = SourceSortOrder.OS;

private static final int EXIT_OKAY = 0;
private static final int EXIT_ERROR = 1;


/**
* Parses command line arguments and populates this command line instance.
* <p>
Expand Down Expand Up @@ -450,4 +455,8 @@ public String getCustomDateTimePattern() {
return customDateTimePattern;
}

@Override
public SourceSortOrder getSourceSortOrder() {
return sourceSortOrder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,12 @@ public String getCustomDatePattern() {
public String getCustomDateTimePattern() {
return null;
}
}

/**
* @return {@link SourceSortOrder#OS}
*/
@Override
public SourceSortOrder getSourceSortOrder() {
return SourceSortOrder.OS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,20 @@ public interface GenerationConfig {
*/
String getRefFragmentPathDelimiters();

/**
* Gets the 'sourceSortOrder' configuration option.
*
* @return
* <p>
* Supported values:
* <ul>
* <li><code>OS</code> (Let the OS influence the order the source files are processed.)</li>
* <li><code>FILES_FIRST</code> (Case sensitive sort, visit the files first. The source files are processed in a breadth
* first sort order.)</li>
* <li><code>SUBDIRS_FIRST</code> (Case sensitive sort, visit the sub-directories before the files. The source files are
* processed in a depth first sort order.)</li>
* </ul>
*/
SourceSortOrder getSourceSortOrder();

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ private static RuleFactory createRuleFactory(GenerationConfig config) {
}

private static void generateRecursive(GenerationConfig config, SchemaMapper mapper, JCodeModel codeModel, String packageName, List<File> schemaFiles) throws IOException {
Collections.sort(schemaFiles);

Collections.sort(schemaFiles, config.getSourceSortOrder().getComparator());

for (File child : schemaFiles) {
if (child.isFile()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed 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.jsonschema2pojo;

import java.io.File;
import java.util.Comparator;

/**
* Defines the order the source files and directories are processed in.
*/
public enum SourceSortOrder {
/**
* <code>OS</code> Let the OS influence the order that the source files are processed.
*/
OS {
@Override
public Comparator<File> getComparator() {
return new Comparator<File>() {
@Override
public int compare(final File fileA, final File fileB) {
return fileA.compareTo(fileB);
}
};
}
},

/**
* <code>FILES_FIRST</code> Case sensitive sort, visit the files first. The source files are processed in a
* breadth first sort order.
*/
FILES_FIRST {
@Override
public Comparator<File> getComparator() {
return new Comparator<File>() {
@Override
public int compare(final File fileA, final File fileB) {
if (fileA.isDirectory() && !fileB.isDirectory()) {
return 1;
}

if (!fileA.isDirectory() && fileB.isDirectory()) {
return -1;
}
return fileA.compareTo(fileB);
}
};
}
},

/**
* <code>SUBDIRS_FIRST</code> Case sensitive sort, visit the sub-directories before the files. The source files
* are processed in a depth first sort order.
*/
SUBDIRS_FIRST {
@Override
public Comparator<File> getComparator() {
return new Comparator<File>() {
@Override
public int compare(final File fileA, final File fileB) {
if (fileA.isDirectory() && !fileB.isDirectory()) {
return -1;
}

if (!fileA.isDirectory() && fileB.isDirectory()) {
return 1;
}
return fileA.compareTo(fileB);
}
};
}
};

public abstract Comparator<File> getComparator();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed 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.jsonschema2pojo;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Comparator;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

public class SourceSortOrderTest {
@Test
public void testTwoFilesAreCompared_FILES_FIRST() throws IOException {
testTwoFilesAreCompared(SourceSortOrder.FILES_FIRST.getComparator());
}

@Test
public void twoDirectoriesAreCompared_FILES_FIRST() throws IOException {
testTwoDirectoriesAreCompared(SourceSortOrder.FILES_FIRST.getComparator());
}

@Test
public void testTwoFilesAreCompared_SUBDIRS_FIRST() throws IOException {
testTwoFilesAreCompared(SourceSortOrder.SUBDIRS_FIRST.getComparator());
}

@Test
public void twoDirectoriesAreCompared_SUBDIRS_FIRST() throws IOException {
testTwoDirectoriesAreCompared(SourceSortOrder.SUBDIRS_FIRST.getComparator());
}

private void testTwoFilesAreCompared(Comparator<File> fileComparator) throws IOException {
final File mockFileA = mockFile();
final File mockFileB = mockFile();

fileComparator.compare(mockFileA, mockFileB);
verify(mockFileA, atLeast(1)).compareTo(mockFileB);
}

private void testTwoDirectoriesAreCompared(Comparator<File> fileComparator) throws IOException {
final File mockDirA = mockDirectory();
final File mockDirB = mockDirectory();

fileComparator.compare(mockDirA, mockDirB);
verify(mockDirA, atLeast(1)).compareTo(mockDirB);
}

@Test
public void filesBeforeDirectories_FILES_FIRST() throws IOException {
final Comparator<File> fileComparator = SourceSortOrder.FILES_FIRST.getComparator();
final File mockFile = mockFile();
final File mockDir = mockDirectory();

assertThat(fileComparator.compare(mockFile, mockDir), lessThan(0));
assertThat(fileComparator.compare(mockDir, mockFile), greaterThan(0));

verify(mockFile, never()).compareTo(any(File.class));
verify(mockDir, never()).compareTo(any(File.class));
}

@Test
public void filesBeforeDirectories_SUBDIRS_FIRST() throws IOException {
final Comparator<File> fileComparator = SourceSortOrder.SUBDIRS_FIRST.getComparator();
final File mockFile = mockFile();
final File mockDir = mockDirectory();

assertThat(fileComparator.compare(mockFile, mockDir), greaterThan(0));
assertThat(fileComparator.compare(mockDir, mockFile), lessThan(0));

verify(mockFile, never()).compareTo(any(File.class));
verify(mockDir, never()).compareTo(any(File.class));
}

private File mockFile() {
return mockFile(false);
}

private File mockDirectory() {
return mockFile(true);
}

private File mockFile(final boolean isDirectory) {
final File mockFile = mock(File.class);
when(mockFile.isDirectory()).thenReturn(isDirectory);
return mockFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jsonschema2pojo.AllFileFilter
import org.jsonschema2pojo.GenerationConfig
import org.jsonschema2pojo.InclusionLevel
import org.jsonschema2pojo.NoopAnnotator
import org.jsonschema2pojo.SourceSortOrder
import org.jsonschema2pojo.SourceType
import org.jsonschema2pojo.rules.RuleFactory

Expand Down Expand Up @@ -77,6 +78,7 @@ public class JsonSchemaExtension implements GenerationConfig {
String customDatePattern
String customDateTimePattern
String refFragmentPathDelimiters
SourceSortOrder sourceSortOrder

public JsonSchemaExtension() {
// See DefaultGenerationConfig
Expand Down Expand Up @@ -122,6 +124,7 @@ public class JsonSchemaExtension implements GenerationConfig {
formatDates = false
formatDateTimes = false
refFragmentPathDelimiters = "#/."
sourceSortOrder = SourceSortOrder.OS
}

@Override
Expand Down Expand Up @@ -166,6 +169,10 @@ public class JsonSchemaExtension implements GenerationConfig {
sourceType = SourceType.valueOf(s.toUpperCase())
}

public void setSourceSortOrder(String sortOrder) {
sourceSortOrder = SourceSortOrder.valueOf(sortOrder.toUpperCase())
}

@Override
public String toString() {
"""|generateBuilders = ${generateBuilders}
Expand Down Expand Up @@ -211,6 +218,7 @@ public class JsonSchemaExtension implements GenerationConfig {
|customDatePattern = ${customDatePattern}
|customDateTimePattern = ${customDateTimePattern}
|refFragmentPathDelimiters = ${refFragmentPathDelimiters}
|sourceSortOrder = ${sourceSortOrder}
""".stripMargin()
}

Expand Down
Loading

0 comments on commit 609b5db

Please sign in to comment.