@@ -64,7 +64,7 @@ public void testFormatAosp() throws Exception {
6464
6565 Path tmpdir = testFolder .newFolder ().toPath ();
6666 Path path = tmpdir .resolve ("A.java" );
67- Files .write (path , input . getBytes ( UTF_8 ) );
67+ Files .writeString (path , input );
6868
6969 StringWriter out = new StringWriter ();
7070 StringWriter err = new StringWriter ();
@@ -116,7 +116,7 @@ public void testFormatLengthUpToEOF() throws Exception {
116116
117117 Path tmpdir = testFolder .newFolder ().toPath ();
118118 Path path = tmpdir .resolve ("Foo.java" );
119- Files .write (path , input . getBytes ( UTF_8 ) );
119+ Files .writeString (path , input );
120120
121121 StringWriter out = new StringWriter ();
122122 StringWriter err = new StringWriter ();
@@ -133,7 +133,7 @@ public void testFormatLengthOutOfRange() throws Exception {
133133
134134 Path tmpdir = testFolder .newFolder ().toPath ();
135135 Path path = tmpdir .resolve ("Foo.java" );
136- Files .write (path , input . getBytes ( UTF_8 ) );
136+ Files .writeString (path , input );
137137
138138 StringWriter out = new StringWriter ();
139139 StringWriter err = new StringWriter ();
@@ -142,7 +142,25 @@ public void testFormatLengthOutOfRange() throws Exception {
142142 String [] args = {"--offset" , "0" , "--length" , "9999" , path .toString ()};
143143 assertThat (main .format (args )).isEqualTo (1 );
144144 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)" );
146164 }
147165
148166 @ Test
@@ -303,7 +321,7 @@ private void importOrdering(String sortArg, String outputResourceName)
303321 String inputResourceName = "com/google/googlejavaformat/java/testimports/A.input" ;
304322 String input = getResource (inputResourceName );
305323 String expectedOutput = getResource (outputResourceName );
306- Files .write (path , input . getBytes ( UTF_8 ) );
324+ Files .writeString (path , input );
307325
308326 StringWriter out = new StringWriter ();
309327 StringWriter err = new StringWriter ();
0 commit comments