]> source.dussan.org Git - poi.git/commitdiff
Applied patch 45898 - fixed XSSFCellFill getFillBackgroundColor and getFillForegroun...
authorJosh Micich <josh@apache.org>
Mon, 29 Sep 2008 04:28:55 +0000 (04:28 +0000)
committerJosh Micich <josh@apache.org>
Mon, 29 Sep 2008 04:28:55 +0000 (04:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@699984 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index 2e8dc12b79868ebc9caee4301fcecdbcf3d33b17..79d28bf115e50a4fe59cb80dc2311ce64dafa18a 100644 (file)
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map.Entry;
 
 import org.apache.poi.ss.usermodel.CellStyle;
@@ -54,149 +55,148 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
 
-
 /**
  * Table of styles shared across all sheets in a workbook.
  *
- * @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $
+ * @author ugo
  */
 public class StylesTable implements StylesSource, XSSFModel {
-    private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
-    private final ArrayList<CTFont> fonts = new ArrayList<CTFont>();
-    private final LinkedList<CTFill> fills = new LinkedList<CTFill>();
-    private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
-    private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
-    private final LinkedList<CTXf> xfs = new LinkedList<CTXf>();
-
-    private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>();
-
-    /**
-     * The first style id available for use as a custom style
-     */
-    public static final long FIRST_CUSTOM_STYLE_ID = 165;
-
-    private StyleSheetDocument doc;
-
-    /**
-     * Create a new StylesTable, by reading it from
-     *  the InputStream of a a PackagePart.
-     *
-     * @param is The input stream containing the XML document.
-     * @throws IOException if an error occurs while reading.
-     */
-    public StylesTable(InputStream is) throws IOException {
-        readFrom(is);
-    }
-    /**
-     * Create a new, empty StylesTable
-     */
-    public StylesTable() {
-       doc = StyleSheetDocument.Factory.newInstance();
-       doc.addNewStyleSheet();
-       // Initialization required in order to make the document readable by MSExcel
-       initialize();
-    }
-
-    /**
-     * Read this shared styles table from an XML file.
-     *
-     * @param is The input stream containing the XML document.
-     * @throws IOException if an error occurs while reading.
-     */
-    public void readFrom(InputStream is) throws IOException {
-        try {
-            doc = StyleSheetDocument.Factory.parse(is);
-            // Grab all the different bits we care about
-            if(doc.getStyleSheet().getNumFmts() != null)
-               for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
-                   numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
-               }
-            if(doc.getStyleSheet().getFonts() != null)
-               for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
-                   fonts.add(font);
-               }
-            if(doc.getStyleSheet().getFills() != null)
-               for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
-                   fills.add(fill);
-               }
-            if(doc.getStyleSheet().getBorders() != null)
-               for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
-                   borders.add(border);
-               }
-            if(doc.getStyleSheet().getCellXfs() != null)
-               for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
-                   xfs.add(xf);
-               }
-            if(doc.getStyleSheet().getCellStyleXfs() != null)
-               for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
-                   styleXfs.add(xf);
-               }
-            // dxf
-            if(doc.getStyleSheet().getDxfs() != null)
-               for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) {
-                   dxfs.add(dxf);
-               }
-
-        } catch (XmlException e) {
-            throw new IOException(e.getLocalizedMessage());
-        }
-    }
-
-    // ===========================================================
-    //  Start of style related getters and setters
-    // ===========================================================
-
-    public String getNumberFormatAt(long idx) {
-        return numberFormats.get(idx);
-    }
-    public synchronized long putNumberFormat(String fmt) {
-        if (numberFormats.containsValue(fmt)) {
-               // Find the key, and return that
-               for(Enumeration<Long> keys = numberFormats.keys(); keys.hasMoreElements();) {
-                       Long key = keys.nextElement();
-                       if(numberFormats.get(key).equals(fmt)) {
-                               return key;
-                       }
-               }
-               throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
-        }
-
-        // Find a spare key, and add that
-        long newKey = FIRST_CUSTOM_STYLE_ID;
-        while(numberFormats.containsKey(newKey)) {
-               newKey++;
-        }
-        numberFormats.put(newKey, fmt);
-        return newKey;
-    }
-
-    public Font getFontAt(long idx) {
-       return new XSSFFont(fonts.get((int) idx));
-    }
-
-    public synchronized long putFont(Font font) {
-       return putFont((XSSFFont)font, fonts);
-    }
+       private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
+       private final List<CTFont> fonts = new ArrayList<CTFont>();
+       private final List<CTFill> fills = new LinkedList<CTFill>();
+       private final List<CTBorder> borders = new LinkedList<CTBorder>();
+       private final List<CTXf> styleXfs = new LinkedList<CTXf>();
+       private final List<CTXf> xfs = new LinkedList<CTXf>();
+
+       private final List<CTDxf> dxfs = new LinkedList<CTDxf>();
+
+       /**
+        * The first style id available for use as a custom style
+        */
+       public static final long FIRST_CUSTOM_STYLE_ID = 165;
+
+       private StyleSheetDocument doc;
+
+       /**
+        * Create a new StylesTable, by reading it from
+        *  the InputStream of a a PackagePart.
+        *
+        * @param is The input stream containing the XML document.
+        * @throws IOException if an error occurs while reading.
+        */
+       public StylesTable(InputStream is) throws IOException {
+               readFrom(is);
+       }
+       /**
+        * Create a new, empty StylesTable
+        */
+       public StylesTable() {
+               doc = StyleSheetDocument.Factory.newInstance();
+               doc.addNewStyleSheet();
+               // Initialization required in order to make the document readable by MSExcel
+               initialize();
+       }
+
+       /**
+        * Read this shared styles table from an XML file.
+        *
+        * @param is The input stream containing the XML document.
+        * @throws IOException if an error occurs while reading.
+        */
+       public void readFrom(InputStream is) throws IOException {
+               try {
+                       doc = StyleSheetDocument.Factory.parse(is);
+                       // Grab all the different bits we care about
+                       if(doc.getStyleSheet().getNumFmts() != null)
+                       for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
+                               numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
+                       }
+                       if(doc.getStyleSheet().getFonts() != null)
+                       for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
+                               fonts.add(font);
+                       }
+                       if(doc.getStyleSheet().getFills() != null)
+                       for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
+                               fills.add(fill);
+                       }
+                       if(doc.getStyleSheet().getBorders() != null)
+                       for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
+                               borders.add(border);
+                       }
+                       if(doc.getStyleSheet().getCellXfs() != null)
+                       for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
+                               xfs.add(xf);
+                       }
+                       if(doc.getStyleSheet().getCellStyleXfs() != null)
+                       for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
+                               styleXfs.add(xf);
+                       }
+                       // dxf
+                       if(doc.getStyleSheet().getDxfs() != null)
+                       for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) {
+                               dxfs.add(dxf);
+                       }
+
+               } catch (XmlException e) {
+                       throw new IOException(e.getLocalizedMessage());
+               }
+       }
+
+       // ===========================================================
+       //  Start of style related getters and setters
+       // ===========================================================
+
+       public String getNumberFormatAt(long idx) {
+               return numberFormats.get(idx);
+       }
+       public synchronized long putNumberFormat(String fmt) {
+               if (numberFormats.containsValue(fmt)) {
+                       // Find the key, and return that
+                       for(Enumeration<Long> keys = numberFormats.keys(); keys.hasMoreElements();) {
+                               Long key = keys.nextElement();
+                               if(numberFormats.get(key).equals(fmt)) {
+                                       return key;
+                               }
+                       }
+                       throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
+               }
+
+               // Find a spare key, and add that
+               long newKey = FIRST_CUSTOM_STYLE_ID;
+               while(numberFormats.containsKey(newKey)) {
+                       newKey++;
+               }
+               numberFormats.put(newKey, fmt);
+               return newKey;
+       }
+
+       public Font getFontAt(long idx) {
+               return new XSSFFont(fonts.get((int) idx));
+       }
+
+       public synchronized long putFont(Font font) {
+               return putFont((XSSFFont)font, fonts);
+       }
 
        public XSSFCellStyle getStyleAt(long idx) {
-       int styleXfId = 0;
+               int styleXfId = 0;
 
-       // 0 is the empty default
-       if(xfs.get((int) idx).getXfId() > 0) {
-               styleXfId = (int) xfs.get((int) idx).getXfId();
-       }
+               // 0 is the empty default
+               if(xfs.get((int) idx).getXfId() > 0) {
+                       styleXfId = (int) xfs.get((int) idx).getXfId();
+               }
 
                return new XSSFCellStyle((int) idx, styleXfId, this);
        }
