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 30KB

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