|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.fesod.sheet.converter; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | +import java.io.File; |
| 24 | +import org.apache.fesod.sheet.ExcelWriter; |
| 25 | +import org.apache.fesod.sheet.FesodSheet; |
| 26 | +import org.apache.fesod.sheet.converters.Converter; |
| 27 | +import org.apache.fesod.sheet.enums.CellDataTypeEnum; |
| 28 | +import org.apache.fesod.sheet.metadata.GlobalConfiguration; |
| 29 | +import org.apache.fesod.sheet.metadata.data.WriteCellData; |
| 30 | +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; |
| 31 | +import org.apache.fesod.sheet.util.TestFileUtil; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +public class ConverterIsolationTest { |
| 35 | + |
| 36 | + public static class TestData {} |
| 37 | + |
| 38 | + public static class ConverterA implements Converter<String> { |
| 39 | + |
| 40 | + @Override |
| 41 | + public Class<String> supportJavaTypeKey() { |
| 42 | + return String.class; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public CellDataTypeEnum supportExcelTypeKey() { |
| 47 | + return CellDataTypeEnum.STRING; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty p, GlobalConfiguration g) { |
| 52 | + return new WriteCellData<>("A-" + value); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testConverterIsolation() { |
| 58 | + ExcelWriter writer1 = FesodSheet.write(new File(TestFileUtil.getPath() + "writer1.xlsx"), TestData.class) |
| 59 | + .registerConverter(new ConverterA()) |
| 60 | + .build(); |
| 61 | + |
| 62 | + ExcelWriter writer2 = FesodSheet.write(new File(TestFileUtil.getPath() + "writer2.xlsx"), TestData.class) |
| 63 | + .build(); |
| 64 | + |
| 65 | + boolean writer2HasConverterA = writer2.writeContext().currentWriteHolder().converterMap().values().stream() |
| 66 | + .anyMatch(c -> c instanceof ConverterA); |
| 67 | + |
| 68 | + writer1.finish(); |
| 69 | + writer2.finish(); |
| 70 | + |
| 71 | + assertFalse(writer2HasConverterA, "Custom converter should not leak between ExcelWriter instances"); |
| 72 | + } |
| 73 | +} |
0 commit comments