View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2012 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13   * specific language governing permissions and limitations under the License.
14   */
15  package ubic.basecode.io.excel;
16  
17  import static org.junit.Assert.assertEquals;
18  
19  import java.io.File;
20  import java.util.List;
21  import java.util.Set;
22  
23  import org.apache.poi.hssf.usermodel.HSSFSheet;
24  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25  import org.junit.Test;
26  
27  /**
28   * @author paul
29   * 
30   */
31  public class ExcelUtilTest {
32  
33      @Test
34      public void testGetSet() {
35          HSSFWorkbook workbook = new HSSFWorkbook();
36          HSSFSheet spreadsheet = workbook.createSheet();
37          ExcelUtil.setValue( spreadsheet, 4, 5, 1.99 );
38          assertEquals( 1.99, Double.parseDouble( ExcelUtil.getValue( spreadsheet, 4, 5 ) ), 0.001 );
39  
40          Set<String> vals = ExcelUtil.grabColumnValues( spreadsheet, 5, false, false );
41          assertEquals( 1, vals.size() );
42      }
43  
44      @Test
45      public void testGetSheetFromFile() throws Exception {
46          String f = new File( this.getClass().getResource( "/data/matrix-testing.xls" ).toURI() ).getAbsolutePath();
47          String s = "range,variance, normalized";
48          HSSFSheet sheetFromFile = ExcelUtil.getSheetFromFile( f, s );
49          String value = ExcelUtil.getValue( sheetFromFile, 10, 10 );
50          assertEquals( "0.158", value );
51          value = ExcelUtil.getValue( sheetFromFile, 0, 0 );
52          assertEquals( "gene", value );
53  
54          List<String> grabColumnValuesList = ExcelUtil.grabColumnValuesList( sheetFromFile, 2, true, false );
55          assertEquals( "-0.157", grabColumnValuesList.get( 3 ) );
56  
57          ExcelUtil.setValue( sheetFromFile, 10, 10, 0.44 );
58          assertEquals( "0.44", ExcelUtil.getValue( sheetFromFile, 10, 10 ) );
59      }
60  
61      @Test
62      public void testSetFormula() {
63          HSSFWorkbook workbook = new HSSFWorkbook();
64          HSSFSheet spreadsheet = workbook.createSheet();
65          ExcelUtil.setFormula( spreadsheet, 1, 1, "HYPERLINK(\"x\",\"x\")" );
66      }
67  
68  }