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

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