forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBTest.java
More file actions
144 lines (114 loc) · 4.05 KB
/
DBTest.java
File metadata and controls
144 lines (114 loc) · 4.05 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
/**
* 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.io.*;
import java.net.UnknownHostException;
import java.util.*;
import java.util.regex.*;
import org.testng.annotations.Test;
import com.mongodb.util.*;
public class DBTest extends TestCase {
public DBTest()
throws UnknownHostException {
super();
cleanupMongo = new Mongo( "127.0.0.1" );
cleanupDB = "com_mongodb_unittest_DBTest";
_db = cleanupMongo.getDB( cleanupDB );
}
@Test(groups = {"basic"})
public void testCreateCollection() {
_db.getCollection( "foo1" ).drop();
_db.getCollection( "foo2" ).drop();
_db.getCollection( "foo3" ).drop();
_db.getCollection( "foo4" ).drop();
BasicDBObject o1 = new BasicDBObject("capped", false);
DBCollection c = _db.createCollection("foo1", o1);
DBObject o2 = BasicDBObjectBuilder.start().add("capped", true)
.add("size", 100000).add("max", 10).get();
c = _db.createCollection("foo2", o2);
for (int i=0; i<30; i++) {
c.insert(new BasicDBObject("x", i));
}
assertTrue(c.find().count() <= 10);
DBObject o3 = BasicDBObjectBuilder.start().add("capped", true)
.add("size", 1000).add("max", 2).get();
c = _db.createCollection("foo3", o3);
for (int i=0; i<30; i++) {
c.insert(new BasicDBObject("x", i));
}
assertEquals(c.find().count(), 2);
try {
DBObject o4 = BasicDBObjectBuilder.start().add("capped", true)
.add("size", -20).get();
c = _db.createCollection("foo4", o4);
}
catch(MongoException e) {
return;
}
assertEquals(0, 1);
}
@Test(groups = {"basic"})
public void testForCollectionExistence()
{
_db.getCollection( "foo1" ).drop();
_db.getCollection( "foo2" ).drop();
_db.getCollection( "foo3" ).drop();
_db.getCollection( "foo4" ).drop();
assertFalse(_db.collectionExists( "foo1" ));
BasicDBObject o1 = new BasicDBObject("capped", false);
DBCollection c = _db.createCollection("foo1", o1);
assertTrue(_db.collectionExists( "foo1" ), "Collection 'foo' was supposed to be created, but 'collectionExists' did not return true.");
assertTrue(_db.collectionExists( "FOO1" ));
assertTrue(_db.collectionExists( "fOo1" ));
_db.getCollection( "foo1" ).drop();
assertFalse(_db.collectionExists( "foo1" ));
}
/*public static class Person extends DBObject {
public Person(){
}
Person( String name ){
_name = name;
}
public String getName(){
return _name;
}
public void setName(String name){
_name = name;
}
String _name;
}
public DBTest()
throws IOException {
_db = new Mongo( "127.0.0.1" , "dbbasetest" );
}
@Test
public void test1(){
DBCollection c = _db.getCollection( "persen.test1" );
c.drop();
c.setObjectClass( Person.class );
Person p = new Person( "eliot" );
c.save( p );
DBObject out = c.findOne();
assertEquals( "eliot" , out.get( "Name" ) );
assertTrue( out instanceof Person , "didn't come out as Person" );
}
*/
final DB _db;
public static void main( String args[] )
throws Exception {
(new DBTest()).runConsole();
}
}