Skip to content

Commit 792dccf

Browse files
author
Brendan W. McAdams
committed
Added test methods for custom encoders & decoders as well as my new
methods for manipulating them.
1 parent 7fe27c3 commit 792dccf

1 file changed

Lines changed: 147 additions & 1 deletion

File tree

src/test/org/bson/BSONTest.java

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void testBasic1()
8484

8585
@Test
8686
public void testArray()
87-
throws IOException {
87+
throws IOException {
8888
_test( new BasicBSONObject( "x" , new int[]{ 1 , 2 , 3 , 4} ) , 41 , "e63397fe37de1349c50e1e4377a45e2d" );
8989
}
9090

@@ -156,7 +156,153 @@ public void testUTF8(){
156156
}
157157

158158
}
159+
160+
161+
@Test
162+
public void testCustomEncoders() throws IOException{
163+
// If clearEncodingHooks isn't working the first test will fail.
164+
Transformer tf = new TestDateTransformer();
165+
BSON.addEncodingHook( TestDate.class, tf );
166+
BSON.clearEncodingHooks();
167+
TestDate td = new TestDate( 2009 , 01 , 23 , 10 , 53 , 42 );
168+
BSONObject o = new BasicBSONObject( "date" , td );
169+
BSONEncoder e = new BSONEncoder();
170+
BSONDecoder d = new BSONDecoder();
171+
BasicBSONCallback cb = new BasicBSONCallback();
172+
OutputBuffer buf = new BasicOutputBuffer();
173+
e.set( buf );
174+
boolean encodeFailed = false;
175+
try {
176+
e.putObject( o );
177+
}
178+
catch ( IllegalArgumentException ieE ) {
179+
encodeFailed = true;
180+
}
181+
assertTrue( encodeFailed, "Expected encoding to fail but it didn't." );
182+
// Reset the buffer
183+
buf.seekStart();
184+
assertTrue( td instanceof TestDate );
185+
assertTrue( tf.transform( td ) instanceof java.util.Date, "Transforming a TestDate should yield a JDK Date" );
186+
187+
BSON.addEncodingHook( TestDate.class, tf );
188+
e.putObject( o );
189+
e.done();
190+
191+
d.decode( new ByteArrayInputStream( buf.toByteArray() ), cb );
192+
Object result = cb.get();
193+
assertTrue( result instanceof BSONObject, "Expected to retrieve a BSONObject but got '" + result.getClass() + "' instead." );
194+
BSONObject bson = (BSONObject) result;
195+
assertNotNull( bson.get( "date" ) );
196+
assertTrue( bson.get( "date" ) instanceof java.util.Date );
197+
198+
// Check that the hooks registered
199+
assertNotNull( BSON.getEncodingHooks( TestDate.class ) );
200+
Vector expect = new Vector( 1 );
201+
expect.add( tf );
202+
assertEquals( BSON.getEncodingHooks( TestDate.class ), expect );
203+
assertTrue( BSON.getEncodingHooks( TestDate.class ).contains( tf ) );
204+
BSON.removeEncodingHook( TestDate.class, tf );
205+
assertFalse( BSON.getEncodingHooks( TestDate.class ).contains( tf ) );
206+
}
159207

208+
@Test
209+
@SuppressWarnings( "deprecation" )
210+
public void testCustomDecoders() throws IOException{
211+
// If clearDecodingHooks isn't working this whole test will fail.
212+
Transformer tf = new TestDateTransformer();
213+
BSON.addDecodingHook( Date.class, tf );
214+
BSON.clearDecodingHooks();
215+
TestDate td = new TestDate( 2009 , 01 , 23 , 10 , 53 , 42 );
216+
Date dt = new Date( 2009 , 01 , 23 , 10 , 53 , 42 );
217+
BSONObject o = new BasicBSONObject( "date" , dt );
218+
BSONDecoder d = new BSONDecoder();
219+
BSONEncoder e = new BSONEncoder();
220+
BasicBSONCallback cb = new BasicBSONCallback();
221+
OutputBuffer buf = new BasicOutputBuffer();
222+
e.set( buf );
223+
e.putObject( o );
224+
e.done();
225+
226+
d.decode( new ByteArrayInputStream( buf.toByteArray() ), cb );
227+
Object result = cb.get();
228+
assertTrue( result instanceof BSONObject, "Expected to retrieve a BSONObject but got '" + result.getClass() + "' instead." );
229+
BSONObject bson = (BSONObject) result;
230+
assertNotNull( bson.get( "date" ) );
231+
assertTrue( bson.get( "date" ) instanceof java.util.Date );
232+
233+
BSON.addDecodingHook( Date.class, tf );
234+
235+
d.decode( new ByteArrayInputStream( buf.toByteArray() ), cb );
236+
bson = (BSONObject) cb.get();
237+
assertNotNull( bson.get( "date" ) );
238+
assertTrue( bson.get( "date" ) instanceof TestDate );
239+
assertEquals( bson.get( "date" ), td );
240+
241+
// Check that the hooks registered
242+
assertNotNull( BSON.getDecodingHooks( Date.class ) );
243+
Vector expect = new Vector( 1 );
244+
expect.add( tf );
245+
assertEquals( BSON.getDecodingHooks( Date.class ), expect );
246+
assertTrue( BSON.getDecodingHooks( Date.class ).contains( tf ) );
247+
BSON.removeDecodingHook( Date.class, tf );
248+
assertFalse( BSON.getDecodingHooks( Date.class ).contains( tf ) );
249+
250+
}
251+
252+
private class TestDate {
253+
final int year;
254+
final int month;
255+
final int date;
256+
final int hour;
257+
final int minute;
258+
final int second;
259+
260+
public TestDate(int year , int month , int date , int hour , int minute , int second) {
261+
this.year = year;
262+
this.month = month;
263+
this.date = date;
264+
this.hour = hour;
265+
this.minute = minute;
266+
this.second = second;
267+
}
268+
269+
public TestDate(int year , int month , int date) {
270+
this( year , month , date , 0 , 0 , 0 );
271+
}
272+
273+
274+
@Override
275+
public boolean equals( Object other ){
276+
if ( this == other )
277+
return true;
278+
if ( !( other instanceof TestDate ) )
279+
return false;
280+
281+
TestDate otherDt = (TestDate) other;
282+
return ( otherDt.year == this.year && otherDt.month == this.month && otherDt.date == this.date && otherDt.hour == this.hour
283+
&& otherDt.minute == this.minute && otherDt.second == this.second );
284+
}
285+
286+
@Override
287+
public String toString(){
288+
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
289+
}
290+
}
291+
292+
private class TestDateTransformer implements Transformer {
293+
@SuppressWarnings("deprecation")
294+
public Object transform(Object o) {
295+
if (o instanceof TestDate) {
296+
TestDate td = (TestDate) o;
297+
return new java.util.Date(td.year, td.month, td.date, td.hour, td.minute, td.second);
298+
}
299+
else if (o instanceof java.util.Date) {
300+
Date d = (Date) o;
301+
return new TestDate(d.getYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
302+
}
303+
else return o;
304+
}
305+
}
160306
List<String> _data = new ArrayList<String>();
161307

162308

0 commit comments

Comments
 (0)