2008-03-13 07:50:50 -04:00
|
|
|
/* ====================================================================
|
|
|
|
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;
|
|
|
|
|
2008-09-29 00:28:55 -04:00
|
|
|
import junit.framework.AssertionFailedError;
|
2008-03-13 07:50:50 -04:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
2008-09-24 13:53:29 -04:00
|
|
|
import org.apache.poi.ss.usermodel.CellStyle;
|
2008-03-16 20:10:17 -04:00
|
|
|
import org.apache.poi.xssf.model.StylesTable;
|
|
|
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
|
2008-03-25 09:19:27 -04:00
|
|
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
|
2008-04-06 09:05:13 -04:00
|
|
|
import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
|
|
|
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
|
2008-10-05 09:56:28 -04:00
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
2008-09-15 06:39:37 -04:00
|
|
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
|
2008-03-13 07:50:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
public class TestXSSFCellStyle extends TestCase {
|
2008-09-15 06:39:37 -04:00
|
|
|
|
2008-09-29 00:28:55 -04:00
|
|
|
private StylesTable stylesTable;
|
|
|
|
private CTBorder ctBorderA;
|
|
|
|
private CTFill ctFill;
|
|
|
|
private CTFont ctFont;
|
|
|
|
private CTXf cellStyleXf;
|
|
|
|
private CTXf cellXf;
|
|
|
|
private CTCellXfs cellXfs;
|
|
|
|
private XSSFCellStyle cellStyle;
|
|
|
|
private CTStylesheet ctStylesheet;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() {
|
|
|
|
stylesTable = new StylesTable();
|
|
|
|
|
|
|
|
ctStylesheet = stylesTable._getRawStylesheet();
|
|
|
|
|
|
|
|
ctBorderA = CTBorder.Factory.newInstance();
|
|
|
|
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
|
|
|
|
long borderId = stylesTable.putBorder(borderA);
|
|
|
|
assertEquals(1, borderId);
|
|
|
|
|
|
|
|
XSSFCellBorder borderB = new XSSFCellBorder();
|
2008-10-05 09:56:28 -04:00
|
|
|
assertEquals(1, stylesTable.putBorder(borderB));
|
2008-09-29 00:28:55 -04:00
|
|
|
|
|
|
|
ctFill = CTFill.Factory.newInstance();
|
|
|
|
XSSFCellFill fill = new XSSFCellFill(ctFill);
|
|
|
|
long fillId = stylesTable.putFill(fill);
|
|
|
|
assertEquals(2, fillId);
|
|
|
|
|
|
|
|
ctFont = CTFont.Factory.newInstance();
|
|
|
|
XSSFFont font = new XSSFFont(ctFont);
|
|
|
|
long fontId = stylesTable.putFont(font);
|
|
|
|
assertEquals(1, fontId);
|
|
|
|
|
|
|
|
cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
|
|
|
|
cellStyleXf.setBorderId(1);
|
|
|
|
cellStyleXf.setFillId(1);
|
|
|
|
cellStyleXf.setFontId(1);
|
|
|
|
|
|
|
|
cellXfs = ctStylesheet.addNewCellXfs();
|
|
|
|
cellXf = cellXfs.addNewXf();
|
|
|
|
cellXf.setXfId(1);
|
|
|
|
cellXf.setBorderId(1);
|
|
|
|
cellXf.setFillId(1);
|
|
|
|
cellXf.setFontId(1);
|
|
|
|
stylesTable.putCellStyleXf(cellStyleXf);
|
|
|
|
stylesTable.putCellXf(cellXf);
|
|
|
|
cellStyle = new XSSFCellStyle(1, 1, stylesTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetBorderBottom() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//default values
|
|
|
|
assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderBottom());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderBottom());
|
|
|
|
//a new border has been added
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(STBorderStyle.MEDIUM, ctBorder.getBottom().getStyle());
|
|
|
|
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
//setting the same border multiple times should not change borderId
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderBottom());
|
|
|
|
}
|
|
|
|
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
|
|
|
|
|
|
|
|
//setting border to none removes the <bottom> element
|
|
|
|
cellStyle.setBorderBottom(CellStyle.BORDER_NONE);
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertFalse(ctBorder.isSetBottom());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetBorderRight() {
|
|
|
|
//default values
|
|
|
|
assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderRight());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderRight());
|
|
|
|
//a new border has been added
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(STBorderStyle.MEDIUM, ctBorder.getRight().getStyle());
|
|
|
|
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
//setting the same border multiple times should not change borderId
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderRight());
|
|
|
|
}
|
|
|
|
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
|
|
|
|
|
|
|
|
//setting border to none removes the <right> element
|
|
|
|
cellStyle.setBorderRight(CellStyle.BORDER_NONE);
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertFalse(ctBorder.isSetRight());
|
|
|
|
}
|
2008-09-29 00:28:55 -04:00
|
|
|
|
|
|
|
public void testGetSetBorderLeft() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//default values
|
|
|
|
assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderLeft());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderLeft());
|
|
|
|
//a new border has been added
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(STBorderStyle.MEDIUM, ctBorder.getLeft().getStyle());
|
|
|
|
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
//setting the same border multiple times should not change borderId
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderLeft());
|
|
|
|
}
|
|
|
|
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
|
|
|
|
|
|
|
|
//setting border to none removes the <left> element
|
|
|
|
cellStyle.setBorderLeft(CellStyle.BORDER_NONE);
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertFalse(ctBorder.isSetLeft());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetBorderTop() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//default values
|
|
|
|
assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderTop());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
|
|
|
|
//a new border has been added
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
|
|
|
|
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
//setting the same border multiple times should not change borderId
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
|
|
|
|
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
|
|
|
|
}
|
|
|
|
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
|
|
|
|
|
|
|
|
//setting border to none removes the <top> element
|
|
|
|
cellStyle.setBorderTop(CellStyle.BORDER_NONE);
|
|
|
|
assertEquals(num, stylesTable.getBorders().size());
|
|
|
|
borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertFalse(ctBorder.isSetTop());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetBottomBorderColor() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//defaults
|
|
|
|
assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getBottomBorderColor());
|
|
|
|
assertNull(cellStyle.getBottomBorderRgbColor());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
|
|
|
|
XSSFColor clr;
|
|
|
|
|
|
|
|
//setting indexed color
|
|
|
|
cellStyle.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getBottomBorderColor());
|
|
|
|
clr = cellStyle.getBottomBorderRgbColor();
|
|
|
|
assertTrue(clr.getCTColor().isSetIndexed());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
|
|
|
|
//a new border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getBottom().getColor().getIndexed());
|
|
|
|
|
|
|
|
//setting XSSFColor
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
clr = new XSSFColor(java.awt.Color.CYAN);
|
|
|
|
cellStyle.setBottomBorderColor(clr);
|
|
|
|
assertEquals(clr.getCTColor().toString(), cellStyle.getBottomBorderRgbColor().getCTColor().toString());
|
|
|
|
byte[] rgb = cellStyle.getBottomBorderRgbColor().getRgb();
|
|
|
|
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
|
|
|
//another border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//passing null unsets the color
|
|
|
|
cellStyle.setBottomBorderColor(null);
|
|
|
|
assertNull(cellStyle.getBottomBorderRgbColor());
|
|
|
|
}
|
2008-09-29 00:28:55 -04:00
|
|
|
|
|
|
|
public void testGetSetTopBorderColor() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//defaults
|
|
|
|
assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getTopBorderColor());
|
|
|
|
assertNull(cellStyle.getTopBorderRgbColor());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
|
|
|
|
XSSFColor clr;
|
|
|
|
|
|
|
|
//setting indexed color
|
|
|
|
cellStyle.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getTopBorderColor());
|
|
|
|
clr = cellStyle.getTopBorderRgbColor();
|
|
|
|
assertTrue(clr.getCTColor().isSetIndexed());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
|
|
|
|
//a new border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getTop().getColor().getIndexed());
|
|
|
|
|
|
|
|
//setting XSSFColor
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
clr = new XSSFColor(java.awt.Color.CYAN);
|
|
|
|
cellStyle.setTopBorderColor(clr);
|
|
|
|
assertEquals(clr.getCTColor().toString(), cellStyle.getTopBorderRgbColor().getCTColor().toString());
|
|
|
|
byte[] rgb = cellStyle.getTopBorderRgbColor().getRgb();
|
|
|
|
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
|
|
|
//another border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//passing null unsets the color
|
|
|
|
cellStyle.setTopBorderColor(null);
|
|
|
|
assertNull(cellStyle.getTopBorderRgbColor());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetLeftBorderColor() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//defaults
|
|
|
|
assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getLeftBorderColor());
|
|
|
|
assertNull(cellStyle.getLeftBorderRgbColor());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
|
|
|
|
XSSFColor clr;
|
|
|
|
|
|
|
|
//setting indexed color
|
|
|
|
cellStyle.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getLeftBorderColor());
|
|
|
|
clr = cellStyle.getLeftBorderRgbColor();
|
|
|
|
assertTrue(clr.getCTColor().isSetIndexed());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
|
|
|
|
//a new border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getLeft().getColor().getIndexed());
|
|
|
|
|
|
|
|
//setting XSSFColor
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
clr = new XSSFColor(java.awt.Color.CYAN);
|
|
|
|
cellStyle.setLeftBorderColor(clr);
|
|
|
|
assertEquals(clr.getCTColor().toString(), cellStyle.getLeftBorderRgbColor().getCTColor().toString());
|
|
|
|
byte[] rgb = cellStyle.getLeftBorderRgbColor().getRgb();
|
|
|
|
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
|
|
|
//another border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//passing null unsets the color
|
|
|
|
cellStyle.setLeftBorderColor(null);
|
|
|
|
assertNull(cellStyle.getLeftBorderRgbColor());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetRightBorderColor() {
|
2008-10-05 09:56:28 -04:00
|
|
|
//defaults
|
|
|
|
assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getRightBorderColor());
|
|
|
|
assertNull(cellStyle.getRightBorderRgbColor());
|
|
|
|
|
|
|
|
int num = stylesTable.getBorders().size();
|
|
|
|
|
|
|
|
XSSFColor clr;
|
|
|
|
|
|
|
|
//setting indexed color
|
|
|
|
cellStyle.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getRightBorderColor());
|
|
|
|
clr = cellStyle.getRightBorderRgbColor();
|
|
|
|
assertTrue(clr.getCTColor().isSetIndexed());
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
|
|
|
|
//a new border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//id of the created border
|
|
|
|
int borderId = (int)cellStyle.getCoreXf().getBorderId();
|
|
|
|
assertTrue(borderId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
|
|
|
|
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getRight().getColor().getIndexed());
|
|
|
|
|
|
|
|
//setting XSSFColor
|
|
|
|
num = stylesTable.getBorders().size();
|
|
|
|
clr = new XSSFColor(java.awt.Color.CYAN);
|
|
|
|
cellStyle.setRightBorderColor(clr);
|
|
|
|
assertEquals(clr.getCTColor().toString(), cellStyle.getRightBorderRgbColor().getCTColor().toString());
|
|
|
|
byte[] rgb = cellStyle.getRightBorderRgbColor().getRgb();
|
|
|
|
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
|
|
|
//another border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getBorders().size());
|
|
|
|
|
|
|
|
//passing null unsets the color
|
|
|
|
cellStyle.setRightBorderColor(null);
|
|
|
|
assertNull(cellStyle.getRightBorderRgbColor());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
public void testGetSetFillBackgroundColor() {
|
|
|
|
|
|
|
|
assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
|
|
|
|
assertNull(cellStyle.getFillBackgroundRgbColor());
|
|
|
|
|
|
|
|
XSSFColor clr;
|
|
|
|
|
|
|
|
int num = stylesTable.getFills().size();
|
|
|
|
|
|
|
|
//setting indexed color
|
|
|
|
cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
|
|
|
|
assertEquals(IndexedColors.RED.getIndex(), cellStyle.getFillBackgroundColor());
|
|
|
|
clr = cellStyle.getFillBackgroundRgbColor();
|
|
|
|
assertTrue(clr.getCTColor().isSetIndexed());
|
|
|
|
assertEquals(IndexedColors.RED.getIndex(), clr.getIndexed());
|
|
|
|
//a new fill was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getFills().size());
|
|
|
|
|
|
|
|
//id of the created border
|
|
|
|
int fillId = (int)cellStyle.getCoreXf().getFillId();
|
|
|
|
assertTrue(fillId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
|
|
|
|
assertEquals(IndexedColors.RED.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
|
|
|
|
|
|
|
|
//setting XSSFColor
|
|
|
|
num = stylesTable.getFills().size();
|
|
|
|
clr = new XSSFColor(java.awt.Color.CYAN);
|
|
|
|
cellStyle.setFillBackgroundColor(clr);
|
|
|
|
assertEquals(clr.getCTColor().toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
|
|
|
|
byte[] rgb = cellStyle.getFillBackgroundRgbColor().getRgb();
|
|
|
|
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
|
|
|
//another border was added to the styles table
|
|
|
|
assertEquals(num + 1, stylesTable.getFills().size());
|
|
|
|
|
|
|
|
//passing null unsets the color
|
|
|
|
cellStyle.setFillBackgroundColor(null);
|
|
|
|
assertNull(cellStyle.getFillBackgroundRgbColor());
|
|
|
|
assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
2008-09-24 13:53:29 -04:00
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
public void testDefaultStyles() {
|
|
|
|
|
|
|
|
XSSFWorkbook wb1 = new XSSFWorkbook();
|
|
|
|
|
|
|
|
XSSFCellStyle style1 = wb1.createCellStyle();
|
|
|
|
assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
|
|
|
|
assertNull(style1.getFillBackgroundRgbColor());
|
|
|
|
|
|
|
|
//compatibility with HSSF
|
|
|
|
HSSFWorkbook wb2 = new HSSFWorkbook();
|
|
|
|
HSSFCellStyle style2 = wb2.createCellStyle();
|
|
|
|
assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor());
|
|
|
|
assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor());
|
|
|
|
assertEquals(style2.getFillPattern(), style1.getFillPattern());
|
|
|
|
|
|
|
|
assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor());
|
|
|
|
assertEquals(style2.getTopBorderColor(), style1.getTopBorderColor());
|
|
|
|
assertEquals(style2.getRightBorderColor(), style1.getRightBorderColor());
|
|
|
|
assertEquals(style2.getBottomBorderColor(), style1.getBottomBorderColor());
|
|
|
|
|
|
|
|
assertEquals(style2.getBorderBottom(), style1.getBorderBottom());
|
|
|
|
assertEquals(style2.getBorderLeft(), style1.getBorderLeft());
|
|
|
|
assertEquals(style2.getBorderRight(), style1.getBorderRight());
|
|
|
|
assertEquals(style2.getBorderTop(), style1.getBorderTop());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void testGetFillForegroundColor() {
|
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
XSSFWorkbook wb = new XSSFWorkbook();
|
|
|
|
StylesTable styles = wb.getStylesSource();
|
|
|
|
assertEquals(1, wb.getNumCellStyles());
|
|
|
|
assertEquals(2, styles.getFills().size());
|
2008-09-29 00:28:55 -04:00
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
XSSFCellStyle defaultStyle = wb.getCellStyleAt((short)0);
|
|
|
|
assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor());
|
|
|
|
assertEquals(null, defaultStyle.getFillForegroundRgbColor());
|
|
|
|
assertEquals(CellStyle.NO_FILL, defaultStyle.getFillPattern());
|
2008-09-29 00:28:55 -04:00
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
XSSFCellStyle customStyle = wb.createCellStyle();
|
|
|
|
|
|
|
|
customStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
|
assertEquals(CellStyle.SOLID_FOREGROUND, customStyle.getFillPattern());
|
|
|
|
assertEquals(3, styles.getFills().size());
|
|
|
|
|
|
|
|
customStyle.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
|
|
|
|
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), customStyle.getFillForegroundColor());
|
|
|
|
assertEquals(4, styles.getFills().size());
|
2008-09-29 00:28:55 -04:00
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
XSSFCellStyle style = wb.createCellStyle();
|
2008-09-29 00:28:55 -04:00
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
|
assertEquals(CellStyle.SOLID_FOREGROUND, style.getFillPattern());
|
|
|
|
assertEquals(4, styles.getFills().size());
|
2008-09-29 00:28:55 -04:00
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
|
|
|
|
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
|
|
|
|
assertEquals(4, styles.getFills().size());
|
|
|
|
}
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetFillPattern() {
|
|
|
|
|
2008-10-05 09:56:28 -04:00
|
|
|
assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
|
|
|
|
|
|
|
|
int num = stylesTable.getFills().size();
|
|
|
|
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
|
assertEquals(CellStyle.SOLID_FOREGROUND, cellStyle.getFillPattern());
|
|
|
|
assertEquals(num + 1, stylesTable.getFills().size());
|
|
|
|
int fillId = (int)cellStyle.getCoreXf().getFillId();
|
|
|
|
assertTrue(fillId > 0);
|
|
|
|
//check changes in the underlying xml bean
|
|
|
|
CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
|
|
|
|
assertEquals(STPatternType.SOLID, ctFill.getPatternFill().getPatternType());
|
|
|
|
|
|
|
|
//setting the same fill multiple time does not update the styles table
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
|
}
|
|
|
|
assertEquals(num + 1, stylesTable.getFills().size());
|
|
|
|
|
|
|
|
cellStyle.setFillPattern(CellStyle.NO_FILL);
|
|
|
|
assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
|
|
|
|
fillId = (int)cellStyle.getCoreXf().getFillId();
|
|
|
|
ctFill = stylesTable.getFillAt(fillId).getCTFill();
|
|
|
|
assertFalse(ctFill.getPatternFill().isSetPatternType());
|
|
|
|
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetFont() {
|
|
|
|
assertNotNull(cellStyle.getFont());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetHidden() {
|
|
|
|
assertFalse(cellStyle.getHidden());
|
|
|
|
cellXf.getProtection().setHidden(true);
|
|
|
|
assertTrue(cellStyle.getHidden());
|
|
|
|
cellStyle.setHidden(false);
|
|
|
|
assertFalse(cellStyle.getHidden());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetLocked() {
|
|
|
|
assertFalse(cellStyle.getLocked());
|
|
|
|
cellXf.getProtection().setLocked(true);
|
|
|
|
assertTrue(cellStyle.getLocked());
|
|
|
|
cellStyle.setLocked(false);
|
|
|
|
assertFalse(cellStyle.getLocked());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetIndent() {
|
|
|
|
assertEquals((short)0, cellStyle.getIndention());
|
2008-10-21 13:56:34 -04:00
|
|
|
cellStyle.setIndention((short)3);
|
2008-09-29 00:28:55 -04:00
|
|
|
assertEquals((short)3, cellStyle.getIndention());
|
|
|
|
cellStyle.setIndention((short) 13);
|
2008-10-21 13:56:34 -04:00
|
|
|
assertEquals((short)13, cellStyle.getIndention());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetAlignement() {
|
|
|
|
assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
|
|
|
assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
|
|
|
|
|
|
|
|
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
|
|
|
|
assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
|
|
|
|
assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
|
|
|
|
assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
|
|
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
|
|
|
|
assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
|
|
|
|
assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
|
|
|
|
assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
|
|
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
|
|
|
|
assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
|
|
|
|
assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetVerticalAlignment() {
|
|
|
|
assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
|
|
|
|
assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
|
|
|
|
assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
|
|
|
|
|
|
|
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
|
|
|
|
assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
|
|
|
|
assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
|
|
|
|
assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
|
|
|
|
|
|
|
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
|
|
|
|
assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
|
|
|
|
assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
|
|
|
|
assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSetWrapText() {
|
|
|
|
assertFalse(cellStyle.getWrapText());
|
2008-10-21 13:56:34 -04:00
|
|
|
cellStyle.setWrapText(true);
|
2008-09-29 00:28:55 -04:00
|
|
|
assertTrue(cellStyle.getWrapText());
|
|
|
|
cellStyle.setWrapText(false);
|
2008-10-21 13:56:34 -04:00
|
|
|
assertFalse(cellStyle.getWrapText());
|
2008-09-29 00:28:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
|
|
|
|
*/
|
|
|
|
public void testCloneStyleSameWB() throws Exception {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
|
|
|
|
*/
|
|
|
|
public void testCloneStyleDiffWB() throws Exception {
|
|
|
|
// TODO
|
|
|
|
}
|
2008-03-13 07:50:50 -04:00
|
|
|
}
|