bson 1.12.5-java → 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +80 -0
  5. data/CONTRIBUTING.md +42 -0
  6. data/NOTICE +2 -0
  7. data/README.md +190 -0
  8. data/Rakefile +109 -0
  9. data/lib/bson-ruby.jar +0 -0
  10. data/lib/bson.rb +60 -87
  11. data/lib/bson/array.rb +104 -0
  12. data/lib/bson/binary.rb +193 -0
  13. data/lib/bson/boolean.rb +48 -0
  14. data/lib/bson/code.rb +109 -0
  15. data/lib/bson/code_with_scope.rb +120 -0
  16. data/lib/bson/document.rb +549 -0
  17. data/lib/bson/encodable.rb +86 -0
  18. data/lib/bson/environment.rb +98 -0
  19. data/lib/bson/false_class.rb +61 -0
  20. data/lib/bson/float.rb +82 -0
  21. data/lib/bson/hash.rb +84 -0
  22. data/lib/bson/int32.rb +59 -0
  23. data/lib/bson/int64.rb +59 -0
  24. data/lib/bson/integer.rb +185 -0
  25. data/lib/bson/json.rb +37 -0
  26. data/lib/bson/max_key.rb +70 -0
  27. data/lib/bson/min_key.rb +70 -0
  28. data/lib/bson/nil_class.rb +70 -0
  29. data/lib/bson/object_id.rb +395 -0
  30. data/lib/bson/regexp.rb +124 -0
  31. data/lib/bson/registry.rb +70 -0
  32. data/lib/bson/specialized.rb +74 -0
  33. data/lib/bson/string.rb +203 -0
  34. data/lib/bson/symbol.rb +87 -0
  35. data/lib/bson/time.rb +72 -0
  36. data/lib/bson/timestamp.rb +113 -0
  37. data/lib/bson/true_class.rb +61 -0
  38. data/lib/bson/undefined.rb +74 -0
  39. data/lib/bson/version.rb +17 -0
  40. data/spec/bson/array_spec.rb +58 -0
  41. data/spec/bson/binary_spec.rb +115 -0
  42. data/spec/bson/boolean_spec.rb +48 -0
  43. data/spec/bson/code_spec.rb +42 -0
  44. data/spec/bson/code_with_scope_spec.rb +74 -0
  45. data/spec/bson/document_spec.rb +778 -0
  46. data/spec/bson/false_class_spec.rb +28 -0
  47. data/spec/bson/float_spec.rb +29 -0
  48. data/spec/bson/hash_spec.rb +56 -0
  49. data/spec/bson/int32_spec.rb +28 -0
  50. data/spec/bson/int64_spec.rb +28 -0
  51. data/spec/bson/integer_spec.rb +76 -0
  52. data/spec/bson/json_spec.rb +53 -0
  53. data/spec/bson/max_key_spec.rb +75 -0
  54. data/spec/bson/min_key_spec.rb +75 -0
  55. data/spec/bson/nil_class_spec.rb +29 -0
  56. data/spec/bson/object_id_spec.rb +527 -0
  57. data/spec/bson/regexp_spec.rb +89 -0
  58. data/spec/bson/registry_spec.rb +55 -0
  59. data/spec/bson/string_spec.rb +298 -0
  60. data/spec/bson/symbol_spec.rb +55 -0
  61. data/spec/bson/time_spec.rb +43 -0
  62. data/spec/bson/timestamp_spec.rb +74 -0
  63. data/spec/bson/true_class_spec.rb +28 -0
  64. data/spec/bson/undefined_spec.rb +29 -0
  65. data/{lib/bson/types/dbref.rb → spec/bson_spec.rb} +22 -16
  66. data/spec/spec_helper.rb +32 -0
  67. data/spec/support/shared_examples.rb +95 -0
  68. metadata +116 -48
  69. metadata.gz.sig +1 -1
  70. data/VERSION +0 -1
  71. data/bin/b2json +0 -63
  72. data/bin/j2bson +0 -64
  73. data/bson.gemspec +0 -34
  74. data/ext/jbson/lib/java-bson.jar +0 -0
  75. data/ext/jbson/target/jbson.jar +0 -0
  76. data/lib/bson/bson_c.rb +0 -37
  77. data/lib/bson/bson_java.rb +0 -49
  78. data/lib/bson/bson_ruby.rb +0 -645
  79. data/lib/bson/byte_buffer.rb +0 -241
  80. data/lib/bson/exceptions.rb +0 -37
  81. data/lib/bson/grow.rb +0 -173
  82. data/lib/bson/ordered_hash.rb +0 -197
  83. data/lib/bson/support/hash_with_indifferent_access.rb +0 -174
  84. data/lib/bson/types/binary.rb +0 -52
  85. data/lib/bson/types/code.rb +0 -55
  86. data/lib/bson/types/min_max_keys.rb +0 -56
  87. data/lib/bson/types/object_id.rb +0 -226
  88. data/lib/bson/types/regex.rb +0 -116
  89. data/lib/bson/types/timestamp.rb +0 -72