-    public synchronized long putStyle(CellStyle style) {
-       XSSFCellStyle xStyle = (XSSFCellStyle)style;
-       CTXf mainXF = xStyle.getCoreXf();
+       public synchronized long putStyle(CellStyle style) {
+               XSSFCellStyle xStyle = (XSSFCellStyle)style;
+               CTXf mainXF = xStyle.getCoreXf();
 
-       if(! xfs.contains(mainXF)) {
-               xfs.add(mainXF);
-       }
+               if(! xfs.contains(mainXF)) {
+                       xfs.add(mainXF);
+               }
                return xfs.indexOf(mainXF);
-    }
+       }
 
        public XSSFCellBorder getBorderAt(long idx) {
                return new XSSFCellBorder(borders.get((int)idx));
@@ -209,7 +209,7 @@ public class StylesTable implements StylesSource, XSSFModel {
                return new XSSFCellFill(fills.get((int) idx));
        }
        public long putFill(XSSFCellFill fill) {
-               return putFill(fill, fills);
+               return fill.putFill(fills);
        }
 
        public CTXf getCellXfAt(long idx) {
@@ -227,228 +227,217 @@ public class StylesTable implements StylesSource, XSSFModel {
                styleXfs.add(cellStyleXf);
                return styleXfs.size();
        }
-    /**
-     * get the size of cell styles
-     */
-    public int getNumCellStyles(){
-        return styleXfs.size();
-    }
-    /**
-     * get the size of fonts
-     */
-    public int getNumberOfFonts(){
-        return this.fonts.size();
-    }
-    /**
-     * For unit testing only
-     */
-    public int _getNumberFormatSize() {
-       return numberFormats.size();
-    }
-    /**
-     * For unit testing only
-     */
-    public int _getFontsSize() {
-       return fonts.size();
-    }
-    /**
-     * For unit testing only
-     */
-    public int _getFillsSize() {
-       return fills.size();
-    }
-    /**
-     * For unit testing only
-     */
-    public int _getBordersSize() {
-       return borders.size();
-    }
-    /**
-     * For unit testing only
-     */
-    public int _getXfsSize() {
-       return xfs.size();
-    }
-    /**
-     * For unit testing only
-     */
-    public int _getStyleXfsSize() {
-       return styleXfs.size();
-    }
-    /**
-     * For unit testing only!
-     */
-    public CTStylesheet _getRawStylesheet() {
-       return doc.getStyleSheet();
-    }
-
-
-    /**
-     * Write this table out as XML.
-     *
-     * @param out The stream to write to.
-     * @throws IOException if an error occurs while writing.
-     */
-    public void writeTo(OutputStream out) throws IOException {
-        XmlOptions options = new XmlOptions();
-        options.setSaveOuter();
-        options.setUseDefaultNamespace();
-
-        // Requests use of whitespace for easier reading
-        options.setSavePrettyPrint();
-
-
-        // Work on the current one
-        // Need to do this, as we don't handle
-        //  all the possible entries yet
-
-       // Formats
-       CTNumFmts formats = CTNumFmts.Factory.newInstance();
-       formats.setCount(numberFormats.size());
-       for (Entry<Long, String> fmt : numberFormats.entrySet()) {
-               CTNumFmt ctFmt = formats.addNewNumFmt();
-               ctFmt.setNumFmtId(fmt.getKey());
-               ctFmt.setFormatCode(fmt.getValue());
-       }
-       doc.getStyleSheet().setNumFmts(formats);
-
-       // Fonts
-       CTFonts ctFonts = CTFonts.Factory.newInstance();
-       ctFonts.setCount(fonts.size());
-       ctFonts.setFontArray(
-                       fonts.toArray(new CTFont[fonts.size()])
-       );
-       doc.getStyleSheet().setFonts(ctFonts);
-
-       // Fills
-       CTFills ctFills = CTFills.Factory.newInstance();
-       ctFills.setCount(fills.size());
-       ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
-       doc.getStyleSheet().setFills(ctFills);
-
-       // Borders
-       CTBorders ctBorders = CTBorders.Factory.newInstance();
-       ctBorders.setCount(borders.size());
-       ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
-       doc.getStyleSheet().setBorders(ctBorders);
-
-       // Xfs
-       if(xfs.size() > 0) {
-               CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
-               ctXfs.setCount(xfs.size());
-               ctXfs.setXfArray(
-                               xfs.toArray(new CTXf[xfs.size()])
-               );
-               doc.getStyleSheet().setCellXfs(ctXfs);
-       }
-
-       // Style xfs
-       if(styleXfs.size() > 0) {
-               CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
-               ctSXfs.setCount(styleXfs.size());
-               ctSXfs.setXfArray(
-                               styleXfs.toArray(new CTXf[styleXfs.size()])
-               );
-               doc.getStyleSheet().setCellStyleXfs(ctSXfs);
-       }
-
-       // Style dxfs
-       if(dxfs.size() > 0) {
-               CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
-               ctDxfs.setCount(dxfs.size());
-               ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
-               );
-               doc.getStyleSheet().setDxfs(ctDxfs);
-       }
-
-        // Save
-        doc.save(out, options);
-    }
-
-       private long putBorder(XSSFCellBorder border, LinkedList<CTBorder> borders) {
-               return border.putBorder(borders);
-       }
-
-       private long putFill(XSSFCellFill fill, LinkedList<CTFill> fills) {
-               return fill.putFill(fills);
+       /**
+        * get the size of cell styles
+        */
+       public int getNumCellStyles(){
+               return styleXfs.size();
+       }
+       /**
+        * get the size of fonts
+        */
+       public int getNumberOfFonts(){
+               return this.fonts.size();
+       }
+       /**
+        * For unit testing only
+        */
+       public int _getNumberFormatSize() {
+               return numberFormats.size();
+       }
+       /**
+        * For unit testing only
+        */
+       public int _getFontsSize() {
+               return fonts.size();
+       }
+       /**
+        * For unit testing only
+        */
+       public int _getFillsSize() {
+               return fills.size();
+       }
+       /**
+        * For unit testing only
+        */
+       public int _getBordersSize() {
+               return borders.size();
+       }
+       /**
+        * For unit testing only
+        */
+       public int _getXfsSize() {
+               return xfs.size();
+       }
+       /**
+        * For unit testing only
+        */
+       public int _getStyleXfsSize() {
+               return styleXfs.size();
+       }
+       /**
+        * For unit testing only!
+        */
+       public CTStylesheet _getRawStylesheet() {
+               return doc.getStyleSheet();
        }
 
-       private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
-           return font.putFont(fonts);
-    }
 
+       /**
+        * Write this table out as XML.
+        *
+        * @param out The stream to write to.
+        * @throws IOException if an error occurs while writing.
+        */
+       public void writeTo(OutputStream out) throws IOException {
+               XmlOptions options = new XmlOptions();
+               options.setSaveOuter();
+               options.setUseDefaultNamespace();
+
+               // Requests use of whitespace for easier reading
+               options.setSavePrettyPrint();
+
+
+               // Work on the current one
+               // Need to do this, as we don't handle
+               //  all the possible entries yet
+
+               // Formats
+               CTNumFmts formats = CTNumFmts.Factory.newInstance();
+               formats.setCount(numberFormats.size());
+               for (Entry<Long, String> fmt : numberFormats.entrySet()) {
+                       CTNumFmt ctFmt = formats.addNewNumFmt();
+                       ctFmt.setNumFmtId(fmt.getKey());
+                       ctFmt.setFormatCode(fmt.getValue());
+               }
+               doc.getStyleSheet().setNumFmts(formats);
+
+               // Fonts
+               CTFonts ctFonts = CTFonts.Factory.newInstance();
+               ctFonts.setCount(fonts.size());
+               ctFonts.setFontArray(
+                               fonts.toArray(new CTFont[fonts.size()])
+               );
+               doc.getStyleSheet().setFonts(ctFonts);
+
+               // Fills
+               CTFills ctFills = CTFills.Factory.newInstance();
+               ctFills.setCount(fills.size());
+               ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
+               doc.getStyleSheet().setFills(ctFills);
+
+               // Borders
+               CTBorders ctBorders = CTBorders.Factory.newInstance();
+               ctBorders.setCount(borders.size());
+               ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
+               doc.getStyleSheet().setBorders(ctBorders);
+
+               // Xfs
+               if(xfs.size() > 0) {
+                       CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
+                       ctXfs.setCount(xfs.size());
+                       ctXfs.setXfArray(
+                                       xfs.toArray(new CTXf[xfs.size()])
+                       );
+                       doc.getStyleSheet().setCellXfs(ctXfs);
+               }
+
+               // Style xfs
+               if(styleXfs.size() > 0) {
+                       CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
+                       ctSXfs.setCount(styleXfs.size());
+                       ctSXfs.setXfArray(
+                                       styleXfs.toArray(new CTXf[styleXfs.size()])
+                       );
+                       doc.getStyleSheet().setCellStyleXfs(ctSXfs);
+               }
+
+               // Style dxfs
+               if(dxfs.size() > 0) {
+                       CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
+                       ctDxfs.setCount(dxfs.size());
+                       ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
+                       );
+                       doc.getStyleSheet().setDxfs(ctDxfs);
+               }
+
+               // Save
+               doc.save(out, options);
+       }
+
+       private long putBorder(XSSFCellBorder border, List<CTBorder> borders) {
+               return border.putBorder((LinkedList<CTBorder>) borders); // TODO - use List instead of LinkedList
+       }
 
