-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'sourceSortOrder' to decide how source files are iterated (#719)
- Loading branch information
1 parent
e775827
commit 609b5db
Showing
15 changed files
with
417 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SourceSortOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
106 changes: 106 additions & 0 deletions
106
jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/SourceSortOrderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.