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

(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java (-1 / +1 lines)
Lines 188-194 Link Here
188
                        src.getFont().getCTFont().toString(), DEFAULT_XML_OPTIONS
188
                        src.getFont().getCTFont().toString(), DEFAULT_XML_OPTIONS
189
                  );
189
                  );
190
                  XSSFFont font = new XSSFFont(ctFont);
190
                  XSSFFont font = new XSSFFont(ctFont);
191
                  font.registerTo(_stylesSource);
191
                  font.registerTo_Extended(_stylesSource, false);
192
                  setFont(font);
192
                  setFont(font);
193
               } catch(XmlException e) {
193
               } catch(XmlException e) {
194
                  throw new POIXMLException(e);
194
                  throw new POIXMLException(e);
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java (-2 / +27 lines)
Lines 16-21 Link Here
16
==================================================================== */
16
==================================================================== */
17
package org.apache.poi.xssf.usermodel;
17
package org.apache.poi.xssf.usermodel;
18
18
19
import java.util.Objects;
20
19
import org.apache.poi.POIXMLException;
21
import org.apache.poi.POIXMLException;
20
import org.apache.poi.util.Internal;
22
import org.apache.poi.util.Internal;
21
import org.apache.poi.ss.usermodel.Font;
23
import org.apache.poi.ss.usermodel.Font;
Lines 553-563 Link Here
553
     *  to the style table
555
     *  to the style table
554
     */
556
     */
555
    public long registerTo(StylesTable styles) {
557
    public long registerTo(StylesTable styles) {
558
        return registerTo_Extended(styles, true);
559
    }
560
    
561
    public long registerTo_Extended(StylesTable styles, boolean force) {
556
        this._themes = styles.getTheme();
562
        this._themes = styles.getTheme();
557
        short idx = (short)styles.putFont(this, true);
563
        short idx = (short)styles.putFont(this, force);
558
        this._index = idx;
564
        this._index = idx;
559
        return idx;
565
        return idx;
560
    }
566
    }
567
    
568
    
561
    /**
569
    /**
562
     * Records the Themes Table that is associated with
570
     * Records the Themes Table that is associated with
563
     *  the current font, used when looking up theme
571
     *  the current font, used when looking up theme
Lines 644-650 Link Here
644
        if(!(o instanceof XSSFFont)) return false;
652
        if(!(o instanceof XSSFFont)) return false;
645
653
646
        XSSFFont cf = (XSSFFont)o;
654
        XSSFFont cf = (XSSFFont)o;
647
        return _ctFont.toString().equals(cf.getCTFont().toString());
655
        
656
        // BUG 60845
657
        boolean equal =
658
                Objects.equals(this.getItalic(), cf.getItalic())
659
                && Objects.equals(this.getBold(), cf.getBold())
660
                && Objects.equals(this.getStrikeout(), cf.getStrikeout())
661
                && Objects.equals(this.getCharSet(), cf.getCharSet())
662
                && Objects.equals(this.getBold(), cf.getBold())
663
                && Objects.equals(this.getColor(), cf.getColor())
664
                && Objects.equals(this.getFamily(), cf.getFamily())
665
                && Objects.equals(this.getFontHeight(), cf.getFontHeight())
666
                && Objects.equals(this.getFontName(), cf.getFontName())
667
                && Objects.equals(this.getScheme(), cf.getScheme())
668
                && Objects.equals(this.getThemeColor(), cf.getThemeColor())
669
                && Objects.equals(this.getTypeOffset(), cf.getTypeOffset())
670
                && Objects.equals(this.getUnderline(), cf.getUnderline())
671
                && Objects.equals(this.getXSSFColor(), cf.getXSSFColor());
672
        return equal;
648
    }
673
    }
649
674
650
}
675
}
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java (-1 / +14 lines)
Lines 17-22 Link Here
17
package org.apache.poi.xssf.usermodel.extensions;
17
package org.apache.poi.xssf.usermodel.extensions;
18
18
19
19
20
import java.util.Objects;
21
20
import org.apache.poi.ss.usermodel.BorderStyle;
22
import org.apache.poi.ss.usermodel.BorderStyle;
21
import org.apache.poi.xssf.model.ThemesTable;
23
import org.apache.poi.xssf.model.ThemesTable;
22
import org.apache.poi.xssf.usermodel.XSSFColor;
24
import org.apache.poi.xssf.usermodel.XSSFColor;
Lines 180-185 Link Here
180
        if (!(o instanceof XSSFCellBorder)) return false;
182
        if (!(o instanceof XSSFCellBorder)) return false;
181
183
182
        XSSFCellBorder cf = (XSSFCellBorder) o;
184
        XSSFCellBorder cf = (XSSFCellBorder) o;
183
        return border.toString().equals(cf.getCTBorder().toString());
185
186
        // bug 60845
187
        boolean equal = true;
188
        for(BorderSide side : BorderSide.values()){
189
            if(!Objects.equals(this.getBorderColor(side), cf.getBorderColor(side))
190
                    || !Objects.equals(this.getBorderStyle(side), cf.getBorderStyle(side))){
191
                equal = false;
192
                break;
193
            }
194
        }
195
        
196
        return equal;
184
    }
197
    }
185
}
198
}
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java (-3 / +21 lines)
Lines 21-26 Link Here
21
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
21
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
22
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
22
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
23
import org.apache.poi.xssf.usermodel.XSSFColor;
23
import org.apache.poi.xssf.usermodel.XSSFColor;
24
25
import java.util.Objects;
26
24
import org.apache.poi.util.Internal;
27
import org.apache.poi.util.Internal;
25
28
26
/**
29
/**
Lines 122-128 Link Here
122
     */
125
     */
123
    public STPatternType.Enum getPatternType() {
126
    public STPatternType.Enum getPatternType() {
124
        CTPatternFill ptrn = _fill.getPatternFill();
127
        CTPatternFill ptrn = _fill.getPatternFill();
125
        return ptrn == null ? null : ptrn.getPatternType();
128
        
129
        return ptrn == null ? STPatternType.NONE : ptrn.getPatternType();
126
    }
130
    }
127
131
128
    /**
132
    /**
Lines 150-156 Link Here
150
     */
154
     */
151
    @Internal
155
    @Internal
152
    public CTFill getCTFill() {
156
    public CTFill getCTFill() {
153
        return _fill;
157
        return _fill; 
154
    }
158
    }
155
159
156
160
Lines 162-167 Link Here
162
        if (!(o instanceof XSSFCellFill)) return false;
166
        if (!(o instanceof XSSFCellFill)) return false;
163
167
164
        XSSFCellFill cf = (XSSFCellFill) o;
168
        XSSFCellFill cf = (XSSFCellFill) o;
165
        return _fill.toString().equals(cf.getCTFill().toString());
169
        
170
        // bug 60845
171
        // Do not compare the representing strings but the properties
172
        // Reason:
173
        //   The strings are different if he XMLObject is a fragment (e.g. the ones from cloneStyle)
174
        //   even if they are in fact representing the same style
175
        
176
        
177
        
178
        boolean equal =
179
                Objects.equals(this.getFillBackgroundColor(), cf.getFillBackgroundColor())
180
                && Objects.equals(this.getFillForegroundColor(), cf.getFillForegroundColor())
181
                && Objects.equals(this.getPatternType(), cf.getPatternType());
182
        
183
        return equal;
166
    }
184
    }
167
}
185
}
(-)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