Tweak XSSFPrintSetup to make it a little clearer what happens, and add a round-trip unit test for it

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1076022 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-03-01 21:02:43 +00:00
parent bf9bc19ecc
commit 03b0eabcf8
2 changed files with 54 additions and 6 deletions

View File

@ -35,16 +35,23 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPageOrder;
* Page setup and page margins settings for the worksheet.
*/
public class XSSFPrintSetup implements PrintSetup {
private CTWorksheet ctWorksheet;
private CTPageSetup pageSetup;
private CTPageMargins pageMargins;
protected XSSFPrintSetup(CTWorksheet worksheet) {
this.ctWorksheet = worksheet;
this.pageSetup = ctWorksheet.getPageSetup() == null ? ctWorksheet.addNewPageSetup() : ctWorksheet.getPageSetup();
this.pageMargins = ctWorksheet.getPageMargins() == null ? ctWorksheet.addNewPageMargins() : ctWorksheet.getPageMargins();
if(ctWorksheet.isSetPageSetup()) {
this.pageSetup = ctWorksheet.getPageSetup();
} else {
this.pageSetup = ctWorksheet.addNewPageSetup();
}
if(ctWorksheet.isSetPageMargins()) {
this.pageMargins = ctWorksheet.getPageMargins();
} else {
this.pageMargins = ctWorksheet.addNewPageMargins();
}
}
/**

View File

@ -24,13 +24,12 @@ import org.apache.poi.ss.usermodel.PaperSize;
import org.apache.poi.ss.usermodel.PageOrder;
import org.apache.poi.ss.usermodel.PrintOrientation;
import org.apache.poi.ss.usermodel.PrintCellComments;
import org.apache.poi.xssf.XSSFITestDataProvider;
/**
* Tests for {@link XSSFPrintSetup}
*/
public class TestXSSFPrintSetup extends TestCase {
public void testSetGetPaperSize() {
CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
CTPageSetup pSetup = worksheet.addNewPageSetup();
@ -207,4 +206,46 @@ public class TestXSSFPrintSetup extends TestCase {
assertEquals(15, pSetup.getCopies());
}
public void testSetSaveRead() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet s1 = wb.createSheet();
assertEquals(false, s1.getCTWorksheet().isSetPageSetup());
assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
XSSFPrintSetup print = s1.getPrintSetup();
assertEquals(true, s1.getCTWorksheet().isSetPageSetup());
assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
print.setCopies((short)3);
print.setLandscape(true);
assertEquals(3, print.getCopies());
assertEquals(true, print.getLandscape());
XSSFSheet s2 = wb.createSheet();
assertEquals(false, s2.getCTWorksheet().isSetPageSetup());
assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
// Round trip and check
wb = XSSFITestDataProvider.instance.writeOutAndReadBack(wb);
s1 = wb.getSheetAt(0);
s2 = wb.getSheetAt(1);
assertEquals(true, s1.getCTWorksheet().isSetPageSetup());
assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
assertEquals(false, s2.getCTWorksheet().isSetPageSetup());
assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
print = s1.getPrintSetup();
assertEquals(3, print.getCopies());
assertEquals(true, print.getLandscape());
}
/**
* Open a file with print settings, save and check.
* Then, change, save, read, check
*/
public void testRoundTrip() {
// TODO
}
}