Surface XSSF Header/Footer Attributes (60887)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814950 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Murphy 2017-11-11 14:18:24 +00:00
parent 2d5b6457c6
commit 70fdcdf848
19 changed files with 918 additions and 115 deletions

View File

@ -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);
}

View File

@ -23,45 +23,52 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
/**
* <p>
* 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.
*</p>
* 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.
* </p>
*
*/
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);
}
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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();
}
}
}

View File

@ -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);
}

View File

@ -4514,5 +4514,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
}
public XSSFHeaderFooterProperties getHeaderFooterProperties() {
return new XSSFHeaderFooterProperties(getSheetTypeHeaderFooter());
}
}

View File

@ -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();
}

View File

@ -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,

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -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());
}
}