001: /*
002: * ModifierArgumentsTest.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.sql.wbcommands;
013:
014: import junit.framework.TestCase;
015: import workbench.db.ColumnIdentifier;
016: import workbench.db.importer.modifier.ColumnValueSubstring;
017: import workbench.db.importer.modifier.ImportValueModifier;
018: import workbench.db.importer.modifier.RegexModifier;
019: import workbench.db.importer.modifier.SubstringModifier;
020: import workbench.util.ArgumentParser;
021:
022: /**
023: *
024: * @author support@sql-workbench.net
025: */
026: public class ModifierArgumentsTest extends TestCase {
027: public ModifierArgumentsTest(String testName) {
028: super (testName);
029: }
030:
031: public void testRegex() {
032: try {
033: ArgumentParser cmdLine = new ArgumentParser();
034: ModifierArguments.addArguments(cmdLine);
035:
036: // -colSubstring should overwrite whatever was specified with -maxLength
037: cmdLine.parse("-colReplacement=firstname=bla:blub");
038: ModifierArguments args = new ModifierArguments(cmdLine);
039:
040: ImportValueModifier mod = args.getModifier();
041: assertNotNull(mod);
042: assertEquals(1, mod.getSize());
043:
044: RegexModifier regex = args.getRegexModifier();
045: assertNotNull(regex);
046:
047: String modified = regex.modifyValue(new ColumnIdentifier(
048: "firstname"), "blast");
049: assertEquals("blubst", modified);
050: } catch (Exception e) {
051: e.printStackTrace();
052: fail(e.getMessage());
053: }
054: }
055:
056: public void testSubstring() {
057: try {
058: ArgumentParser cmdLine = new ArgumentParser();
059: ModifierArguments.addArguments(cmdLine);
060:
061: // -colSubstring should overwrite whatever was specified with -maxLength
062: cmdLine
063: .parse("-maxLength=firstname=2 -colSubstring=firstname=5:10,lastname = 1:3");
064: ModifierArguments args = new ModifierArguments(cmdLine);
065:
066: ImportValueModifier mod = args.getModifier();
067: assertNotNull(mod);
068: assertEquals(2, mod.getSize());
069:
070: SubstringModifier substring = args.getSubstringModifier();
071: assertNotNull(substring);
072:
073: ColumnIdentifier fname = new ColumnIdentifier("firstname");
074: ColumnValueSubstring s1 = substring.getSubstring(fname);
075: assertNotNull(s1);
076: assertEquals(5, s1.getStart());
077: assertEquals(10, s1.getEnd());
078:
079: ColumnIdentifier lname = new ColumnIdentifier("lastname");
080: ColumnValueSubstring s2 = substring.getSubstring(lname);
081: assertNotNull(s2);
082: assertEquals(1, s2.getStart());
083: assertEquals(3, s2.getEnd());
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail(e.getMessage());
087: }
088: }
089:
090: public void testMaxLength() {
091: try {
092: ArgumentParser cmdLine = new ArgumentParser();
093: ModifierArguments.addArguments(cmdLine);
094:
095: cmdLine
096: .parse("-gag=test -maxlength=firstname=100, lastname = 10");
097: ModifierArguments args = new ModifierArguments(cmdLine);
098:
099: ImportValueModifier mod = args.getModifier();
100: assertNotNull(mod);
101: assertEquals(2, mod.getSize());
102:
103: SubstringModifier substring = args.getSubstringModifier();
104: assertNotNull(substring);
105:
106: ColumnIdentifier fname = new ColumnIdentifier("firstname");
107: ColumnValueSubstring s1 = substring.getSubstring(fname);
108: assertNotNull(s1);
109: assertEquals(0, s1.getStart());
110: assertEquals(100, s1.getEnd());
111:
112: ColumnIdentifier lname = new ColumnIdentifier("lastname");
113: ColumnValueSubstring s2 = substring.getSubstring(lname);
114: assertNotNull(s2);
115: assertEquals(0, s2.getStart());
116: assertEquals(10, s2.getEnd());
117:
118: String modified = s2.getSubstring("12345678901234567890");
119: assertEquals("1234567890", modified);
120: } catch (Exception e) {
121: e.printStackTrace();
122: fail(e.getMessage());
123: }
124: }
125: }
|