Repository navigation
fix: handle escaped closing brace in template placeholder parsing (#462) #1218
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
Mikkey-f
wants to merge
5
commits into
apache:main
Choose a base branch
from
Mikkey-f:fix/462-escaped-placeholder
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4d2154
fix: handle escaped closing brace in template placeholder parsing
roeniss 4661e87
test: add regression tests for escaped placeholder parsing (#462)
Mikkey-f 7f1a46d
Merge branch 'main' into fix/462-escaped-placeholder
Mikkey-f e894d9b
test: cover escaped placeholder before a normal placeholder (#462)
Mikkey-f bbe472d
fix: resume parsing at the next placeholder after an escaped brace (#…
Mikkey-f 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
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
105 changes: 105 additions & 0 deletions
105
fesod-sheet/src/test/java/org/apache/fesod/sheet/fill/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,105 @@ | ||||||||
| /* | ||||||||
| * 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.fill; | ||||||||
|
|
||||||||
| 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.testkit.Tags; | ||||||||
| import org.apache.fesod.sheet.testkit.base.AbstractExcelTest; | ||||||||
| import org.apache.fesod.sheet.testkit.enums.ExcelFormat; | ||||||||
| 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.Tag; | ||||||||
| import org.junit.jupiter.api.Test; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Regression tests for issue #462: a template placeholder whose closing brace is escaped (for | ||||||||
| * example {@code {foo\}}) has no valid closing brace to parse, so the text must be kept as-is | ||||||||
| * instead of breaking the fill. | ||||||||
| */ | ||||||||
| @Tag(Tags.ROUND_TRIP) | ||||||||
| public class EscapedPlaceholderTest extends AbstractExcelTest { | ||||||||
|
|
||||||||
| private static final String TEMPLATE_SHEET_NAME = "Sheet1"; | ||||||||
| private static final String VARIABLE = "foo"; | ||||||||
| private static final String VALUE = "replaced_value"; | ||||||||
|
|
||||||||
| @Test | ||||||||
| void escapedClosingBraceIsKeptAsLiteralText() throws IOException { | ||||||||
| // {foo\} used to blow up with StringIndexOutOfBoundsException while parsing the suffix | ||||||||
| Assertions.assertEquals("{foo\\}", fillTemplateWith("{foo\\}")); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void escapedOpeningBraceIsKeptAsLiteralText() throws IOException { | ||||||||
| Assertions.assertEquals("\\{foo}", fillTemplateWith("\\{foo}")); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void escapedPlaceholderInsideTextIsKeptAsLiteralText() throws IOException { | ||||||||
| Assertions.assertEquals("prefix {foo\\} suffix", fillTemplateWith("prefix {foo\\} suffix")); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void normalPlaceholderIsStillReplaced() throws IOException { | ||||||||
| Assertions.assertEquals(VALUE, fillTemplateWith("{foo}")); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void escapedPlaceholderBeforeNormalPlaceholderIsHandledIndependently() throws IOException { | ||||||||
| Assertions.assertEquals("{foo\\} second_value", fillTemplateWith("{foo\\} {bar}")); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Fills a single-cell template holding {@code cellText} and returns the filled value read back | ||||||||
| * from the output file. | ||||||||
| */ | ||||||||
| private String fillTemplateWith(String cellText) throws IOException { | ||||||||
| File template = createTemplate(cellText); | ||||||||
| File output = createTempFile("escaped-placeholder-out", ExcelFormat.XLSX); | ||||||||
| Map<String, Object> data = new HashMap<>(); | ||||||||
| data.put(VARIABLE, VALUE); | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This supplies the normal placeholder used by the regression case.
Suggested change
|
||||||||
| data.put("bar", "second_value"); | ||||||||
| FesodSheet.write(output).withTemplate(template).sheet().doFill(data); | ||||||||
| List<Map<Integer, String>> rows = | ||||||||
| FesodSheet.read(output).sheet().headRowNumber(0).doReadSync(); | ||||||||
| return rows.get(0).get(0); | ||||||||
| } | ||||||||
|
|
||||||||
| private File createTemplate(String cellText) throws IOException { | ||||||||
| File template = createTempFile("escaped-placeholder", ExcelFormat.XLSX); | ||||||||
| try (Workbook workbook = new XSSFWorkbook(); | ||||||||
| FileOutputStream fos = new FileOutputStream(template)) { | ||||||||
| Sheet sheet = workbook.createSheet(TEMPLATE_SHEET_NAME); | ||||||||
| Row row = sheet.createRow(0); | ||||||||
| row.createCell(0).setCellValue(cellText); | ||||||||
| workbook.write(fos); | ||||||||
| } | ||||||||
| return template; | ||||||||
| } | ||||||||
| } | ||||||||
Oops, something went wrong.
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.
Please add the mixed case that exposes the remaining parser error.