01: package org.drools.decisiontable.parser.csv;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: import junit.framework.TestCase;
23:
24: import org.drools.decisiontable.parser.SheetListener;
25:
26: public class CsvParserTest extends TestCase {
27:
28: public void testCsv() {
29: final MockSheetListener listener = new MockSheetListener();
30: final CsvLineParser lineParser = new CsvLineParser();
31: final CsvParser parser = new CsvParser(listener, lineParser);
32:
33: parser.parseFile(getClass().getResourceAsStream(
34: "/data/TestCsv.csv"));
35: assertEquals("A", listener.getCell(0, 0));
36: assertEquals("B", listener.getCell(0, 1));
37: assertEquals("", listener.getCell(2, 0));
38: assertEquals("C", listener.getCell(1, 0));
39: assertEquals("D", listener.getCell(1, 1));
40: assertEquals("E", listener.getCell(1, 3));
41:
42: }
43:
44: /**
45: * Test the handling of merged cells.
46: */
47: public void testCellMergeHandling() {
48: CsvParser parser = new CsvParser((SheetListener) null, null);
49: assertEquals(SheetListener.NON_MERGED, parser.calcStartMerge(
50: SheetListener.NON_MERGED, 1, "foo"));
51: assertEquals(42, parser.calcStartMerge(
52: SheetListener.NON_MERGED, 42, "..."));
53:
54: assertEquals(42, parser.calcStartMerge(42, 43, "..."));
55:
56: assertEquals(SheetListener.NON_MERGED, parser.calcStartMerge(
57: 42, 44, "VanHalen"));
58:
59: assertEquals("VanHalen", parser.calcCellText(
60: SheetListener.NON_MERGED, "VanHalen"));
61: assertEquals("VanHalen", parser.calcCellText(42, "VanHalen..."));
62: assertEquals("", parser.calcCellText(42, "..."));
63:
64: }
65:
66: static class MockSheetListener implements SheetListener {
67:
68: Map data = new HashMap();
69:
70: public String getCell(final int row, final int col) {
71: return (String) this .data.get(cellKey(row, col));
72: }
73:
74: public void startSheet(final String name) {
75: }
76:
77: public void finishSheet() {
78: }
79:
80: public void newRow(final int rowNumber, final int columns) {
81:
82: }
83:
84: public void newCell(final int row, final int column,
85: final String value, final int mergeCellStart) {
86:
87: this .data.put(cellKey(row, column), value);
88: }
89:
90: String cellKey(final int row, final int column) {
91: return "R" + row + "C" + column;
92: }
93:
94: }
95: }
|