You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFCellStyle.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.ss.usermodel.CellStyle;
  20. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  21. import org.apache.poi.ss.usermodel.IndexedColors;
  22. import org.apache.poi.ss.usermodel.VerticalAlignment;
  23. import org.apache.poi.xssf.XSSFTestDataSamples;
  24. import org.apache.poi.xssf.model.StylesTable;
  25. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
  26. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
  32. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
  33. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
  34. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
  35. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  36. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
  37. public class TestXSSFCellStyle extends TestCase {
  38. private StylesTable stylesTable;
  39. private CTBorder ctBorderA;
  40. private CTFill ctFill;
  41. private CTFont ctFont;
  42. private CTXf cellStyleXf;
  43. private CTXf cellXf;
  44. private CTCellXfs cellXfs;
  45. private XSSFCellStyle cellStyle;
  46. private CTStylesheet ctStylesheet;
  47. @Override
  48. protected void setUp() {
  49. stylesTable = new StylesTable();
  50. ctStylesheet = stylesTable.getCTStylesheet();
  51. ctBorderA = CTBorder.Factory.newInstance();
  52. XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
  53. long borderId = stylesTable.putBorder(borderA);
  54. assertEquals(1, borderId);
  55. XSSFCellBorder borderB = new XSSFCellBorder();
  56. assertEquals(1, stylesTable.putBorder(borderB));
  57. ctFill = CTFill.Factory.newInstance();
  58. XSSFCellFill fill = new XSSFCellFill(ctFill);
  59. long fillId = stylesTable.putFill(fill);
  60. assertEquals(2, fillId);
  61. ctFont = CTFont.Factory.newInstance();
  62. XSSFFont font = new XSSFFont(ctFont);
  63. long fontId = stylesTable.putFont(font);
  64. assertEquals(1, fontId);
  65. cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
  66. cellStyleXf.setBorderId(1);
  67. cellStyleXf.setFillId(1);
  68. cellStyleXf.setFontId(1);
  69. cellXfs = ctStylesheet.addNewCellXfs();
  70. cellXf = cellXfs.addNewXf();
  71. cellXf.setXfId(1);
  72. cellXf.setBorderId(1);
  73. cellXf.setFillId(1);
  74. cellXf.setFontId(1);
  75. stylesTable.putCellStyleXf(cellStyleXf);
  76. stylesTable.putCellXf(cellXf);
  77. cellStyle = new XSSFCellStyle(1, 1, stylesTable, null);
  78. }
  79. public void testGetSetBorderBottom() {
  80. //default values
  81. assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderBottom());
  82. int num = stylesTable.getBorders().size();
  83. cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
  84. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderBottom());
  85. //a new border has been added
  86. assertEquals(num + 1, stylesTable.getBorders().size());
  87. //id of the created border
  88. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  89. assertTrue(borderId > 0);
  90. //check changes in the underlying xml bean
  91. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  92. assertEquals(STBorderStyle.MEDIUM, ctBorder.getBottom().getStyle());
  93. num = stylesTable.getBorders().size();
  94. //setting the same border multiple times should not change borderId
  95. for (int i = 0; i < 3; i++) {
  96. cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
  97. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderBottom());
  98. }
  99. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  100. assertEquals(num, stylesTable.getBorders().size());
  101. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  102. //setting border to none removes the <bottom> element
  103. cellStyle.setBorderBottom(CellStyle.BORDER_NONE);
  104. assertEquals(num, stylesTable.getBorders().size());
  105. borderId = (int)cellStyle.getCoreXf().getBorderId();
  106. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  107. assertFalse(ctBorder.isSetBottom());
  108. }
  109. public void testGetSetBorderRight() {
  110. //default values
  111. assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderRight());
  112. int num = stylesTable.getBorders().size();
  113. cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
  114. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderRight());
  115. //a new border has been added
  116. assertEquals(num + 1, stylesTable.getBorders().size());
  117. //id of the created border
  118. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  119. assertTrue(borderId > 0);
  120. //check changes in the underlying xml bean
  121. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  122. assertEquals(STBorderStyle.MEDIUM, ctBorder.getRight().getStyle());
  123. num = stylesTable.getBorders().size();
  124. //setting the same border multiple times should not change borderId
  125. for (int i = 0; i < 3; i++) {
  126. cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
  127. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderRight());
  128. }
  129. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  130. assertEquals(num, stylesTable.getBorders().size());
  131. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  132. //setting border to none removes the <right> element
  133. cellStyle.setBorderRight(CellStyle.BORDER_NONE);
  134. assertEquals(num, stylesTable.getBorders().size());
  135. borderId = (int)cellStyle.getCoreXf().getBorderId();
  136. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  137. assertFalse(ctBorder.isSetRight());
  138. }
  139. public void testGetSetBorderLeft() {
  140. //default values
  141. assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderLeft());
  142. int num = stylesTable.getBorders().size();
  143. cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
  144. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderLeft());
  145. //a new border has been added
  146. assertEquals(num + 1, stylesTable.getBorders().size());
  147. //id of the created border
  148. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  149. assertTrue(borderId > 0);
  150. //check changes in the underlying xml bean
  151. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  152. assertEquals(STBorderStyle.MEDIUM, ctBorder.getLeft().getStyle());
  153. num = stylesTable.getBorders().size();
  154. //setting the same border multiple times should not change borderId
  155. for (int i = 0; i < 3; i++) {
  156. cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
  157. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderLeft());
  158. }
  159. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  160. assertEquals(num, stylesTable.getBorders().size());
  161. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  162. //setting border to none removes the <left> element
  163. cellStyle.setBorderLeft(CellStyle.BORDER_NONE);
  164. assertEquals(num, stylesTable.getBorders().size());
  165. borderId = (int)cellStyle.getCoreXf().getBorderId();
  166. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  167. assertFalse(ctBorder.isSetLeft());
  168. }
  169. public void testGetSetBorderTop() {
  170. //default values
  171. assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderTop());
  172. int num = stylesTable.getBorders().size();
  173. cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
  174. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
  175. //a new border has been added
  176. assertEquals(num + 1, stylesTable.getBorders().size());
  177. //id of the created border
  178. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  179. assertTrue(borderId > 0);
  180. //check changes in the underlying xml bean
  181. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  182. assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
  183. num = stylesTable.getBorders().size();
  184. //setting the same border multiple times should not change borderId
  185. for (int i = 0; i < 3; i++) {
  186. cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
  187. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
  188. }
  189. assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
  190. assertEquals(num, stylesTable.getBorders().size());
  191. assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
  192. //setting border to none removes the <top> element
  193. cellStyle.setBorderTop(CellStyle.BORDER_NONE);
  194. assertEquals(num, stylesTable.getBorders().size());
  195. borderId = (int)cellStyle.getCoreXf().getBorderId();
  196. ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  197. assertFalse(ctBorder.isSetTop());
  198. }
  199. public void testGetSetBottomBorderColor() {
  200. //defaults
  201. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getBottomBorderColor());
  202. assertNull(cellStyle.getBottomBorderXSSFColor());
  203. int num = stylesTable.getBorders().size();
  204. XSSFColor clr;
  205. //setting indexed color
  206. cellStyle.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
  207. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getBottomBorderColor());
  208. clr = cellStyle.getBottomBorderXSSFColor();
  209. assertTrue(clr.getCTColor().isSetIndexed());
  210. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  211. //a new border was added to the styles table
  212. assertEquals(num + 1, stylesTable.getBorders().size());
  213. //id of the created border
  214. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  215. assertTrue(borderId > 0);
  216. //check changes in the underlying xml bean
  217. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  218. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getBottom().getColor().getIndexed());
  219. //setting XSSFColor
  220. num = stylesTable.getBorders().size();
  221. clr = new XSSFColor(java.awt.Color.CYAN);
  222. cellStyle.setBottomBorderColor(clr);
  223. assertEquals(clr.getCTColor().toString(), cellStyle.getBottomBorderXSSFColor().getCTColor().toString());
  224. byte[] rgb = cellStyle.getBottomBorderXSSFColor().getRgb();
  225. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  226. //another border was added to the styles table
  227. assertEquals(num + 1, stylesTable.getBorders().size());
  228. //passing null unsets the color
  229. cellStyle.setBottomBorderColor(null);
  230. assertNull(cellStyle.getBottomBorderXSSFColor());
  231. }
  232. public void testGetSetTopBorderColor() {
  233. //defaults
  234. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getTopBorderColor());
  235. assertNull(cellStyle.getTopBorderXSSFColor());
  236. int num = stylesTable.getBorders().size();
  237. XSSFColor clr;
  238. //setting indexed color
  239. cellStyle.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
  240. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getTopBorderColor());
  241. clr = cellStyle.getTopBorderXSSFColor();
  242. assertTrue(clr.getCTColor().isSetIndexed());
  243. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  244. //a new border was added to the styles table
  245. assertEquals(num + 1, stylesTable.getBorders().size());
  246. //id of the created border
  247. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  248. assertTrue(borderId > 0);
  249. //check changes in the underlying xml bean
  250. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  251. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getTop().getColor().getIndexed());
  252. //setting XSSFColor
  253. num = stylesTable.getBorders().size();
  254. clr = new XSSFColor(java.awt.Color.CYAN);
  255. cellStyle.setTopBorderColor(clr);
  256. assertEquals(clr.getCTColor().toString(), cellStyle.getTopBorderXSSFColor().getCTColor().toString());
  257. byte[] rgb = cellStyle.getTopBorderXSSFColor().getRgb();
  258. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  259. //another border was added to the styles table
  260. assertEquals(num + 1, stylesTable.getBorders().size());
  261. //passing null unsets the color
  262. cellStyle.setTopBorderColor(null);
  263. assertNull(cellStyle.getTopBorderXSSFColor());
  264. }
  265. public void testGetSetLeftBorderColor() {
  266. //defaults
  267. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getLeftBorderColor());
  268. assertNull(cellStyle.getLeftBorderXSSFColor());
  269. int num = stylesTable.getBorders().size();
  270. XSSFColor clr;
  271. //setting indexed color
  272. cellStyle.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
  273. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getLeftBorderColor());
  274. clr = cellStyle.getLeftBorderXSSFColor();
  275. assertTrue(clr.getCTColor().isSetIndexed());
  276. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  277. //a new border was added to the styles table
  278. assertEquals(num + 1, stylesTable.getBorders().size());
  279. //id of the created border
  280. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  281. assertTrue(borderId > 0);
  282. //check changes in the underlying xml bean
  283. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  284. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getLeft().getColor().getIndexed());
  285. //setting XSSFColor
  286. num = stylesTable.getBorders().size();
  287. clr = new XSSFColor(java.awt.Color.CYAN);
  288. cellStyle.setLeftBorderColor(clr);
  289. assertEquals(clr.getCTColor().toString(), cellStyle.getLeftBorderXSSFColor().getCTColor().toString());
  290. byte[] rgb = cellStyle.getLeftBorderXSSFColor().getRgb();
  291. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  292. //another border was added to the styles table
  293. assertEquals(num + 1, stylesTable.getBorders().size());
  294. //passing null unsets the color
  295. cellStyle.setLeftBorderColor(null);
  296. assertNull(cellStyle.getLeftBorderXSSFColor());
  297. }
  298. public void testGetSetRightBorderColor() {
  299. //defaults
  300. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getRightBorderColor());
  301. assertNull(cellStyle.getRightBorderXSSFColor());
  302. int num = stylesTable.getBorders().size();
  303. XSSFColor clr;
  304. //setting indexed color
  305. cellStyle.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
  306. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getRightBorderColor());
  307. clr = cellStyle.getRightBorderXSSFColor();
  308. assertTrue(clr.getCTColor().isSetIndexed());
  309. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  310. //a new border was added to the styles table
  311. assertEquals(num + 1, stylesTable.getBorders().size());
  312. //id of the created border
  313. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  314. assertTrue(borderId > 0);
  315. //check changes in the underlying xml bean
  316. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  317. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getRight().getColor().getIndexed());
  318. //setting XSSFColor
  319. num = stylesTable.getBorders().size();
  320. clr = new XSSFColor(java.awt.Color.CYAN);
  321. cellStyle.setRightBorderColor(clr);
  322. assertEquals(clr.getCTColor().toString(), cellStyle.getRightBorderXSSFColor().getCTColor().toString());
  323. byte[] rgb = cellStyle.getRightBorderXSSFColor().getRgb();
  324. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  325. //another border was added to the styles table
  326. assertEquals(num + 1, stylesTable.getBorders().size());
  327. //passing null unsets the color
  328. cellStyle.setRightBorderColor(null);
  329. assertNull(cellStyle.getRightBorderXSSFColor());
  330. }
  331. public void testGetSetFillBackgroundColor() {
  332. assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
  333. assertNull(cellStyle.getFillBackgroundXSSFColor());
  334. XSSFColor clr;
  335. int num = stylesTable.getFills().size();
  336. //setting indexed color
  337. cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
  338. assertEquals(IndexedColors.RED.getIndex(), cellStyle.getFillBackgroundColor());
  339. clr = cellStyle.getFillBackgroundXSSFColor();
  340. assertTrue(clr.getCTColor().isSetIndexed());
  341. assertEquals(IndexedColors.RED.getIndex(), clr.getIndexed());
  342. //a new fill was added to the styles table
  343. assertEquals(num + 1, stylesTable.getFills().size());
  344. //id of the created border
  345. int fillId = (int)cellStyle.getCoreXf().getFillId();
  346. assertTrue(fillId > 0);
  347. //check changes in the underlying xml bean
  348. CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
  349. assertEquals(IndexedColors.RED.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
  350. //setting XSSFColor
  351. num = stylesTable.getFills().size();
  352. clr = new XSSFColor(java.awt.Color.CYAN);
  353. cellStyle.setFillBackgroundColor(clr);
  354. assertEquals(clr.getCTColor().toString(), cellStyle.getFillBackgroundXSSFColor().getCTColor().toString());
  355. byte[] rgb = cellStyle.getFillBackgroundXSSFColor().getRgb();
  356. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  357. //another border was added to the styles table
  358. assertEquals(num + 1, stylesTable.getFills().size());
  359. //passing null unsets the color
  360. cellStyle.setFillBackgroundColor(null);
  361. assertNull(cellStyle.getFillBackgroundXSSFColor());
  362. assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
  363. }
  364. public void testDefaultStyles() {
  365. XSSFWorkbook wb1 = new XSSFWorkbook();
  366. XSSFCellStyle style1 = wb1.createCellStyle();
  367. assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
  368. assertNull(style1.getFillBackgroundXSSFColor());
  369. //compatibility with HSSF
  370. HSSFWorkbook wb2 = new HSSFWorkbook();
  371. HSSFCellStyle style2 = wb2.createCellStyle();
  372. assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor());
  373. assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor());
  374. assertEquals(style2.getFillPattern(), style1.getFillPattern());
  375. assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor());
  376. assertEquals(style2.getTopBorderColor(), style1.getTopBorderColor());
  377. assertEquals(style2.getRightBorderColor(), style1.getRightBorderColor());
  378. assertEquals(style2.getBottomBorderColor(), style1.getBottomBorderColor());
  379. assertEquals(style2.getBorderBottom(), style1.getBorderBottom());
  380. assertEquals(style2.getBorderLeft(), style1.getBorderLeft());
  381. assertEquals(style2.getBorderRight(), style1.getBorderRight());
  382. assertEquals(style2.getBorderTop(), style1.getBorderTop());
  383. }
  384. public void testGetFillForegroundColor() {
  385. XSSFWorkbook wb = new XSSFWorkbook();
  386. StylesTable styles = wb.getStylesSource();
  387. assertEquals(1, wb.getNumCellStyles());
  388. assertEquals(2, styles.getFills().size());
  389. XSSFCellStyle defaultStyle = wb.getCellStyleAt((short)0);
  390. assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor());
  391. assertEquals(null, defaultStyle.getFillForegroundXSSFColor());
  392. assertEquals(CellStyle.NO_FILL, defaultStyle.getFillPattern());
  393. XSSFCellStyle customStyle = wb.createCellStyle();
  394. customStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  395. assertEquals(CellStyle.SOLID_FOREGROUND, customStyle.getFillPattern());
  396. assertEquals(3, styles.getFills().size());
  397. customStyle.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  398. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), customStyle.getFillForegroundColor());
  399. assertEquals(4, styles.getFills().size());
  400. for (int i = 0; i < 3; i++) {
  401. XSSFCellStyle style = wb.createCellStyle();
  402. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  403. assertEquals(CellStyle.SOLID_FOREGROUND, style.getFillPattern());
  404. assertEquals(4, styles.getFills().size());
  405. style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  406. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
  407. assertEquals(4, styles.getFills().size());
  408. }
  409. }
  410. public void testGetFillPattern() {
  411. assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
  412. int num = stylesTable.getFills().size();
  413. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  414. assertEquals(CellStyle.SOLID_FOREGROUND, cellStyle.getFillPattern());
  415. assertEquals(num + 1, stylesTable.getFills().size());
  416. int fillId = (int)cellStyle.getCoreXf().getFillId();
  417. assertTrue(fillId > 0);
  418. //check changes in the underlying xml bean
  419. CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
  420. assertEquals(STPatternType.SOLID, ctFill.getPatternFill().getPatternType());
  421. //setting the same fill multiple time does not update the styles table
  422. for (int i = 0; i < 3; i++) {
  423. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  424. }
  425. assertEquals(num + 1, stylesTable.getFills().size());
  426. cellStyle.setFillPattern(CellStyle.NO_FILL);
  427. assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
  428. fillId = (int)cellStyle.getCoreXf().getFillId();
  429. ctFill = stylesTable.getFillAt(fillId).getCTFill();
  430. assertFalse(ctFill.getPatternFill().isSetPatternType());
  431. }
  432. public void testGetFont() {
  433. assertNotNull(cellStyle.getFont());
  434. }
  435. public void testGetSetHidden() {
  436. assertFalse(cellStyle.getHidden());
  437. cellStyle.setHidden(true);
  438. assertTrue(cellStyle.getHidden());
  439. cellStyle.setHidden(false);
  440. assertFalse(cellStyle.getHidden());
  441. }
  442. public void testGetSetLocked() {
  443. assertTrue(cellStyle.getLocked());
  444. cellStyle.setLocked(true);
  445. assertTrue(cellStyle.getLocked());
  446. cellStyle.setLocked(false);
  447. assertFalse(cellStyle.getLocked());
  448. }
  449. public void testGetSetIndent() {
  450. assertEquals((short)0, cellStyle.getIndention());
  451. cellStyle.setIndention((short)3);
  452. assertEquals((short)3, cellStyle.getIndention());
  453. cellStyle.setIndention((short) 13);
  454. assertEquals((short)13, cellStyle.getIndention());
  455. }
  456. public void testGetSetAlignement() {
  457. assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  458. assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
  459. cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
  460. assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
  461. assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
  462. assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  463. cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
  464. assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
  465. assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
  466. assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  467. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  468. assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
  469. assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
  470. assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  471. }
  472. public void testGetSetVerticalAlignment() {
  473. assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
  474. assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
  475. assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  476. cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  477. assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
  478. assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
  479. assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  480. cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
  481. assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
  482. assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
  483. assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  484. }
  485. public void testGetSetWrapText() {
  486. assertFalse(cellStyle.getWrapText());
  487. cellStyle.setWrapText(true);
  488. assertTrue(cellStyle.getWrapText());
  489. cellStyle.setWrapText(false);
  490. assertFalse(cellStyle.getWrapText());
  491. }
  492. /**
  493. * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
  494. */
  495. public void testCloneStyleSameWB() {
  496. XSSFWorkbook wb = new XSSFWorkbook();
  497. assertEquals(1, wb.getNumberOfFonts());
  498. XSSFFont fnt = wb.createFont();
  499. fnt.setFontName("TestingFont");
  500. assertEquals(2, wb.getNumberOfFonts());
  501. XSSFCellStyle orig = wb.createCellStyle();
  502. orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  503. orig.setFont(fnt);
  504. orig.setDataFormat((short)18);
  505. assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
  506. assertTrue(fnt == orig.getFont());
  507. assertTrue(18 == orig.getDataFormat());
  508. XSSFCellStyle clone = wb.createCellStyle();
  509. assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  510. assertFalse(fnt == clone.getFont());
  511. assertFalse(18 == clone.getDataFormat());
  512. clone.cloneStyleFrom(orig);
  513. assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  514. assertTrue(fnt == clone.getFont());
  515. assertTrue(18 == clone.getDataFormat());
  516. assertEquals(2, wb.getNumberOfFonts());
  517. }
  518. /**
  519. * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
  520. */
  521. public void testCloneStyleDiffWB() {
  522. XSSFWorkbook wbOrig = new XSSFWorkbook();
  523. assertEquals(1, wbOrig.getNumberOfFonts());
  524. assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
  525. XSSFFont fnt = wbOrig.createFont();
  526. fnt.setFontName("TestingFont");
  527. assertEquals(2, wbOrig.getNumberOfFonts());
  528. assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
  529. XSSFDataFormat fmt = wbOrig.createDataFormat();
  530. fmt.getFormat("MadeUpOne");
  531. fmt.getFormat("MadeUpTwo");
  532. XSSFCellStyle orig = wbOrig.createCellStyle();
  533. orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  534. orig.setFont(fnt);
  535. orig.setDataFormat(fmt.getFormat("Test##"));
  536. assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
  537. assertTrue(fnt == orig.getFont());
  538. assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
  539. assertEquals(2, wbOrig.getNumberOfFonts());
  540. assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
  541. // Now a style on another workbook
  542. XSSFWorkbook wbClone = new XSSFWorkbook();
  543. assertEquals(1, wbClone.getNumberOfFonts());
  544. assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
  545. assertEquals(1, wbClone.getNumCellStyles());
  546. XSSFDataFormat fmtClone = wbClone.createDataFormat();
  547. XSSFCellStyle clone = wbClone.createCellStyle();
  548. assertEquals(1, wbClone.getNumberOfFonts());
  549. assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
  550. assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  551. assertFalse("TestingFont" == clone.getFont().getFontName());
  552. clone.cloneStyleFrom(orig);
  553. assertEquals(2, wbClone.getNumberOfFonts());
  554. assertEquals(2, wbClone.getNumCellStyles());
  555. assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
  556. assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment());
  557. assertEquals("TestingFont", clone.getFont().getFontName());
  558. assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
  559. assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
  560. // Save it and re-check
  561. XSSFWorkbook wbReload = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
  562. assertEquals(2, wbReload.getNumberOfFonts());
  563. assertEquals(2, wbReload.getNumCellStyles());
  564. assertEquals(1, wbReload.getStylesSource().getNumberFormats().size());
  565. XSSFCellStyle reload = wbReload.getCellStyleAt((short)1);
  566. assertEquals(HSSFCellStyle.ALIGN_RIGHT, reload.getAlignment());
  567. assertEquals("TestingFont", reload.getFont().getFontName());
  568. assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
  569. assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
  570. }
  571. /**
  572. * Avoid ArrayIndexOutOfBoundsException when creating cell style
  573. * in a workbook that has an empty xf table.
  574. */
  575. public void testBug52348() {
  576. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
  577. StylesTable st = workbook.getStylesSource();
  578. assertEquals(0, st._getStyleXfsSize());
  579. XSSFCellStyle style = workbook.createCellStyle(); // no exception at this point
  580. assertNull(style.getStyleXf());
  581. }
  582. }