001: /*
002: * LobFileStatementTest.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.util;
013:
014: import java.io.File;
015: import java.io.FileNotFoundException;
016: import junit.framework.*;
017: import workbench.TestUtil;
018: import workbench.WbTestCase;
019:
020: /**
021: *
022: * @author support@sql-workbench.net
023: */
024: public class LobFileStatementTest extends WbTestCase {
025: private TestUtil util;
026:
027: public LobFileStatementTest(String testName) {
028: super (testName);
029: util = getTestUtil();
030: }
031:
032: public void setUp() {
033: util.emptyBaseDirectory();
034: }
035:
036: public void testSyntaxError() {
037: boolean hasException = false;
038: try {
039: String sql = "update bla set col = {$blobfile=dummy_file.data where x = 1";
040: LobFileStatement stmt = new LobFileStatement(sql);
041: } catch (FileNotFoundException e) {
042: // a FileNotFound is not expected as the syntax is not correct
043: hasException = false;
044: } catch (IllegalArgumentException e) {
045: hasException = true;
046: }
047: assertEquals("Wrong exception or no exception thrown", true,
048: hasException);
049:
050: try {
051: String sql = "insert into test (x,y,z) values (1,2, {$blobfile=dummy_file.data)";
052: LobFileStatement stmt = new LobFileStatement(sql);
053: hasException = false;
054: } catch (FileNotFoundException e) {
055: // a FileNotFound is not expected as the syntax is not correct
056: hasException = false;
057: } catch (IllegalArgumentException e) {
058: hasException = true;
059: }
060: assertEquals("Wrong exception or no exception thrown", true,
061: hasException);
062:
063: try {
064: String sql = "insert into test (x,y,z) values (1,2, {$blobfile=dummy_file_should_not_be_found.data})";
065: LobFileStatement stmt = new LobFileStatement(sql);
066: hasException = false;
067: } catch (FileNotFoundException e) {
068: hasException = true;
069: } catch (IllegalArgumentException e) {
070: // The syntax is correct, it should not throw an exception
071: hasException = false;
072: }
073: assertEquals("Wrong exception or no exception thrown", true,
074: hasException);
075:
076: }
077:
078: public void testGetParameterCount() {
079: File f = new File(util.getBaseDir(), "test.data");
080: try {
081: // LobFileStatement checks for the presence of the file!
082: f.createNewFile();
083: String sql = "update bla set col = {$blobfile="
084: + f.getAbsolutePath() + "} where x = 1";
085: LobFileStatement stmt = new LobFileStatement(sql);
086: assertEquals("Wrong parameter count", 1, stmt
087: .getParameterCount());
088: LobFileParameter[] parms = stmt.getParameters();
089: assertEquals("Wrong parameter type", true, parms[0]
090: .isBinary());
091:
092: f = new File(util.getBaseDir(), "some file.data");
093: f.createNewFile();
094:
095: sql = "update bla set col = {$clobfile='"
096: + f.getAbsolutePath()
097: + "' encoding=utf8} where x = 1";
098: stmt = new LobFileStatement(sql);
099: assertEquals("Wrong parameter count", 1, stmt
100: .getParameterCount());
101: parms = stmt.getParameters();
102: assertEquals("Wrong parameter type", false, parms[0]
103: .isBinary());
104: assertEquals("Wrong encoding", "utf8", parms[0]
105: .getEncoding());
106:
107: File target = new File(parms[0].getFilename());
108: assertEquals("Wrong filename parsed", "some file.data", f
109: .getName());
110: } catch (Exception e) {
111: e.printStackTrace();
112: fail("could not parse statement");
113: } finally {
114: f.delete();
115: }
116: }
117:
118: public void testGetPreparedSql() {
119: File f = new File(util.getBaseDir(), "test.data");
120: try {
121: // LobFileStatement checks for the presence of the file!
122: f.createNewFile();
123:
124: String sql = "update bla set col = {$blobfile="
125: + f.getAbsolutePath() + "} where x = 1";
126: LobFileStatement stmt = new LobFileStatement(sql);
127: String newSql = stmt.getPreparedSql();
128: assertEquals("Wrong SQL generated",
129: "update bla set col = ? where x = 1", newSql);
130:
131: sql = "update bla set col = {$clobfile='"
132: + f.getAbsolutePath() + "'} where x = 1";
133: stmt = new LobFileStatement(sql);
134: assertEquals("Wrong SQL generated",
135: "update bla set col = ? where x = 1", stmt
136: .getPreparedSql());
137:
138: sql = "update bla set col = {$clobfile='"
139: + f.getAbsolutePath()
140: + "' encoding='UTF-8'} where x = 1";
141: stmt = new LobFileStatement(sql);
142: assertEquals("Wrong SQL generated",
143: "update bla set col = ? where x = 1", stmt
144: .getPreparedSql());
145:
146: } catch (Exception e) {
147: e.printStackTrace();
148: fail("could not parse statement");
149: } finally {
150: f.delete();
151: }
152: }
153:
154: }
|