diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java index 9dd28daf2..0b2dad42c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java @@ -36,7 +36,7 @@ public class XSSFEvenFooter extends XSSFHeaderFooter implements Footer{ * @see XSSFSheet#getEvenFooter() * @param headerFooter */ - public XSSFEvenFooter(CTHeaderFooter headerFooter) { + protected XSSFEvenFooter(CTHeaderFooter headerFooter) { super(headerFooter); headerFooter.setDifferentOddEven(true); } @@ -57,6 +57,9 @@ public class XSSFEvenFooter extends XSSFHeaderFooter implements Footer{ public void setText(String text) { if(text == null) { getHeaderFooter().unsetEvenFooter(); + if (!getHeaderFooter().isSetEvenHeader()) { + getHeaderFooter().unsetDifferentOddEven(); + } } else { getHeaderFooter().setEvenFooter(text); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java index 1bf9555fc..c6b7444df 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java @@ -23,45 +23,52 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; /** *

- * Even page header value. Corresponds to even printed pages. - * Even page(s) in the sheet may not be printed, for example, if the print area is specified to be - * a range such that it falls outside an even page's scope. - * If no even header is specified, then odd header value is assumed for even page headers. - *

+ * Even page header value. Corresponds to even printed pages. Even page(s) in + * the sheet may not be printed, for example, if the print area is specified to + * be a range such that it falls outside an even page's scope. If no even header + * is specified, then odd header value is assumed for even page headers. + *

* */ -public class XSSFEvenHeader extends XSSFHeaderFooter implements Header{ +public class XSSFEvenHeader extends XSSFHeaderFooter implements Header { /** * Create an instance of XSSFEvenHeader from the supplied XML bean + * * @see XSSFSheet#getEvenHeader() * @param headerFooter */ - public XSSFEvenHeader(CTHeaderFooter headerFooter) { - super(headerFooter); - headerFooter.setDifferentOddEven(true); + protected XSSFEvenHeader(CTHeaderFooter headerFooter) { + super(headerFooter); + headerFooter.setDifferentOddEven(true); } /** * Get the content text representing this header + * * @return text */ public String getText() { - return getHeaderFooter().getEvenHeader(); + return getHeaderFooter().getEvenHeader(); } /** * Set a text for the header. If null unset the value - * @see XSSFHeaderFooter to see how to create a string with Header/Footer Formatting Syntax - * @param text - a string representing the header. + * + * @see XSSFHeaderFooter to see how to create a string with Header/Footer + * Formatting Syntax + * @param text + * - a string representing the header. */ public void setText(String text) { - if(text == null) { - getHeaderFooter().unsetEvenHeader(); - } else { - getHeaderFooter().setEvenHeader(text); - } + if (text == null) { + getHeaderFooter().unsetEvenHeader(); + if (!getHeaderFooter().isSetEvenFooter()) { + getHeaderFooter().unsetDifferentOddEven(); + } + } else { + getHeaderFooter().setEvenHeader(text); + } } - } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java index b0f2cb517..711292397 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java @@ -56,6 +56,9 @@ public class XSSFFirstFooter extends XSSFHeaderFooter implements Footer{ public void setText(String text) { if(text == null) { getHeaderFooter().unsetFirstFooter(); + if (!getHeaderFooter().isSetFirstHeader()) { + getHeaderFooter().unsetDifferentFirst(); + } } else { getHeaderFooter().setFirstFooter(text); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java index 9ffd45781..9c5d6e805 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java @@ -56,6 +56,9 @@ public class XSSFFirstHeader extends XSSFHeaderFooter implements Header{ public void setText(String text) { if(text == null) { getHeaderFooter().unsetFirstHeader(); + if (!getHeaderFooter().isSetFirstFooter()) { + getHeaderFooter().unsetDifferentFirst(); + } } else { getHeaderFooter().setFirstHeader(text); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHeaderFooterProperties.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHeaderFooterProperties.java new file mode 100644 index 000000000..eab6f125e --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHeaderFooterProperties.java @@ -0,0 +1,138 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import org.apache.poi.util.Internal; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; + +/** + */ +public class XSSFHeaderFooterProperties { + private CTHeaderFooter headerFooter; + + /** + * Create an instance of XSSFAbstractHeaderFooter from the supplied XML bean + * + * @param headerFooter + */ + public XSSFHeaderFooterProperties(CTHeaderFooter headerFooter) { + this.headerFooter = headerFooter; + } + + /** + * Returns the underlying CTHeaderFooter xml bean + * + * @return the underlying CTHeaderFooter xml bean + */ + @Internal + public CTHeaderFooter getHeaderFooter() { + return this.headerFooter; + } + + /** + * returns alignWithMargins attribute + */ + public boolean getAlignWithMargins() { + return getHeaderFooter().isSetAlignWithMargins() ? getHeaderFooter().getAlignWithMargins() : false; + } + + /** + * returns differentFirst attribute + */ + public boolean getDifferentFirst() { + return getHeaderFooter().isSetDifferentFirst() ? getHeaderFooter().getDifferentFirst() : false; + } + + /** + * returns differentOddEven attribute + */ + public boolean getDifferentOddEven() { + return getHeaderFooter().isSetDifferentOddEven() ? getHeaderFooter().getDifferentOddEven() : false; + } + + /** + * returns scaleWithDoc attribute + */ + public boolean getScaleWithDoc() { + return getHeaderFooter().isSetScaleWithDoc() ? getHeaderFooter().getScaleWithDoc() : false; + } + + /** + * set alignWithMargins attribute + */ + public void setAlignWithMargins(boolean flag) { + getHeaderFooter().setAlignWithMargins(flag); + } + + /** + * set differentFirst attribute + */ + public void setDifferentFirst(boolean flag) { + getHeaderFooter().setDifferentFirst(flag); + } + + /** + * set differentOddEven attribute + */ + public void setDifferentOddEven(boolean flag) { + getHeaderFooter().setDifferentOddEven(flag); + } + + /** + * set scaleWithDoc attribute + */ + public void setScaleWithDoc(boolean flag) { + getHeaderFooter().setScaleWithDoc(flag); + } + + /** + * remove alignWithMargins attribute + */ + public void removeAlignWithMargins() { + if (getHeaderFooter().isSetAlignWithMargins()) { + getHeaderFooter().unsetAlignWithMargins(); + } + } + + /** + * remove differentFirst attribute + */ + public void removeDifferentFirst() { + if (getHeaderFooter().isSetDifferentFirst()) { + getHeaderFooter().unsetDifferentFirst(); + } + } + + /** + * remove differentOddEven attribute + */ + public void removeDifferentOddEven() { + if (getHeaderFooter().isSetDifferentOddEven()) { + getHeaderFooter().unsetDifferentOddEven(); + } + } + + /** + * remove scaleWithDoc attribute + */ + public void removeScaleWithDoc() { + if (getHeaderFooter().isSetScaleWithDoc()) { + getHeaderFooter().unsetScaleWithDoc(); + } + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java index ca3950f81..6d422dd68 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java @@ -34,7 +34,7 @@ public class XSSFOddFooter extends XSSFHeaderFooter implements Footer{ * @see XSSFSheet#getOddFooter() * @param headerFooter */ - public XSSFOddFooter(CTHeaderFooter headerFooter) { + protected XSSFOddFooter(CTHeaderFooter headerFooter) { super(headerFooter); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index cef6ae3fb..a604f1844 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -4514,5 +4514,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } } - + public XSSFHeaderFooterProperties getHeaderFooterProperties() { + return new XSSFHeaderFooterProperties(getSheetTypeHeaderFooter()); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java index b9d0841d3..a21a4254b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java @@ -125,14 +125,14 @@ public abstract class XSSFHeaderFooter implements HeaderFooter { private CTHeaderFooter headerFooter; private boolean stripFields; - + /** - * Create an instance of XSSFHeaderFooter from the supplied XML bean + * Create an instance of XSSFAbstractHeaderFooter from the supplied XML bean * * @param headerFooter */ public XSSFHeaderFooter(CTHeaderFooter headerFooter) { - this.headerFooter = headerFooter; + this.headerFooter = headerFooter; this.helper = new HeaderFooterHelper(); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java index 6ecad0a74..7ee7546b3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java @@ -42,7 +42,7 @@ import org.junit.runners.Suite; TestXSSFDrawing.class, TestXSSFFont.class, TestXSSFFormulaEvaluation.class, - TestXSSFHeaderFooter.class, + //TestXSSFHeaderFooter.class, //converted to junit4 TestXSSFHyperlink.class, TestXSSFName.class, TestXSSFPicture.class, diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFEvenFooter.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFEvenFooter.java new file mode 100644 index 000000000..62e855242 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFEvenFooter.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFEvenFooter { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testSetGetText() { + XSSFEvenFooter footer = (XSSFEvenFooter) sheet.getEvenFooter(); + assertNotNull(footer); + assertNull(footer.getText()); + footer.setText("this is a test"); + assertEquals("this is a test", footer.getText()); + } +} \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFEvenHeader.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFEvenHeader.java new file mode 100644 index 000000000..e3b12bfe9 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFEvenHeader.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFEvenHeader { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testSetGetText() { + XSSFEvenHeader header = (XSSFEvenHeader) sheet.getEvenHeader(); + assertNotNull(header); + assertNull(header.getText()); + header.setText("this is a test"); + assertEquals("this is a test", header.getText()); + } +} \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFirstFooter.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFirstFooter.java new file mode 100644 index 000000000..e06faeca1 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFirstFooter.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFFirstFooter { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testSetGetText() { + XSSFFirstFooter footer = (XSSFFirstFooter) sheet.getFirstFooter(); + assertNotNull(footer); + assertNull(footer.getText()); + footer.setText("this is a test"); + assertEquals("this is a test", footer.getText()); + } +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFirstHeader.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFirstHeader.java new file mode 100644 index 000000000..ab9e57597 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFirstHeader.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFFirstHeader { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testSetGetText() { + XSSFFirstHeader header = (XSSFFirstHeader) sheet.getFirstHeader(); + assertNotNull(header); + assertNull(header.getText()); + header.setText("this is a test"); + assertEquals("this is a test", header.getText()); + } +} \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooter.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooter.java deleted file mode 100644 index 7c50d6aed..000000000 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooter.java +++ /dev/null @@ -1,90 +0,0 @@ -/* ==================================================================== - 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.poi.xssf.usermodel; - -import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; - -import junit.framework.TestCase; - -/** - * Tests for {@link XSSFHeaderFooter} - */ -public class TestXSSFHeaderFooter extends TestCase { - public void testStripFields() { - String simple = "I am a test header"; - String withPage = "I am a&P test header"; - String withLots = "I&A am&N a&P test&T header&U"; - String withFont = "I&22 am a&\"Arial,bold\" test header"; - String withOtherAnds = "I am a&P test header&&"; - String withOtherAnds2 = "I am a&P test header&a&b"; - - assertEquals(simple, XSSFOddHeader.stripFields(simple)); - assertEquals(simple, XSSFOddHeader.stripFields(withPage)); - assertEquals(simple, XSSFOddHeader.stripFields(withLots)); - assertEquals(simple, XSSFOddHeader.stripFields(withFont)); - assertEquals(simple + "&&", XSSFOddHeader.stripFields(withOtherAnds)); - assertEquals(simple + "&a&b", XSSFOddHeader.stripFields(withOtherAnds2)); - - // Now test the default strip flag - XSSFEvenHeader head = new XSSFEvenHeader(CTHeaderFooter.Factory.newInstance()); - head.setCenter("Center"); - head.setLeft("In the left"); - - assertEquals("In the left", head.getLeft()); - assertEquals("Center", head.getCenter()); - assertEquals("", head.getRight()); - - head.setLeft("Top &P&F&D Left"); - assertEquals("Top &P&F&D Left", head.getLeft()); - assertFalse(head.areFieldsStripped()); - - head.setAreFieldsStripped(true); - assertEquals("Top Left", head.getLeft()); - assertTrue(head.areFieldsStripped()); - - // Now even more complex - head.setCenter("HEADER TEXT &P&N&D&T&Z&F&F&A&V"); - assertEquals("HEADER TEXT &V", head.getCenter()); - } - - public void testGetSetCenterLeftRight() { - - XSSFOddFooter footer = new XSSFOddFooter(CTHeaderFooter.Factory.newInstance()); - assertEquals("", footer.getCenter()); - footer.setCenter("My first center section"); - assertEquals("My first center section", footer.getCenter()); - footer.setCenter("No, let's update the center section"); - assertEquals("No, let's update the center section", footer.getCenter()); - footer.setLeft("And add a left one"); - footer.setRight("Finally the right section is added"); - assertEquals("And add a left one", footer.getLeft()); - assertEquals("Finally the right section is added", footer.getRight()); - - // Test changing the three sections value - footer.setCenter("Second center version"); - footer.setLeft("Second left version"); - footer.setRight("Second right version"); - assertEquals("Second center version", footer.getCenter()); - assertEquals("Second left version", footer.getLeft()); - assertEquals("Second right version", footer.getRight()); - - } - - // TODO Rest of tests -} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooterProperties.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooterProperties.java new file mode 100644 index 000000000..92e7f7fcb --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooterProperties.java @@ -0,0 +1,109 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFHeaderFooterProperties { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + private XSSFHeaderFooterProperties hfProp; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + hfProp = sheet.getHeaderFooterProperties(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testGetAlignWithMargins() { + assertFalse(hfProp.getAlignWithMargins()); + hfProp.setAlignWithMargins(true); + assertTrue(hfProp.getAlignWithMargins()); + } + + @Test + public void testGetDifferentFirst() { + assertFalse(hfProp.getDifferentFirst()); + hfProp.setDifferentFirst(true); + assertTrue(hfProp.getDifferentFirst()); + hfProp.setDifferentFirst(false); + assertFalse(hfProp.getDifferentFirst()); + } + + @Test + public void testGetDifferentOddEven() { + assertFalse(hfProp.getDifferentOddEven()); + hfProp.setDifferentOddEven(true); + assertTrue(hfProp.getDifferentOddEven()); + hfProp.setDifferentOddEven(false); + assertFalse(hfProp.getDifferentOddEven()); + } + + @Test + public void testGetScaleWithDoc() { + assertFalse(hfProp.getScaleWithDoc()); + hfProp.setScaleWithDoc(true); + assertTrue(hfProp.getScaleWithDoc()); + } + + @Test + public void testRemoveAlignWithMargins() { + hfProp.setAlignWithMargins(true); + assertTrue(hfProp.getHeaderFooter().isSetAlignWithMargins()); + hfProp.removeAlignWithMargins(); + assertFalse(hfProp.getHeaderFooter().isSetAlignWithMargins()); + } + + @Test + public void testRemoveDifferentFirst() { + hfProp.setDifferentFirst(true); + assertTrue(hfProp.getHeaderFooter().isSetDifferentFirst()); + hfProp.removeDifferentFirst(); + assertFalse(hfProp.getHeaderFooter().isSetDifferentFirst()); + } + + @Test + public void testRemoveDifferentOddEven() { + hfProp.setDifferentOddEven(true); + assertTrue(hfProp.getHeaderFooter().isSetDifferentOddEven()); + hfProp.removeDifferentOddEven(); + assertFalse(hfProp.getHeaderFooter().isSetDifferentOddEven()); + } + + @Test + public void testRemoveScaleWithDoc() { + hfProp.setScaleWithDoc(true); + assertTrue(hfProp.getHeaderFooter().isSetScaleWithDoc()); + hfProp.removeScaleWithDoc(); + assertFalse(hfProp.getHeaderFooter().isSetScaleWithDoc()); + } + +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFOddFooter.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFOddFooter.java new file mode 100644 index 000000000..0a20a20e5 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFOddFooter.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFOddFooter { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testSetGetText() { + XSSFOddFooter footer = (XSSFOddFooter) sheet.getOddFooter(); + assertNotNull(footer); + assertNull(footer.getText()); + footer.setText("this is a test"); + assertEquals("this is a test", footer.getText()); + } +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFOddHeader.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFOddHeader.java new file mode 100644 index 000000000..4f53c2f24 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFOddHeader.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.poi.xssf.usermodel; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestXSSFOddHeader { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testSetGetText() { + XSSFOddHeader header = (XSSFOddHeader) sheet.getOddHeader(); + assertNotNull(header); + assertNull(header.getText()); + header.setText("this is a test"); + assertEquals("this is a test", header.getText()); + } +} \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 5a4045629..09adc3561 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -1990,4 +1990,15 @@ public final class TestXSSFSheet extends BaseTestXSheet { assertEquals("There should not be any comments left!", 0, sheet.getCellComments().size()); } + + @Test + public void testGetHeaderFooterProperties() throws IOException { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sh = wb.createSheet(); + + XSSFHeaderFooterProperties hfProp = sh.getHeaderFooterProperties(); + assertNotNull(hfProp); + + wb.close(); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java new file mode 100644 index 000000000..5a78f2bc1 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFHeaderFooter.java @@ -0,0 +1,317 @@ +/* ==================================================================== + 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.poi.xssf.usermodel.extensions; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.poi.xssf.usermodel.XSSFOddHeader; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; + +public class TestXSSFHeaderFooter { + + private XSSFWorkbook wb; + private XSSFSheet sheet; + private XSSFHeaderFooter hO; + private XSSFHeaderFooter hE; + private XSSFHeaderFooter hF; + private XSSFHeaderFooter fO; + private XSSFHeaderFooter fE; + private XSSFHeaderFooter fF; + + @Before + public void before() { + wb = new XSSFWorkbook(); + sheet = wb.createSheet(); + hO = (XSSFHeaderFooter) sheet.getOddHeader(); + hE = (XSSFHeaderFooter) sheet.getEvenHeader(); + hF = (XSSFHeaderFooter) sheet.getFirstHeader(); + fO = (XSSFHeaderFooter) sheet.getOddFooter(); + fE = (XSSFHeaderFooter) sheet.getEvenFooter(); + fF = (XSSFHeaderFooter) sheet.getFirstFooter(); + } + + @After + public void after() throws Exception { + wb.close(); + } + + @Test + public void testGetHeaderFooter() { + CTHeaderFooter ctHf; + ctHf = hO.getHeaderFooter(); + assertNotNull(ctHf); + ctHf = hE.getHeaderFooter(); + assertNotNull(ctHf); + ctHf = hF.getHeaderFooter(); + assertNotNull(ctHf); + ctHf = fO.getHeaderFooter(); + assertNotNull(ctHf); + ctHf = fE.getHeaderFooter(); + assertNotNull(ctHf); + ctHf = fF.getHeaderFooter(); + assertNotNull(ctHf); + } + + @Test + public void testGetValue() { + assertEquals("", hO.getValue()); + assertEquals("", hE.getValue()); + assertEquals("", hF.getValue()); + assertEquals("", fO.getValue()); + assertEquals("", fE.getValue()); + assertEquals("", fF.getValue()); + hO.setLeft("Left value"); + hO.setCenter("Center value"); + hO.setRight("Right value"); + hE.setLeft("LeftEvalue"); + hE.setCenter("CenterEvalue"); + hE.setRight("RightEvalue"); + hF.setLeft("LeftFvalue"); + hF.setCenter("CenterFvalue"); + hF.setRight("RightFvalue"); + assertEquals("&CCenter value&LLeft value&RRight value", hO.getValue()); + assertEquals("&CCenterEvalue&LLeftEvalue&RRightEvalue", hE.getValue()); + assertEquals("&CCenterFvalue&LLeftFvalue&RRightFvalue", hF.getValue()); + fO.setLeft("Left value1"); + fO.setCenter("Center value1"); + fO.setRight("Right value1"); + fE.setLeft("LeftEvalue1"); + fE.setCenter("CenterEvalue1"); + fE.setRight("RightEvalue1"); + fF.setLeft("LeftFvalue1"); + fF.setCenter("CenterFvalue1"); + fF.setRight("RightFvalue1"); + assertEquals("&CCenter value1&LLeft value1&RRight value1", fO.getValue()); + assertEquals("&CCenterEvalue1&LLeftEvalue1&RRightEvalue1", fE.getValue()); + assertEquals("&CCenterFvalue1&LLeftFvalue1&RRightFvalue1", fF.getValue()); + } + + @Ignore("Test not yet created") + public void testAreFieldsStripped() { + fail("Not yet implemented"); + } + + @Ignore("Test not yet created") + public void testSetAreFieldsStripped() { + fail("Not yet implemented"); + } + + @Test + public void testStripFields() { + String simple = "I am a test header"; + String withPage = "I am a&P test header"; + String withLots = "I&A am&N a&P test&T header&U"; + String withFont = "I&22 am a&\"Arial,bold\" test header"; + String withOtherAnds = "I am a&P test header&&"; + String withOtherAnds2 = "I am a&P test header&a&b"; + + assertEquals(simple, XSSFOddHeader.stripFields(simple)); + assertEquals(simple, XSSFOddHeader.stripFields(withPage)); + assertEquals(simple, XSSFOddHeader.stripFields(withLots)); + assertEquals(simple, XSSFOddHeader.stripFields(withFont)); + assertEquals(simple + "&&", XSSFOddHeader.stripFields(withOtherAnds)); + assertEquals(simple + "&a&b", XSSFOddHeader.stripFields(withOtherAnds2)); + + // Now test the default strip flag + hE.setCenter("Center"); + hE.setLeft("In the left"); + + assertEquals("In the left", hE.getLeft()); + assertEquals("Center", hE.getCenter()); + assertEquals("", hE.getRight()); + + hE.setLeft("Top &P&F&D Left"); + assertEquals("Top &P&F&D Left", hE.getLeft()); + assertFalse(hE.areFieldsStripped()); + + hE.setAreFieldsStripped(true); + assertEquals("Top Left", hE.getLeft()); + assertTrue(hE.areFieldsStripped()); + + // Now even more complex + hE.setCenter("HEADER TEXT &P&N&D&T&Z&F&F&A&V"); + assertEquals("HEADER TEXT &V", hE.getCenter()); + } + + @Test + public void testGetCenter() { + assertEquals("", hO.getCenter()); + assertEquals("", hE.getCenter()); + assertEquals("", hF.getCenter()); + assertEquals("", fO.getCenter()); + assertEquals("", fE.getCenter()); + assertEquals("", fF.getCenter()); + hO.setCenter("Center value"); + hE.setCenter("CenterEvalue"); + hF.setCenter("CenterFvalue"); + assertEquals("Center value", hO.getCenter()); + assertEquals("CenterEvalue", hE.getCenter()); + assertEquals("CenterFvalue", hF.getCenter()); + fO.setCenter("Center value1"); + fE.setCenter("CenterEvalue1"); + fF.setCenter("CenterFvalue1"); + assertEquals("Center value1", fO.getCenter()); + assertEquals("CenterEvalue1", fE.getCenter()); + assertEquals("CenterFvalue1", fF.getCenter()); + } + + @Test + public void testGetLeft() { + assertEquals("", hO.getLeft()); + assertEquals("", hE.getLeft()); + assertEquals("", hF.getLeft()); + assertEquals("", fO.getLeft()); + assertEquals("", fE.getLeft()); + assertEquals("", fF.getLeft()); + hO.setLeft("Left value"); + hE.setLeft("LeftEvalue"); + hF.setLeft("LeftFvalue"); + assertEquals("Left value", hO.getLeft()); + assertEquals("LeftEvalue", hE.getLeft()); + assertEquals("LeftFvalue", hF.getLeft()); + fO.setLeft("Left value1"); + fE.setLeft("LeftEvalue1"); + fF.setLeft("LeftFvalue1"); + assertEquals("Left value1", fO.getLeft()); + assertEquals("LeftEvalue1", fE.getLeft()); + assertEquals("LeftFvalue1", fF.getLeft()); + } + + @Test + public void testGetRight() { + assertEquals("", hO.getValue()); + assertEquals("", hE.getValue()); + assertEquals("", hF.getValue()); + assertEquals("", fO.getValue()); + assertEquals("", fE.getValue()); + assertEquals("", fF.getValue()); + hO.setRight("Right value"); + hE.setRight("RightEvalue"); + hF.setRight("RightFvalue"); + assertEquals("Right value", hO.getRight()); + assertEquals("RightEvalue", hE.getRight()); + assertEquals("RightFvalue", hF.getRight()); + fO.setRight("Right value1"); + fE.setRight("RightEvalue1"); + fF.setRight("RightFvalue1"); + assertEquals("Right value1", fO.getRight()); + assertEquals("RightEvalue1", fE.getRight()); + assertEquals("RightFvalue1", fF.getRight()); + } + + @Test + public void testSetCenter() { + assertEquals("", hO.getValue()); + assertEquals("", hE.getValue()); + assertEquals("", hF.getValue()); + assertEquals("", fO.getValue()); + assertEquals("", fE.getValue()); + assertEquals("", fF.getValue()); + hO.setCenter("Center value"); + hE.setCenter("CenterEvalue"); + hF.setCenter("CenterFvalue"); + assertEquals("&CCenter value", hO.getValue()); + assertEquals("&CCenterEvalue", hE.getValue()); + assertEquals("&CCenterFvalue", hF.getValue()); + fO.setCenter("Center value1"); + fE.setCenter("CenterEvalue1"); + fF.setCenter("CenterFvalue1"); + assertEquals("&CCenter value1", fO.getValue()); + assertEquals("&CCenterEvalue1", fE.getValue()); + assertEquals("&CCenterFvalue1", fF.getValue()); + } + + @Test + public void testSetLeft() { + assertEquals("", hO.getValue()); + assertEquals("", hE.getValue()); + assertEquals("", hF.getValue()); + assertEquals("", fO.getValue()); + assertEquals("", fE.getValue()); + assertEquals("", fF.getValue()); + hO.setLeft("Left value"); + hE.setLeft("LeftEvalue"); + hF.setLeft("LeftFvalue"); + assertEquals("&LLeft value", hO.getValue()); + assertEquals("&LLeftEvalue", hE.getValue()); + assertEquals("&LLeftFvalue", hF.getValue()); + fO.setLeft("Left value1"); + fE.setLeft("LeftEvalue1"); + fF.setLeft("LeftFvalue1"); + assertEquals("&LLeft value1", fO.getValue()); + assertEquals("&LLeftEvalue1", fE.getValue()); + assertEquals("&LLeftFvalue1", fF.getValue()); + } + + @Test + public void testSetRight() { + assertEquals("", hO.getValue()); + assertEquals("", hE.getValue()); + assertEquals("", hF.getValue()); + assertEquals("", fO.getValue()); + assertEquals("", fE.getValue()); + assertEquals("", fF.getValue()); + hO.setRight("Right value"); + hE.setRight("RightEvalue"); + hF.setRight("RightFvalue"); + assertEquals("&RRight value", hO.getValue()); + assertEquals("&RRightEvalue", hE.getValue()); + assertEquals("&RRightFvalue", hF.getValue()); + fO.setRight("Right value1"); + fE.setRight("RightEvalue1"); + fF.setRight("RightFvalue1"); + assertEquals("&RRight value1", fO.getValue()); + assertEquals("&RRightEvalue1", fE.getValue()); + assertEquals("&RRightFvalue1", fF.getValue()); + } + + + + @Test + public void testGetSetCenterLeftRight() { + + assertEquals("", fO.getCenter()); + fO.setCenter("My first center section"); + assertEquals("My first center section", fO.getCenter()); + fO.setCenter("No, let's update the center section"); + assertEquals("No, let's update the center section", fO.getCenter()); + fO.setLeft("And add a left one"); + fO.setRight("Finally the right section is added"); + assertEquals("And add a left one", fO.getLeft()); + assertEquals("Finally the right section is added", fO.getRight()); + + // Test changing the three sections value + fO.setCenter("Second center version"); + fO.setLeft("Second left version"); + fO.setRight("Second right version"); + assertEquals("Second center version", fO.getCenter()); + assertEquals("Second left version", fO.getLeft()); + assertEquals("Second right version", fO.getRight()); + + } +}