View | Details | Raw Unified | Return to bug 60845
Collapse All | Expand All

(-)a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java (-89 / +113 lines)
Lines 67-81 Link Here
67
		ctBorderA = CTBorder.Factory.newInstance();
67
		ctBorderA = CTBorder.Factory.newInstance();
68
		XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
68
		XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
69
		long borderId = stylesTable.putBorder(borderA);
69
		long borderId = stylesTable.putBorder(borderA);
70
		assertEquals(1, borderId);
70
		assertEquals(0, borderId);
71
71
72
		XSSFCellBorder borderB = new XSSFCellBorder();
72
		XSSFCellBorder borderB = new XSSFCellBorder();
73
		assertEquals(1, stylesTable.putBorder(borderB));
73
		assertEquals(0, stylesTable.putBorder(borderB));
74
74
75
		ctFill = CTFill.Factory.newInstance();
75
		ctFill = CTFill.Factory.newInstance();
76
		XSSFCellFill fill = new XSSFCellFill(ctFill);
76
		XSSFCellFill fill = new XSSFCellFill(ctFill);
77
		long fillId = stylesTable.putFill(fill);
77
		long fillId = stylesTable.putFill(fill);
78
		assertEquals(2, fillId);
78
		assertEquals(0, fillId);
79
79
80
		ctFont = CTFont.Factory.newInstance();
80
		ctFont = CTFont.Factory.newInstance();
81
		XSSFFont font = new XSSFFont(ctFont);
81
		XSSFFont font = new XSSFFont(ctFont);
Lines 133-139 Link Here
133
        assertEquals(num, stylesTable.getBorders().size());
133
        assertEquals(num, stylesTable.getBorders().size());
134
        borderId = (int)cellStyle.getCoreXf().getBorderId();
134
        borderId = (int)cellStyle.getCoreXf().getBorderId();
135
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
135
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
136
        assertFalse(ctBorder.isSetBottom());
136
        //none is not the same as "not set", therefore the following doesn't work any more
137
        //assertFalse(ctBorder.isSetBottom());
138
        //replacement:
139
        assertEquals(ctBorder.getBottom().getStyle(), STBorderStyle.NONE);
137
    }
140
    }
138
141
139
	@Test
142
	@Test
Lines 168-174 Link Here
168
        assertEquals(num, stylesTable.getBorders().size());
171
        assertEquals(num, stylesTable.getBorders().size());
169
        borderId = (int)cellStyle.getCoreXf().getBorderId();
172
        borderId = (int)cellStyle.getCoreXf().getBorderId();
170
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
173
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
171
        assertFalse(ctBorder.isSetRight());
174
        //none is not the same as "not set", therefore the following doesn't work any more
175
        //assertFalse(ctBorder.isSetRight());
176
        //replacement:
177
        assertEquals(ctBorder.getRight().getStyle(), STBorderStyle.NONE);
172
    }
178
    }
173
179
174
	@Test
180
	@Test
Lines 203-209 Link Here
203
        assertEquals(num, stylesTable.getBorders().size());
209
        assertEquals(num, stylesTable.getBorders().size());
204
        borderId = (int)cellStyle.getCoreXf().getBorderId();
210
        borderId = (int)cellStyle.getCoreXf().getBorderId();
205
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
211
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
206
        assertFalse(ctBorder.isSetLeft());
212
        //none is not the same as "not set", therefore the following doesn't work any more
213
        //assertFalse(ctBorder.isSetLeft());
214
        //replacement:
215
        assertEquals(ctBorder.getLeft().getStyle(), STBorderStyle.NONE);
207
	}
216
	}
208
217
209
	@Test
218
	@Test
Lines 238-244 Link Here
238
        assertEquals(num, stylesTable.getBorders().size());
247
        assertEquals(num, stylesTable.getBorders().size());
239
        borderId = (int)cellStyle.getCoreXf().getBorderId();
248
        borderId = (int)cellStyle.getCoreXf().getBorderId();
240
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
249
        ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
241
        assertFalse(ctBorder.isSetTop());
250
        //none is not the same as "not set", therefore the following doesn't work any more
251
        //assertFalse(ctBorder.isSetTop());
252
        //replacement:
253
        assertEquals(ctBorder.getTop().getStyle(), STBorderStyle.NONE);
242
    }
254
    }
243
    
255
    
