/
WhenAsciidoctorIsCalledUsingCli.java
227 lines (155 loc) · 7.33 KB
/
WhenAsciidoctorIsCalledUsingCli.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package org.asciidoctor.cli;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import org.asciidoctor.util.ClasspathResources;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class WhenAsciidoctorIsCalledUsingCli {
@Rule
public ClasspathResources classpath = new ClasspathResources();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
public String pwd = new File("").getAbsolutePath();
@Test
public void with_no_options_file_should_be_rendered_in_place_and_in_html5_format() {
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke(inputPath);
File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc$", ".html"));
assertThat(expectedFile.exists(), is(true));
expectedFile.delete();
}
@Test
public void file_should_be_rendered_to_docbook_with_docbook_backend() {
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("-b", "docbook", inputPath);
File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc", ".xml"));
assertThat(expectedFile.exists(), is(true));
expectedFile.delete();
}
@Test
public void single_attributes_should_be_interpreted_as_boolean() throws IOException {
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("-a", "linkcss!", inputPath);
File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc$", ".html"));
Document doc = Jsoup.parse(expectedFile, "UTF-8");
Elements cssStyle = doc.select("style");
assertThat(cssStyle.html(), is(not("")));
Elements link = doc.select("link");
assertThat(link.html(), is("".trim()));
expectedFile.delete();
}
@Test
public void composed_attributes_should_be_built_as_attributes_map() throws IOException {
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("-a", "stylesheet=mystyles.css", "-a", "linkcss", inputPath);
File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc$", ".html"));
Document doc = Jsoup.parse(expectedFile, "UTF-8");
Elements link = doc.select("link[href]");
String attr = link.attr("href");
assertThat(attr, is("./mystyles.css"));
expectedFile.delete();
}
@Test
public void destination_dir_should_render_files_to_ouput_directory() {
File outputDirectory = temporaryFolder.getRoot();
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("-D", outputDirectory.getAbsolutePath(), inputPath);
File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc$", ".html"));
assertThat(expectedFile.exists(), is(true));
}
@Test(expected=IllegalArgumentException.class)
public void no_input_file_should_throw_an_exception() {
new AsciidoctorInvoker().invoke("");
}
@Test(expected=IllegalArgumentException.class)
public void invalid_input_file_should_throw_an_exception() {
new AsciidoctorInvoker().invoke("myunknown.adoc");
}
@Test
public void more_than_one_input_file_should_throw_an_exception() {
File inputFile1 = classpath.getResource("rendersample.asciidoc");
String inputPath1 = inputFile1.getPath().substring(pwd.length() + 1);
File inputFile2 = classpath.getResource("tocsample.asciidoc");
String inputPath2 = inputFile2.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke(inputPath1, inputPath2);
File expectedFile1 = new File(inputPath1.replaceFirst("\\.asciidoc$", ".html"));
assertThat(expectedFile1.exists(), is(true));
expectedFile1.delete();
File expectedFile2 = new File(inputPath2.replaceFirst("\\.asciidoc$", ".html"));
assertThat(expectedFile2.exists(), is(true));
expectedFile2.delete();
}
@Test
public void glob_expression_can_be_used_to_render_AsciiDoc_files() {
File inputFile = classpath.getResource("toc2sample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
String inputGlob = new File(new File(inputPath).getParentFile(), "toc2*.asciidoc").getPath();
new AsciidoctorInvoker().invoke(inputGlob);
File expectedFile = new File(inputPath.replaceFirst("\\.asciidoc$", ".html"));
assertThat(expectedFile.exists(), is(true));
expectedFile.delete();
}
@Test
public void help_option_should_show_usage_information() {
ByteArrayOutputStream output = redirectStdout();
new AsciidoctorInvoker().invoke("--help");
String helpMessage = output.toString();
assertThat(helpMessage, startsWith("Usage: asciidoctor [options] input file"));
}
@Test
public void no_parameters_should_show_usage_information() {
ByteArrayOutputStream output = redirectStdout();
new AsciidoctorInvoker().invoke();
String helpMessage = output.toString();
assertThat(helpMessage, startsWith("Usage: asciidoctor [options] input file"));
}
@Test
public void output_file_hyphen_symbol_should_render_output_to_stdout() {
ByteArrayOutputStream output = redirectStdout();
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("-o", "-", inputPath);
Document doc = Jsoup.parse(output.toString(), "UTF-8");
Elements link = doc.select("div[class]");
String attr = link.attr("class");
assertThat(attr, is("sect1"));
}
@Test
public void verbose_option_should_fill_monitor_map() {
ByteArrayOutputStream output = redirectStdout();
File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("--verbose", inputPath);
String outputConsole = output.toString();
assertThat(outputConsole, startsWith("Time to read and parse source"));
}
@Test
public void quiet_option_should_not_write_to_console() {
ByteArrayOutputStream output = redirectStdout();
File inputFile = classpath.getResource("brokeninclude.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
new AsciidoctorInvoker().invoke("--quiet", inputPath);
String outputConsole = output.toString();
assertThat(outputConsole.trim(), is(""));
}
private ByteArrayOutputStream redirectStdout() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
return output;
}
}