forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBRefTest.java
More file actions
156 lines (120 loc) · 5.07 KB
/
DBRefTest.java
File metadata and controls
156 lines (120 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import java.net.*;
import java.util.*;
import org.bson.*;
import org.bson.types.*;
import org.testng.annotations.*;
import com.mongodb.util.*;
public class DBRefTest extends TestCase {
public DBRefTest() {
try {
cleanupMongo = new Mongo( "127.0.0.1" );
cleanupDB = "com_monogodb_unittest_DBRefTest";
_db = cleanupMongo.getDB( cleanupDB );
}
catch(UnknownHostException e) {
throw new MongoException("couldn't connect");
}
}
@Test(groups = {"basic"})
public void testDBRefBaseToString(){
ObjectId id = new ObjectId("123456789012345678901234");
DBRefBase ref = new DBRefBase(_db, "foo.bar", id);
assertEquals("{ \"$ref\" : \"foo.bar\", \"$id\" : \"123456789012345678901234\" }", ref.toString());
}
@Test(groups = {"basic"})
public void testDBRef(){
DBRef ref = new DBRef(_db, "hello", (Object)"world");
DBObject o = new BasicDBObject("!", ref);
OutMessage out = new OutMessage( cleanupMongo );
out.putObject( o );
DefaultDBCallback cb = new DefaultDBCallback( null );
BSONDecoder decoder = new BasicBSONDecoder();
decoder.decode( out.toByteArray() , cb );
DBObject read = cb.dbget();
String correct = null;
correct = "{\"!\":{\"$ref\":\"hello\",\"$id\":\"world\"}}";
String got = read.toString().replaceAll( " +" , "" );
assertEquals( correct , got );
}
@Test(groups = {"basic"})
public void testDBRefFetches(){
DBCollection coll = _db.getCollection("x");
coll.drop();
BasicDBObject obj = new BasicDBObject("_id", 321325243);
coll.save(obj);
DBRef ref = new DBRef(_db, "x", 321325243);
DBObject deref = ref.fetch();
assertTrue(deref != null);
assertEquals(321325243, ((Number)deref.get("_id")).intValue());
DBObject refobj = BasicDBObjectBuilder.start().add("$ref", "x").add("$id", 321325243).get();
deref = DBRef.fetch(_db, refobj);
assertTrue(deref != null);
assertEquals(321325243, ((Number)deref.get("_id")).intValue());
}
@SuppressWarnings("unchecked")
@Test
public void testRefListRoundTrip(){
DBCollection a = _db.getCollection( "reflistfield" );
List<DBRef> refs = new ArrayList<DBRef>();
refs.add(new DBRef(_db, "other", 12));
refs.add(new DBRef(_db, "other", 14));
refs.add(new DBRef(_db, "other", 16));
a.save( BasicDBObjectBuilder.start( "refs" , refs).get() );
DBObject loaded = a.findOne();
assertNotNull( loaded );
List<DBRef> refsLoaded = (List<DBRef>) loaded.get("refs");
assertNotNull( refsLoaded );
assertEquals(3, refsLoaded.size());
assertEquals(DBRef.class, refsLoaded.get(0).getClass());
assertEquals(12, refsLoaded.get(0).getId());
assertEquals(14, refsLoaded.get(1).getId());
assertEquals(16, refsLoaded.get(2).getId());
}
@Test
public void testRoundTrip(){
DBCollection a = _db.getCollection( "refroundtripa" );
DBCollection b = _db.getCollection( "refroundtripb" );
a.drop();
b.drop();
a.save( BasicDBObjectBuilder.start( "_id" , 17 ).add( "n" , 111 ).get() );
b.save( BasicDBObjectBuilder.start( "n" , 12 ).add( "l" , new DBRef( _db , "refroundtripa" , 17 ) ).get() );
assertEquals( 12 , b.findOne().get( "n" ) );
assertEquals( DBRef.class , b.findOne().get( "l" ).getClass() );
assertEquals( 111 , ((DBRef)(b.findOne().get( "l" ))).fetch().get( "n" ) );
}
@Test
public void testFindByDBRef(){
DBCollection b = _db.getCollection( "b" );
b.drop();
DBRef ref = new DBRef( _db , "fake" , 17 );
b.save( BasicDBObjectBuilder.start( "n" , 12 ).add( "l" , ref ).get() );
assertEquals( 12 , b.findOne().get( "n" ) );
assertEquals( DBRef.class , b.findOne().get( "l" ).getClass() );
DBObject loaded = b.findOne(BasicDBObjectBuilder.start( "l" , ref ).get() );
assertEquals( 12 , loaded.get( "n" ) );
assertEquals( DBRef.class , loaded.get( "l" ).getClass() );
assertEquals( ref.getId(), ((DBRef)loaded.get( "l" )).getId());
assertEquals( ref.getRef(), ((DBRef)loaded.get( "l" )).getRef());
assertEquals( ref.getDB(), ((DBRef)loaded.get( "l" )).getDB());
}
DB _db;
public static void main( String args[] ) {
(new DBRefTest()).runConsole();
}
}