+       private long putFont(XSSFFont font, List<CTFont> fonts) {
+               return font.putFont((ArrayList<CTFont>) fonts); // TODO - use List instead of ArrayList
+       }
 
        private void initialize() {
                //CTFont ctFont = createDefaultFont();
-           XSSFFont xssfFont = createDefaultFont();
-           fonts.add(xssfFont.getCTFont());
+               XSSFFont xssfFont = createDefaultFont();
+               fonts.add(xssfFont.getCTFont());
 
-           CTFill[] ctFill = createDefaultFills();
-           fills.add(ctFill[0]);
-           fills.add(ctFill[1]);
+               CTFill[] ctFill = createDefaultFills();
+               fills.add(ctFill[0]);
+               fills.add(ctFill[1]);
 
-           CTBorder ctBorder = createDefaultBorder();
-           borders.add(ctBorder);
+               CTBorder ctBorder = createDefaultBorder();
+               borders.add(ctBorder);
 
-           CTXf styleXf = createDefaultXf();
-           styleXfs.add(styleXf);
-           CTXf xf = createDefaultXf();
-           xf.setXfId(0);
-           xfs.add(xf);
+               CTXf styleXf = createDefaultXf();
+               styleXfs.add(styleXf);
+               CTXf xf = createDefaultXf();
+               xf.setXfId(0);
+               xfs.add(xf);
        }
 
        private CTXf createDefaultXf() {
-           CTXf ctXf = CTXf.Factory.newInstance();
-           ctXf.setNumFmtId(0);
-           ctXf.setFontId(0);
-           ctXf.setFillId(0);
-           ctXf.setBorderId(0);
-           return ctXf;
+               CTXf ctXf = CTXf.Factory.newInstance();
+               ctXf.setNumFmtId(0);
+               ctXf.setFontId(0);
+               ctXf.setFillId(0);
+               ctXf.setBorderId(0);
+               return ctXf;
        }
        private CTBorder createDefaultBorder() {
-           CTBorder ctBorder = CTBorder.Factory.newInstance();
-           ctBorder.addNewBottom();
-           ctBorder.addNewTop();
-           ctBorder.addNewLeft();
-           ctBorder.addNewRight();
-           ctBorder.addNewDiagonal();
-           return ctBorder;
+               CTBorder ctBorder = CTBorder.Factory.newInstance();
+               ctBorder.addNewBottom();
+               ctBorder.addNewTop();
+               ctBorder.addNewLeft();
+               ctBorder.addNewRight();
+               ctBorder.addNewDiagonal();
+               return ctBorder;
        }
 
 
        private CTFill[] createDefaultFills() {
-           CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
-           ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
-           ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
-           return ctFill;
+               CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
+               ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
+               ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
+               return ctFill;
        }
 
        private XSSFFont createDefaultFont() {
-           CTFont ctFont = CTFont.Factory.newInstance();
-           XSSFFont xssfFont=new XSSFFont(ctFont);
-           xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
-           xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
-           xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
-           xssfFont.setFamily(FontFamily.SWISS);
-           xssfFont.setScheme(FontScheme.MINOR);
-           return xssfFont;
+               CTFont ctFont = CTFont.Factory.newInstance();
+               XSSFFont xssfFont=new XSSFFont(ctFont);
+               xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
+               xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
+               xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
+               xssfFont.setFamily(FontFamily.SWISS);
+               xssfFont.setScheme(FontScheme.MINOR);
+               return xssfFont;
        }
 
-
        public CTDxf getDxf(long idx) {
-           if(dxfs.size()==0)
+               if(dxfs.size()==0)
                return CTDxf.Factory.newInstance();
-           else
+               else
                return dxfs.get((int) idx);
        }
 
-
        public long putDxf(CTDxf dxf) {
-           this.dxfs.add(dxf);
-           return this.dxfs.size();
+               this.dxfs.add(dxf);
+               return this.dxfs.size();
        }
-       
-       
-       
 }
index a390a3646a309370d560ebab390b8c8b2827724c..b61e62d5d9615e3968adcb52ea1777bf53c6f0e3 100755 (executable)
@@ -77,12 +77,14 @@ public enum IndexedColors {
     BROWN(60),
     PLUM(61),
     INDIGO(62),
-    GREY_80_PERCENT(63);
+    GREY_80_PERCENT(63),
+    AUTOMATIC(64),
+    ;
 
-    private short index;
+    private int index;
 
     IndexedColors(int idx){
-        index = (short)idx;
+        index = idx;
     }
 
     /**
@@ -91,6 +93,6 @@ public enum IndexedColors {
      * @return index of this color
      */
     public short getIndex(){
-        return index;
+        return (short)index;
     }
 }
index 5ef5f9f422e64caf72dd6d89a9d55b97aedff09a..53bb41f0f63de8c7f3433b6702858ebc693c98de 100644 (file)
 ==================================================================== */
 package org.apache.poi.xssf.usermodel.extensions;
 
-import java.util.LinkedList;
+import java.util.List;
 
+import org.apache.poi.xssf.usermodel.IndexedColors;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
 
