Skip to content

Commit 6dbb0b8

Browse files
committed
Fix output file truncation bug
Remove incorrect code that stripped the last 2 bytes from the output file. The original comment said 'Strip double line break' but this was actually removing the final '>' character and newline from the closing tag. Changes: - Fixed Main.java to write complete file content - Added try-with-resources for FileOutputStream to fix resource leak - Added integration test with closing_tag_input.xml and closing_tag_expected.xml
1 parent ddc160e commit 6dbb0b8

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/main/java/com/bytehamster/androidxmlformatter/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public static void main(String[] args) throws Exception {
6969
ByteArrayOutputStream stream = new ByteArrayOutputStream();
7070
outputter.output(doc, stream);
7171
byte[] content = stream.toByteArray();
72-
new FileOutputStream(filename)
73-
.write(content, 0, content.length - 2); // Strip double line break
72+
try (FileOutputStream fos = new FileOutputStream(filename)) {
73+
fos.write(content, 0, content.length);
74+
}
7475
}
7576
}
7677
}

src/test/java/com/bytehamster/androidxmlformatter/IntegrationTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ void testNamespaceSort() throws Exception {
194194
);
195195
}
196196

197+
// === Closing Tag Preservation Test ===
198+
199+
@Test
200+
@DisplayName("Closing tag: verify closing tag is not truncated")
201+
void testClosingTagNotTruncated() throws Exception {
202+
assertFormattedOutputMatches(
203+
"closing_tag",
204+
4, // indention
205+
4, // attribute indention
206+
new String[] { "android" }, // namespace order
207+
new String[] { "id", "layout_width", "layout_height" }, // attribute order
208+
false, // attribute sort
209+
false // namespace sort
210+
);
211+
}
212+
197213
// === Combined Options Test ===
198214

199215
@Test
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<Button
8+
android:id="@+id/button"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content" />
11+
12+
</LinearLayout>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_height="match_parent"
4+
android:layout_width="match_parent">
5+
6+
<Button
7+
android:id="@+id/button"
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content" />
10+
11+
</LinearLayout>

0 commit comments

Comments
 (0)