@@ -64,7 +64,7 @@ public void testFormatAosp() throws Exception {
64
64
65
65
Path tmpdir = testFolder .newFolder ().toPath ();
66
66
Path path = tmpdir .resolve ("A.java" );
67
- Files .write (path , input . getBytes ( UTF_8 ) );
67
+ Files .writeString (path , input );
68
68
69
69
StringWriter out = new StringWriter ();
70
70
StringWriter err = new StringWriter ();
@@ -116,7 +116,7 @@ public void testFormatLengthUpToEOF() throws Exception {
116
116
117
117
Path tmpdir = testFolder .newFolder ().toPath ();
118
118
Path path = tmpdir .resolve ("Foo.java" );
119
- Files .write (path , input . getBytes ( UTF_8 ) );
119
+ Files .writeString (path , input );
120
120
121
121
StringWriter out = new StringWriter ();
122
122
StringWriter err = new StringWriter ();
@@ -133,7 +133,7 @@ public void testFormatLengthOutOfRange() throws Exception {
133
133
134
134
Path tmpdir = testFolder .newFolder ().toPath ();
135
135
Path path = tmpdir .resolve ("Foo.java" );
136
- Files .write (path , input . getBytes ( UTF_8 ) );
136
+ Files .writeString (path , input );
137
137
138
138
StringWriter out = new StringWriter ();
139
139
StringWriter err = new StringWriter ();
@@ -142,7 +142,25 @@ public void testFormatLengthOutOfRange() throws Exception {
142
142
String [] args = {"--offset" , "0" , "--length" , "9999" , path .toString ()};
143
143
assertThat (main .format (args )).isEqualTo (1 );
144
144
assertThat (err .toString ())
145
- .contains ("error: invalid length 9999, offset + length (9999) is outside the file" );
145
+ .contains ("error: invalid offset (0) or length (9999); offset + length (9999)" );
146
+ }
147
+
148
+ @ Test
149
+ public void testFormatOffsetOutOfRange () throws Exception {
150
+ String input = "class Foo{}\n " ;
151
+
152
+ Path tmpdir = testFolder .newFolder ().toPath ();
153
+ Path path = tmpdir .resolve ("Foo.java" );
154
+ Files .writeString (path , input );
155
+
156
+ StringWriter out = new StringWriter ();
157
+ StringWriter err = new StringWriter ();
158
+
159
+ Main main = new Main (new PrintWriter (out , true ), new PrintWriter (err , true ), System .in );
160
+ String [] args = {"--offset" , "9998" , "--length" , "1" , path .toString ()};
161
+ assertThat (main .format (args )).isEqualTo (1 );
162
+ assertThat (err .toString ())
163
+ .contains ("error: invalid offset (9998) or length (1); offset + length (9999)" );
146
164
}
147
165
148
166
@ Test
@@ -303,7 +321,7 @@ private void importOrdering(String sortArg, String outputResourceName)
303
321
String inputResourceName = "com/google/googlejavaformat/java/testimports/A.input" ;
304
322
String input = getResource (inputResourceName );
305
323
String expectedOutput = getResource (outputResourceName );
306
- Files .write (path , input . getBytes ( UTF_8 ) );
324
+ Files .writeString (path , input );
307
325
308
326
StringWriter out = new StringWriter ();
309
327
StringWriter err = new StringWriter ();
0 commit comments