@@ -24,131 +24,114 @@ import org.bson.json.JsonReader
24
24
import org.bson.json.JsonWriter
25
25
import spock.lang.Specification
26
26
27
- /**
28
- *
29
- */
30
27
@SuppressWarnings (' UnnecessaryObjectReferences' )
31
- class LimitedLookAheadMarkSpecification extends Specification {
32
-
33
- static BsonDocument bsonDoc
34
- static BasicOutputBuffer buffer
35
- static StringWriter stringWriter
36
-
37
- def setupSpec () {
38
- bsonDoc = new BsonDocument ()
39
- buffer = new BasicOutputBuffer ()
40
- stringWriter = new StringWriter ()
41
- }
42
-
43
- def cleanup () {
44
- bsonDoc = new BsonDocument ()
45
- buffer = new BasicOutputBuffer ()
46
- stringWriter = new StringWriter ()
47
- }
28
+ class LimitedLookaheadMarkSpecification extends Specification {
48
29
49
30
def ' should throw if mark without resetting previous mark' (BsonWriter writer) {
50
31
given :
51
32
writer. with {
52
- writeStartDocument()
53
- writeInt64(' int64' , 52L )
54
- writeEndDocument()
55
- }
33
+ writeStartDocument()
34
+ writeInt64(' int64' , 52L )
35
+ writeEndDocument()
36
+ }
56
37
57
- when :
58
38
BsonReader reader
59
39
if (writer instanceof BsonDocumentWriter ) {
60
- reader = new BsonDocumentReader (bsonDoc )
40
+ reader = new BsonDocumentReader (writer . document )
61
41
} else if (writer instanceof BsonBinaryWriter ) {
62
- BasicOutputBuffer buffer = (BasicOutputBuffer )writer. getBsonOutput();
42
+ BasicOutputBuffer buffer = (BasicOutputBuffer ) writer. getBsonOutput();
63
43
reader = new BsonBinaryReader (new ByteBufferBsonInput (buffer. getByteBuffers(). get(0 )), true )
64
- } else {
65
- reader = new JsonReader (stringWriter . toString())
44
+ } else if (writer instanceof JsonWriter ) {
45
+ reader = new JsonReader (writer . writer . toString())
66
46
}
47
+
67
48
reader. readStartDocument()
68
49
reader. mark()
50
+
51
+ when :
69
52
reader. mark()
70
53
71
54
then :
72
55
thrown(BSONException )
73
56
74
57
where :
75
58
writer << [
76
- new BsonDocumentWriter (bsonDoc ),
77
- new BsonBinaryWriter (buffer , false ),
78
- new JsonWriter (stringWriter )
59
+ new BsonDocumentWriter (new BsonDocument () ),
60
+ new BsonBinaryWriter (new BasicOutputBuffer () , false ),
61
+ new JsonWriter (new StringWriter () )
79
62
]
80
- }
63
+ }
81
64
82
65
def ' should throw if reset without mark' (BsonWriter writer) {
83
- given :
84
- writer. with {
85
- writeStartDocument()
86
- writeInt64(' int64' , 52L )
87
- writeEndDocument()
88
- }
66
+ given :
67
+ writer. with {
68
+ writeStartDocument()
69
+ writeInt64(' int64' , 52L )
70
+ writeEndDocument()
71
+ }
72
+
73
+ BsonReader reader
74
+ if (writer instanceof BsonDocumentWriter ) {
75
+ reader = new BsonDocumentReader (writer. document)
76
+ } else if (writer instanceof BsonBinaryWriter ) {
77
+ BasicOutputBuffer buffer = (BasicOutputBuffer ) writer. getBsonOutput();
78
+ reader = new BsonBinaryReader (new ByteBufferBsonInput (buffer. getByteBuffers(). get(0 )), true )
79
+ } else if (writer instanceof JsonWriter ) {
80
+ reader = new JsonReader (writer. writer. toString())
81
+ }
89
82
90
- when :
91
- BsonReader reader
92
- if (writer instanceof BsonDocumentWriter ) {
93
- reader = new BsonDocumentReader (bsonDoc)
94
- } else if (writer instanceof BsonBinaryWriter ) {
95
- BasicOutputBuffer buffer = (BasicOutputBuffer )writer. getBsonOutput();
96
- reader = new BsonBinaryReader (new ByteBufferBsonInput (buffer. getByteBuffers(). get(0 )), true )
97
- } else {
98
- reader = new JsonReader (stringWriter. toString())
99
- }
83
+ reader. readStartDocument()
100
84
101
- reader . readStartDocument()
102
- reader. reset()
85
+ when :
86
+ reader. reset()
103
87
104
- then :
105
- thrown(BSONException )
88
+ then :
89
+ thrown(BSONException )
106
90
107
- where :
108
- writer << [
109
- new BsonDocumentWriter (bsonDoc ),
110
- new BsonBinaryWriter (buffer , false ),
111
- new JsonWriter (stringWriter )
112
- ]
113
- }
91
+ where :
92
+ writer << [
93
+ new BsonDocumentWriter (new BsonDocument () ),
94
+ new BsonBinaryWriter (new BasicOutputBuffer () , false ),
95
+ new JsonWriter (new StringWriter () )
96
+ ]
97
+ }
114
98
115
- def ' Lookahead should work at various states' (BsonWriter writer) {
116
- given :
117
- writer. with {
118
- writeStartDocument()
119
- writeInt64(' int64' , 52L )
120
- writeStartArray(' array' )
121
- writeInt32(1 )
122
- writeInt64(2L )
123
- writeStartArray()
124
- writeInt32(3 )
125
- writeInt32(4 )
126
- writeEndArray()
127
- writeStartDocument()
128
- writeInt32(' a' , 5 )
129
- writeEndDocument()
130
- writeNull()
131
- writeEndArray()
132
- writeStartDocument(' document' )
133
- writeInt32(' a' , 6 )
134
- writeEndDocument()
135
- writeEndDocument()
136
- }
99
+ def ' Lookahead should work at various states' (BsonWriter writer) {
100
+ given :
101
+ writer. with {
102
+ writeStartDocument()
103
+ writeInt64(' int64' , 52L )
104
+ writeStartArray(' array' )
105
+ writeInt32(1 )
106
+ writeInt64(2L )
107
+ writeStartArray()
108
+ writeInt32(3 )
109
+ writeInt32(4 )
110
+ writeEndArray()
111
+ writeStartDocument()
112
+ writeInt32(' a' , 5 )
113
+ writeEndDocument()
114
+ writeNull()
115
+ writeEndArray()
116
+ writeStartDocument(' document' )
117
+ writeInt32(' a' , 6 )
118
+ writeEndDocument()
119
+ writeEndDocument()
120
+ }
137
121
138
122
139
- when :
140
- BsonReader reader
141
- if (writer instanceof BsonDocumentWriter ) {
142
- reader = new BsonDocumentReader (bsonDoc )
143
- } else if (writer instanceof BsonBinaryWriter ) {
144
- BasicOutputBuffer buffer = (BasicOutputBuffer )writer. getBsonOutput();
123
+ when :
124
+ BsonReader reader
125
+ if (writer instanceof BsonDocumentWriter ) {
126
+ reader = new BsonDocumentReader (writer . document )
127
+ } else if (writer instanceof BsonBinaryWriter ) {
128
+ BasicOutputBuffer buffer = (BasicOutputBuffer ) writer. getBsonOutput();
145
129
reader = new BsonBinaryReader (new ByteBufferBsonInput (buffer. getByteBuffers(). get(0 )), true )
146
- } else {
147
- reader = new JsonReader (stringWriter . toString())
148
- }
130
+ } else if (writer instanceof JsonWriter ) {
131
+ reader = new JsonReader (writer . writer . toString())
132
+ }
149
133
150
134
then :
151
-
152
135
reader. readStartDocument()
153
136
// mark beginning of document * 1
154
137
reader. mark()
@@ -253,9 +236,9 @@ class LimitedLookAheadMarkSpecification extends Specification {
253
236
254
237
where :
255
238
writer << [
256
- new BsonDocumentWriter (bsonDoc ),
257
- new BsonBinaryWriter (buffer , false ),
258
- // new JsonWriter(stringWriter )
239
+ new BsonDocumentWriter (new BsonDocument () ),
240
+ new BsonBinaryWriter (new BasicOutputBuffer () , false ),
241
+ new JsonWriter (new StringWriter () )
259
242
]
260
243
}
261
244
}
0 commit comments