244
    private void testGetSetBorderXMLBean(BorderStyle border, STBorderStyle.Enum expected) {
256
    private void testGetSetBorderXMLBean(BorderStyle border, STBorderStyle.Enum expected) {
Lines 258-267 Link Here
258
        cellStyle.setBorderTop(BorderStyle.NONE);
270
        cellStyle.setBorderTop(BorderStyle.NONE);
259
        assertEquals(BorderStyle.NONE, cellStyle.getBorderTopEnum());
271
        assertEquals(BorderStyle.NONE, cellStyle.getBorderTopEnum());
260
        int borderId = (int)cellStyle.getCoreXf().getBorderId();
272
        int borderId = (int)cellStyle.getCoreXf().getBorderId();
261
        assertTrue(borderId > 0);
273
        // The default Style is already "none"
274
        // Therefore the new style already exists as Id=0
275
        //assertTrue(borderId > 0);
276
        // replacement:
277
        assertTrue(borderId == 0);
262
        //check changes in the underlying xml bean
278
        //check changes in the underlying xml bean
263
        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
279
        CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
264
        assertNull(ctBorder.getTop());
280
        assertNotNull(ctBorder.getTop());
265
        // no border style and STBorderStyle.NONE are equivalent
281
        // no border style and STBorderStyle.NONE are equivalent
266
        // POI prefers to unset the border style than explicitly set it STBorderStyle.NONE
282
        // POI prefers to unset the border style than explicitly set it STBorderStyle.NONE
267
    }
283
    }
Lines 759-846 Link Here
759
      
775
      
760
      wb.close();
776
      wb.close();
761
	}
777
	}
762
778
	
763
	/**
779
    /**     
764
	 * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
780
     * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
765
	 */
781
     */
766
	@Test
782
    @Test
767
    public void testCloneStyleDiffWB() throws IOException {
783
    public void testCloneStyleDiffWB() throws IOException {
768
       XSSFWorkbook wbOrig = new XSSFWorkbook();
784
        XSSFWorkbook wbOrig = new XSSFWorkbook();
769
       assertEquals(1, wbOrig.getNumberOfFonts());
785
        assertEquals(1, wbOrig.getNumberOfFonts());
770
       assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
786
        assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
771
       
787
        
772
       XSSFFont fnt = wbOrig.createFont();
788
        XSSFFont fnt = wbOrig.createFont();
773
       fnt.setFontName("TestingFont");
789
        fnt.setFontName("TestingFont");
774
       assertEquals(2, wbOrig.getNumberOfFonts());
790
        assertEquals(2, wbOrig.getNumberOfFonts());
775
       assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
791
        assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
776
       
792
        
777
       XSSFDataFormat fmt = wbOrig.createDataFormat();
793
        XSSFDataFormat fmt = wbOrig.createDataFormat();
778
       fmt.getFormat("MadeUpOne");
794
        fmt.getFormat("MadeUpOne");
779
       fmt.getFormat("MadeUpTwo");
795
        fmt.getFormat("MadeUpTwo");
780
       
796
        
781
       XSSFCellStyle orig = wbOrig.createCellStyle();
797
        XSSFCellStyle orig = wbOrig.createCellStyle();
782
       orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
798
        orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
783
       orig.setFont(fnt);
799
        orig.setFont(fnt);
784
       orig.setDataFormat(fmt.getFormat("Test##"));
800
        orig.setDataFormat(fmt.getFormat("Test##"));
785
       
801
        orig.setFillPattern(FillPatternType.SOLID_FOREGROUND);
786
       assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
802
        orig.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
787
       assertTrue(fnt == orig.getFont());
803
        
788
       assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
804
        XSSFCellStyle origEmpty = wbOrig.createCellStyle();
789
       
805
        
790
       assertEquals(2, wbOrig.getNumberOfFonts());
806
        assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
791
       assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
807
        assertTrue(fnt == orig.getFont());
792
       
808
        assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
793
       
809
        
794
       // Now a style on another workbook
810
        assertEquals(2, wbOrig.getNumberOfFonts());
795
       XSSFWorkbook wbClone = new XSSFWorkbook();
811
        assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
796
       assertEquals(1, wbClone.getNumberOfFonts());
812
        
797
       assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
813
        
798
       assertEquals(1, wbClone.getNumCellStyles());
814
        // Now a style on another workbook
799
       
815
        XSSFWorkbook wbClone = new XSSFWorkbook();
800
       XSSFDataFormat fmtClone = wbClone.createDataFormat();
816
        assertEquals(1, wbClone.getNumberOfFonts());
801
       XSSFCellStyle clone = wbClone.createCellStyle();
817
        assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
802
       
818
        assertEquals(1, wbClone.getNumCellStyles());
803
       assertEquals(1, wbClone.getNumberOfFonts());
819
        
804
       assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
820
        XSSFDataFormat fmtClone = wbClone.createDataFormat();
805
       
821
        XSSFCellStyle clone = wbClone.createCellStyle();
806
       assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
822
        
807
       assertNotEquals("TestingFont", clone.getFont().getFontName());
823
        assertEquals(1, wbClone.getNumberOfFonts());
808
       
824
        assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
809
       clone.cloneStyleFrom(orig);
825
        
810
       
826
        assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
811
       assertEquals(2, wbClone.getNumberOfFonts());
827
        assertNotEquals("TestingFont", clone.getFont().getFontName());
812
       assertEquals(2, wbClone.getNumCellStyles());
828
        
813
       assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
829
        clone.cloneStyleFrom(orig);
814
       
830
        
815
       assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment());
831
        assertEquals(2, wbClone.getNumberOfFonts());
816
       assertEquals("TestingFont", clone.getFont().getFontName());
832
        assertEquals(2, wbClone.getNumCellStyles());
817
       assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
833
        assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
818
       assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
834
        
819
       
835
        assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment());
