-
Notifications
You must be signed in to change notification settings - Fork 487
fix: handle escaped closing brace in template placeholder parsing #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roeniss
wants to merge
3
commits into
apache:main
Choose a base branch
from
roeniss:fix/462
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
...t-examples/src/test/java/org/apache/fesod/sheet/temp/issue462/EscapedPlaceholderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fesod.sheet.temp.issue462; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.fesod.sheet.FesodSheet; | ||
| import org.apache.fesod.sheet.util.TestFileUtil; | ||
| import org.apache.poi.ss.usermodel.Cell; | ||
| import org.apache.poi.ss.usermodel.Row; | ||
| import org.apache.poi.ss.usermodel.Sheet; | ||
| import org.apache.poi.ss.usermodel.Workbook; | ||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Test case for issue #462: ArrayIndexOutOfBoundsException when placeholder content is '{foo\}' | ||
| * | ||
| * When a placeholder has an escaped closing brace like {foo\}, the parser should handle it | ||
| * gracefully without throwing an exception. | ||
| */ | ||
| public class EscapedPlaceholderTest { | ||
|
|
||
| private static final String TEMPLATE_PATH = TestFileUtil.getPath() + "temp/issue462" + File.separator; | ||
|
|
||
| @BeforeAll | ||
| public static void createTemplates() throws IOException { | ||
| // Create template with escaped placeholder {foo\} | ||
| createTemplate("escaped_suffix.xlsx", "{foo\\}"); | ||
|
|
||
| // Create template with escaped prefix \{foo} | ||
| createTemplate("escaped_prefix.xlsx", "\\{foo}"); | ||
|
|
||
| // Create template with normal placeholder for comparison | ||
| createTemplate("normal.xlsx", "{foo}"); | ||
|
|
||
| // Create template with mixed content | ||
| createTemplate("mixed.xlsx", "prefix {foo\\} suffix"); | ||
| } | ||
|
|
||
| private static void createTemplate(String fileName, String content) throws IOException { | ||
| File templateFile = new File(TEMPLATE_PATH + fileName); | ||
| try (Workbook workbook = new XSSFWorkbook(); | ||
| FileOutputStream fos = new FileOutputStream(templateFile)) { | ||
| Sheet sheet = workbook.createSheet("Sheet1"); | ||
| Row row = sheet.createRow(0); | ||
| Cell cell = row.createCell(0); | ||
| cell.setCellValue(content); | ||
| workbook.write(fos); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test that template with {foo\} (escaped closing brace) does not throw exception. | ||
| * The escaped brace means there's no valid closing brace, so this should be treated as literal text. | ||
| */ | ||
| @Test | ||
| public void testEscapedSuffixPlaceholder() { | ||
| String templateFileName = TEMPLATE_PATH + "escaped_suffix.xlsx"; | ||
| String outputFileName = TEMPLATE_PATH + "output_escaped_suffix.xlsx"; | ||
|
|
||
| Map<String, Object> data = new HashMap<>(); | ||
| data.put("foo", "replaced_value"); | ||
|
|
||
| // This should not throw an exception | ||
| Assertions.assertDoesNotThrow(() -> { | ||
| FesodSheet.write(new File(outputFileName)) | ||
| .withTemplate(new File(templateFileName)) | ||
| .sheet() | ||
| .doFill(data); | ||
| }); | ||
|
|
||
| // Verify output file exists and can be read | ||
| List<Object> result = FesodSheet.read(new File(outputFileName)) | ||
| .sheet() | ||
| .headRowNumber(0) | ||
| .doReadSync(); | ||
| Assertions.assertFalse(result.isEmpty()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that template with \{foo} (escaped opening brace) is treated as literal text. | ||
| */ | ||
| @Test | ||
| public void testEscapedPrefixPlaceholder() { | ||
| String templateFileName = TEMPLATE_PATH + "escaped_prefix.xlsx"; | ||
| String outputFileName = TEMPLATE_PATH + "output_escaped_prefix.xlsx"; | ||
|
|
||
| Map<String, Object> data = new HashMap<>(); | ||
| data.put("foo", "replaced_value"); | ||
|
|
||
| // This should not throw an exception | ||
| Assertions.assertDoesNotThrow(() -> { | ||
| FesodSheet.write(new File(outputFileName)) | ||
| .withTemplate(new File(templateFileName)) | ||
| .sheet() | ||
| .doFill(data); | ||
| }); | ||
|
|
||
| // Verify the output | ||
| List<Object> result = FesodSheet.read(new File(outputFileName)) | ||
| .sheet() | ||
| .headRowNumber(0) | ||
| .doReadSync(); | ||
| Assertions.assertFalse(result.isEmpty()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that normal placeholder {foo} still works correctly. | ||
| */ | ||
| @Test | ||
| public void testNormalPlaceholder() { | ||
| String templateFileName = TEMPLATE_PATH + "normal.xlsx"; | ||
| String outputFileName = TEMPLATE_PATH + "output_normal.xlsx"; | ||
|
|
||
| Map<String, Object> data = new HashMap<>(); | ||
| data.put("foo", "replaced_value"); | ||
|
|
||
| FesodSheet.write(new File(outputFileName)) | ||
| .withTemplate(new File(templateFileName)) | ||
| .sheet() | ||
| .doFill(data); | ||
|
|
||
| // Verify the placeholder was replaced | ||
| List<Object> result = FesodSheet.read(new File(outputFileName)) | ||
| .sheet() | ||
| .headRowNumber(0) | ||
| .doReadSync(); | ||
| Assertions.assertFalse(result.isEmpty()); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Map<String, String> row = (Map<String, String>) result.get(0); | ||
| Assertions.assertEquals("replaced_value", row.get(0)); | ||
| } | ||
|
|
||
| /** | ||
| * Test mixed content with escaped placeholder in the middle. | ||
| */ | ||
| @Test | ||
| public void testMixedContentWithEscapedPlaceholder() { | ||
| String templateFileName = TEMPLATE_PATH + "mixed.xlsx"; | ||
| String outputFileName = TEMPLATE_PATH + "output_mixed.xlsx"; | ||
|
|
||
| Map<String, Object> data = new HashMap<>(); | ||
| data.put("foo", "replaced_value"); | ||
|
|
||
| // This should not throw an exception | ||
| Assertions.assertDoesNotThrow(() -> { | ||
| FesodSheet.write(new File(outputFileName)) | ||
| .withTemplate(new File(templateFileName)) | ||
| .sheet() | ||
| .doFill(data); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createTemplatewrites toTEMPLATE_PATH + fileName, but it never ensures thattemp/issue462/exists under the test classpath root. Sincesrc/test/resources/temp/exists buttemp/issue462/does not,new FileOutputStream(templateFile)will throwFileNotFoundExceptionon a clean checkout. Create the parent directories before opening the stream (or reuseTestFileUtil.createNewFile(...)which already creates parent dirs).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@psxjoy I think this feedback doesn't make sense, what do you think?