001: /*
002: * DataStoreReplacerTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.storage;
013:
014: /**
015: *
016: * @author support@sql-workbench.net
017: */
018: public class DataStoreReplacerTest extends junit.framework.TestCase {
019:
020: public DataStoreReplacerTest(String testName) {
021: super (testName);
022: }
023:
024: public void testReplaceSelection() {
025: try {
026: String[] cols = new String[] { "firstname", "lastname",
027: "numeric" };
028: int[] types = new int[] { java.sql.Types.VARCHAR,
029: java.sql.Types.VARCHAR, java.sql.Types.INTEGER };
030:
031: DataStore ds = new DataStore(cols, types);
032: for (int i = 0; i < 20; i++) {
033: int row = ds.addRow();
034: ds.setValue(row, 0, "First" + Integer.toString(i));
035: ds.setValue(row, 1, "Last" + Integer.toString(i));
036: ds.setValue(row, 2, new Integer(i));
037: }
038: int[] selected = new int[] { 0, 1, 2 };
039: DataStoreReplacer replacer = new DataStoreReplacer(ds);
040: replacer.setSelectedRows(selected);
041: Position pos = replacer.find("2", true, true, false);
042: assertEquals("Value not found", new Position(2, 2), pos);
043:
044: pos = replacer.find("5", true, true, false);
045: assertEquals("Value outside selection found",
046: Position.NO_POSITION, pos);
047:
048: int replaced = replacer.replaceAll("First", "Other",
049: selected, true, false, false);
050: assertEquals("Wrong number of values replaced", 3, replaced);
051:
052: Object value = ds.getValue(0, 0);
053: assertEquals("Value not replaced", "Other0", value);
054:
055: value = ds.getValue(6, 0);
056: assertEquals("Wrong row value replaced", "First6", value);
057: } catch (Exception e) {
058: e.printStackTrace();
059: fail(e.getMessage());
060: }
061: }
062:
063: public void testReplace() {
064: try {
065: String[] cols = new String[] { "firstname", "lastname",
066: "numeric" };
067: int[] types = new int[] { java.sql.Types.VARCHAR,
068: java.sql.Types.VARCHAR, java.sql.Types.INTEGER };
069:
070: DataStore ds = new DataStore(cols, types);
071:
072: int row = ds.addRow();
073: ds.setValue(row, 0, "Arthur");
074: ds.setValue(row, 1, "Dent");
075: ds.setValue(row, 2, new Integer(5));
076:
077: row = ds.addRow();
078: ds.setValue(row, 0, "Zaphod");
079: ds.setValue(row, 1, "Beeblebrox");
080: ds.setValue(row, 2, new Integer(12));
081:
082: int mary1Row = ds.addRow();
083: ds.setValue(mary1Row, 0, "Mary");
084: ds.setValue(mary1Row, 1, "Moviestar");
085: ds.setValue(mary1Row, 2, new Integer(23));
086:
087: int mary2Row = ds.addRow();
088: ds.setValue(mary2Row, 0, "Mary");
089: ds.setValue(mary2Row, 1, "Poppins");
090: ds.setValue(mary2Row, 2, new Integer(42));
091:
092: DataStoreReplacer replacer = new DataStoreReplacer(ds);
093: int replaced = replacer.replaceAll("Mary", "Yram", null,
094: true, false, false);
095: assertEquals("Wrong number of replacements", 2, replaced);
096:
097: String value = ds.getValueAsString(mary1Row, 0);
098: assertEquals("Wrong new value", "Yram", value);
099:
100: try {
101: replacer.replaceAll("42", "gaga", null, false, true,
102: false);
103: fail("No ConverterException thrown");
104: } catch (Exception e) {
105: }
106:
107: value = ds.getValueAsString(mary2Row, 2);
108: assertEquals("Value was replaced", "42", value);
109:
110: try {
111: replacer.replaceAll("42", "24", null, false, true,
112: false);
113: value = ds.getValueAsString(mary2Row, 2);
114: assertEquals("Value was not replaced", "24", value);
115: } catch (Exception e) {
116: fail("Replacement threw an exception");
117: }
118:
119: } catch (Exception e) {
120: fail(e.getMessage());
121: }
122: }
123:
124: public void testFind() {
125: try {
126: String[] cols = new String[] { "firstname", "lastname",
127: "numeric" };
128: int[] types = new int[] { java.sql.Types.VARCHAR,
129: java.sql.Types.VARCHAR, java.sql.Types.INTEGER };
130:
131: DataStore ds = new DataStore(cols, types);
132:
133: int row = ds.addRow();
134: ds.setValue(row, 0, "Arthur");
135: ds.setValue(row, 1, "Dent");
136: ds.setValue(row, 2, new Integer(5));
137:
138: row = ds.addRow();
139: ds.setValue(row, 0, "Zaphod");
140: ds.setValue(row, 1, "Beeblebrox");
141: ds.setValue(row, 2, new Integer(12));
142:
143: int mary1Row = ds.addRow();
144: ds.setValue(mary1Row, 0, "Mary");
145: ds.setValue(mary1Row, 1, "Moviestar");
146: ds.setValue(mary1Row, 2, new Integer(23));
147:
148: int mary2Row = ds.addRow();
149: ds.setValue(mary2Row, 0, "Mary");
150: ds.setValue(mary2Row, 1, "Poppins");
151: ds.setValue(mary2Row, 2, new Integer(42));
152:
153: DataStoreReplacer instance = new DataStoreReplacer(ds);
154:
155: Position expResult = new Position(0, 1);
156: Position result = instance.find("dent", true, false, false);
157: assertEquals("Value not found", expResult, result);
158:
159: result = instance.find("gaga", true, false, false);
160: assertEquals("Value found", Position.NO_POSITION, result);
161:
162: result = instance.find("Mary", false, false, false);
163: assertEquals("First value not found", new Position(
164: mary1Row, 0), result);
165:
166: result = instance.findNext();
167: assertEquals("Second value not found", new Position(
168: mary2Row, 0), result);
169:
170: result = instance.find("12", true, false, false);
171: assertEquals("Numeric value not found", new Position(1, 2),
172: result);
173:
174: result = instance.find("M[a|o]", true, false, true);
175: assertEquals("Regex not found", new Position(2, 0), result);
176: result = instance.findNext();
177: assertEquals("Regex not found", new Position(2, 1), result);
178: result = instance.findNext();
179: assertEquals("Regex not found", new Position(3, 0), result);
180: result = instance.findNext();
181: assertEquals("Regex found", Position.NO_POSITION, result);
182:
183: result = instance.find("[1|4]2", true, false, true);
184: assertEquals("Regex not found", new Position(1, 2), result);
185: result = instance.findNext();
186: assertEquals("Regex not found", new Position(mary2Row, 2),
187: result);
188: result = instance.findNext();
189: assertEquals("Regex found", Position.NO_POSITION, result);
190:
191: } catch (Exception e) {
192: e.printStackTrace();
193: fail(e.getMessage());
194: }
195: }
196:
197: }
|