|
20 | 20 |
|
21 | 21 | import com.sun.javadoc.DocErrorReporter;
|
22 | 22 | import com.sun.javadoc.SourcePosition;
|
| 23 | +import com.sun.tools.javadoc.Main; |
| 24 | +import org.junit.AfterClass; |
23 | 25 | import org.junit.BeforeClass;
|
24 |
| -import org.junit.Ignore; |
25 | 26 | import org.junit.Test;
|
26 | 27 |
|
27 |
| -import java.io.BufferedReader; |
| 28 | +import java.io.File; |
28 | 29 | import java.io.FileInputStream;
|
29 | 30 | import java.io.IOException;
|
30 | 31 | import java.io.InputStream;
|
31 |
| -import java.io.InputStreamReader; |
| 32 | +import java.io.PrintWriter; |
| 33 | +import java.io.StringWriter; |
32 | 34 | import java.util.Properties;
|
33 | 35 |
|
34 | 36 | import static org.junit.Assert.*;
|
| 37 | +import static springfox.javadoc.doclet.SwaggerPropertiesDoclet.*; |
35 | 38 |
|
36 |
| -/** |
37 |
| - * unit tests for {@link SwaggerPropertiesDoclet} |
38 |
| - * |
39 |
| - * @author MartinNeumannBeTSE |
40 |
| - */ |
41 | 39 | public class SwaggerPropertiesDocletTest {
|
42 | 40 |
|
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); |
47 | 44 |
|
48 | 45 | @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(); |
53 | 53 | }
|
54 | 54 |
|
55 | 55 | @Test
|
56 |
| - public void testValidOptionLength() throws IOException { |
57 |
| - assertEquals(2, SwaggerPropertiesDoclet.optionLength("-classdir")); |
| 56 | + public void testValidOptionLength() { |
| 57 | + assertEquals(2, optionLength("-classdir")); |
58 | 58 | }
|
59 | 59 |
|
60 | 60 | @Test
|
61 |
| - public void testInvalidOptionLength() throws IOException { |
62 |
| - assertEquals(0, SwaggerPropertiesDoclet.optionLength("dummy")); |
| 61 | + public void testInvalidOptionLength() { |
| 62 | + assertEquals(0, optionLength("dummy")); |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | @Test
|
66 | 66 | public void testValidOptions() {
|
67 | 67 | String[][] options = new String[][] { new String[] { "foo", "bar" }, new String[] { "-classdir", "dummy" } };
|
68 | 68 | DummyDocErrorReporter reporter = new DummyDocErrorReporter();
|
69 |
| - assertTrue(SwaggerPropertiesDoclet.validOptions(options, reporter)); |
| 69 | + assertTrue(validOptions(options, reporter)); |
70 | 70 | assertTrue(reporter.getErrors().isEmpty());
|
71 | 71 | }
|
72 | 72 |
|
73 | 73 | @Test
|
74 | 74 | public void testInvalidOptions() {
|
75 | 75 | String[][] options = new String[][] { new String[] { "foo", "bar" }, new String[] { "baz", "dummy" } };
|
76 | 76 | DummyDocErrorReporter reporter = new DummyDocErrorReporter();
|
77 |
| - assertFalse(SwaggerPropertiesDoclet.validOptions(options, reporter)); |
| 77 | + assertFalse(validOptions(options, reporter)); |
78 | 78 | assertTrue(reporter.getErrors().contains("-classdir"));
|
79 | 79 | }
|
80 | 80 |
|
81 | 81 | @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 | + } |
124 | 114 |
|
| 115 | + private Properties generatedProperties() throws IOException { |
125 | 116 | // 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); |
128 | 118 | assertNotNull(inputStream);
|
129 | 119 |
|
130 | 120 | // check that the properties match the example sources
|
131 | 121 | Properties props = new Properties();
|
132 | 122 | 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 | + } |
139 | 131 | }
|
140 | 132 |
|
141 | 133 | public class DummyDocErrorReporter implements DocErrorReporter {
|
142 | 134 |
|
| 135 | + |
143 | 136 | private final StringBuilder errors = new StringBuilder();
|
144 | 137 | private final StringBuilder notices = new StringBuilder();
|
145 | 138 | private final StringBuilder warnings = new StringBuilder();
|
|
0 commit comments