forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix apache#210 JavaDeserializer._constructor.newInstance([null, null]…
…) NPE Example: org.springframework.jdbc.UncategorizedSQLException
- Loading branch information
Showing
4 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/JavaDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.alibaba.com.caucho.hessian.io; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.sql.SQLException; | ||
|
||
import org.junit.Test; | ||
|
||
import com.alibaba.com.caucho.hessian.io.model.ConstructAlwaysNPE; | ||
import com.alibaba.com.caucho.hessian.io.model.ConstructNPE; | ||
|
||
public class JavaDeserializerTest { | ||
|
||
/** | ||
* <a href="https://github.com/apache/incubator-dubbo/issues/210">#210</a> | ||
* @see org.springframework.jdbc.UncategorizedSQLException | ||
*/ | ||
@Test | ||
public void testConstructorNPE() throws Exception { | ||
String sql = "select * from demo"; | ||
SQLException sqlEx = new SQLException("just a sql exception"); | ||
ConstructNPE normalNPE = new ConstructNPE("junit", sql, sqlEx); | ||
ConstructAlwaysNPE alwaysNPE = new ConstructAlwaysNPE("junit", sql, sqlEx); | ||
|
||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
Hessian2Output out = new Hessian2Output(bout); | ||
out.writeObject(normalNPE); | ||
out.writeObject(alwaysNPE); | ||
out.flush(); | ||
|
||
SerializerFactory factory = new SerializerFactory(); | ||
for (int repeat = 0; repeat < 2; repeat++) { | ||
Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(bout.toByteArray())); | ||
input.setSerializerFactory(factory); | ||
|
||
assertDesEquals(normalNPE, (ConstructNPE) input.readObject()); | ||
assertCompatibleConstructNPE(factory.getDeserializer(normalNPE.getClass()), true); | ||
|
||
try { | ||
input.readObject(); | ||
fail("must be always throw NullPointerException"); | ||
} catch (HessianProtocolException e) { | ||
assertEquals(InvocationTargetException.class, e.getCause().getClass()); | ||
assertEquals(NullPointerException.class, e.getCause().getCause().getClass()); | ||
} | ||
assertCompatibleConstructNPE(factory.getDeserializer(alwaysNPE.getClass()), false); | ||
} | ||
} | ||
|
||
private void assertDesEquals(ConstructNPE expected, ConstructNPE actual) { | ||
assertEquals(expected.getMessage(), actual.getMessage()); | ||
assertEquals(expected.getCause().getClass(), actual.getCause().getClass()); | ||
assertEquals(expected.getSql(), actual.getSql()); | ||
} | ||
|
||
private void assertCompatibleConstructNPE(Deserializer deserializer, boolean compatible) throws Exception { | ||
assertEquals(JavaDeserializer.class, deserializer.getClass()); | ||
assertEquals(compatible, getFieldValue(deserializer, "compatibleConstructNPE")); | ||
Object[] args = (Object[]) getFieldValue(deserializer, "_constructorArgs"); | ||
for (int i = 0; i < args.length; i++) { | ||
if (compatible) { | ||
assertNotNull(args[i]); | ||
} else { | ||
assertNull(args[i]); | ||
} | ||
} | ||
} | ||
|
||
public Object getFieldValue(Object bean, String fieldName) throws Exception { | ||
Field field = bean.getClass().getDeclaredField(fieldName); | ||
if (!field.isAccessible()) field.setAccessible(true); | ||
return field.get(bean); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/model/ConstructAlwaysNPE.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.alibaba.com.caucho.hessian.io.model; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* <a href="https://github.com/apache/incubator-dubbo/issues/210">#210</a> | ||
*/ | ||
public class ConstructAlwaysNPE extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
private final String sql; | ||
|
||
public ConstructAlwaysNPE(String task, String sql, SQLException ex) { | ||
super(task + "; uncategorized SQLException for SQL [" + sql + "]; SQL state [" + | ||
ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex); | ||
if (sql.length() == 0) throw new NullPointerException("sql=" + sql); | ||
this.sql = sql; | ||
} | ||
|
||
public SQLException getSQLException() { | ||
return (SQLException) getCause(); | ||
} | ||
|
||
public String getSql() { | ||
return this.sql; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
hessian-lite/src/test/java/com/alibaba/com/caucho/hessian/io/model/ConstructNPE.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.alibaba.com.caucho.hessian.io.model; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* <a href="https://github.com/apache/incubator-dubbo/issues/210">#210</a> | ||
* @see org.springframework.jdbc.UncategorizedSQLException | ||
*/ | ||
public class ConstructNPE extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
private final String sql; | ||
|
||
public ConstructNPE(String task, String sql, SQLException ex) { | ||
super(task + "; uncategorized SQLException for SQL [" + sql + "]; SQL state [" + | ||
ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex); | ||
this.sql = sql; | ||
} | ||
|
||
public SQLException getSQLException() { | ||
return (SQLException) getCause(); | ||
} | ||
|
||
public String getSql() { | ||
return this.sql; | ||
} | ||
} |