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
209 lines (164 loc) · 7.84 KB
/
Copy pathDBRefTest.java
File metadata and controls
209 lines (164 loc) · 7.84 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
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2008-2014 MongoDB, 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.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class DBRefTest extends TestCase {
@Test
public void testEqualsAndHashCode() {
DBRef ref = new DBRef(getDatabase(), "foo.bar", 4);
DBRef other = new DBRef(getDatabase(), "foo.bar", 4);
assertEquals(ref, ref);
assertEquals(ref, other);
assertNotEquals(ref, new DBRefBase(getDatabase(), "foo.bar", 4));
assertEquals(ref.hashCode(), other.hashCode());
}
@Test
public void testDBRefBaseToString(){
ObjectId id = new ObjectId("123456789012345678901234");
DBRefBase ref = new DBRefBase(getDatabase(), "foo.bar", id);
assertEquals("{ \"$ref\" : \"foo.bar\", \"$id\" : \"123456789012345678901234\" }", ref.toString());
}
@Test
public void testDBRef(){
DBRef ref = new DBRef(getDatabase(), "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
public void testDBRefFetches(){
DBCollection coll = collection;
BasicDBObject obj = new BasicDBObject("_id", 321325243);
coll.save(obj);
DBRef ref = new DBRef(getDatabase(), coll.getName(), 321325243);
DBObject deref = ref.fetch();
assertTrue(deref != null);
assertEquals(321325243, ((Number)deref.get("_id")).intValue());
DBObject refobj = BasicDBObjectBuilder.start().add("$ref", coll.getName()).add("$id", 321325243).get();
deref = DBRef.fetch(getDatabase(), refobj);
assertTrue(deref != null);
assertEquals(321325243, ((Number)deref.get("_id")).intValue());
}
@SuppressWarnings("unchecked")
@Test
public void testRefListRoundTrip(){
DBCollection a = collection;
List<DBRef> refs = new ArrayList<DBRef>();
refs.add(new DBRef(getDatabase(), "other", 12));
refs.add(new DBRef(getDatabase(), "other", 14));
refs.add(new DBRef(getDatabase(), "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 = getDatabase().getCollection( collection.getName() + ".refroundtripa" );
DBCollection b = getDatabase().getCollection( collection.getName() + ".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( getDatabase() , a.getName() , 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 = getDatabase().getCollection( "b" );
b.drop();
DBRef ref = new DBRef( getDatabase() , "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());
}
@Test
public void testGetEntityWithSingleDBRefWithCompoundId() {
DBCollection a = getDatabase().getCollection("a");
a.drop();
BasicDBObject compoundId = new BasicDBObject("name", "someName").append("email", "test@example.com");
BasicDBObject entity = new BasicDBObject("_id", "testId").append("ref", new DBRef(getDatabase(), "fake", compoundId));
a.save(entity);
DBObject fetched = a.findOne(new BasicDBObject("_id", "testId"));
assertNotNull(fetched);
assertFalse(fetched.containsField("$id"));
assertEquals(fetched, entity);
}
@Test
public void testGetEntityWithArrayOfDBRefsWithCompoundIds() {
DBCollection a = getDatabase().getCollection("a");
a.drop();
BasicDBObject compoundId1 = new BasicDBObject("name", "someName").append("email", "test@example.com");
BasicDBObject compoundId2 = new BasicDBObject("name", "someName2").append("email", "test2@example.com");
BasicDBList listOfRefs = new BasicDBList();
listOfRefs.add(new DBRef(getDatabase(), "fake", compoundId1));
listOfRefs.add(new DBRef(getDatabase(), "fake", compoundId2));
BasicDBObject entity = new BasicDBObject("_id", "testId").append("refs", listOfRefs);
a.save(entity);
DBObject fetched = a.findOne(new BasicDBObject("_id", "testId"));
assertNotNull(fetched);
assertEquals(fetched, entity);
}
@Test
public void testGetEntityWithMapOfDBRefsWithCompoundIds() {
DBCollection base = getDatabase().getCollection("basecollection");
base.drop();
BasicDBObject compoundId1 = new BasicDBObject("name", "someName").append("email", "test@example.com");
BasicDBObject compoundId2 = new BasicDBObject("name", "someName2").append("email", "test2@example.com");
BasicDBObject mapOfRefs = new BasicDBObject()
.append("someName", new DBRef(getDatabase(), "compoundkeys", compoundId1))
.append("someName2", new DBRef(getDatabase(), "compoundkeys", compoundId2));
BasicDBObject entity = new BasicDBObject("_id", "testId").append("refs", mapOfRefs);
base.save(entity);
DBObject fetched = base.findOne(new BasicDBObject("_id", "testId"));
assertNotNull(fetched);
DBObject fetchedRefs = (DBObject) fetched.get("refs");
assertFalse(fetchedRefs.keySet().contains("$id"));
assertEquals(fetched, entity);
}
}