-public class XSSFCellFill {
+public final class XSSFCellFill {
        
-       private CTFill fill;
+       private CTFill _fill;
        
        public XSSFCellFill(CTFill fill) {
-               this.fill = fill;
+               _fill = fill;
        }
        
        public XSSFCellFill() {
-               this.fill = CTFill.Factory.newInstance();
+               _fill = CTFill.Factory.newInstance();
        }
        
        public XSSFColor getFillBackgroundColor() {
-               return new XSSFColor(getPatternFill().getBgColor());
+               CTColor ctColor = getPatternFill().getBgColor();
+               if (ctColor == null) {
+                       XSSFColor result = new XSSFColor();
+                       result.setIndexed(IndexedColors.AUTOMATIC.getIndex());
+                       return result;
+               }
+               return new XSSFColor(ctColor);
        }
 
        public XSSFColor getFillForegroundColor() {
-               return new XSSFColor(getPatternFill().getFgColor());
+               CTColor ctColor = getPatternFill().getFgColor();
+               if (ctColor == null) {
+                       XSSFColor result = new XSSFColor();
+                       result.setIndexed(IndexedColors.AUTOMATIC.getIndex());
+                       return result;
+               }
+               return new XSSFColor(ctColor);
        }
 
        public Enum getPatternType() {
                return getPatternFill().getPatternType();
        }
        
-       public long putFill(LinkedList<CTFill> fills) {
-               if (fills.contains(fill)) {
-                       return fills.indexOf(fill);
+       /**
+        * @return the index of the just added fill
+        */
+       public int putFill(List<CTFill> fills) {
+               if (fills.contains(_fill)) {
+                       return fills.indexOf(_fill);
                }
-               fills.add(fill);
+               fills.add(_fill);
                return fills.size() - 1;
        }
 
        private CTPatternFill getPatternFill() {
-               CTPatternFill patternFill = fill.getPatternFill();
+               CTPatternFill patternFill = _fill.getPatternFill();
                if (patternFill == null) {
-                       patternFill = fill.addNewPatternFill();
+                       patternFill = _fill.addNewPatternFill();
                }
                return patternFill;
        }
 
        public CTFill getCTFill() {
-               return this.fill;
+               return _fill;
        }
        
-        public void setFillBackgroundColor(long index) {
-                CTColor ctColor=getPatternFill().addNewBgColor();
-                ctColor.setIndexed(index);
-                fill.getPatternFill().setBgColor(ctColor);
-        }
+       public void setFillBackgroundColor(long index) {
+               CTColor ctColor=getPatternFill().addNewBgColor();
+               ctColor.setIndexed(index);
+               _fill.getPatternFill().setBgColor(ctColor);
+       }
 
        public void setFillForegroundColor(long index) {
                CTColor ctColor=getPatternFill().addNewFgColor();
                ctColor.setIndexed(index);
-               fill.getPatternFill().setFgColor(ctColor);
+               _fill.getPatternFill().setFgColor(ctColor);
        }
        
-        public void setFillBackgroundRgbColor(XSSFColor color) {
-             fill.getPatternFill().setBgColor(color.getCTColor());
-        }
+       public void setFillBackgroundRgbColor(XSSFColor color) {
+               _fill.getPatternFill().setBgColor(color.getCTColor());
+       }
 
        public void setFillForegroundRgbColor(XSSFColor color) {
-               fill.getPatternFill().setFgColor(color.getCTColor());
+               _fill.getPatternFill().setFgColor(color.getCTColor());
        }
        
        public void setPatternType(Enum patternType) {
                getPatternFill().setPatternType(patternType);
        }
-
-    
 }
index 4a178b7a4d2e3ec24c27984b35500a07edffab43..e1207d681d37ad28b53e829131354f1c62c414ad 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
 import org.apache.poi.ss.usermodel.CellStyle;
@@ -30,328 +31,362 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
 
 public class TestXSSFCellStyle extends TestCase {
 
-    private StylesTable stylesTable;
-    private CTBorder ctBorderA;
-    private CTFill ctFill;
-    private CTFont ctFont;
-    private CTXf cellStyleXf;
-    private CTXf cellXf;
-    private CTCellXfs cellXfs;
-    private XSSFCellStyle cellStyle;
-    private CTStylesheet ctStylesheet;
-
-    public void setUp() {
-        stylesTable = new StylesTable();
-        
-        ctStylesheet = stylesTable._getRawStylesheet();
-        
-        // Until we do XSSFBorder properly, cheat
-        ctBorderA = CTBorder.Factory.newInstance();
-        XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
-        long borderId = stylesTable.putBorder(borderA);
-        assertEquals(1, borderId);
-
-        XSSFCellBorder borderB = new XSSFCellBorder();
-        assertEquals(2, stylesTable.putBorder(borderB));
-
-        ctFill = CTFill.Factory.newInstance();
-        XSSFCellFill fill = new XSSFCellFill(ctFill);
-        long fillId = stylesTable.putFill(fill);
-        assertEquals(2, fillId);
-
-        ctFont = CTFont.Factory.newInstance();
-        XSSFFont font = new XSSFFont(ctFont);
-        long fontId = stylesTable.putFont(font);
-        assertEquals(1, fontId);
-
-        cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
-        cellStyleXf.setBorderId(1);
-        cellStyleXf.setFillId(1);
-        cellStyleXf.setFontId(1);
-        
-        cellXfs = ctStylesheet.addNewCellXfs();
-        cellXf = cellXfs.addNewXf();
-        cellXf.setXfId(1);
-        cellXf.setBorderId(1);
-        cellXf.setFillId(1);
-        cellXf.setFontId(1);
-        stylesTable.putCellStyleXf(cellStyleXf);
-        long id=stylesTable.putCellXf(cellXf);
-        cellStyle = new XSSFCellStyle(1, 1, stylesTable);
-    }
-
-    public void testGetSetBorderBottom() {
-        ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
-        assertEquals((short)1, cellStyle.getBorderBottom());
-        cellStyle.setBorderBottom((short) 2);
-        assertEquals(STBorderStyle.THIN, ctBorderA.getBottom().getStyle());
-        cellStyle.setBorderBottomEnum(STBorderStyle.THICK);
-        assertEquals(6, ctBorderA.getBottom().getStyle().intValue());
-    }
-
-    public void testGetBorderBottomAsString() {
-        ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
-        assertEquals("thin", cellStyle.getBorderBottomAsString());
-    }
-
-    public void testGetSetBorderRight() {
-        ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
-        assertEquals((short)2, cellStyle.getBorderRight());
-        cellStyle.setBorderRight((short) 2);
-        assertEquals(STBorderStyle.THIN, ctBorderA.getRight().getStyle());
-        cellStyle.setBorderRightEnum(STBorderStyle.THICK);
-        assertEquals(6, ctBorderA.getRight().getStyle().intValue());
-    }
-
-    public void testGetBorderRightAsString() {
-        ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
-        assertEquals("medium", cellStyle.getBorderRightAsString());
-    }
-
-    public void testGetSetBorderLeft() {
-        ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
-        assertEquals((short)3, cellStyle.getBorderLeft());
-        cellStyle.setBorderLeft((short) 2);
-        assertEquals(STBorderStyle.THIN, ctBorderA.getLeft().getStyle());
-        cellStyle.setBorderLeftEnum(STBorderStyle.THICK);
-        assertEquals(6, ctBorderA.getLeft().getStyle().intValue());
-    }
-
-    public void testGetBorderLeftAsString() {
-        ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
-        assertEquals("dashed", cellStyle.getBorderLeftAsString());
-    }
-
-    public void testGetSetBorderTop() {
-        ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
-        assertEquals((short)7, cellStyle.getBorderTop());
-        cellStyle.setBorderTop((short) 2);
-        assertEquals(STBorderStyle.THIN, ctBorderA.getTop().getStyle());
-        cellStyle.setBorderTopEnum(STBorderStyle.THICK);
-        assertEquals(6, ctBorderA.getTop().getStyle().intValue());
-    }
-
-    public void testGetBorderTopAsString() {
-        ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
-        assertEquals("hair", cellStyle.getBorderTopAsString());
-    }
-
-    public void testGetSetBottomBorderColor() {
-        CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
-        ctColor.setIndexed(2);
-        XSSFColor color = new XSSFColor(ctColor);
-        assertEquals((short)2, cellStyle.getBottomBorderColor());
-        CTColor anotherCtColor = CTColor.Factory.newInstance();
-        anotherCtColor.setIndexed(4);
-        anotherCtColor.setTheme(3);
-        anotherCtColor.setRgb("1234".getBytes());
-        XSSFColor anotherColor = new XSSFColor(anotherCtColor);
-        cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
-        assertEquals((short)4, cellStyle.getBottomBorderColor());
-        assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
-    }
-
-    public void testGetSetTopBorderColor() {
-        CTColor ctColor = ctBorderA.addNewTop().addNewColor();
-        ctColor.setIndexed(5);
-        XSSFColor color = new XSSFColor(ctColor);
-        assertEquals((short)5, cellStyle.getTopBorderColor());
-        CTColor anotherCtColor = CTColor.Factory.newInstance();
-        anotherCtColor.setIndexed(7);
-        anotherCtColor.setTheme(3);
-        anotherCtColor.setRgb("abcd".getBytes());
-        XSSFColor anotherColor = new XSSFColor(anotherCtColor);
-        cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
-        assertEquals((short)7, cellStyle.getTopBorderColor());
-        assertEquals(new String("abcd".getBytes()), new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
-    }
-
-    public void testGetSetLeftBorderColor() {
-        CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
-        ctColor.setIndexed(2);
-        XSSFColor color = new XSSFColor(ctColor);
-        assertEquals((short)2, cellStyle.getLeftBorderColor());
-        CTColor anotherCtColor = CTColor.Factory.newInstance();
-        anotherCtColor.setIndexed(4);
-        anotherCtColor.setTheme(3);
-        anotherCtColor.setRgb("1234".getBytes());
-        XSSFColor anotherColor = new XSSFColor(anotherCtColor);
-        cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
-        assertEquals((short)4, cellStyle.getLeftBorderColor());
-        assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
-    }
-
-    public void testGetSetRightBorderColor() {
-        CTColor ctColor = ctBorderA.addNewRight().addNewColor();
-        ctColor.setIndexed(8);
-        XSSFColor color = new XSSFColor(ctColor);
-        assertEquals((short)8, cellStyle.getRightBorderColor());
-        CTColor anotherCtColor = CTColor.Factory.newInstance();
-        anotherCtColor.setIndexed(14);
-        anotherCtColor.setTheme(3);
-        anotherCtColor.setRgb("af67".getBytes());
-        XSSFColor anotherColor = new XSSFColor(anotherCtColor);
-        cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
-        assertEquals((short)14, cellStyle.getRightBorderColor());
-        assertEquals(new String("af67".getBytes()), new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
-    }
-
-    public void testGetFillBackgroundColor() {
-       setUp();
-       CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
-        CTColor ctBgColor = ctPatternFill.addNewBgColor();
-        ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
-        ctPatternFill.setBgColor(ctBgColor);
-
-        XSSFCellFill cellFill=new XSSFCellFill(ctFill);
-        long index=stylesTable.putFill(cellFill);
-        ((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
-
-        assertEquals(2,cellStyle.getCoreXf().getFillId());
-        assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
-        
-        cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
-        assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
-        
-        //test rgb color - XSSFColor
-        CTColor ctColor=CTColor.Factory.newInstance();
-        ctColor.setRgb("FFFFFF".getBytes());
-        ctPatternFill.setBgColor(ctColor);
-        assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
-        
-        cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor));
-        assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]);
-        assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]);
-        assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]);
-        assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
-    }
-
-    public void testGetFillForegroundColor() {
-       setUp();
-       CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
-        CTColor ctFgColor = ctPatternFill.addNewFgColor();
-        ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
-        ctPatternFill.setFgColor(ctFgColor);
-
-        XSSFCellFill cellFill=new XSSFCellFill(ctFill);
-        long index=stylesTable.putFill(cellFill);
-        ((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
-
-        assertEquals(2,cellStyle.getCoreXf().getFillId());
-        assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
-        
-        cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
-        assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed());
-        
-        //test rgb color - XSSFColor
-        CTColor ctColor=CTColor.Factory.newInstance();
-        ctColor.setRgb("FFFFFF".getBytes());
-        ctPatternFill.setFgColor(ctColor);
-        assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString());
-        
-        cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
-        assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]);
-        assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]);
-        assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]);
-        assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
-    }
-
-    public void testGetFillPattern() {
-       setUp();
-        CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
-        ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
-        XSSFCellFill cellFill=new XSSFCellFill(ctFill);
-        long index=stylesTable.putFill(cellFill);
-        ((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
+       private static final int AUTO_COLOR_INDEX = 64;
+       private StylesTable stylesTable;
+       private CTBorder ctBorderA;
+       private CTFill ctFill;
+       private CTFont ctFont;
+       private CTXf cellStyleXf;
+       private CTXf cellXf;
+       private CTCellXfs cellXfs;
+       private XSSFCellStyle cellStyle;
+       private CTStylesheet ctStylesheet;
+
+       @Override
+       protected void setUp() {
+               stylesTable = new StylesTable();
+               
+               ctStylesheet = stylesTable._getRawStylesheet();
+               
+               // Until we do XSSFBorder properly, cheat
+               ctBorderA = CTBorder.Factory.newInstance();
+               XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
+               long borderId = stylesTable.putBorder(borderA);
+               assertEquals(1, borderId);
+
+               XSSFCellBorder borderB = new XSSFCellBorder();
+               assertEquals(2, stylesTable.putBorder(borderB));
+
+               ctFill = CTFill.Factory.newInstance();
+               XSSFCellFill fill = new XSSFCellFill(ctFill);
+               long fillId = stylesTable.putFill(fill);
+               assertEquals(2, fillId);
+
+               ctFont = CTFont.Factory.newInstance();
+               XSSFFont font = new XSSFFont(ctFont);
+               long fontId = stylesTable.putFont(font);
+               assertEquals(1, fontId);
+
+               cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
+               cellStyleXf.setBorderId(1);
+               cellStyleXf.setFillId(1);
+               cellStyleXf.setFontId(1);
+               
+               cellXfs = ctStylesheet.addNewCellXfs();
+               cellXf = cellXfs.addNewXf();
+               cellXf.setXfId(1);
+               cellXf.setBorderId(1);
+               cellXf.setFillId(1);
+               cellXf.setFontId(1);
+               stylesTable.putCellStyleXf(cellStyleXf);
+               stylesTable.putCellXf(cellXf);
+               cellStyle = new XSSFCellStyle(1, 1, stylesTable);
+       }
+
+       public void testGetSetBorderBottom() {
+               ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
+               assertEquals((short)1, cellStyle.getBorderBottom());
+               cellStyle.setBorderBottom((short) 2);
+               assertEquals(STBorderStyle.THIN, ctBorderA.getBottom().getStyle());
+               cellStyle.setBorderBottomEnum(STBorderStyle.THICK);
+               assertEquals(6, ctBorderA.getBottom().getStyle().intValue());
+       }
+
+       public void testGetBorderBottomAsString() {
+               ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
+               assertEquals("thin", cellStyle.getBorderBottomAsString());
+       }
+
+       public void testGetSetBorderRight() {
+               ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
+               assertEquals((short)2, cellStyle.getBorderRight());
+               cellStyle.setBorderRight((short) 2);
+               assertEquals(STBorderStyle.THIN, ctBorderA.getRight().getStyle());
+               cellStyle.setBorderRightEnum(STBorderStyle.THICK);
+               assertEquals(6, ctBorderA.getRight().getStyle().intValue());
+       }
+
+       public void testGetBorderRightAsString() {
+               ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
+               assertEquals("medium", cellStyle.getBorderRightAsString());
+       }
+
+       public void testGetSetBorderLeft() {
+               ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
+               assertEquals((short)3, cellStyle.getBorderLeft());
+               cellStyle.setBorderLeft((short) 2);
+               assertEquals(STBorderStyle.THIN, ctBorderA.getLeft().getStyle());
+               cellStyle.setBorderLeftEnum(STBorderStyle.THICK);
+               assertEquals(6, ctBorderA.getLeft().getStyle().intValue());
+       }
+
+       public void testGetBorderLeftAsString() {
+               ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
+               assertEquals("dashed", cellStyle.getBorderLeftAsString());
+       }
+
+       public void testGetSetBorderTop() {
+               ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
+               assertEquals((short)7, cellStyle.getBorderTop());
+               cellStyle.setBorderTop((short) 2);
+               assertEquals(STBorderStyle.THIN, ctBorderA.getTop().getStyle());
+               cellStyle.setBorderTopEnum(STBorderStyle.THICK);
+               assertEquals(6, ctBorderA.getTop().getStyle().intValue());
+       }
+
+       public void testGetBorderTopAsString() {
+               ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
+               assertEquals("hair", cellStyle.getBorderTopAsString());
+       }
+
+       public void testGetSetBottomBorderColor() {
+               CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
+               ctColor.setIndexed(2);
+               assertEquals((short)2, cellStyle.getBottomBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(4);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("1234".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
+               assertEquals((short)4, cellStyle.getBottomBorderColor());
+               assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
+       }
+
+       public void testGetSetTopBorderColor() {
+               CTColor ctColor = ctBorderA.addNewTop().addNewColor();
+               ctColor.setIndexed(5);
+               assertEquals((short)5, cellStyle.getTopBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(7);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("abcd".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
+               assertEquals((short)7, cellStyle.getTopBorderColor());
+               assertEquals("abcd", new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
+       }
+
+       public void testGetSetLeftBorderColor() {
+               CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
+               ctColor.setIndexed(2);
+               assertEquals((short)2, cellStyle.getLeftBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(4);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("1234".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
+               assertEquals((short)4, cellStyle.getLeftBorderColor());
+               assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
+       }
+
+       public void testGetSetRightBorderColor() {
+               CTColor ctColor = ctBorderA.addNewRight().addNewColor();
+               ctColor.setIndexed(8);
+               assertEquals((short)8, cellStyle.getRightBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(14);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("af67".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
+               assertEquals((short)14, cellStyle.getRightBorderColor());
+               assertEquals("af67", new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
+       }
+
+       public void testGetFillBackgroundColor() {
+
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+               CTColor ctBgColor = ctPatternFill.addNewBgColor();
+               ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
+               ctPatternFill.setBgColor(ctBgColor);
+
+               XSSFCellFill cellFill=new XSSFCellFill(ctFill);
+               long index=stylesTable.putFill(cellFill);
+               cellStyle.getCoreXf().setFillId(index);
+
+               assertEquals(2,cellStyle.getCoreXf().getFillId());
+               assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
+               
+               cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
+               assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
+               
+               //test rgb color - XSSFColor
+               CTColor ctColor=CTColor.Factory.newInstance();
+               ctColor.setRgb("FFFFFF".getBytes());
+               ctPatternFill.setBgColor(ctColor);
+               assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
+               
+               cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor));
+               assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]);
+               assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]);
+               assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]);
+               assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
+       }
        
-        assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());
-        
-        cellStyle.setFillPattern(CellStyle.BRICKS);
-        assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue());
-    }
-
-    public void testGetFont() {
-        assertNotNull(cellStyle.getFont());
-    }
-
-    public void testGetSetHidden() {
-        assertFalse(cellStyle.getHidden());
-        cellXf.getProtection().setHidden(true);
-        assertTrue(cellStyle.getHidden());
-        cellStyle.setHidden(false);
-        assertFalse(cellStyle.getHidden());
-    }
-
-    public void testGetSetLocked() {
-        assertFalse(cellStyle.getLocked());
-        cellXf.getProtection().setLocked(true);
-        assertTrue(cellStyle.getLocked());
-        cellStyle.setLocked(false);
-        assertFalse(cellStyle.getLocked());
-    }
-
-    public void testGetSetIndent() {
-        assertEquals((short)0, cellStyle.getIndention());
-        cellXf.getAlignment().setIndent(3);
-        assertEquals((short)3, cellStyle.getIndention());
-        cellStyle.setIndention((short) 13);
-        assertEquals((short)13, cellXf.getAlignment().getIndent());
-    }
-
-    public void testGetSetAlignement() {
-        assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
-        assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
-
-        cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
-        assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
-        assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
-        assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
-
-        cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
-        assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
-        assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
-        assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
-
-        cellStyle.setAlignment(HorizontalAlignment.CENTER);
-        assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
-        assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
-        assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
-    }
-
-    public void testGetSetVerticalAlignment() {
-        assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
-        assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
-        assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
-
-        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
-        assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
-        assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
-        assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
-
-        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
-        assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
-        assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
-        assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
-    }
-
-    public void testGetSetWrapText() {
-        assertFalse(cellStyle.getWrapText());
-        cellXf.getAlignment().setWrapText(true);
-        assertTrue(cellStyle.getWrapText());
-        cellStyle.setWrapText(false);
-        assertFalse(cellXf.getAlignment().getWrapText());
-    }
-
-    /**
-     * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
-     */
-    public void testCloneStyleSameWB() throws Exception {
-        // TODO
-    }
-    /**
-     * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
-     */
-    public void testCloneStyleDiffWB() throws Exception {
-        // TODO
-    }
+       public void testGetFillBackgroundColor_default() {
+
+               XSSFWorkbook wb = new XSSFWorkbook();
+
+               XSSFCellStyle style = wb.createCellStyle();
+
+               short color;
+               try {
+                       color = style.getFillBackgroundColor();
+               } catch (NullPointerException e) {
+                       throw new AssertionFailedError("Identified bug 45898");
+               }
+               assertEquals(AUTO_COLOR_INDEX, color);
+               XSSFColor xcolor=style.getFillBackgroundRgbColor();
+               assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
+       }
+       
+
+       public void testGetFillForegroundColor() {
+
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+               CTColor ctFgColor = ctPatternFill.addNewFgColor();
+               ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
+               ctPatternFill.setFgColor(ctFgColor);
+
+               XSSFCellFill cellFill=new XSSFCellFill(ctFill);
+               long index=stylesTable.putFill(cellFill);
+               cellStyle.getCoreXf().setFillId(index);
+
+               assertEquals(2,cellStyle.getCoreXf().getFillId());
+               assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
+               
+               cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
+               assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed());
+               
+               //test rgb color - XSSFColor
+               CTColor ctColor=CTColor.Factory.newInstance();
+               ctColor.setRgb("FFFFFF".getBytes());
+               ctPatternFill.setFgColor(ctColor);
+               assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString());
+               
+               cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
+               assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]);
+               assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]);
+               assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]);
+               assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
+       }
+       
+       public void testGetFillForegroundColor_default() {
+
+               XSSFWorkbook wb = new XSSFWorkbook();
+
+               XSSFCellStyle style = wb.createCellStyle();
+
+               short color;
+               try {
+                       color = style.getFillForegroundColor();
+               } catch (NullPointerException e) {
+                       throw new AssertionFailedError("Identified bug 45898");
+               }
+               assertEquals(AUTO_COLOR_INDEX, color);
+               XSSFColor xcolor=style.getFillForegroundRgbColor();
+               assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
+       }
+       
+
+       public void testGetFillPattern() {
+
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+               ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
+               XSSFCellFill cellFill=new XSSFCellFill(ctFill);
+               long index=stylesTable.putFill(cellFill);
+               cellStyle.getCoreXf().setFillId(index);
+       
+               assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());
+               
+               cellStyle.setFillPattern(CellStyle.BRICKS);
+               assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue());
+       }
+
+       public void testGetFont() {
+               assertNotNull(cellStyle.getFont());
+       }
+
+       public void testGetSetHidden() {
+               assertFalse(cellStyle.getHidden());
+               cellXf.getProtection().setHidden(true);
+               assertTrue(cellStyle.getHidden());
+               cellStyle.setHidden(false);
+               assertFalse(cellStyle.getHidden());
+       }
+
+       public void testGetSetLocked() {
+               assertFalse(cellStyle.getLocked());
+               cellXf.getProtection().setLocked(true);
+               assertTrue(cellStyle.getLocked());
+               cellStyle.setLocked(false);
+               assertFalse(cellStyle.getLocked());
+       }
+
+       public void testGetSetIndent() {
+               assertEquals((short)0, cellStyle.getIndention());
+               cellXf.getAlignment().setIndent(3);
+               assertEquals((short)3, cellStyle.getIndention());
+               cellStyle.setIndention((short) 13);
+               assertEquals((short)13, cellXf.getAlignment().getIndent());
+       }
+
+       public void testGetSetAlignement() {
+               assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
+               assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
+
+               cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
+               assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
+               assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
+               assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
+
+               cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
+               assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
+               assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
+               assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
+
+               cellStyle.setAlignment(HorizontalAlignment.CENTER);
+               assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
+               assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
+               assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
+       }
+
+       public void testGetSetVerticalAlignment() {
+               assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
+               assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
+               assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
+
+               cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
+               assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
+               assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
+               assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
+
+               cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
+               assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
+               assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
+               assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
+       }
+
+       public void testGetSetWrapText() {
+               assertFalse(cellStyle.getWrapText());
+               cellXf.getAlignment().setWrapText(true);
+               assertTrue(cellStyle.getWrapText());
+               cellStyle.setWrapText(false);
+               assertFalse(cellXf.getAlignment().getWrapText());
+       }
+
+       /**
+        * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
+        */
+       public void testCloneStyleSameWB() throws Exception {
+               // TODO
+       }
+       /**
+        * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
+        */
+       public void testCloneStyleDiffWB() throws Exception {
+               // TODO
+       }
 }
