Skip to content

Commit

Permalink
Added a test to read the custom sort order file. Updated the fileUtil…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
Ekryd committed Apr 12, 2018
1 parent 7566239 commit 18553bf
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 8 deletions.
8 changes: 4 additions & 4 deletions sorter/src/main/java/sortpom/wrapper/WrapperFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Concrete implementation of a wrapper factory that sorts xml according to
* sort order from fileUtil.
*
* <p>
* Thank you Christian Haelg for your sortProperties patch.
*
* @author Bjorn Ekryd
Expand Down Expand Up @@ -69,12 +69,12 @@ private void initializeSortOrderMap() {
}
}

private Document createDocumentFromDefaultSortOrderFile()
Document createDocumentFromDefaultSortOrderFile()
throws JDOMException, IOException {
try (InputStream inputStream = new ByteArrayInputStream(fileUtil.getDefaultSortOrderXmlBytes())){
try (InputStream inputStream = new ByteArrayInputStream(fileUtil.getDefaultSortOrderXmlBytes())) {
SAXBuilder parser = new SAXBuilder();
return parser.build(inputStream);
}
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions sorter/src/test/java/sortpom/util/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public void defaultSortOrderFromNonExistingShouldThrowException() {

@Test
public void defaultSortOrderFromUrlShouldWork() throws IOException {
FileUtil fileUtil = createFileUtil("https://opensource.org/licenses/BSD-3-Clause");
FileUtil fileUtil = createFileUtil("https://en.wikipedia.org/wiki/Sweden");

try {
byte[] defaultSortOrderXmlBytes = fileUtil.getDefaultSortOrderXmlBytes();
Assert.assertThat(new String(defaultSortOrderXmlBytes), containsString("The 3-Clause BSD License"));
Assert.assertThat(new String(defaultSortOrderXmlBytes), containsString("Sverige"));
} catch (UnknownHostException e) {
// This is ok, we were not online when the test was perfomed
// Which actually makes this test a bit pointless :-(
Expand All @@ -67,15 +67,15 @@ public void defaultSortOrderFromNonExistingHostShouldThrowException() throws IOE

@Test
public void defaultSortOrderFromNonExistingPageShouldThrowException() throws IOException {
FileUtil fileUtil = createFileUtil("https://opensource.org/this.does.not.work");
FileUtil fileUtil = createFileUtil("https://github.com/Ekryd/sortpom/where_are_the_donations");

try {
fileUtil.getDefaultSortOrderXmlBytes();
fail();
} catch (UnknownHostException e) {
// This is ok, we were not online when the test was performed
} catch (FileNotFoundException e) {
assertThat(e.getMessage(), is("https://opensource.org/this.does.not.work"));
assertThat(e.getMessage(), is("https://github.com/Ekryd/sortpom/where_are_the_donations"));
}
}

Expand Down
58 changes: 58 additions & 0 deletions sorter/src/test/java/sortpom/wrapper/CustomSortOrderFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package sortpom.wrapper;

import org.apache.commons.io.IOUtils;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.junit.Before;
import org.junit.Test;
import sortpom.parameter.PluginParameters;
import sortpom.util.FileUtil;
import sortpom.wrapper.operation.HierarchyRootWrapper;

import java.io.FileInputStream;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

/**
* @author bjorn
* @since 2018-04-12
*/
public class CustomSortOrderFileTest {
private static final String UTF_8 = "UTF-8";

@Before
public void setUp() throws Exception {
}

@Test
public void compareDefaultSortOrderFileToString() throws IOException, JDOMException {
String expected = IOUtils.toString(new FileInputStream("src/test/resources/sortOrderFiles/with_newline_tagsToString.txt"), UTF_8);
assertEquals(expected, getToStringOnCustomSortOrderFile());
}

private String getToStringOnCustomSortOrderFile() throws IOException, JDOMException {
PluginParameters pluginParameters = PluginParameters.builder()
.setPomFile(null).setFileOutput(false, ".bak", null)
.setEncoding("UTF-8")
.setFormatting("\r\n", true, true)
.setIndent(2, false)
.setSortOrder("src/test/resources/sortOrderFiles/with_newline_tags.xml", null)
.setSortEntities("scope,groupId,artifactId", "groupId,artifactId", true, true).build();

FileUtil fileUtil = new FileUtil();
fileUtil.setup(pluginParameters);

WrapperFactoryImpl wrapperFactory = new WrapperFactoryImpl(fileUtil);

Document documentFromDefaultSortOrderFile = wrapperFactory.createDocumentFromDefaultSortOrderFile();
new HierarchyRootWrapper(wrapperFactory.create(documentFromDefaultSortOrderFile.getRootElement()));

HierarchyRootWrapper rootWrapper = new HierarchyRootWrapper(wrapperFactory.create(documentFromDefaultSortOrderFile.getRootElement()));
rootWrapper.createWrappedStructure(wrapperFactory);

return rootWrapper.toString();
}


}
21 changes: 21 additions & 0 deletions sorter/src/test/resources/sortOrderFiles/with_newline_tags.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion/>
<newLine/>
<groupId/>
<artifactId/>
<version/>
<newLine/>
<name/>
<url/>
<dependencies>
<dependency>
<groupId/>
<artifactId/>
<version/>
<classifier/>
</dependency>
<newLine/>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <project [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
children=
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <modelVersion [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <newLine [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <groupId [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <artifactId [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <version [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <newLine [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <name [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <url [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <dependencies [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
children=
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <dependency [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
children=
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <groupId [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <artifactId [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <version [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <classifier [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
}
HierarchyWrapper{
elementContent=UnsortedWrapper{content=[Element: <newLine [Namespace: http://maven.apache.org/POM/4.0.0]/>]}
}
}
}

0 comments on commit 18553bf

Please sign in to comment.