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
199 lines (157 loc) · 6.91 KB
/
Copy pathDBRefTest.java
File metadata and controls
199 lines (157 loc) · 6.91 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* 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 com.mongodb.util.TestCase;
import org.bson.BSONDecoder;
import org.bson.BasicBSONDecoder;
import org.bson.io.BasicOutputBuffer;
import org.bson.io.OutputBuffer;
import org.bson.types.ObjectId;
import org.testng.annotations.Test;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class DBRefTest extends TestCase {
public DBRefTest() {
try {
cleanupMongo = new Mongo( "127.0.0.1" );
cleanupDB = "com_monogodb_unittest_DBRefTest";
cleanupOtherDB = "com_monogodb_unittest_DBRefTest_other";
_db = cleanupMongo.getDB( cleanupDB );
_dbOther = cleanupMongo.getDB( cleanupOtherDB );
}
catch(UnknownHostException e) {
throw new MongoException("couldn't connect");
}
}
@Test(groups = {"basic"})
public void testEqualsAndHashCode() {
DBRef ref = new DBRef(_db, "foo.bar", 4);
DBRef other = new DBRef(_db, "foo.bar", 4);
assertEquals(ref, ref);
assertEquals(ref, other);
assertNotEquals(ref, new DBRefBase(_db, "foo.bar", 4));
assertEquals(ref.hashCode(), other.hashCode());
}
@Test(groups = {"basic"})
public void testNotEqualsAndHashCodeForDifferentDatabases() {
DBRef ref = new DBRef(_db, "foo.bar", 4);
DBRef other = new DBRef(_dbOther, "foo.bar", 4);
assertEquals(ref, ref);
assertEquals(other, other);
assertNotEquals(ref, other);
assertNotEquals(other, new DBRefBase(_db, "foo.bar", 4));
assertNotEquals(ref.hashCode(), other.hashCode());
}
@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\", \"$db\" : \"com_monogodb_unittest_DBRefTest\" }", ref.toString());
}
@Test(groups = {"basic"})
public void testDBRef(){
DBRef ref = new DBRef(_db, "hello", "world");
DBObject o = new BasicDBObject("!", ref);
DBEncoder encoder = DefaultDBEncoder.FACTORY.create();
OutputBuffer buf = new BasicOutputBuffer();
encoder.writeObject(buf, o);
DefaultDBCallback cb = new DefaultDBCallback( _db.getCollection("foo.bar") );
BSONDecoder decoder = new BasicBSONDecoder();
decoder.decode( buf.toByteArray() , cb );
DBObject read = cb.dbget();
assertEquals("{\"!\":{\"$ref\":\"hello\",\"$id\":\"world\",\"$db\":\"com_monogodb_unittest_DBRefTest\"}}", read.toString().replaceAll(" +", ""));
}
@Test(groups = {"basic"})
public void testDBRefWithNullDBCallbackState(){
DBRef ref = new DBRef(_db, "hello", "world");
DBObject o = new BasicDBObject("!", ref);
DBEncoder encoder = DefaultDBEncoder.FACTORY.create();
OutputBuffer buf = new BasicOutputBuffer();
encoder.writeObject(buf, o);
DefaultDBCallback cb = new DefaultDBCallback( null );
BSONDecoder decoder = new BasicBSONDecoder();
decoder.decode( buf.toByteArray() , cb );
DBObject read = cb.dbget();
assertEquals("{\"!\":{\"$ref\":\"hello\",\"$id\":\"world\"}}", read.toString().replaceAll( " +" , "" ));
}
@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;
DB _dbOther;
public static void main( String args[] ) {
(new DBRefTest()).runConsole();
}
}