001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.poi.hssf.model.Sheet;
026:
027: /**
028: * Tests HSSFWorkbook method setSheetOrder()
029: *
030: *
031: * @author Ruel Loehr (loehr1 at us.ibm.com)
032: */
033:
034: public class TestHSSFSheetOrder extends TestCase {
035: public TestHSSFSheetOrder(String s) {
036: super (s);
037: }
038:
039: /**
040: * Test the sheet set order method
041: */
042:
043: public void testBackupRecord() throws Exception {
044: HSSFWorkbook wb = new HSSFWorkbook();
045:
046: for (int i = 0; i < 10; i++) {
047: HSSFSheet s = wb.createSheet("Sheet " + i);
048: Sheet sheet = s.getSheet();
049: }
050:
051: // Check the initial order
052: assertEquals(0, wb.getSheetIndex("Sheet 0"));
053: assertEquals(1, wb.getSheetIndex("Sheet 1"));
054: assertEquals(2, wb.getSheetIndex("Sheet 2"));
055: assertEquals(3, wb.getSheetIndex("Sheet 3"));
056: assertEquals(4, wb.getSheetIndex("Sheet 4"));
057: assertEquals(5, wb.getSheetIndex("Sheet 5"));
058: assertEquals(6, wb.getSheetIndex("Sheet 6"));
059: assertEquals(7, wb.getSheetIndex("Sheet 7"));
060: assertEquals(8, wb.getSheetIndex("Sheet 8"));
061: assertEquals(9, wb.getSheetIndex("Sheet 9"));
062:
063: // Change
064: wb.getWorkbook().setSheetOrder("Sheet 6", 0);
065: wb.getWorkbook().setSheetOrder("Sheet 3", 7);
066: wb.getWorkbook().setSheetOrder("Sheet 1", 9);
067:
068: // Check they're currently right
069: assertEquals(0, wb.getSheetIndex("Sheet 6"));
070: assertEquals(1, wb.getSheetIndex("Sheet 0"));
071: assertEquals(2, wb.getSheetIndex("Sheet 2"));
072: assertEquals(3, wb.getSheetIndex("Sheet 4"));
073: assertEquals(4, wb.getSheetIndex("Sheet 5"));
074: assertEquals(5, wb.getSheetIndex("Sheet 7"));
075: assertEquals(6, wb.getSheetIndex("Sheet 3"));
076: assertEquals(7, wb.getSheetIndex("Sheet 8"));
077: assertEquals(8, wb.getSheetIndex("Sheet 9"));
078: assertEquals(9, wb.getSheetIndex("Sheet 1"));
079:
080: // Read it in and see if it is correct.
081: ByteArrayOutputStream baos = new ByteArrayOutputStream();
082: wb.write(baos);
083: ByteArrayInputStream bais = new ByteArrayInputStream(baos
084: .toByteArray());
085: HSSFWorkbook wbr = new HSSFWorkbook(bais);
086:
087: assertEquals(0, wbr.getSheetIndex("Sheet 6"));
088: assertEquals(1, wbr.getSheetIndex("Sheet 0"));
089: assertEquals(2, wbr.getSheetIndex("Sheet 2"));
090: assertEquals(3, wbr.getSheetIndex("Sheet 4"));
091: assertEquals(4, wbr.getSheetIndex("Sheet 5"));
092: assertEquals(5, wbr.getSheetIndex("Sheet 7"));
093: assertEquals(6, wbr.getSheetIndex("Sheet 3"));
094: assertEquals(7, wbr.getSheetIndex("Sheet 8"));
095: assertEquals(8, wbr.getSheetIndex("Sheet 9"));
096: assertEquals(9, wbr.getSheetIndex("Sheet 1"));
097:
098: // Now get the index by the sheet, not the name
099: for (int i = 0; i < 10; i++) {
100: HSSFSheet s = wbr.getSheetAt(i);
101: assertEquals(i, wbr.getSheetIndex(s));
102: }
103: }
104: }
|