index c781190378113532804bd5dc49350adc3a79a717..38769743c1dccab25554f1f545ba3572b23aaf0e 100644 (file)
 
 package org.apache.poi.xssf.usermodel;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.Name;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.StylesSource;
+import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.StylesTable;
 import org.openxml4j.opc.ContentTypes;
 import org.openxml4j.opc.Package;
@@ -39,323 +40,309 @@ import org.openxml4j.opc.PackagingURIHelper;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
 
-public class TestXSSFWorkbook extends TestCase {
-    public TestXSSFWorkbook(String name) {
-               super(name);
-               
+public final class TestXSSFWorkbook extends TestCase {
+
+       @Override
+       protected void setUp() throws Exception {
                // Use system out logger
-           System.setProperty(
-                   "org.apache.poi.util.POILogger",
-                   "org.apache.poi.util.SystemOutLogger"
-           );
+               System.setProperty(
+                               "org.apache.poi.util.POILogger",
+                               "org.apache.poi.util.SystemOutLogger"
+               );
        }
 
        public void testGetSheetIndex() {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        assertEquals(0, workbook.getSheetIndex(sheet1));
-        assertEquals(0, workbook.getSheetIndex("sheet1"));
-        assertEquals(1, workbook.getSheetIndex(sheet2));
-        assertEquals(1, workbook.getSheetIndex("sheet2"));
-        assertEquals(-1, workbook.getSheetIndex("noSheet"));
-    }
-    
-    public void testSetSheetOrder() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        assertSame(sheet1, workbook.getSheetAt(0));
-        assertSame(sheet2, workbook.getSheetAt(1));
-        workbook.setSheetOrder("sheet2", 0);
-        assertSame(sheet2, workbook.getSheetAt(0));
-        assertSame(sheet1, workbook.getSheetAt(1));
-        // Test reordering of CTSheets
-        CTWorkbook ctwb = workbook.getWorkbook();
-        CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
-        assertEquals("sheet2", ctsheets[0].getName());
-        assertEquals("sheet1", ctsheets[1].getName());
-        
-        // Borderline case: only one sheet
-        workbook = new XSSFWorkbook();
-        sheet1 = workbook.createSheet("sheet1");
-        assertSame(sheet1, workbook.getSheetAt(0));
-        workbook.setSheetOrder("sheet1", 0);
-        assertSame(sheet1, workbook.getSheetAt(0));
-    }
-    
-    public void testSetSelectedTab() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        assertEquals(0, workbook.getSelectedTab());
-        workbook.setSelectedTab((short) 0);
-        assertEquals(0, workbook.getSelectedTab());
-        workbook.setSelectedTab((short) 1);
-        assertEquals(1, workbook.getSelectedTab());
-    }
-    
-    public void testSetSheetName() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        assertEquals("sheet1", workbook.getSheetName(0));
-        workbook.setSheetName(0, "sheet2");
-        assertEquals("sheet2", workbook.getSheetName(0));
-    }
-    
-    public void testCloneSheet() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet");
-        Sheet sheet2 = workbook.cloneSheet(0);
-        assertEquals(2, workbook.getNumberOfSheets());
-        assertEquals("sheet(1)", workbook.getSheetName(1));
-        workbook.setSheetName(1, "clonedsheet");
-        Sheet sheet3 = workbook.cloneSheet(1);
-        assertEquals(3, workbook.getNumberOfSheets());
-        assertEquals("clonedsheet(1)", workbook.getSheetName(2));
-    }
-    
-    public void testGetSheetByName() {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        assertSame(sheet1, workbook.getSheet("sheet1"));
-        assertSame(sheet2, workbook.getSheet("sheet2"));
-        assertNull(workbook.getSheet("nosheet"));
-    }
-    
-    public void testRemoveSheetAt() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        Sheet sheet3 = workbook.createSheet("sheet3");
-        workbook.removeSheetAt(1);
-        assertEquals(2, workbook.getNumberOfSheets());
-        assertEquals("sheet3", workbook.getSheetName(1));
-        workbook.removeSheetAt(0);
-        assertEquals(1, workbook.getNumberOfSheets());
-        assertEquals("sheet3", workbook.getSheetName(0));
-        workbook.removeSheetAt(0);
-        assertEquals(0, workbook.getNumberOfSheets());
-    }
-    
-    /**
-     * Tests that we can save a new document
-     */
-    public void testSaveNew() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        Sheet sheet3 = workbook.createSheet("sheet3");
-        File file = File.createTempFile("poi-", ".xlsx");
-        System.out.println("Saving newly created file to " + file.getAbsolutePath());
-        OutputStream out = new FileOutputStream(file);
-        workbook.write(out);
-        out.close();
-    }
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               Sheet sheet1 = workbook.createSheet("sheet1");
+               Sheet sheet2 = workbook.createSheet("sheet2");
+               assertEquals(0, workbook.getSheetIndex(sheet1));
+               assertEquals(0, workbook.getSheetIndex("sheet1"));
+               assertEquals(1, workbook.getSheetIndex(sheet2));
+               assertEquals(1, workbook.getSheetIndex("sheet2"));
+               assertEquals(-1, workbook.getSheetIndex("noSheet"));
+       }
+       
+       public void testSetSheetOrder() {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               Sheet sheet1 = workbook.createSheet("sheet1");
+               Sheet sheet2 = workbook.createSheet("sheet2");
+               assertSame(sheet1, workbook.getSheetAt(0));
+               assertSame(sheet2, workbook.getSheetAt(1));
+               workbook.setSheetOrder("sheet2", 0);
+               assertSame(sheet2, workbook.getSheetAt(0));
+               assertSame(sheet1, workbook.getSheetAt(1));
+               // Test reordering of CTSheets
+               CTWorkbook ctwb = workbook.getWorkbook();
+               CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
+               assertEquals("sheet2", ctsheets[0].getName());
+               assertEquals("sheet1", ctsheets[1].getName());
+               
+               // Borderline case: only one sheet
+               workbook = new XSSFWorkbook();
+               sheet1 = workbook.createSheet("sheet1");
+               assertSame(sheet1, workbook.getSheetAt(0));
+               workbook.setSheetOrder("sheet1", 0);
+               assertSame(sheet1, workbook.getSheetAt(0));
+       }
+       
+       public void testSetSelectedTab() {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               workbook.createSheet("sheet1");
+               workbook.createSheet("sheet2");
+               assertEquals(0, workbook.getSelectedTab());
+               workbook.setSelectedTab((short) 0);
+               assertEquals(0, workbook.getSelectedTab());
+               workbook.setSelectedTab((short) 1);
+               assertEquals(1, workbook.getSelectedTab());
+       }
+       
+       public void testSetSheetName() {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               workbook.createSheet("sheet1");
+               assertEquals("sheet1", workbook.getSheetName(0));
+               workbook.setSheetName(0, "sheet2");
+               assertEquals("sheet2", workbook.getSheetName(0));
+       }
+       
+       public void testCloneSheet() {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               workbook.createSheet("sheet");
+               workbook.cloneSheet(0);
+               assertEquals(2, workbook.getNumberOfSheets());
+               assertEquals("sheet(1)", workbook.getSheetName(1));
+               workbook.setSheetName(1, "clonedsheet");
+               workbook.cloneSheet(1);
+               assertEquals(3, workbook.getNumberOfSheets());
+               assertEquals("clonedsheet(1)", workbook.getSheetName(2));
+       }
+       
+       public void testGetSheetByName() {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               Sheet sheet1 = workbook.createSheet("sheet1");
+               Sheet sheet2 = workbook.createSheet("sheet2");
+               assertSame(sheet1, workbook.getSheet("sheet1"));
+               assertSame(sheet2, workbook.getSheet("sheet2"));
+               assertNull(workbook.getSheet("nosheet"));
+       }
+       
+       public void testRemoveSheetAt() {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               workbook.createSheet("sheet1");
+               workbook.createSheet("sheet2");
+               workbook.createSheet("sheet3");
+               workbook.removeSheetAt(1);
+               assertEquals(2, workbook.getNumberOfSheets());
+               assertEquals("sheet3", workbook.getSheetName(1));
+               workbook.removeSheetAt(0);
+               assertEquals(1, workbook.getNumberOfSheets());
+               assertEquals("sheet3", workbook.getSheetName(0));
+               workbook.removeSheetAt(0);
+               assertEquals(0, workbook.getNumberOfSheets());
+       }
+       
+       /**
+        * Tests that we can save a new document
+        */
+       public void testSaveNew() throws IOException {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               workbook.createSheet("sheet1");
+               workbook.createSheet("sheet2");
+               workbook.createSheet("sheet3");
+               File file = File.createTempFile("poi-", ".xlsx");
+               System.out.println("Saving newly created file to " + file.getAbsolutePath());
+               OutputStream out = new FileOutputStream(file);
+               workbook.write(out);
+               out.close();
+       }
 
