modify blobtest case failed issue
This commit is contained in:
@ -10,9 +10,8 @@ import static org.junit.Assert.assertNotNull;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.postgresql.PGProperty;
|
||||||
import org.postgresql.core.ServerVersion;
|
import org.postgresql.core.ServerVersion;
|
||||||
import org.postgresql.largeobject.LargeObject;
|
|
||||||
import org.postgresql.largeobject.LargeObjectManager;
|
|
||||||
import org.postgresql.test.TestUtil;
|
import org.postgresql.test.TestUtil;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -20,7 +19,6 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.sql.Blob;
|
import java.sql.Blob;
|
||||||
import java.sql.Clob;
|
import java.sql.Clob;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@ -29,6 +27,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Some simple tests based on problems reported by users. Hopefully these will help prevent previous
|
* Some simple tests based on problems reported by users. Hopefully these will help prevent previous
|
||||||
@ -42,29 +41,20 @@ public class BlobTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
con = TestUtil.openDB();
|
Properties props = new Properties();
|
||||||
TestUtil.createTable(con, "testblob", "id name,lo oid");
|
// blob and clob can exchange must set string is not varchar!
|
||||||
|
props.setProperty(PGProperty.STRING_TYPE.getName(), "unspecified");
|
||||||
|
con = TestUtil.openDB(props);
|
||||||
|
TestUtil.createTable(con, "testblob", "id name,lo blob");
|
||||||
con.setAutoCommit(false);
|
con.setAutoCommit(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
con.setAutoCommit(true);
|
con.setAutoCommit(true);
|
||||||
try {
|
|
||||||
Statement stmt = con.createStatement();
|
|
||||||
try {
|
|
||||||
stmt.execute("SELECT lo_unlink(lo) FROM testblob");
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
stmt.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
TestUtil.dropTable(con, "testblob");
|
TestUtil.dropTable(con, "testblob");
|
||||||
TestUtil.closeDB(con);
|
TestUtil.closeDB(con);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetNull() throws Exception {
|
public void testSetNull() throws Exception {
|
||||||
@ -78,21 +68,12 @@ public class BlobTest {
|
|||||||
|
|
||||||
pstmt.setObject(1, null, Types.BLOB);
|
pstmt.setObject(1, null, Types.BLOB);
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
pstmt.setClob(1, (Clob) null);
|
|
||||||
pstmt.executeUpdate();
|
|
||||||
|
|
||||||
pstmt.setNull(1, Types.CLOB);
|
|
||||||
pstmt.executeUpdate();
|
|
||||||
|
|
||||||
pstmt.setObject(1, null, Types.CLOB);
|
|
||||||
pstmt.executeUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSet() throws SQLException {
|
public void testSet() throws SQLException {
|
||||||
Statement stmt = con.createStatement();
|
Statement stmt = con.createStatement();
|
||||||
stmt.execute("INSERT INTO testblob(id,lo) VALUES ('1', lo_creat(-1))");
|
stmt.execute("INSERT INTO testblob(id,lo) VALUES ('1', empty_blob())");
|
||||||
ResultSet rs = stmt.executeQuery("SELECT lo FROM testblob");
|
ResultSet rs = stmt.executeQuery("SELECT lo FROM testblob");
|
||||||
assertTrue(rs.next());
|
assertTrue(rs.next());
|
||||||
|
|
||||||
@ -138,9 +119,7 @@ public class BlobTest {
|
|||||||
|
|
||||||
// Now compare the blob & the file. Note this actually tests the
|
// Now compare the blob & the file. Note this actually tests the
|
||||||
// InputStream implementation!
|
// InputStream implementation!
|
||||||
assertTrue(compareBlobsLOAPI());
|
|
||||||
assertTrue(compareBlobs());
|
assertTrue(compareBlobs());
|
||||||
assertTrue(compareClobs());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -163,11 +142,7 @@ public class BlobTest {
|
|||||||
ResultSet rs = stmt.executeQuery("SELECT lo FROM testblob");
|
ResultSet rs = stmt.executeQuery("SELECT lo FROM testblob");
|
||||||
assertTrue(rs.next());
|
assertTrue(rs.next());
|
||||||
|
|
||||||
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();
|
InputStream bis = rs.getBlob(1).getBinaryStream();
|
||||||
|
|
||||||
long oid = rs.getLong(1);
|
|
||||||
LargeObject blob = lom.open(oid);
|
|
||||||
InputStream bis = blob.getInputStream();
|
|
||||||
|
|
||||||
assertEquals('<', bis.read());
|
assertEquals('<', bis.read());
|
||||||
bis.mark(4);
|
bis.mark(4);
|
||||||
@ -263,102 +238,22 @@ public class BlobTest {
|
|||||||
assertEquals(length, lob.length());
|
assertEquals(length, lob.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper - uploads a file into a blob using old style methods. We use this because it always
|
* Helper - uploads a file into a blob using old style methods. We use this because it always
|
||||||
* works, and we can use it as a base to test the new methods.
|
* works, and we can use it as a base to test the new methods.
|
||||||
*/
|
*/
|
||||||
private long uploadFile(String file, int method) throws Exception {
|
private long uploadFile(String file, int method) throws Exception {
|
||||||
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();
|
try (InputStream fis = getClass().getResourceAsStream(file)) {
|
||||||
|
String sql = String.format("INSERT INTO %s VALUES (?, ?)", "testblob");
|
||||||
InputStream fis = getClass().getResourceAsStream(file);
|
try (PreparedStatement ps = con.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, file);
|
||||||
long oid = lom.createLO(LargeObjectManager.READWRITE);
|
ps.setBlob(2, fis);
|
||||||
LargeObject blob = lom.open(oid);
|
ps.execute();
|
||||||
|
|
||||||
int s;
|
|
||||||
int t;
|
|
||||||
byte[] buf;
|
|
||||||
OutputStream os;
|
|
||||||
|
|
||||||
switch (method) {
|
|
||||||
case LOOP:
|
|
||||||
buf = new byte[2048];
|
|
||||||
t = 0;
|
|
||||||
while ((s = fis.read(buf, 0, buf.length)) > 0) {
|
|
||||||
t += s;
|
|
||||||
blob.write(buf, 0, s);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NATIVE_STREAM:
|
|
||||||
os = blob.getOutputStream();
|
|
||||||
s = fis.read();
|
|
||||||
while (s > -1) {
|
|
||||||
os.write(s);
|
|
||||||
s = fis.read();
|
|
||||||
}
|
|
||||||
os.close();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
fail("Unknown method in uploadFile");
|
|
||||||
}
|
|
||||||
|
|
||||||
blob.close();
|
|
||||||
fis.close();
|
|
||||||
|
|
||||||
// Insert into the table
|
|
||||||
Statement st = con.createStatement();
|
|
||||||
st.executeUpdate(TestUtil.insertSQL("testblob", "id,lo", "'" + file + "'," + oid));
|
|
||||||
con.commit();
|
con.commit();
|
||||||
st.close();
|
|
||||||
|
|
||||||
return oid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Helper - compares the blobs in a table with a local file. Note this uses the postgresql
|
|
||||||
* specific Large Object API
|
|
||||||
*/
|
|
||||||
private boolean compareBlobsLOAPI() throws Exception {
|
|
||||||
boolean result = true;
|
|
||||||
|
|
||||||
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();
|
|
||||||
|
|
||||||
Statement st = con.createStatement();
|
|
||||||
ResultSet rs = st.executeQuery(TestUtil.selectSQL("testblob", "id,lo"));
|
|
||||||
assertNotNull(rs);
|
|
||||||
|
|
||||||
while (rs.next()) {
|
|
||||||
String file = rs.getString(1);
|
|
||||||
long oid = rs.getLong(2);
|
|
||||||
|
|
||||||
InputStream fis = getClass().getResourceAsStream(file);
|
|
||||||
LargeObject blob = lom.open(oid);
|
|
||||||
InputStream bis = blob.getInputStream();
|
|
||||||
|
|
||||||
int f = fis.read();
|
|
||||||
int b = bis.read();
|
|
||||||
int c = 0;
|
|
||||||
while (f >= 0 && b >= 0 & result) {
|
|
||||||
result = (f == b);
|
|
||||||
f = fis.read();
|
|
||||||
b = bis.read();
|
|
||||||
c++;
|
|
||||||
}
|
}
|
||||||
result = result && f == -1 && b == -1;
|
return 1;
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
fail("Large Object API Blob compare failed at " + c + " of " + blob.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
blob.close();
|
|
||||||
fis.close();
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
st.close();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -401,45 +296,4 @@ public class BlobTest {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Helper - compares the clobs in a table with a local file.
|
|
||||||
*/
|
|
||||||
private boolean compareClobs() throws Exception {
|
|
||||||
boolean result = true;
|
|
||||||
|
|
||||||
Statement st = con.createStatement();
|
|
||||||
ResultSet rs = st.executeQuery(TestUtil.selectSQL("testblob", "id,lo"));
|
|
||||||
assertNotNull(rs);
|
|
||||||
|
|
||||||
while (rs.next()) {
|
|
||||||
String file = rs.getString(1);
|
|
||||||
Clob clob = rs.getClob(2);
|
|
||||||
|
|
||||||
InputStream fis = getClass().getResourceAsStream(file);
|
|
||||||
InputStream bis = clob.getAsciiStream();
|
|
||||||
|
|
||||||
int f = fis.read();
|
|
||||||
int b = bis.read();
|
|
||||||
int c = 0;
|
|
||||||
while (f >= 0 && b >= 0 & result) {
|
|
||||||
result = (f == b);
|
|
||||||
f = fis.read();
|
|
||||||
b = bis.read();
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
result = result && f == -1 && b == -1;
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
fail("Clob compare failed at " + c + " of " + clob.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
bis.close();
|
|
||||||
fis.close();
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
st.close();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user