820
       // Save it and re-check
836
        assertEquals("TestingFont", clone.getFont().getFontName());
821
       XSSFWorkbook wbReload = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
837
        assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
822
       assertEquals(2, wbReload.getNumberOfFonts());
838
        assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
823
       assertEquals(2, wbReload.getNumCellStyles());
839
        assertEquals(clone.getFillPatternEnum(), FillPatternType.SOLID_FOREGROUND);
824
       assertEquals(1, wbReload.getStylesSource().getNumberFormats().size());
840
        assertEquals(clone.getFillForegroundColor(), IndexedColors.BRIGHT_GREEN.getIndex());
825
       
841
        
826
       XSSFCellStyle reload = wbReload.getCellStyleAt((short)1);
842
        // Save it and re-check
827
       assertEquals(HSSFCellStyle.ALIGN_RIGHT, reload.getAlignment());
843
        XSSFWorkbook wbReload = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
828
       assertEquals("TestingFont", reload.getFont().getFontName());
844
        assertEquals(2, wbReload.getNumberOfFonts());
829
       assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
845
        assertEquals(2, wbReload.getNumCellStyles());
830
       assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
846
        assertEquals(1, wbReload.getStylesSource().getNumberFormats().size());
847
        
848
        XSSFCellStyle reload = wbReload.getCellStyleAt((short)1);
849
        assertEquals(HSSFCellStyle.ALIGN_RIGHT, reload.getAlignment());
850
        assertEquals("TestingFont", reload.getFont().getFontName());
851
        assertEquals(fmtClone.getFormat("Test##"), reload.getDataFormat());
852
        assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
853
        assertEquals(clone.getFillPatternEnum(), FillPatternType.SOLID_FOREGROUND);
854
        assertEquals(clone.getFillForegroundColor(), IndexedColors.BRIGHT_GREEN.getIndex());
831
855
832
       XSSFWorkbook wbOrig2 = XSSFTestDataSamples.writeOutAndReadBack(wbOrig);
856
        XSSFWorkbook wbOrig2 = XSSFTestDataSamples.writeOutAndReadBack(wbOrig);
833
       assertNotNull(wbOrig2);
857
        assertNotNull(wbOrig2);
834
       wbOrig2.close();
858
        wbOrig2.close();
835
       
859
        
836
       XSSFWorkbook wbClone2 = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
860
        XSSFWorkbook wbClone2 = XSSFTestDataSamples.writeOutAndReadBack(wbClone);
837
       assertNotNull(wbClone2);
861
        assertNotNull(wbClone2);
838
       wbClone2.close();
862
        wbClone2.close();
839
       
863
        
840
       wbReload.close();
864
        wbReload.close();
841
       wbClone.close();
865
        wbClone.close();
842
       wbOrig.close();
866
        wbOrig.close();
843
   }
867
    }
844
868
845
    /**
869
    /**
846
     * Avoid ArrayIndexOutOfBoundsException  when creating cell style
870
     * Avoid ArrayIndexOutOfBoundsException  when creating cell style

Return to bug 60845