1
1
/*
2
- * Copyright (c) 2008-2014 MongoDB, Inc.
2
+ * Copyright 2008-2017 MongoDB, Inc.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package com.mongodb
18
18
19
- import org.bson.BSONDecoder
20
- import org.bson.BasicBSONDecoder
21
- import org.bson.io.BasicOutputBuffer
22
- import org.bson.io.OutputBuffer
23
19
import spock.lang.Specification
24
20
25
21
class DBRefSpecification extends Specification {
@@ -60,10 +56,10 @@ class DBRefSpecification extends Specification {
60
56
61
57
def ' equivalent instances should be equal and have the same hash code' () {
62
58
given :
63
- DBRef referenceA = new DBRef (' foo.bar' , 4 );
64
- DBRef referenceB = new DBRef (' foo.bar' , 4 );
65
- DBRef referenceC = new DBRef (' mydb' , ' foo.bar' , 4 );
66
- DBRef referenceD = new DBRef (' mydb' , ' foo.bar' , 4 );
59
+ DBRef referenceA = new DBRef (' foo.bar' , 4 )
60
+ DBRef referenceB = new DBRef (' foo.bar' , 4 )
61
+ DBRef referenceC = new DBRef (' mydb' , ' foo.bar' , 4 )
62
+ DBRef referenceD = new DBRef (' mydb' , ' foo.bar' , 4 )
67
63
68
64
expect :
69
65
referenceA. equals(referenceA)
@@ -75,11 +71,11 @@ class DBRefSpecification extends Specification {
75
71
76
72
def ' non-equivalent instances should not be equal and have different hash codes' () {
77
73
given :
78
- DBRef referenceA = new DBRef (' foo.bar' , 4 );
79
- DBRef referenceB = new DBRef (' foo.baz' , 4 );
80
- DBRef referenceC = new DBRef (' foo.bar' , 5 );
81
- DBRef referenceD = new DBRef (' mydb' , ' foo.bar' , 4 );
82
- DBRef referenceE = new DBRef (' yourdb' , ' foo.bar' , 4 );
74
+ DBRef referenceA = new DBRef (' foo.bar' , 4 )
75
+ DBRef referenceB = new DBRef (' foo.baz' , 4 )
76
+ DBRef referenceC = new DBRef (' foo.bar' , 5 )
77
+ DBRef referenceD = new DBRef (' mydb' , ' foo.bar' , 4 )
78
+ DBRef referenceE = new DBRef (' yourdb' , ' foo.bar' , 4 )
83
79
84
80
expect :
85
81
! referenceA. equals(null )
@@ -100,55 +96,17 @@ class DBRefSpecification extends Specification {
100
96
new DBRef (' mydb' , ' foo.bar' , 4 ). toString() == ' { "$ref" : "foo.bar", "$id" : "4, "$db" : "mydb" }'
101
97
}
102
98
103
- def ' should encode and decode DBRefs' () {
104
- given :
105
- DBRef reference = new DBRef (' coll' , ' hello world' );
106
- DBObject document = new BasicDBObject (' !' , reference);
107
- OutputBuffer buffer = new BasicOutputBuffer ();
108
-
109
- when :
110
- DefaultDBEncoder . FACTORY . create(). writeObject(buffer, document);
111
- DefaultDBCallback callback = new DefaultDBCallback (null );
112
- BSONDecoder decoder = new BasicBSONDecoder ();
113
- decoder. decode(buffer. toByteArray(), callback);
114
- DBRef decoded = ((DBObject ) callback. get()). get(' !' );
115
-
116
- then :
117
- decoded. databaseName == null
118
- decoded. collectionName == ' coll'
119
- decoded. id == ' hello world'
120
- }
121
-
122
- def ' should encode and decode DBRefs with a database name' () {
123
- given :
124
- DBRef reference = new DBRef (' db' , ' coll' , ' hello world' );
125
- DBObject document = new BasicDBObject (' !' , reference);
126
- OutputBuffer buffer = new BasicOutputBuffer ();
127
-
128
- when :
129
- DefaultDBEncoder . FACTORY . create(). writeObject(buffer, document);
130
- DefaultDBCallback callback = new DefaultDBCallback (null );
131
- BSONDecoder decoder = new BasicBSONDecoder ();
132
- decoder. decode(buffer. toByteArray(), callback);
133
- DBRef decoded = ((DBObject ) callback. get()). get(' !' );
134
-
135
- then :
136
- decoded. databaseName == ' db'
137
- decoded. collectionName == ' coll'
138
- decoded. id == ' hello world'
139
- }
140
-
141
99
def ' testSerialization' () throws Exception {
142
100
given :
143
- DBRef originalDBRef = new DBRef (' col' , 42 );
101
+ DBRef originalDBRef = new DBRef (' col' , 42 )
144
102
145
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
146
- ObjectOutputStream objectOutputStream = new ObjectOutputStream (outputStream);
103
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()
104
+ ObjectOutputStream objectOutputStream = new ObjectOutputStream (outputStream)
147
105
148
106
when :
149
- objectOutputStream. writeObject(originalDBRef);
150
- ObjectInputStream objectInputStream = new ObjectInputStream (new ByteArrayInputStream (outputStream. toByteArray()));
151
- DBRef deserializedDBRef = (DBRef ) objectInputStream. readObject();
107
+ objectOutputStream. writeObject(originalDBRef)
108
+ ObjectInputStream objectInputStream = new ObjectInputStream (new ByteArrayInputStream (outputStream. toByteArray()))
109
+ DBRef deserializedDBRef = (DBRef ) objectInputStream. readObject()
152
110
153
111
then :
154
112
originalDBRef == deserializedDBRef
0 commit comments