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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 testGetSetBorderThin() {
  200. cellStyle.setBorderTop(CellStyle.BORDER_THIN);
  201. assertEquals(CellStyle.BORDER_THIN, cellStyle.getBorderTop());
  202. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  203. assertTrue(borderId > 0);
  204. //check changes in the underlying xml bean
  205. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  206. assertEquals(STBorderStyle.THIN, ctBorder.getTop().getStyle());
  207. }
  208. public void testGetSetBorderMedium() {
  209. cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
  210. assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
  211. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  212. assertTrue(borderId > 0);
  213. //check changes in the underlying xml bean
  214. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  215. assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
  216. }
  217. public void testGetSetBorderThick() {
  218. cellStyle.setBorderTop(CellStyle.BORDER_THICK);
  219. assertEquals(CellStyle.BORDER_THICK, cellStyle.getBorderTop());
  220. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  221. assertTrue(borderId > 0);
  222. //check changes in the underlying xml bean
  223. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  224. assertEquals(STBorderStyle.THICK, ctBorder.getTop().getStyle());
  225. }
  226. public void testGetSetBorderHair() {
  227. cellStyle.setBorderTop(CellStyle.BORDER_HAIR);
  228. assertEquals(CellStyle.BORDER_HAIR, cellStyle.getBorderTop());
  229. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  230. assertTrue(borderId > 0);
  231. //check changes in the underlying xml bean
  232. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  233. assertEquals(STBorderStyle.HAIR, ctBorder.getTop().getStyle());
  234. }
  235. public void testGetSetBorderDotted() {
  236. cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
  237. assertEquals(CellStyle.BORDER_DOTTED, cellStyle.getBorderTop());
  238. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  239. assertTrue(borderId > 0);
  240. //check changes in the underlying xml bean
  241. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  242. assertEquals(STBorderStyle.DOTTED, ctBorder.getTop().getStyle());
  243. }
  244. public void testGetSetBorderDashed() {
  245. cellStyle.setBorderTop(CellStyle.BORDER_DASHED);
  246. assertEquals(CellStyle.BORDER_DASHED, cellStyle.getBorderTop());
  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(STBorderStyle.DASHED, ctBorder.getTop().getStyle());
  252. }
  253. public void testGetSetBorderDashDot() {
  254. cellStyle.setBorderTop(CellStyle.BORDER_DASH_DOT);
  255. assertEquals(CellStyle.BORDER_DASH_DOT, cellStyle.getBorderTop());
  256. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  257. assertTrue(borderId > 0);
  258. //check changes in the underlying xml bean
  259. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  260. assertEquals(STBorderStyle.DASH_DOT, ctBorder.getTop().getStyle());
  261. }
  262. public void testGetSetBorderDashDotDot() {
  263. cellStyle.setBorderTop(CellStyle.BORDER_DASH_DOT_DOT);
  264. assertEquals(CellStyle.BORDER_DASH_DOT_DOT, cellStyle.getBorderTop());
  265. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  266. assertTrue(borderId > 0);
  267. //check changes in the underlying xml bean
  268. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  269. assertEquals(STBorderStyle.DASH_DOT_DOT, ctBorder.getTop().getStyle());
  270. }
  271. public void testGetSetBorderMediumDashDot() {
  272. cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASH_DOT);
  273. assertEquals(CellStyle.BORDER_MEDIUM_DASH_DOT, cellStyle.getBorderTop());
  274. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  275. assertTrue(borderId > 0);
  276. //check changes in the underlying xml bean
  277. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  278. assertEquals(STBorderStyle.MEDIUM_DASH_DOT, ctBorder.getTop().getStyle());
  279. }
  280. public void testGetSetBorderMediumDashDotDot() {
  281. cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASH_DOT_DOT);
  282. assertEquals(CellStyle.BORDER_MEDIUM_DASH_DOT_DOT, cellStyle.getBorderTop());
  283. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  284. assertTrue(borderId > 0);
  285. //check changes in the underlying xml bean
  286. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  287. assertEquals(STBorderStyle.MEDIUM_DASH_DOT_DOT, ctBorder.getTop().getStyle());
  288. }
  289. public void testGetSetBorderMediumDashed() {
  290. cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
  291. assertEquals(CellStyle.BORDER_MEDIUM_DASHED, cellStyle.getBorderTop());
  292. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  293. assertTrue(borderId > 0);
  294. //check changes in the underlying xml bean
  295. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  296. assertEquals(STBorderStyle.MEDIUM_DASHED, ctBorder.getTop().getStyle());
  297. }
  298. public void testGetSetBorderSlantDashDot() {
  299. cellStyle.setBorderTop(CellStyle.BORDER_SLANTED_DASH_DOT);
  300. assertEquals(CellStyle.BORDER_SLANTED_DASH_DOT, cellStyle.getBorderTop());
  301. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  302. assertTrue(borderId > 0);
  303. //check changes in the underlying xml bean
  304. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  305. assertEquals(STBorderStyle.SLANT_DASH_DOT, ctBorder.getTop().getStyle());
  306. }
  307. public void testGetSetBorderDouble() {
  308. cellStyle.setBorderTop(CellStyle.BORDER_DOUBLE);
  309. assertEquals(CellStyle.BORDER_DOUBLE, cellStyle.getBorderTop());
  310. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  311. assertTrue(borderId > 0);
  312. //check changes in the underlying xml bean
  313. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  314. assertEquals(STBorderStyle.DOUBLE, ctBorder.getTop().getStyle());
  315. }
  316. public void testGetSetBottomBorderColor() {
  317. //defaults
  318. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getBottomBorderColor());
  319. assertNull(cellStyle.getBottomBorderXSSFColor());
  320. int num = stylesTable.getBorders().size();
  321. XSSFColor clr;
  322. //setting indexed color
  323. cellStyle.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
  324. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getBottomBorderColor());
  325. clr = cellStyle.getBottomBorderXSSFColor();
  326. assertTrue(clr.getCTColor().isSetIndexed());
  327. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  328. //a new border was added to the styles table
  329. assertEquals(num + 1, stylesTable.getBorders().size());
  330. //id of the created border
  331. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  332. assertTrue(borderId > 0);
  333. //check changes in the underlying xml bean
  334. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  335. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getBottom().getColor().getIndexed());
  336. //setting XSSFColor
  337. num = stylesTable.getBorders().size();
  338. clr = new XSSFColor(java.awt.Color.CYAN);
  339. cellStyle.setBottomBorderColor(clr);
  340. assertEquals(clr.getCTColor().toString(), cellStyle.getBottomBorderXSSFColor().getCTColor().toString());
  341. byte[] rgb = cellStyle.getBottomBorderXSSFColor().getRgb();
  342. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  343. //another border was added to the styles table
  344. assertEquals(num + 1, stylesTable.getBorders().size());
  345. //passing null unsets the color
  346. cellStyle.setBottomBorderColor(null);
  347. assertNull(cellStyle.getBottomBorderXSSFColor());
  348. }
  349. public void testGetSetTopBorderColor() {
  350. //defaults
  351. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getTopBorderColor());
  352. assertNull(cellStyle.getTopBorderXSSFColor());
  353. int num = stylesTable.getBorders().size();
  354. XSSFColor clr;
  355. //setting indexed color
  356. cellStyle.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
  357. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getTopBorderColor());
  358. clr = cellStyle.getTopBorderXSSFColor();
  359. assertTrue(clr.getCTColor().isSetIndexed());
  360. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  361. //a new border was added to the styles table
  362. assertEquals(num + 1, stylesTable.getBorders().size());
  363. //id of the created border
  364. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  365. assertTrue(borderId > 0);
  366. //check changes in the underlying xml bean
  367. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  368. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getTop().getColor().getIndexed());
  369. //setting XSSFColor
  370. num = stylesTable.getBorders().size();
  371. clr = new XSSFColor(java.awt.Color.CYAN);
  372. cellStyle.setTopBorderColor(clr);
  373. assertEquals(clr.getCTColor().toString(), cellStyle.getTopBorderXSSFColor().getCTColor().toString());
  374. byte[] rgb = cellStyle.getTopBorderXSSFColor().getRgb();
  375. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  376. //another border was added to the styles table
  377. assertEquals(num + 1, stylesTable.getBorders().size());
  378. //passing null unsets the color
  379. cellStyle.setTopBorderColor(null);
  380. assertNull(cellStyle.getTopBorderXSSFColor());
  381. }
  382. public void testGetSetLeftBorderColor() {
  383. //defaults
  384. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getLeftBorderColor());
  385. assertNull(cellStyle.getLeftBorderXSSFColor());
  386. int num = stylesTable.getBorders().size();
  387. XSSFColor clr;
  388. //setting indexed color
  389. cellStyle.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
  390. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getLeftBorderColor());
  391. clr = cellStyle.getLeftBorderXSSFColor();
  392. assertTrue(clr.getCTColor().isSetIndexed());
  393. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  394. //a new border was added to the styles table
  395. assertEquals(num + 1, stylesTable.getBorders().size());
  396. //id of the created border
  397. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  398. assertTrue(borderId > 0);
  399. //check changes in the underlying xml bean
  400. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  401. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getLeft().getColor().getIndexed());
  402. //setting XSSFColor
  403. num = stylesTable.getBorders().size();
  404. clr = new XSSFColor(java.awt.Color.CYAN);
  405. cellStyle.setLeftBorderColor(clr);
  406. assertEquals(clr.getCTColor().toString(), cellStyle.getLeftBorderXSSFColor().getCTColor().toString());
  407. byte[] rgb = cellStyle.getLeftBorderXSSFColor().getRgb();
  408. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  409. //another border was added to the styles table
  410. assertEquals(num + 1, stylesTable.getBorders().size());
  411. //passing null unsets the color
  412. cellStyle.setLeftBorderColor(null);
  413. assertNull(cellStyle.getLeftBorderXSSFColor());
  414. }
  415. public void testGetSetRightBorderColor() {
  416. //defaults
  417. assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getRightBorderColor());
  418. assertNull(cellStyle.getRightBorderXSSFColor());
  419. int num = stylesTable.getBorders().size();
  420. XSSFColor clr;
  421. //setting indexed color
  422. cellStyle.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
  423. assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getRightBorderColor());
  424. clr = cellStyle.getRightBorderXSSFColor();
  425. assertTrue(clr.getCTColor().isSetIndexed());
  426. assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
  427. //a new border was added to the styles table
  428. assertEquals(num + 1, stylesTable.getBorders().size());
  429. //id of the created border
  430. int borderId = (int)cellStyle.getCoreXf().getBorderId();
  431. assertTrue(borderId > 0);
  432. //check changes in the underlying xml bean
  433. CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
  434. assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getRight().getColor().getIndexed());
  435. //setting XSSFColor
  436. num = stylesTable.getBorders().size();
  437. clr = new XSSFColor(java.awt.Color.CYAN);
  438. cellStyle.setRightBorderColor(clr);
  439. assertEquals(clr.getCTColor().toString(), cellStyle.getRightBorderXSSFColor().getCTColor().toString());
  440. byte[] rgb = cellStyle.getRightBorderXSSFColor().getRgb();
  441. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  442. //another border was added to the styles table
  443. assertEquals(num + 1, stylesTable.getBorders().size());
  444. //passing null unsets the color
  445. cellStyle.setRightBorderColor(null);
  446. assertNull(cellStyle.getRightBorderXSSFColor());
  447. }
  448. public void testGetSetFillBackgroundColor() {
  449. assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
  450. assertNull(cellStyle.getFillBackgroundXSSFColor());
  451. XSSFColor clr;
  452. int num = stylesTable.getFills().size();
  453. //setting indexed color
  454. cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
  455. assertEquals(IndexedColors.RED.getIndex(), cellStyle.getFillBackgroundColor());
  456. clr = cellStyle.getFillBackgroundXSSFColor();
  457. assertTrue(clr.getCTColor().isSetIndexed());
  458. assertEquals(IndexedColors.RED.getIndex(), clr.getIndexed());
  459. //a new fill was added to the styles table
  460. assertEquals(num + 1, stylesTable.getFills().size());
  461. //id of the created border
  462. int fillId = (int)cellStyle.getCoreXf().getFillId();
  463. assertTrue(fillId > 0);
  464. //check changes in the underlying xml bean
  465. CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
  466. assertEquals(IndexedColors.RED.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
  467. //setting XSSFColor
  468. num = stylesTable.getFills().size();
  469. clr = new XSSFColor(java.awt.Color.CYAN);
  470. cellStyle.setFillBackgroundColor(clr);
  471. assertEquals(clr.getCTColor().toString(), cellStyle.getFillBackgroundXSSFColor().getCTColor().toString());
  472. byte[] rgb = cellStyle.getFillBackgroundXSSFColor().getRgb();
  473. assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
  474. //another border was added to the styles table
  475. assertEquals(num + 1, stylesTable.getFills().size());
  476. //passing null unsets the color
  477. cellStyle.setFillBackgroundColor(null);
  478. assertNull(cellStyle.getFillBackgroundXSSFColor());
  479. assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
  480. }
  481. public void testDefaultStyles() {
  482. XSSFWorkbook wb1 = new XSSFWorkbook();
  483. XSSFCellStyle style1 = wb1.createCellStyle();
  484. assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
  485. assertNull(style1.getFillBackgroundXSSFColor());
  486. //compatibility with HSSF
  487. HSSFWorkbook wb2 = new HSSFWorkbook();
  488. HSSFCellStyle style2 = wb2.createCellStyle();
  489. assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor());
  490. assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor());
  491. assertEquals(style2.getFillPattern(), style1.getFillPattern());
  492. assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor());
  493. assertEquals(style2.getTopBorderColor(), style1.getTopBorderColor());
  494. assertEquals(style2.getRightBorderColor(), style1.getRightBorderColor());
  495. assertEquals(style2.getBottomBorderColor(), style1.getBottomBorderColor());
  496. assertEquals(style2.getBorderBottom(), style1.getBorderBottom());
  497. assertEquals(style2.getBorderLeft(), style1.getBorderLeft());
  498. assertEquals(style2.getBorderRight(), style1.getBorderRight());
  499. assertEquals(style2.getBorderTop(), style1.getBorderTop());
  500. }
  501. public void testGetFillForegroundColor() {
  502. XSSFWorkbook wb = new XSSFWorkbook();
  503. StylesTable styles = wb.getStylesSource();
  504. assertEquals(1, wb.getNumCellStyles());
  505. assertEquals(2, styles.getFills().size());
  506. XSSFCellStyle defaultStyle = wb.getCellStyleAt((short)0);
  507. assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor());
  508. assertEquals(null, defaultStyle.getFillForegroundXSSFColor());
  509. assertEquals(CellStyle.NO_FILL, defaultStyle.getFillPattern());
  510. XSSFCellStyle customStyle = wb.createCellStyle();
  511. customStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  512. assertEquals(CellStyle.SOLID_FOREGROUND, customStyle.getFillPattern());
  513. assertEquals(3, styles.getFills().size());
  514. customStyle.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  515. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), customStyle.getFillForegroundColor());
  516. assertEquals(4, styles.getFills().size());
  517. for (int i = 0; i < 3; i++) {
  518. XSSFCellStyle style = wb.createCellStyle();
  519. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  520. assertEquals(CellStyle.SOLID_FOREGROUND, style.getFillPattern());
  521. assertEquals(4, styles.getFills().size());
  522. style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
  523. assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
  524. assertEquals(4, styles.getFills().size());
  525. }
  526. }
  527. public void testGetFillPattern() {
  528. assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
  529. int num = stylesTable.getFills().size();
  530. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  531. assertEquals(CellStyle.SOLID_FOREGROUND, cellStyle.getFillPattern());
  532. assertEquals(num + 1, stylesTable.getFills().size());
  533. int fillId = (int)cellStyle.getCoreXf().getFillId();
  534. assertTrue(fillId > 0);
  535. //check changes in the underlying xml bean
  536. CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
  537. assertEquals(STPatternType.SOLID, ctFill.getPatternFill().getPatternType());
  538. //setting the same fill multiple time does not update the styles table
  539. for (int i = 0; i < 3; i++) {
  540. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  541. }
  542. assertEquals(num + 1, stylesTable.getFills().size());
  543. cellStyle.setFillPattern(CellStyle.NO_FILL);
  544. assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
  545. fillId = (int)cellStyle.getCoreXf().getFillId();
  546. ctFill = stylesTable.getFillAt(fillId).getCTFill();
  547. assertFalse(ctFill.getPatternFill().isSetPatternType());
  548. }
  549. public void testGetFont() {
  550. assertNotNull(cellStyle.getFont());
  551. }
  552. public void testGetSetHidden() {
  553. assertFalse(cellStyle.getHidden());
  554. cellStyle.setHidden(true);
  555. assertTrue(cellStyle.getHidden());
  556. cellStyle.setHidden(false);
  557. assertFalse(cellStyle.getHidden());
  558. }
  559. public void testGetSetLocked() {
  560. assertTrue(cellStyle.getLocked());
  561. cellStyle.setLocked(true);
  562. assertTrue(cellStyle.getLocked());
  563. cellStyle.setLocked(false);
  564. assertFalse(cellStyle.getLocked());
  565. }
  566. public void testGetSetIndent() {
  567. assertEquals((short)0, cellStyle.getIndention());
  568. cellStyle.setIndention((short)3);
  569. assertEquals((short)3, cellStyle.getIndention());
  570. cellStyle.setIndention((short) 13);
  571. assertEquals((short)13, cellStyle.getIndention());
  572. }
  573. public void testGetSetAlignement() {
  574. assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  575. assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
  576. cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
  577. assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
  578. assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
  579. assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  580. cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
  581. assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
  582. assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
  583. assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  584. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  585. assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
  586. assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
  587. assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
  588. }
  589. public void testGetSetVerticalAlignment() {
  590. assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
  591. assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
  592. assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  593. cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  594. assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
  595. assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
  596. assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  597. cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
  598. assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
  599. assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
  600. assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
  601. }
  602. public void testGetSetWrapText() {
  603. assertFalse(cellStyle.getWrapText());
  604. cellStyle.setWrapText(true);
  605. assertTrue(cellStyle.getWrapText());
  606. cellStyle.setWrapText(false);
  607. assertFalse(cellStyle.getWrapText());
  608. }
  609. /**
  610. * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
  611. */
  612. public void testCloneStyleSameWB() {
  613. XSSFWorkbook wb = new XSSFWorkbook();
  614. assertEquals(1, wb.getNumberOfFonts());
  615. XSSFFont fnt = wb.createFont();
  616. fnt.setFontName("TestingFont");
  617. assertEquals(2, wb.getNumberOfFonts());
  618. XSSFCellStyle orig = wb.createCellStyle();
  619. orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  620. orig.setFont(fnt);
  621. orig.setDataFormat((short)18);
  622. assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
  623. assertTrue(fnt == orig.getFont());
  624. assertTrue(18 == orig.getDataFormat());
  625. XSSFCellStyle clone = wb.createCellStyle();
  626. assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  627. assertFalse(fnt == clone.getFont());
  628. assertFalse(18 == clone.getDataFormat());
  629. clone.cloneStyleFrom(orig);
  630. assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  631. assertTrue(fnt == clone.getFont());
  632. assertTrue(18 == clone.getDataFormat());
  633. assertEquals(2, wb.getNumberOfFonts());
  634. }
  635. /**
  636. * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
  637. */
  638. public void testCloneStyleDiffWB() {
  639. XSSFWorkbook wbOrig = new XSSFWorkbook();
  640. assertEquals(1, wbOrig.getNumberOfFonts());
  641. assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
  642. XSSFFont fnt = wbOrig.createFont();
  643. fnt.setFontName("TestingFont");
  644. assertEquals(2, wbOrig.getNumberOfFonts());
  645. assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
  646. XSSFDataFormat fmt = wbOrig.createDataFormat();
  647. fmt.getFormat("MadeUpOne");
  648. fmt.getFormat("MadeUpTwo");
  649. XSSFCellStyle orig = wbOrig.createCellStyle();
  650. orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  651. orig.setFont(fnt);
  652. orig.setDataFormat(fmt.getFormat("Test##"));
  653. assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
  654. assertTrue(fnt == orig.getFont());
  655. assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
  656. assertEquals(2, wbOrig.getNumberOfFonts());
  657. assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
  658. // Now a style on another workbook
  659. XSSFWorkbook wbClone = new XSSFWorkbook();
  660. assertEquals(1, wbClone.getNumberOfFonts());
  661. assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
  662. assertEquals(1, wbClone.getNumCellStyles());
  663. XSSFDataFormat fmtClone = wbClone.createDataFormat();
  664. XSSFCellStyle clone = wbClone.createCellStyle();
  665. assertEquals(1, wbClone.getNumberOfFonts());
  666. assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
  667. assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  668. assertFalse("TestingFont" == clone.getFont().getFontName());
  669. clone.cloneStyleFrom(orig);
  670. assertEquals(2, wbClone.getNumberOfFonts());
  671. assertEquals(2, wbClone.getNumCellStyles());
  672. assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
  673. assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment());
  674. assertEquals("TestingFont", clone.getFont().getFontName());
  675. assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
  676. assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
  677. // Save it and re-check
  678. XSSFWorkbook wbReload = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
  679. assertEquals(2, wbReload.getNumberOfFonts());
  680. assertEquals(2, wbReload.getNumCellStyles());
  681. assertEquals(1, wbReload.getStylesSource().getNumberFormats().size());
  682. XSSFCellStyle reload = wbReload.getCellStyleAt((short)1);
  683. assertEquals(HSSFCellStyle.ALIGN_RIGHT, reload.getAlignment());
  684. assertEquals("TestingFont", reload.getFont().getFontName());
  685. assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
  686. assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
  687. }
  688. /**
  689. * Avoid ArrayIndexOutOfBoundsException when creating cell style
  690. * in a workbook that has an empty xf table.
  691. */
  692. public void testBug52348() {
  693. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
  694. StylesTable st = workbook.getStylesSource();
  695. assertEquals(0, st._getStyleXfsSize());
  696. XSSFCellStyle style = workbook.createCellStyle(); // no exception at this point
  697. assertNull(style.getStyleXf());
  698. }
  699. }