### Eclipse Workspace Patch 1.0 #P ApachePOI Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (revision 1726153) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (working copy) @@ -540,13 +540,30 @@ */ @Override public XSSFSheet cloneSheet(int sheetNum) { + return cloneSheet(sheetNum, null); + } + /** + * Create an XSSFSheet from an existing sheet in the XSSFWorkbook. + * The cloned sheet is a deep copy of the original but with a new given + * name. + * + * @return XSSFSheet representing the cloned sheet. + * @throws IllegalArgumentException if the sheet index or the sheet + * name is invalid + * @throws POIXMLException if there were errors when cloning + */ + public XSSFSheet cloneSheet(int sheetNum, String newName) { validateSheetIndex(sheetNum); - XSSFSheet srcSheet = sheets.get(sheetNum); - String srcName = srcSheet.getSheetName(); - String clonedName = getUniqueSheetName(srcName); - XSSFSheet clonedSheet = createSheet(clonedName); + if (newName == null) { + String srcName = srcSheet.getSheetName(); + newName = getUniqueSheetName(srcName); + } else { + validateSheetName(newName); + } + + XSSFSheet clonedSheet = createSheet(newName); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); srcSheet.write(out); @@ -763,8 +780,7 @@ throw new IllegalArgumentException("sheetName must not be null"); } - if (containsSheet( sheetname, sheets.size() )) - throw new IllegalArgumentException( "The workbook already contains a sheet of this name"); + validateSheetName(sheetname); // YK: Mimic Excel and silently truncate sheet names longer than 31 characters if(sheetname.length() > 31) sheetname = sheetname.substring(0, 31); @@ -804,6 +820,13 @@ return wrapper; } + private void validateSheetName( + final String sheetname) throws IllegalArgumentException { + if (containsSheet( sheetname, sheets.size() )) { + throw new IllegalArgumentException("The workbook already contains a sheet of this name"); + } + } + protected XSSFDialogsheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) { XSSFSheet sheet = createSheet(sheetname); return new XSSFDialogsheet(sheet); Index: src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java =================================================================== --- src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java (revision 1726153) +++ src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java (working copy) @@ -47,7 +47,8 @@ TestCellReference.class, TestCTColComparator.class, TestNumericRanges.class, - TestCellFormatPart.class + TestCellFormatPart.class, + TestXSSFCloneSheet.class }) public final class AllXSSFTests { } Index: src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java =================================================================== --- src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java (revision 0) +++ src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java (working copy) @@ -0,0 +1,65 @@ +package org.apache.poi.xssf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFCloneSheet { + + private static final String OTHER_SHEET_NAME = "Another"; + private static final String VALID_SHEET_NAME = "Sheet01"; + private XSSFWorkbook wb; + + @Before + public void setUp() { + wb = new XSSFWorkbook(); + wb.createSheet(VALID_SHEET_NAME); + } + + + @Test + public void testCloneSheetIntValid() { + wb.cloneSheet(0); + assertEquals(2, wb.getNumberOfSheets()); + try { + wb.cloneSheet(2); + fail("ShouldFail"); + } catch (IllegalArgumentException e) { + + } + } + + @Test + public void testCloneSheetIntInvalid() { + try { + wb.cloneSheet(1); + fail("Should Fail"); + } catch (IllegalArgumentException e) { + + } + assertEquals(1, wb.getNumberOfSheets()); + } + + @Test + public void testCloneSheetIntStringValidName() { + XSSFSheet cloned = wb.cloneSheet(0, OTHER_SHEET_NAME); + assertEquals(OTHER_SHEET_NAME, cloned.getSheetName()); + assertEquals(2, wb.getNumberOfSheets()); + } + + @Test + public void testCloneSheetIntStringInvalidName() { + try { + wb.cloneSheet(0, VALID_SHEET_NAME); + fail("Should fail"); + } catch (IllegalArgumentException e) { + + } + assertEquals(1, wb.getNumberOfSheets()); + } + +}