Skip to content

Commit e01da8b

Browse files
committed
Made the tests portable accross maven/gradle
(4)
1 parent 77fefa4 commit e01da8b

File tree

4 files changed

+69
-85
lines changed

4 files changed

+69
-85
lines changed

Diff for: build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ repositories {
3131
dependencies {
3232
compile 'io.springfox:springfox-swagger2:2.9.0'
3333
compile 'org.springframework:spring-webmvc:4.3.9.RELEASE'
34-
testCompile group: 'junit', name: 'junit', version:'4.12'
34+
testCompile 'junit:junit:4.12'
35+
testCompile 'org.mockito:mockito-core:2.18.3'
3536
compile files("${System.getProperty('java.home')}/../lib/tools.jar")
3637
}

Diff for: src/main/java/springfox/javadoc/doclet/SwaggerPropertiesDoclet.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static boolean validOptions(
142142
* @param root {@link RootDoc}
143143
* @return true if it started successfully
144144
*/
145-
@SuppressWarnings("unused")
145+
@SuppressWarnings({ "unused", "WeakerAccess", "UnusedReturnValue" })
146146
public static boolean start(RootDoc root) {
147147

148148
String propertyFilePath = docletOptions.getPropertyFilePath();
@@ -158,6 +158,7 @@ public static boolean start(RootDoc root) {
158158
String out = sb.toString();
159159
root.printNotice("Writing output to " + out);
160160
File file = new File(out);
161+
//noinspection ResultOfMethodCallIgnored
161162
file.getParentFile().mkdirs();
162163
OutputStream javadoc = null;
163164
try {

Diff for: src/test/java/springfox/javadoc/doclet/SwaggerPropertiesDocletTest.java

+65-72
Original file line numberDiff line numberDiff line change
@@ -20,126 +20,119 @@
2020

2121
import com.sun.javadoc.DocErrorReporter;
2222
import com.sun.javadoc.SourcePosition;
23+
import com.sun.tools.javadoc.Main;
24+
import org.junit.AfterClass;
2325
import org.junit.BeforeClass;
24-
import org.junit.Ignore;
2526
import org.junit.Test;
2627

27-
import java.io.BufferedReader;
28+
import java.io.File;
2829
import java.io.FileInputStream;
2930
import java.io.IOException;
3031
import java.io.InputStream;
31-
import java.io.InputStreamReader;
32+
import java.io.PrintWriter;
33+
import java.io.StringWriter;
3234
import java.util.Properties;
3335

3436
import static org.junit.Assert.*;
37+
import static springfox.javadoc.doclet.SwaggerPropertiesDoclet.*;
3538

36-
/**
37-
* unit tests for {@link SwaggerPropertiesDoclet}
38-
*
39-
* @author MartinNeumannBeTSE
40-
*/
4139
public class SwaggerPropertiesDocletTest {
4240

43-
/**
44-
* contains all full paths to relevant directories
45-
*/
46-
private static final Properties PROPERTIES = new Properties();
41+
private static final String BUILD_PROPERTY_FILE_LOCATION = "./build/property-file-location";
42+
private static final String GENERATED_PROPERTY_FILE =
43+
String.format("%s/%s", BUILD_PROPERTY_FILE_LOCATION, SPRINGFOX_JAVADOC_PROPERTIES);
4744

4845
@BeforeClass
49-
public static void loadProperties() throws IOException {
50-
InputStream inputStream = SwaggerPropertiesDocletTest.class.getResourceAsStream("/test.properties");
51-
PROPERTIES.load(inputStream);
52-
inputStream.close();
46+
public static void setupFixture() {
47+
deletePropertyFile();
48+
}
49+
50+
@AfterClass
51+
public static void cleanupFixture() {
52+
deletePropertyFile();
5353
}
5454

5555
@Test
56-
public void testValidOptionLength() throws IOException {
57-
assertEquals(2, SwaggerPropertiesDoclet.optionLength("-classdir"));
56+
public void testValidOptionLength() {
57+
assertEquals(2, optionLength("-classdir"));
5858
}
5959

6060
@Test
61-
public void testInvalidOptionLength() throws IOException {
62-
assertEquals(0, SwaggerPropertiesDoclet.optionLength("dummy"));
61+
public void testInvalidOptionLength() {
62+
assertEquals(0, optionLength("dummy"));
6363
}
6464

6565
@Test
6666
public void testValidOptions() {
6767
String[][] options = new String[][] { new String[] { "foo", "bar" }, new String[] { "-classdir", "dummy" } };
6868
DummyDocErrorReporter reporter = new DummyDocErrorReporter();
69-
assertTrue(SwaggerPropertiesDoclet.validOptions(options, reporter));
69+
assertTrue(validOptions(options, reporter));
7070
assertTrue(reporter.getErrors().isEmpty());
7171
}
7272

7373
@Test
7474
public void testInvalidOptions() {
7575
String[][] options = new String[][] { new String[] { "foo", "bar" }, new String[] { "baz", "dummy" } };
7676
DummyDocErrorReporter reporter = new DummyDocErrorReporter();
77-
assertFalse(SwaggerPropertiesDoclet.validOptions(options, reporter));
77+
assertFalse(validOptions(options, reporter));
7878
assertTrue(reporter.getErrors().contains("-classdir"));
7979
}
8080

8181
@Test
82-
@Ignore
83-
public void testPropertiesGeneration() throws IOException, InterruptedException {
84-
// read in the classpath file as a string.
85-
// Using the @<file-path> syntax supported by the javadoc tool won't work, if
86-
// the classpath contains whitespace.
87-
FileInputStream fis = new FileInputStream(PROPERTIES.getProperty("classpath.file"));
88-
StringBuilder classpath = new StringBuilder();
89-
String line;
90-
BufferedReader classpathReader = new BufferedReader(new InputStreamReader(fis));
91-
while ((line = classpathReader.readLine()) != null) {
92-
classpath.append(line);
93-
}
94-
classpathReader.close();
95-
96-
97-
// all the paths are read from the properties file because the javadoc
98-
// command-line tool needs full paths.
99-
// The properties file contains placeholders that are replaced by the maven
100-
// resource filter plugin to convert project-relative paths to full paths.
101-
StringBuilder command = new StringBuilder();
102-
command.append(PROPERTIES.getProperty("java.home"))
103-
.append("/../bin/javadoc");
104-
command.append(" -doclet springfox.javadoc.doclet.SwaggerPropertiesDoclet");
105-
command.append(" -docletpath ")
106-
.append(PROPERTIES.getProperty("doclet.path"));
107-
command.append(" -sourcepath ")
108-
.append(PROPERTIES.getProperty("source.path"));
109-
command.append(" -subpackages ")
110-
.append(PROPERTIES.getProperty("package.name"));
111-
command.append(" -classdir ")
112-
.append(PROPERTIES.getProperty("class.dir"));
113-
// enclose the classpath in quotes so that it still works if the classpath
114-
// contains whitespace
115-
command.append(" -classpath ")
116-
.append("\"")
117-
.append(classpath.toString()).append("\"");
118-
119-
// run the javadoc command-line tool to execute the SwaggerPropertiesDoclet on
120-
// the classes in the springfox.javadoc.example package
121-
Process process = Runtime.getRuntime().exec(command.toString());
122-
process.waitFor();
123-
assertEquals(0, process.exitValue()); // make sure there were no errors
82+
public void testPropertiesGeneration() throws IOException {
83+
84+
StringWriter err = new StringWriter();
85+
StringWriter warn = new StringWriter();
86+
StringWriter notice = new StringWriter();
87+
88+
String[] args = new String[] {
89+
"-sourcepath",
90+
"./src/test/java",
91+
"-subpackages",
92+
"springfox.javadoc",
93+
"springfox.javadoc",
94+
"-classdir",
95+
BUILD_PROPERTY_FILE_LOCATION
96+
};
97+
98+
Main.execute(
99+
"SwaggerPropertiesDoclet",
100+
new PrintWriter(err),
101+
new PrintWriter(warn),
102+
new PrintWriter(notice),
103+
SwaggerPropertiesDoclet.class.getName(),
104+
args);
105+
106+
Properties props = generatedProperties();
107+
assertEquals("test method", props.getProperty("/test/test.GET.notes"));
108+
assertEquals("dummy value", props.getProperty("/test/test.GET.return"));
109+
assertEquals("dummy param", props.getProperty("/test/test.GET.param.param"));
110+
assertEquals("without value or path", props.getProperty("/test.POST.notes"));
111+
assertEquals("retval", props.getProperty("/test.POST.return"));
112+
assertEquals("param", props.getProperty("/test.POST.param.bar"));
113+
}
124114

115+
private Properties generatedProperties() throws IOException {
125116
// read in the properties file created by the SwaggerPropertiesDoclet
126-
InputStream inputStream = SwaggerPropertiesDocletTest.class
127-
.getResourceAsStream("/" + SwaggerPropertiesDoclet.SPRINGFOX_JAVADOC_PROPERTIES);
117+
InputStream inputStream = new FileInputStream(GENERATED_PROPERTY_FILE);
128118
assertNotNull(inputStream);
129119

130120
// check that the properties match the example sources
131121
Properties props = new Properties();
132122
props.load(inputStream);
133-
assertEquals("test method", props.getProperty("/test/test.GET.notes"));
134-
assertEquals("dummy value", props.getProperty("/test/test.GET.return"));
135-
assertEquals("dummy param", props.getProperty("/test/test.GET.param.param"));
136-
assertEquals("without value or path", props.getProperty("/test.POST.notes"));
137-
assertEquals("retval", props.getProperty("/test.POST.return"));
138-
assertEquals("param", props.getProperty("/test.POST.param.bar"));
123+
return props;
124+
}
125+
126+
private static void deletePropertyFile() {
127+
File propertyFile = new File(GENERATED_PROPERTY_FILE);
128+
if (propertyFile.exists()) {
129+
propertyFile.delete();
130+
}
139131
}
140132

141133
public class DummyDocErrorReporter implements DocErrorReporter {
142134

135+
143136
private final StringBuilder errors = new StringBuilder();
144137
private final StringBuilder notices = new StringBuilder();
145138
private final StringBuilder warnings = new StringBuilder();

Diff for: src/test/resources/test.properties

-11
This file was deleted.

0 commit comments

Comments
 (0)