@@ -0,0 +1,29 @@
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "spec_helper"
16
+
17
+ describe NilClass do
18
+
19
+ describe "#to_bson/#from_bson" do
20
+
21
+ let(:type) { 10.chr }
22
+ let(:obj) { nil }
23
+ let(:bson) { BSON::NO_VALUE }
24
+
25
+ it_behaves_like "a bson element"
26
+ it_behaves_like "a serializable bson element"
27
+ it_behaves_like "a deserializable bson element"
28
+ end
29
+ end
@@ -0,0 +1,527 @@
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "spec_helper"
16
+ require "yaml"
17
+
18
+ describe BSON::ObjectId do
19
+
20
+ describe "#==" do
21
+
22
+ context "when data is identical" do
23
+
24
+ let(:time) do
25
+ Time.now
26
+ end
27
+
28
+ let(:object_id) do
29
+ described_class.from_time(time)
30
+ end
31
+
32
+ let(:other_id) do
33
+ described_class.from_time(time)
34
+ end
35
+
36
+ it "returns true" do
37
+ expect(object_id).to eq(other_id)
38
+ end
39
+ end
40
+
41
+ context "when the data is different" do
42
+
43
+ let(:time) do
44
+ Time.now
45
+ end
46
+
47
+ let(:object_id) do
48
+ described_class.from_time(time)
49
+ end
50
+
51
+ it "returns false" do
52
+ expect(object_id).to_not eq(described_class.new)
53
+ end
54
+ end
55
+
56
+ context "when other is not an object id" do
57
+
58
+ it "returns false" do
59
+ expect(described_class.new).to_not eq(nil)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#===" do
65
+
66
+ let(:object_id) do
67
+ described_class.new
68
+ end
69
+
70
+ context "when comparing with another object id" do
71
+
72
+ context "when the data is equal" do
73
+
74
+ let(:other) do
75
+ described_class.from_string(object_id.to_s)
76
+ end
77
+
78
+ it "returns true" do
79
+ expect(object_id === other).to be_true
80
+ end
81
+ end
82
+
83
+ context "when the data is not equal" do
84
+
85
+ let(:other) do
86
+ described_class.new
87
+ end
88
+
89
+ it "returns false" do
90
+ expect(object_id === other).to be_false
91
+ end
92
+ end
93
+ end
94
+
95
+ context "when comparing to an object id class" do
96
+
97
+ it "returns false" do
98
+ expect(object_id === BSON::ObjectId).to be_false
99
+ end
100
+ end
101
+
102
+ context "when comparing with a string" do
103
+
104
+ context "when the data is equal" do
105
+
106
+ let(:other) do
107
+ object_id.to_s
108
+ end
109
+
110
+ it "returns true" do
111
+ expect(object_id === other).to be_true
112
+ end
113
+ end
114
+
115
+ context "when the data is not equal" do
116
+
117
+ let(:other) do
118
+ described_class.new.to_s
119
+ end
120
+
121
+ it "returns false" do
122
+ expect(object_id === other).to be_false
123
+ end
124
+ end
125
+ end
126
+
127
+ context "when comparing with a non string or object id" do
128
+
129
+ it "returns false" do
130
+ expect(object_id === "test").to be_false
131
+ end
132
+ end
133
+
134
+ context "when comparing with a non object id class" do
135
+
136
+ it "returns false" do
137
+ expect(object_id === String).to be_false
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "#<" do
143
+
144
+ let(:object_id) do
145
+ described_class.from_time(Time.utc(2012, 1, 1))
146
+ end
147
+
148
+ let(:other_id) do
149
+ described_class.from_time(Time.utc(2012, 1, 30))
150
+ end
151
+
152
+ context "when the generation time before the other" do
153
+
154
+ it "returns true" do
155
+ expect(object_id < other_id).to be_true
156
+ end
157
+ end
158
+
159
+ context "when the generation time is after the other" do
160
+
161
+ it "returns false" do
162
+ expect(other_id < object_id).to be_false
163
+ end
164
+ end
165
+ end
166
+
167
+ describe "#>" do
168
+
169
+ let(:object_id) do
170
+ described_class.from_time(Time.utc(2012, 1, 1))
171
+ end
172
+
173
+ let(:other_id) do
174
+ described_class.from_time(Time.utc(2012, 1, 30))
175
+ end
176
+
177
+ context "when the generation time before the other" do
178
+
179
+ it "returns false" do
180
+ expect(object_id > other_id).to be_false
181
+ end
182
+ end
183
+
184
+ context "when the generation time is after the other" do
185
+
186
+ it "returns true" do
187
+ expect(other_id > object_id).to be_true
188
+ end
189
+ end
190
+ end
191
+
192
+ describe "#<=>" do
193
+
194
+ let(:object_id) do
195
+ described_class.from_time(Time.utc(2012, 1, 1))
196
+ end
197
+
198
+ let(:other_id) do
199
+ described_class.from_time(Time.utc(2012, 1, 30))
200
+ end
201
+
202
+ context "when the generation time before the other" do
203
+
204
+ it "returns -1" do
205
+ expect(object_id <=> other_id).to eq(-1)
206
+ end
207
+ end
208
+
209
+ context "when the generation time is after the other" do
210
+
211
+ it "returns false" do
212
+ expect(other_id <=> object_id).to eq(1)
213
+ end
214
+ end
215
+ end
216
+
217
+ describe "#as_json" do
218
+
219
+ let(:object) do
220
+ described_class.new
221
+ end
222
+
223
+ it "returns the object id with $oid key" do
224
+ expect(object.as_json).to eq({ "$oid" => object.to_s })
225
+ end
226
+
227
+ it_behaves_like "a JSON serializable object"
228
+ end
229
+
230
+ describe "::BSON_TYPE" do
231
+
232
+ it "returns 0x07" do
233
+ expect(BSON::ObjectId::BSON_TYPE).to eq(7.chr)
234
+ end
235
+ end
236
+
237
+ describe "#bson_type" do
238
+
239
+ let(:code) do
240
+ described_class.new
241
+ end
242
+
243
+ it "returns 0x0D" do
244
+ expect(code.bson_type).to eq(BSON::ObjectId::BSON_TYPE)
245
+ end
246
+ end
247
+
248
+ describe "#eql" do
249
+
250
+ context "when data is identical" do
251
+
252
+ let(:time) do
253
+ Time.now
254
+ end
255
+
256
+ let(:object_id) do
257
+ described_class.from_time(time)
258
+ end
259
+
260
+ let(:other_id) do
261
+ described_class.from_time(time)
262
+ end
263
+
264
+ it "returns true" do
265
+ expect(object_id).to eql(other_id)
266
+ end
267
+ end
268
+
269
+ context "when the data is different" do
270
+
271
+ let(:time) do
272
+ Time.now
273
+ end
274
+
275
+ let(:object_id) do
276
+ described_class.from_time(time)
277
+ end
278
+
279
+ it "returns false" do
280
+ expect(object_id).to_not eql(described_class.new)
281
+ end
282
+ end
283
+
284
+ context "when other is not an object id" do
285
+
286
+ it "returns false" do
287
+ expect(described_class.new).to_not eql(nil)
288
+ end
289
+ end
290
+ end
291
+
292
+ describe ".from_string" do
293
+
294
+ context "when the string is valid" do
295
+
296
+ let(:string) do
297
+ "4e4d66343b39b68407000001"
298
+ end
299
+
300
+ let(:object_id) do
301
+ described_class.from_string(string)
302
+ end
303
+
304
+ it "initializes with the string's bytes" do
305
+ expect(object_id.to_s).to eq(string)
306
+ end
307
+ end
308
+
309
+ context "when the string is not valid" do
310
+
311
+ it "raises an error" do
312
+ expect {
313
+ described_class.from_string("asadsf")
314
+ }.to raise_error(BSON::ObjectId::Invalid)
315
+ end
316
+ end
317
+ end
318
+
319
+ describe ".from_time" do
320
+
321
+ context "when no unique option is provided" do
322
+
323
+ let(:time) do
324
+ Time.at((Time.now.utc - 64800).to_i).utc
325
+ end
326
+
327
+ let(:object_id) do
328
+ described_class.from_time(time)
329
+ end
330
+
331
+ it "sets the generation time" do
332
+ expect(object_id.generation_time).to eq(time)
333
+ end
334
+
335
+ it "does not include process or sequence information" do
336
+ expect(object_id.to_s =~ /\A[0-9a-f]{8}[0]{16}\Z/).to be_true
337
+ end
338
+ end
339
+
340
+ context "when a unique option is provided" do
341
+
342
+ let(:time) do
343
+ Time.at((Time.now.utc - 64800).to_i).utc
344
+ end
345
+
346
+ let(:object_id) do
347
+ described_class.from_time(time, :unique => true)
348
+ end
349
+
350
+ let(:non_unique) do
351
+ described_class.from_time(time, :unique => true)
352
+ end
353
+
354
+ it "creates a new unique object id" do
355
+ expect(object_id).to_not eq(non_unique)
356
+ end
357
+ end
358
+ end
359
+
360
+ describe "#generation_time" do
361
+
362
+ let(:time) do
363
+ Time.utc(2013, 1, 1)
364
+ end
365
+
366
+ let(:object_id) do
367
+ described_class.from_time(time)
368
+ end
369
+
370
+ it "returns the generation time" do
371
+ expect(object_id.generation_time).to eq(time)
372
+ end
373
+ end
374
+
375
+ describe "#hash" do
376
+
377
+ let(:object_id) do
378
+ described_class.new
379
+ end
380
+
381
+ it "returns a hash of the raw bytes" do
382
+ expect(object_id.hash).to eq(object_id.to_bson.hash)
383
+ end
384
+ end
385
+
386
+ describe "#inspect" do
387
+
388
+ let(:object_id) do
389
+ described_class.new
390
+ end
391
+
392
+ it "returns the inspection with the object id to_s" do
393
+ expect(object_id.inspect).to eq("BSON::ObjectId('#{object_id.to_s}')")
394
+ end
395
+ end
396
+
397
+ describe ".legal?" do
398
+
399
+ context "when the string is too short to be an object id" do
400
+
401
+ it "returns false" do
402
+ expect(described_class).to_not be_legal("a" * 23)
403
+ end
404
+ end
405
+
406
+ context "when the string contains invalid hex characters" do
407
+
408
+ it "returns false" do
409
+ expect(described_class).to_not be_legal("y" + "a" * 23)
410
+ end
411
+ end
412
+
413
+ context "when the string is a valid object id" do
414
+
415
+ it "returns true" do
416
+ expect(described_class).to be_legal("a" * 24)
417
+ end
418
+ end
419
+
420
+ context "when checking against another object id" do
421
+
422
+ let(:object_id) do
423
+ described_class.new
424
+ end
425
+
426
+ it "returns true" do
427
+ expect(described_class).to be_legal(object_id)
428
+ end
429
+ end
430
+ end
431
+
432
+ describe "#marshal_dump" do
433
+
434
+ let(:object_id) do
435
+ described_class.new
436
+ end
437
+
438
+ let(:dumped) do
439
+ Marshal.dump(object_id)
440
+ end
441
+
442
+ it "dumps the raw bytes data" do
443
+ expect(Marshal.load(dumped)).to eq(object_id)
444
+ end
445
+ end
446
+
447
+ describe "#marshal_load" do
448
+
449
+ context "when the object id was dumped in the old format" do
450
+
451
+ let(:legacy) do
452
+ "\x04\bo:\x13BSON::ObjectId\x06:\n" +
453
+ "@data[\x11iUi\x01\xE2i,i\x00i\x00i\x00i\x00i\x00i\x00i\x00i\x00i\x00"
454
+ end
455
+
456
+ let(:object_id) do
457
+ Marshal.load(legacy)
458
+ end
459
+
460
+ let(:expected) do
461
+ described_class.from_time(Time.utc(2013, 1, 1))
462
+ end
463
+
464
+ it "properly loads the object id" do
465
+ expect(object_id).to eq(expected)
466
+ end
467
+
468
+ it "removes the bad legacy data" do
469
+ object_id.to_bson
470
+ expect(object_id.instance_variable_get(:@data)).to be_nil
471
+ end
472
+ end
473
+ end
474
+
475
+ describe "#to_bson/#from_bson" do
476
+
477
+ let(:time) { Time.utc(2013, 1, 1) }
478
+ let(:type) { 7.chr }
479
+ let(:obj) { described_class.from_time(time) }
480
+ let(:bson) { obj.to_bson }
481
+
482
+ it_behaves_like "a bson element"
483
+ it_behaves_like "a serializable bson element"
484
+ it_behaves_like "a deserializable bson element"
485
+ end
486
+
487
+ describe "#to_s" do
488
+
489
+ let(:time) do
490
+ Time.utc(2013, 1, 1)
491
+ end
492
+
493
+ let(:expected) do
494
+ "50e227000000000000000000"
495
+ end
496
+
497
+ let(:object_id) do
498
+ described_class.from_time(time)
499
+ end
500
+
501
+ it "returns a hex string representation of the id" do
502
+ expect(object_id.to_s).to eq(expected)
503
+ end
504
+
505
+ unless BSON::Environment.ruby_18?
506
+
507
+ it "returns the string in UTF-8" do
508
+ expect(object_id.to_s.encoding).to eq(Encoding.find(BSON::UTF8))
509
+ end
510
+ end
511
+
512
+ it "converts to a readable yaml string" do
513
+ expect(YAML.dump(object_id.to_s)).to include(expected)
514
+ end
515
+ end
516
+
517
+ context "when the class is loaded" do
518
+
519
+ let(:registered) do
520
+ BSON::Registry.get(BSON::ObjectId::BSON_TYPE)
521
+ end
522
+
523
+ it "registers the type" do
524
+ expect(registered).to eq(described_class)
525
+ end
526
+ end
527
+ end