-    /**
-     * Tests that we can save, and then re-load a new document
-     */
-    public void testSaveLoadNew() throws Exception {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet1 = workbook.createSheet("sheet1");
-        Sheet sheet2 = workbook.createSheet("sheet2");
-        Sheet sheet3 = workbook.createSheet("sheet3");
-        
-        RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
-        
-        sheet1.createRow(0).createCell((short)0).setCellValue(1.2);
-        sheet1.createRow(1).createCell((short)0).setCellValue(rts);
-        sheet2.createRow(0);
-        
-        assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
-        assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
-        assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
-        assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
-        assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
-        assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
-        
-        File file = File.createTempFile("poi-", ".xlsx");
-        OutputStream out = new FileOutputStream(file);
-        workbook.write(out);
-        out.close();
-        
-        // Check the package contains what we'd expect it to
-        Package pkg = Package.open(file.toString());
-        PackagePart wbRelPart = 
-               pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
-        assertNotNull(wbRelPart);
-        assertTrue(wbRelPart.isRelationshipPart());
-        assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
-        
-        PackagePart wbPart = 
-               pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
-        // Links to the three sheets, shared strings and styles
-        assertTrue(wbPart.hasRelationships());
-        assertEquals(5, wbPart.getRelationships().size());
-        
-        // Load back the XSSFWorkbook
-        workbook = new XSSFWorkbook(pkg);
-        assertEquals(3, workbook.getNumberOfSheets());
-        assertNotNull(workbook.getSheetAt(0));
-        assertNotNull(workbook.getSheetAt(1));
-        assertNotNull(workbook.getSheetAt(2));
-        
-        assertNotNull(workbook.getSharedStringSource());
-        assertNotNull(workbook.getStylesSource());
-        
-        assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
-        assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
-        assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
-        assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
-        assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
-        assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
-        
-        sheet1 = workbook.getSheetAt(0);
-        assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
-        assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
-    }
-    
-    public void testExisting() throws Exception {
-               File xml = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "Formatting.xlsx"
-               );
-               assertTrue(xml.exists());
-       
-               XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+       /**
+        * Tests that we can save, and then re-load a new document
+        */
+       public void testSaveLoadNew() throws Exception {
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               Sheet sheet1 = workbook.createSheet("sheet1");
+               Sheet sheet2 = workbook.createSheet("sheet2");
+               workbook.createSheet("sheet3");
+               
+               RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
+               
+               sheet1.createRow(0).createCell((short)0).setCellValue(1.2);
+               sheet1.createRow(1).createCell((short)0).setCellValue(rts);
+               sheet2.createRow(0);
+               
+               assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
+               assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
+               assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
+               assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
+               assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
+               assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
+               
+               File file = File.createTempFile("poi-", ".xlsx");
+               OutputStream out = new FileOutputStream(file);
+               workbook.write(out);
+               out.close();
+               
+               // Check the package contains what we'd expect it to
+               Package pkg = Package.open(file.toString());
+               PackagePart wbRelPart = 
+                       pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
+               assertNotNull(wbRelPart);
+               assertTrue(wbRelPart.isRelationshipPart());
+               assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
+               
+               PackagePart wbPart = 
+                       pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
+               // Links to the three sheets, shared strings and styles
+               assertTrue(wbPart.hasRelationships());
+               assertEquals(5, wbPart.getRelationships().size());
+               
+               // Load back the XSSFWorkbook
+               workbook = new XSSFWorkbook(pkg);
+               assertEquals(3, workbook.getNumberOfSheets());
+               assertNotNull(workbook.getSheetAt(0));
+               assertNotNull(workbook.getSheetAt(1));
+               assertNotNull(workbook.getSheetAt(2));
+               
+               assertNotNull(workbook.getSharedStringSource());
+               assertNotNull(workbook.getStylesSource());
+               
+               assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
+               assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
+               assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
+               assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
+               assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
+               assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
+               
+               sheet1 = workbook.getSheetAt(0);
+               assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
+               assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
+       }
+       
+       public void testExisting() throws Exception {
+               
+               XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
                assertNotNull(workbook.getSharedStringSource());
                assertNotNull(workbook.getStylesSource());
                
                // And check a few low level bits too
-               Package pkg = Package.open(xml.toString());
-        PackagePart wbPart = 
-               pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
-        
-        // Links to the three sheets, shared, styles and themes
-        assertTrue(wbPart.hasRelationships());
-        assertEquals(6, wbPart.getRelationships().size());
+               Package pkg = Package.open(HSSFTestDataSamples.openSampleFileStream("Formatting.xlsx"));
+               PackagePart wbPart = 
+                       pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
+               
+               // Links to the three sheets, shared, styles and themes
+               assertTrue(wbPart.hasRelationships());
+               assertEquals(6, wbPart.getRelationships().size());
 
-    }
-    
-    public void testFindFont(){
-        //get default font and check against default value
-       XSSFWorkbook workbook = new XSSFWorkbook();
-       Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, IndexedColors.BLACK.getIndex(), (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE);
-       assertNotNull(fontFind);
-       
-       //get default font, then change 2 values and check against different values (height changes)
-       Font font=workbook.createFont();
-       ((XSSFFont)font).setBold(true);
-       font.setUnderline(Font.U_DOUBLE);
-       StylesSource styleSource=new StylesTable();
-       long index=styleSource.putFont(font);
-       System.out.println("index="+index);
-       workbook.setStylesSource(styleSource);
-       fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
-        assertNull(fontFind);
-    }
+       }
+       
+       public void testFindFont(){
+               //get default font and check against default value
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, IndexedColors.BLACK.getIndex(), (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE);
+               assertNotNull(fontFind);
+               
+               //get default font, then change 2 values and check against different values (height changes)
+               Font font=workbook.createFont();
+               ((XSSFFont)font).setBold(true);
+               font.setUnderline(Font.U_DOUBLE);
+               StylesSource styleSource=new StylesTable();
+               long index=styleSource.putFont(font);
+               System.out.println("index="+index);
+               workbook.setStylesSource(styleSource);
+               fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
+               assertNull(fontFind);
+       }
 
