2323import java .util .*;
2424import java .util .regex .Pattern ;
2525
26+ import org .bson .BSON ;
2627import org .bson .Transformer ;
2728import org .bson .types .*;
2829import org .testng .annotations .Test ;
@@ -184,7 +185,7 @@ public void testBinary()
184185 bb .order ( Bytes .ORDER );
185186 bb .putInt ( 5 );
186187 bb .put ( "eliot" .getBytes () );
187- out .put ( "a" , new Binary ( ( byte ) 2 , raw ) );
188+ out .put ( "a" , "eliot" . getBytes ( ) );
188189 c .save ( out );
189190
190191 out = c .findOne ();
@@ -198,6 +199,39 @@ public void testBinary()
198199 assertEquals ( Util .toHex ( raw ) , Util .toHex ( blah .getData () ) );
199200 }
200201
202+ }
203+
204+ @ Test
205+ public void testBinaryOld ()
206+ throws MongoException {
207+ DBCollection c = _db .getCollection ( "testBinary" );
208+ c .drop ();
209+ c .save ( BasicDBObjectBuilder .start ().add ( "a" , "eliot" .getBytes () ).get () );
210+
211+ DBObject out = c .findOne ();
212+ byte [] b = (byte [])(out .get ( "a" ) );
213+ assertEquals ( "eliot" , new String ( b ) );
214+
215+ {
216+ byte [] raw = new byte [9 ];
217+ ByteBuffer bb = ByteBuffer .wrap ( raw );
218+ bb .order ( Bytes .ORDER );
219+ bb .putInt ( 5 );
220+ bb .put ( "eliot" .getBytes () );
221+ out .put ( "a" , new Binary ( BSON .B_BINARY , "eliot" .getBytes () ) );
222+ c .save ( out );
223+
224+ out = c .findOne ();
225+ Binary blah = (Binary )(out .get ( "a" ) );
226+ assertEquals ( "eliot" , new String ( blah .getData () ) );
227+
228+ out .put ( "a" , new Binary ( (byte )111 , raw ) );
229+ c .save ( out );
230+ blah = (Binary )c .findOne ().get ( "a" );
231+ assertEquals ( 111 , blah .getType () );
232+ assertEquals ( Util .toHex ( raw ) , Util .toHex ( blah .getData () ) );
233+ }
234+
201235 }
202236 @ Test
203237 public void testUUID ()
0 commit comments