16
16
* limitations under the License.
17
17
*/
18
18
19
- import java .io .File ;
20
- import java .io .IOException ;
21
- import java .io .InputStream ;
22
- import java .io .OutputStream ;
23
- import java .io .Reader ;
24
- import java .io .StringWriter ;
25
- import java .io .Writer ;
19
+ import java .io .*;
26
20
import java .nio .file .Files ;
27
21
28
- import org .codehaus .plexus .util .IOUtil ;
29
- import org .codehaus .plexus .util .StringUtils ;
30
22
import org .junit .jupiter .api .Test ;
31
23
24
+ import static org .codehaus .plexus .util .xml .TestUtils .readAllFrom ;
32
25
import static org .junit .jupiter .api .Assertions .assertNotNull ;
33
26
import static org .junit .jupiter .api .Assertions .assertTrue ;
34
27
@@ -73,20 +66,14 @@ void prettyFormatInputStreamOutputStream() throws Exception {
73
66
File testDocument = new File (getBasedir (), "src/test/resources/testDocument.xhtml" );
74
67
assertTrue (testDocument .exists ());
75
68
76
- InputStream is = null ;
77
- OutputStream os = null ;
78
- try {
79
- is = Files .newInputStream (testDocument .toPath ());
80
- os = Files .newOutputStream (getTestOutputFile ("target/test/prettyFormatTestDocumentOutputStream.xml" )
81
- .toPath ());
82
-
69
+ try (InputStream is = Files .newInputStream (testDocument .toPath ());
70
+ OutputStream os =
71
+ Files .newOutputStream (getTestOutputFile ("target/test/prettyFormatTestDocumentOutputStream.xml" )
72
+ .toPath ())) {
83
73
assertNotNull (is );
84
74
assertNotNull (os );
85
75
86
76
XmlUtil .prettyFormat (is , os );
87
- } finally {
88
- IOUtil .close (is );
89
- IOUtil .close (os );
90
77
}
91
78
}
92
79
@@ -100,19 +87,14 @@ void prettyFormatReaderWriter() throws Exception {
100
87
File testDocument = new File (getBasedir (), "src/test/resources/testDocument.xhtml" );
101
88
assertTrue (testDocument .exists ());
102
89
103
- Reader reader = null ;
104
- Writer writer = null ;
105
- try {
106
- reader = ReaderFactory .newXmlReader (testDocument );
107
- writer = WriterFactory .newXmlWriter (getTestOutputFile ("target/test/prettyFormatTestDocumentWriter.xml" ));
90
+ try (Reader reader = ReaderFactory .newXmlReader (testDocument );
91
+ Writer writer = WriterFactory .newXmlWriter (
92
+ getTestOutputFile ("target/test/prettyFormatTestDocumentWriter.xml" ))) {
108
93
109
94
assertNotNull (reader );
110
95
assertNotNull (writer );
111
96
112
97
XmlUtil .prettyFormat (reader , writer );
113
- } finally {
114
- IOUtil .close (reader );
115
- IOUtil .close (writer );
116
98
}
117
99
}
118
100
@@ -126,25 +108,22 @@ void prettyFormatString() throws Exception {
126
108
File testDocument = new File (getBasedir (), "src/test/resources/testDocument.xhtml" );
127
109
assertTrue (testDocument .exists ());
128
110
129
- Reader reader = null ;
130
- Writer writer = null ;
131
111
String content ;
132
- try {
133
- reader = ReaderFactory . newXmlReader ( testDocument );
134
- content = IOUtil . toString ( reader );
112
+ try ( Reader reader = ReaderFactory . newXmlReader ( testDocument )) {
113
+ content = readAllFrom ( reader );
114
+ }
135
115
136
- reader = ReaderFactory .newXmlReader (testDocument );
137
- writer = new StringWriter ();
116
+ String contentPretty ;
117
+ try (Reader reader = ReaderFactory .newXmlReader (testDocument )) {
118
+ Writer writer = new StringWriter ();
138
119
XmlUtil .prettyFormat (reader , writer );
139
- } finally {
140
- IOUtil .close (reader );
141
- IOUtil .close (writer );
120
+ contentPretty = writer .toString ();
142
121
}
143
122
144
123
assertNotNull (content );
145
124
146
- int countEOL = StringUtils .countMatches (content , XmlUtil .DEFAULT_LINE_SEPARATOR );
147
- assertTrue (countEOL < StringUtils .countMatches (writer . toString () , XmlUtil .DEFAULT_LINE_SEPARATOR ));
125
+ int countEOL = TestUtils .countMatches (content , XmlUtil .DEFAULT_LINE_SEPARATOR );
126
+ assertTrue (countEOL < TestUtils .countMatches (contentPretty , XmlUtil .DEFAULT_LINE_SEPARATOR ));
148
127
}
149
128
150
129
/**
@@ -157,19 +136,14 @@ void prettyFormatReaderWriter2() throws Exception {
157
136
File testDocument = new File (getBasedir (), "src/test/resources/test.xdoc.xhtml" );
158
137
assertTrue (testDocument .exists ());
159
138
160
- Reader reader = null ;
161
- Writer writer = null ;
162
- try {
163
- reader = ReaderFactory .newXmlReader (testDocument );
164
- writer = WriterFactory .newXmlWriter (getTestOutputFile ("target/test/prettyFormatTestXdocWriter.xml" ));
139
+ try (Reader reader = ReaderFactory .newXmlReader (testDocument );
140
+ Writer writer =
141
+ WriterFactory .newXmlWriter (getTestOutputFile ("target/test/prettyFormatTestXdocWriter.xml" ))) {
165
142
166
143
assertNotNull (reader );
167
144
assertNotNull (writer );
168
145
169
146
XmlUtil .prettyFormat (reader , writer );
170
- } finally {
171
- IOUtil .close (reader );
172
- IOUtil .close (writer );
173
147
}
174
148
}
175
149
}
0 commit comments