-    public void testGetCellStyleAt(){
-       XSSFWorkbook workbook = new XSSFWorkbook();
-        short i = 0;
-        //get default style
-        CellStyle cellStyleAt = workbook.getCellStyleAt(i);
-        assertNotNull(cellStyleAt);
-        
-        //get custom style
-        StylesSource styleSource = workbook.getStylesSource();
-        CellStyle customStyle = new XSSFCellStyle(styleSource);
-        Font font = new XSSFFont();
-        font.setFontName("Verdana");
-        customStyle.setFont(font);
-        Long x = styleSource.putStyle(customStyle);
-        cellStyleAt = workbook.getCellStyleAt(x.shortValue());
-        assertNotNull(cellStyleAt);        
-    }
-    
-    public void testGetFontAt(){
-       XSSFWorkbook workbook = new XSSFWorkbook();
-        StylesSource styleSource = workbook.getStylesSource();
-        short i = 0;
-        //get default font
-        Font fontAt = workbook.getFontAt(i);
-        assertNotNull(fontAt);
-        
-        //get customized font
-        Font customFont = new XSSFFont();
-        customFont.setItalic(true);
-        Long x = styleSource.putFont(customFont);
-        fontAt = workbook.getFontAt(x.shortValue());
-        assertNotNull(fontAt);
-    }
-    
-    public void testGetNumberOfFonts(){
-       XSSFWorkbook wb = new XSSFWorkbook();
+       public void testGetCellStyleAt(){
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               short i = 0;
+               //get default style
+               CellStyle cellStyleAt = workbook.getCellStyleAt(i);
+               assertNotNull(cellStyleAt);
+               
+               //get custom style
+               StylesSource styleSource = workbook.getStylesSource();
+               CellStyle customStyle = new XSSFCellStyle(styleSource);
+               Font font = new XSSFFont();
+               font.setFontName("Verdana");
+               customStyle.setFont(font);
+               Long x = styleSource.putStyle(customStyle);
+               cellStyleAt = workbook.getCellStyleAt(x.shortValue());
+               assertNotNull(cellStyleAt);             
+       }
+       
+       public void testGetFontAt(){
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               StylesSource styleSource = workbook.getStylesSource();
+               short i = 0;
+               //get default font
+               Font fontAt = workbook.getFontAt(i);
+               assertNotNull(fontAt);
+               
+               //get customized font
+               Font customFont = new XSSFFont();
+               customFont.setItalic(true);
+               Long x = styleSource.putFont(customFont);
+               fontAt = workbook.getFontAt(x.shortValue());
+               assertNotNull(fontAt);
+       }
+       
+       public void testGetNumberOfFonts(){
+               XSSFWorkbook wb = new XSSFWorkbook();
 
-        XSSFFont f1=wb.createFont();
-       f1.setBold(true);
-       wb.createCellStyle().setFont(f1);
+               XSSFFont f1=wb.createFont();
+               f1.setBold(true);
+               wb.createCellStyle().setFont(f1);
 
-        XSSFFont f2=wb.createFont();
-       f2.setUnderline(Font.U_DOUBLE);
-        wb.createCellStyle().setFont(f2);
+               XSSFFont f2=wb.createFont();
+               f2.setUnderline(Font.U_DOUBLE);
+               wb.createCellStyle().setFont(f2);
 
-        XSSFFont f3=wb.createFont();
-       f3.setFontHeightInPoints((short)23);
-        wb.createCellStyle().setFont(f3);
+               XSSFFont f3=wb.createFont();
+               f3.setFontHeightInPoints((short)23);
+               wb.createCellStyle().setFont(f3);
 
-        assertEquals(4,wb.getNumberOfFonts());
-       assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
-    }
-    
-    public void testGetNumCellStyles(){
-       XSSFWorkbook workbook = new XSSFWorkbook();
-        short i = workbook.getNumCellStyles();
-        //get default cellStyles
-        assertEquals(1, i);
-        //get wrong value
-        assertNotSame(2, i);        
-    }
-    
-    public void testGetDisplayedTab(){
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        short i = (short) workbook.getFirstVisibleTab();
-        //get default diplayedTab
-        assertEquals(0, i);        
-    }
-    
-    public void testSetDisplayedTab(){
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        workbook.setFirstVisibleTab(new Integer(1).shortValue());
-        short i = (short) workbook.getFirstVisibleTab();
-        //0 (defualt value) is not longer set
-        assertNotSame(0, i);
-        //1 is the default tab
-        assertEquals(1, i);
-    }
-    
-    
-    public void testLoadSave() throws Exception {
-               File xml = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "Formatting.xlsx"
-               );
-               assertTrue(xml.exists());
-       
-               XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+               assertEquals(4,wb.getNumberOfFonts());
+               assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
+       }
+       
+       public void testGetNumCellStyles(){
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               short i = workbook.getNumCellStyles();
+               //get default cellStyles
+               assertEquals(1, i);
+               //get wrong value
+               assertNotSame(2, i);            
+       }
+       
+       public void testGetDisplayedTab(){
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               short i = (short) workbook.getFirstVisibleTab();
+               //get default diplayedTab
+               assertEquals(0, i);             
+       }
+       
+       public void testSetDisplayedTab(){
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               workbook.setFirstVisibleTab(new Integer(1).shortValue());
+               short i = (short) workbook.getFirstVisibleTab();
+               //0 (defualt value) is not longer set
+               assertNotSame(0, i);
+               //1 is the default tab
+               assertEquals(1, i);
+       }
+       
+       
+       public void testLoadSave() {
+               XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
                assertEquals(3, workbook.getNumberOfSheets());
                assertEquals("dd/mm/yyyy", workbook.getSheetAt(0).getRow(1).getCell(0).getRichStringCellValue().getString());
                assertNotNull(workbook.getSharedStringSource());
                assertNotNull(workbook.getStylesSource());
                
                // Write out, and check
-               File tmpFile = File.createTempFile("poi-tmp", ".xlsx");
-               workbook.write(new FileOutputStream(tmpFile));
-               
                // Load up again, check all still there
-               XSSFWorkbook wb2 = new XSSFWorkbook(tmpFile.toString());
+               XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
                assertEquals(3, wb2.getNumberOfSheets());
                assertNotNull(wb2.getSheetAt(0));
                assertNotNull(wb2.getSheetAt(1));
@@ -367,16 +354,10 @@ public class TestXSSFWorkbook extends TestCase {
                assertEquals("yy/mm/dd", wb2.getSheetAt(0).getRow(4).getCell(0).getRichStringCellValue().getString());
                assertNotNull(wb2.getSharedStringSource());
                assertNotNull(wb2.getStylesSource());
-    }
-    
-    public void testStyles() throws Exception {
-               File xml = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "Formatting.xlsx"
-               );
-               assertTrue(xml.exists());
-       
-               XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+       }
+       
+       public void testStyles() {
+               XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
                
                StylesSource ss = workbook.getStylesSource();
                assertNotNull(ss);
@@ -403,12 +384,9 @@ public class TestXSSFWorkbook extends TestCase {
                
                
                // Save, load back in again, and check
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        workbook.write(baos);
-        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-        workbook = new XSSFWorkbook(Package.open(bais));
-        
-        ss = workbook.getStylesSource();
+               workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+               
+               ss = workbook.getStylesSource();
                assertNotNull(ss);
                assertTrue(ss instanceof StylesTable);
                st = (StylesTable)ss;
@@ -417,75 +395,96 @@ public class TestXSSFWorkbook extends TestCase {
                assertEquals(2, st._getFontsSize());
                assertEquals(2, st._getFillsSize());
                assertEquals(1, st._getBordersSize());
-    }
-    
-    public void testNamedRanges() throws Exception {
-       // First up, a new file
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        assertEquals(0, workbook.getNumberOfNames());
-        
-        Name nameA = workbook.createName();
-        nameA.setReference("A2");
-        nameA.setNameName("ForA2");
-        
-        XSSFName nameB = workbook.createName();
-        nameB.setReference("B3");
-        nameB.setNameName("ForB3");
-        nameB.setComment("B3 Comment");
-        
-        // Save and re-load
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        workbook.write(baos);
-        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-        workbook = new XSSFWorkbook(Package.open(bais));
-        
-        assertEquals(2, workbook.getNumberOfNames());
-        assertEquals("A2", workbook.getNameAt(0).getReference());
-        assertEquals("ForA2", workbook.getNameAt(0).getNameName());
-        assertNull(workbook.getNameAt(0).getComment());
-        
-        assertEquals("B3", workbook.getNameAt(1).getReference());
-        assertEquals("ForB3", workbook.getNameAt(1).getNameName());
-        assertEquals("B3 Comment", workbook.getNameAt(1).getComment());
-        
-        assertEquals("ForA2", workbook.getNameName(0));
-        assertEquals(1, workbook.getNameIndex("ForB3"));
-        assertEquals(-1, workbook.getNameIndex("ForB3!!"));
-       
-        
-        // Now, an existing file with named ranges
-               File xml = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "WithVariousData.xlsx"
-               );
-               assertTrue(xml.exists());
-       
-               workbook = new XSSFWorkbook(xml.toString());
-               
-        assertEquals(2, workbook.getNumberOfNames());
-        assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
-        assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
-        assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
-        
-        assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
-        assertEquals("AllBStrings", workbook.getNameAt(1).getNameName());
-        assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
-        
-        // Tweak, save, and re-check
-        workbook.getNameAt(1).setNameName("BStringsFun");
-        
-        baos = new ByteArrayOutputStream();
-        workbook.write(baos);
-        bais = new ByteArrayInputStream(baos.toByteArray());
-        workbook = new XSSFWorkbook(Package.open(bais));
-               
-        assertEquals(2, workbook.getNumberOfNames());
-        assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
-        assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
-        assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
-        
-        assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
-        assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
-        assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
-    }
+       }
+       
+       public void testNamedRanges() {
+               // First up, a new file
+               XSSFWorkbook workbook = new XSSFWorkbook();
+               assertEquals(0, workbook.getNumberOfNames());
+               
+               Name nameA = workbook.createName();
+               nameA.setReference("A2");
+               nameA.setNameName("ForA2");
+               
+               XSSFName nameB = workbook.createName();
+               nameB.setReference("B3");
+               nameB.setNameName("ForB3");
+               nameB.setComment("B3 Comment");
+               
+               // Save and re-load
+               workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+               
+               assertEquals(2, workbook.getNumberOfNames());
+               assertEquals("A2", workbook.getNameAt(0).getReference());
+               assertEquals("ForA2", workbook.getNameAt(0).getNameName());
+               assertNull(workbook.getNameAt(0).getComment());
+               
+               assertEquals("B3", workbook.getNameAt(1).getReference());
+               assertEquals("ForB3", workbook.getNameAt(1).getNameName());
+               assertEquals("B3 Comment", workbook.getNameAt(1).getComment());
+               
+               assertEquals("ForA2", workbook.getNameName(0));
+               assertEquals(1, workbook.getNameIndex("ForB3"));
+               assertEquals(-1, workbook.getNameIndex("ForB3!!"));
+               
+               
+               // Now, an existing file with named ranges
+               workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
+               
+               assertEquals(2, workbook.getNumberOfNames());
+               assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
+               assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
+               assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
+               
+               assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
+               assertEquals("AllBStrings", workbook.getNameAt(1).getNameName());
+               assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
+               
+               // Tweak, save, and re-check
+               workbook.getNameAt(1).setNameName("BStringsFun");
+               
+               workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+               
+               assertEquals(2, workbook.getNumberOfNames());
+               assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
+               assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
+               assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
+               
+               assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
+               assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
+               assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
+       }
+       
+       public void testDuplicateNames() {
+
+               XSSFWorkbook wb = new XSSFWorkbook();
+               wb.createSheet("Sheet1");
+               wb.createSheet();
+               wb.createSheet("name1");
+               try {
+               wb.createSheet("name1");
+                       fail();
+               } catch (IllegalArgumentException e) {
+                       assertEquals("The workbook already contains a sheet of this name", e.getMessage());
+               }
+
+               wb.createSheet();
+
+               try {
+                       wb.setSheetName(3, "name1");
+                       fail();
+               } catch (IllegalArgumentException e) {
+                       assertEquals("The workbook already contains a sheet of this name", e.getMessage());
+               }
+
+               try {
+                       wb.setSheetName(3, "Sheet1");
+                       fail();
+               } catch (IllegalArgumentException e) {
+                       assertEquals("The workbook already contains a sheet of this name", e.getMessage());
+               }
+
+               wb.setSheetName(3, "name2");
+               wb.setSheetName(3, "Sheet3");
+       }
 }