Skip to content

Commit

Permalink
fix: PgSQLXML setCharacterStream() results in null value (#1608)
Browse files Browse the repository at this point in the history
* fix: PgSQLXML setCharacterStream() results in null value fixes Issue #731
  • Loading branch information
davecramer committed Nov 15, 2019
1 parent e64b0a2 commit 1e37026
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgSQLXML.java
Expand Up @@ -175,6 +175,7 @@ public synchronized OutputStream setBinaryStream() throws SQLException {
public synchronized Writer setCharacterStream() throws SQLException {
checkFreed();
initialize();
active = true;
stringWriter = new StringWriter();
return stringWriter;
}
Expand Down
50 changes: 50 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/jdbc/PgSQLXMLTest.java
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Before;
import org.junit.Test;

import java.io.Writer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLXML;
import java.sql.Statement;

public class PgSQLXMLTest extends BaseTest4 {

@Override
@Before
public void setUp() throws Exception {
super.setUp();
TestUtil.createTempTable(con, "xmltab","x xml");
}

@Test
public void setCharacterStream() throws Exception {
String exmplar = "<x>value</x>";
SQLXML pgSQLXML = con.createSQLXML();
Writer writer = pgSQLXML.setCharacterStream();
writer.write(exmplar);
PreparedStatement preparedStatement = con.prepareStatement("insert into xmltab values (?)");
preparedStatement.setSQLXML(1,pgSQLXML);
preparedStatement.execute();

Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("select * from xmltab");
assertTrue(rs.next());
SQLXML result = rs.getSQLXML(1);
assertNotNull(result);
assertEquals(exmplar, result.getString());
}
}
Expand Up @@ -13,6 +13,7 @@
import org.postgresql.core.ReturningParserTest;
import org.postgresql.core.v3.V3ParameterListTests;
import org.postgresql.jdbc.DeepBatchedInsertStatementTest;
import org.postgresql.jdbc.PgSQLXMLTest;
import org.postgresql.jdbc.PrimitiveArraySupportTest;
import org.postgresql.test.core.JavaVersionTest;
import org.postgresql.test.core.LogServerMessagePropertyTest;
Expand Down Expand Up @@ -82,6 +83,7 @@
PGPropertyTest.class,
PGTimestampTest.class,
PGTimeTest.class,
PgSQLXMLTest.class,
PreparedStatementTest.class,
PrimitiveArraySupportTest.class,
QuotationTest.class,
Expand Down

0 comments on commit 1e37026

Please sign in to comment.