/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ package org.apache.poi.hssf.usermodel; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Random; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.TestNumberFormatIndexRecord; import junit.framework.TestCase; /** * This tests the HSSFCell to see if it is ok. * * @author Donald S. Bell */ public class TestHSSFCell extends TestCase { HSSFWorkbook a_workbook; /** * Constructor for TestHSSFCell. * * @param arg0 */ public TestHSSFCell(String arg0) { super(arg0); } /** * This method returns a cell from a new * workbook. */ private HSSFCell getNewCell() { // Declare the return value here so we // have it. HSSFCell cellRV = null; // Get a new workbook HSSFWorkbook workbook = new HSSFWorkbook(); // Create a sheet in the workbook because // by default a workbook doesn't have one. HSSFSheet sheet = workbook.createSheet(); // Now get a new row. HSSFRow row = sheet.createRow((short) 0); // Now get the new cell. cellRV = row.createCell((short) 0); return cellRV; } /** * This makes sure an invalid "Excel date" returns null * instead of the date. */ private void testInvalidDateValue(Date valueToTest) { // Get a new cell and see if we can // just set it's value to a date // and make sure we get that value // back. HSSFCell cell = getNewCell(); cell.setCellValue(valueToTest); Date returnedValue = cell.getDateCellValue(); if (returnedValue != null) { String message = "Did not return a null value. "; message += " The returned value was " + returnedValue + "."; fail(message); } } private void testGetBooleanValue(boolean valueToTest) { // Get a new cell and see if we can // just set it's value to a boolean // and make sure we get that value // back. HSSFCell cell = getNewCell(); cell.setCellValue(valueToTest); boolean returnedValue = cell.getBooleanCellValue(); if (valueToTest != returnedValue) { String message = "Did not return the expected boolean value. "; message += "The expected return value was " + valueToTest; message += " the returned value was " + returnedValue + "."; fail(message); } } private void testGetDateValue(Date valueToTest) { // Get a new cell and see if we can // just set it's value to a date // and make sure we get that value // back. HSSFCell cell = getNewCell(); cell.setCellValue(valueToTest); Date returnedValue = cell.getDateCellValue(); if (valueToTest.equals(returnedValue) == false) { String message = "Did not return the expected date value. "; message += "The expected return value was " + valueToTest; message += " the returned value was " + returnedValue + "."; fail(message); } } private void testGetNumericValue(double valueToTest) { // Get a new cell and see if we can // just set it's value to a double // and make sure we get that value // back. HSSFCell cell = getNewCell(); cell.setCellValue(valueToTest); double returnedValue = cell.getNumericCellValue(); if (valueToTest != returnedValue) { String message = "Did not return the expected numeric value. "; message += "The expected return value was " + valueToTest; message += " the returned value was " + returnedValue + "."; fail(message); } } private void testGetStringValue(String valueToTest) { // Get a new cell and see if we can // just set it's value to a String // and make sure we get that value // back. HSSFCell cell = getNewCell(); cell.setCellValue(valueToTest); String returnedValue = cell.getStringCellValue(); if (valueToTest.equals(returnedValue) == false) { String message = "Did not return the expected String value. "; message += "The expected return value was \"" + valueToTest + "\""; message += " the returned value was \"" + returnedValue + "\"."; fail(message); } } /** * This tests that setting an cell value to a date * that is not a valid Excel Date does not return * this date object. */ public void testInvalidExcelDateCellValues() { // Just some interesting dates that are found on // a calendar. Calendar cal = Calendar.getInstance(); // Special Excel date because it is // before 1900. cal.set(1899, 11, 31, 23, 59); testInvalidDateValue(cal.getTime()); // Some more invalid dates - this just tests // the function some more. // US declares independance cal.set(1776, 6, 4); testInvalidDateValue(cal.getTime()); } public void testGetNumericCellValue() { // We do a loop of ten random numbers // this isn't completely scientific, but // it pretty much checks to make sure we // don't accidently hardcode something. Random numberGenerator = new Random(); for (int i = 0; i < 10; i++) { double randomNumber = numberGenerator.nextDouble(); testGetNumericValue(randomNumber); } } /** * This tests that a cell can be set to different * date values and that that date value will be * returned. */ public void testGetDateCellValue() { // Just some interesting dates that are found on // a calendar. Calendar cal = Calendar.getInstance(); // Just a cool date cal.set(1976, 3, 13); testGetDateValue(cal.getTime()); // Opening shoot of Iraqi Freedom cal.set(2003, 2, 19); testGetDateValue(cal.getTime()); // The first transcontinental demonstration of // radio telephone occurred on this day cal.set(1915, 8, 29); testGetDateValue(cal.getTime()); // Ford Model A Production Begins cal.set(1927, 10, 1, 8, 0); testGetDateValue(cal.getTime()); // Special Excel Date cal.set(1900, 1, 1); testGetDateValue(cal.getTime()); // Another Special Excel dates cal.set(1990, 1, 1, 0, 1); testGetDateValue(cal.getTime()); } public void testGetStringCellValue() { // Just do some random strings in the tests // right now. testGetStringValue(""); testGetStringValue("The different apache projects are wonderful."); testGetStringValue("Open source software helps developers"); testGetStringValue("George Bush is cool!"); testGetStringValue("George Bush is uncool!"); testGetStringValue("Bill Clinton was a great president."); testGetStringValue("Bill Clinton was a terrible president."); testGetStringValue("I wish I could be president of the United States"); testGetStringValue("I don't want to be the United States' president."); testGetStringValue("My favorite state in Arkansas!"); testGetStringValue("Pig Sooie"); testGetStringValue("Go HOGS!!!!!!!!"); testGetStringValue("God bless the Queen."); testGetStringValue("War - what is it good for?"); testGetStringValue("Peace"); testGetStringValue("Speak softly, but carry a big stick."); testGetStringValue("You can't be friends with everyone."); testGetStringValue("MS Excel"); testGetStringValue("MS"); testGetStringValue("Java"); } /** * This tests that a cell can be set to false * and that a false will be returned. */ public void testGetBooleanCellValueSetToFalse() { testGetBooleanValue(false); } /** * This tests that a cell can be set to true * and that a true will be returned. */ public void testGetBooleanCellValueSetToTrue() { testGetBooleanValue(true); } /* * Test for void setCellValue(Date) */ public void testSetCellValueDate() { // Get a new cell and see if we can // just set it's value to a date // object. HSSFCell cell = getNewCell(); cell.setCellValue(new Date()); } /* * Test for void setCellValue(null) */ public void testSetCellValueDateToNull() { // Get a new cell and see if we can // just set it's value to a null // date object. HSSFCell cell = getNewCell(); Date nullDate = null; try { cell.setCellValue(nullDate); fail("A NullPointerException should have been thrown."); } catch (NullPointerException nfe) { // Expected. } } /* * Test for void setCellValue(Calendar) */ public void testSetCellValueCalendar() { // Get a new cell and see if we can // just set it's value to a calendar // object. HSSFCell cell = getNewCell(); cell.setCellValue(Calendar.getInstance()); } /* * Test for void setCellValue(Calendar) */ public void testSetCellValueCalendarToNull() { // Get a new cell and see if we can // just set it's value to a null // calendar object. HSSFCell cell = getNewCell(); Calendar nullCalendar = null; try { cell.setCellValue(nullCalendar); fail("A NullPointerException should have been thrown."); } catch (NullPointerException nfe) { // Expected. } } /* * Test for void setCellValue(double) */ public void testSetCellValueNumeric() { // Get a new cell and see if we can // just set it's value to a double. HSSFCell cell = getNewCell(); cell.setCellValue(2.342); } /* * Test for void setCellValue(String) */ public void testSetCellValueString() { // Get a new cell and see if we can // just set it's value to a String. HSSFCell cell = getNewCell(); cell.setCellValue("A nice little string"); } }