/src/test/com/mongodb/DBCollectionTest.java

https://github.com/efroese/mongo-java-driver · Java · 253 lines · 145 code · 48 blank · 60 comment · 1 complexity · 6bf8fcb1bc9ac70e9b6d94eeb7ef13e3 MD5 · raw file

  1. /**
  2. * Copyright (C) 2008 10gen Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb;
  17. import java.io.IOException;
  18. import java.util.List;
  19. import org.bson.types.*;
  20. import org.testng.annotations.Test;
  21. import com.mongodb.util.TestCase;
  22. public class DBCollectionTest extends TestCase {
  23. public DBCollectionTest()
  24. throws IOException , MongoException {
  25. super();
  26. cleanupMongo = new Mongo( "127.0.0.1" );
  27. cleanupDB = "com_mongodb_unittest_DBCollectionTest";
  28. _db = cleanupMongo.getDB( cleanupDB );
  29. }
  30. @Test(groups = {"basic"})
  31. public void testMultiInsert() {
  32. DBCollection c = _db.getCollection("testmultiinsert");
  33. c.drop();
  34. DBObject obj = c.findOne();
  35. assertEquals(obj, null);
  36. DBObject inserted1 = BasicDBObjectBuilder.start().add("x",1).add("y",2).get();
  37. DBObject inserted2 = BasicDBObjectBuilder.start().add("x",3).add("y",3).get();
  38. c.insert(inserted1,inserted2);
  39. c.insert(new DBObject[] {inserted1,inserted2});
  40. }
  41. /*
  42. TODO: fix... build is broken.
  43. @Test(groups = {"basic"})
  44. public void testFindOne() {
  45. DBCollection c = _db.getCollection("test");
  46. c.drop();
  47. DBObject obj = c.findOne();
  48. assertEquals(obj, null);
  49. obj = c.findOne(null);
  50. assertEquals(obj, null);
  51. obj = c.findOne(null, null);
  52. assertEquals(obj, null);
  53. // Test that findOne works when fields is specified but no match is found
  54. // *** This is a Regression test for JAVA-411 ***
  55. obj = c.findOne(null, new BasicDBObject("_id", true));
  56. assertEquals(obj, null);
  57. DBObject inserted = BasicDBObjectBuilder.start().add("x",1).add("y",2).get();
  58. c.insert(inserted);
  59. c.insert(BasicDBObjectBuilder.start().add("_id", 123).add("x",2).add("z",2).get());
  60. obj = c.findOne(123);
  61. assertEquals(obj.get("_id"), 123);
  62. assertEquals(obj.get("x"), 2);
  63. assertEquals(obj.get("z"), 2);
  64. obj = c.findOne(123, new BasicDBObject("x", 1));
  65. assertEquals(obj.get("_id"), 123);
  66. assertEquals(obj.get("x"), 2);
  67. assertEquals(obj.containsField("z"), false);
  68. obj = c.findOne(new BasicDBObject("x", 1));
  69. assertEquals(obj.get("x"), 1);
  70. assertEquals(obj.get("y"), 2);
  71. obj = c.findOne(new BasicDBObject("x", 1), new BasicDBObject("y", 1));
  72. assertEquals(obj.containsField("x"), false);
  73. assertEquals(obj.get("y"), 2);
  74. }
  75. */
  76. @Test
  77. public void testDropIndex(){
  78. DBCollection c = _db.getCollection( "dropindex1" );
  79. c.drop();
  80. c.save( new BasicDBObject( "x" , 1 ) );
  81. assertEquals( 1 , c.getIndexInfo().size() );
  82. c.ensureIndex( new BasicDBObject( "x" , 1 ) );
  83. assertEquals( 2 , c.getIndexInfo().size() );
  84. c.dropIndexes();
  85. assertEquals( 1 , c.getIndexInfo().size() );
  86. c.ensureIndex( new BasicDBObject( "x" , 1 ) );
  87. assertEquals( 2 , c.getIndexInfo().size() );
  88. c.ensureIndex( new BasicDBObject( "y" , 1 ) );
  89. assertEquals( 3 , c.getIndexInfo().size() );
  90. c.dropIndex( new BasicDBObject( "x" , 1 ) );
  91. assertEquals( 2 , c.getIndexInfo().size() );
  92. }
  93. @Test
  94. public void testGenIndexName(){
  95. BasicDBObject o = new BasicDBObject();
  96. o.put( "x" , 1 );
  97. assertEquals("x_1", DBCollection.genIndexName(o));
  98. o.put( "x" , "1" );
  99. assertEquals("x_1", DBCollection.genIndexName(o));
  100. o.put( "x" , "2d" );
  101. assertEquals("x_2d", DBCollection.genIndexName(o));
  102. o.put( "y" , -1 );
  103. assertEquals("x_2d_y_-1", DBCollection.genIndexName(o));
  104. o.put( "x" , 1 );
  105. o.put( "y" , 1 );
  106. o.put( "a" , 1 );
  107. assertEquals( "x_1_y_1_a_1" , DBCollection.genIndexName(o) );
  108. o = new BasicDBObject();
  109. o.put( "z" , 1 );
  110. o.put( "a" , 1 );
  111. assertEquals( "z_1_a_1" , DBCollection.genIndexName(o) );
  112. }
  113. @Test
  114. public void testDistinct(){
  115. DBCollection c = _db.getCollection( "distinct1" );
  116. c.drop();
  117. for ( int i=0; i<100; i++ ){
  118. BasicDBObject o = new BasicDBObject();
  119. o.put( "_id" , i );
  120. o.put( "x" , i % 10 );
  121. c.save( o );
  122. }
  123. List l = c.distinct( "x" );
  124. assertEquals( 10 , l.size() );
  125. l = c.distinct( "x" , new BasicDBObject( "_id" , new BasicDBObject( "$gt" , 95 ) ) );
  126. assertEquals( 4 , l.size() );
  127. }
  128. @Test
  129. public void testEnsureIndex(){
  130. DBCollection c = _db.getCollection( "ensureIndex1" );
  131. c.drop();
  132. c.save( new BasicDBObject( "x" , 1 ) );
  133. assertEquals( 1 , c.getIndexInfo().size() );
  134. c.ensureIndex( new BasicDBObject( "x" , 1 ) , new BasicDBObject( "unique" , true ) );
  135. assertEquals( 2 , c.getIndexInfo().size() );
  136. assertEquals( Boolean.TRUE , c.getIndexInfo().get(1).get( "unique" ) );
  137. }
  138. @Test
  139. public void testIndexExceptions(){
  140. DBCollection c = _db.getCollection( "indexExceptions" );
  141. c.drop();
  142. c.insert( new BasicDBObject( "x" , 1 ) );
  143. c.insert( new BasicDBObject( "x" , 1 ) );
  144. c.ensureIndex( new BasicDBObject( "y" , 1 ) );
  145. c.resetIndexCache();
  146. c.ensureIndex( new BasicDBObject( "y" , 1 ) ); // make sure this doesn't throw
  147. c.resetIndexCache();
  148. Exception failed = null;
  149. try {
  150. c.ensureIndex( new BasicDBObject( "x" , 1 ) , new BasicDBObject( "unique" , true ) );
  151. }
  152. catch ( MongoException.DuplicateKey e ){
  153. failed = e;
  154. }
  155. assertNotNull( failed );
  156. }
  157. @Test(groups = {"bulkContinue"})
  158. public void testMultiInsertNoContinue() {
  159. DBCollection c = _db.getCollection("testmultiinsertNoContinue");
  160. c.drop();
  161. DBObject obj = c.findOne();
  162. assertEquals(obj, null);
  163. ObjectId id = new ObjectId();
  164. DBObject inserted1 = BasicDBObjectBuilder.start("_id", id).add("x",1).add("y",2).get();
  165. DBObject inserted2 = BasicDBObjectBuilder.start("_id", id).add("x",3).add("y",4).get();
  166. DBObject inserted3 = BasicDBObjectBuilder.start().add("x",5).add("y",6).get();
  167. WriteResult r = c.insert(false, inserted1,inserted2, inserted3);
  168. assertEquals( c.count(), 1);
  169. }
  170. @Test(groups = {"bulkContinue"})
  171. public void testMultiInsertWithContinue() {
  172. DBCollection c = _db.getCollection("testmultiinsertWithContinue");
  173. c.drop();
  174. DBObject obj = c.findOne();
  175. assertEquals(obj, null);
  176. ObjectId id = new ObjectId();
  177. DBObject inserted1 = BasicDBObjectBuilder.start("_id", id).add("x",1).add("y",2).get();
  178. DBObject inserted2 = BasicDBObjectBuilder.start("_id", id).add("x",3).add("y",4).get();
  179. DBObject inserted3 = BasicDBObjectBuilder.start().add("x",5).add("y",6).get();
  180. WriteResult r = c.insert(true, inserted1,inserted2, inserted3);
  181. assertEquals( c.count(), 2 );
  182. }
  183. @Test( expectedExceptions = IllegalArgumentException.class )
  184. public void testDotKeysFail() {
  185. DBCollection c = _db.getCollection("testdotkeysFail");
  186. c.drop();
  187. DBObject obj = BasicDBObjectBuilder.start().add("x",1).add("y",2).add("foo.bar","baz").get();
  188. c.insert(obj);
  189. }
  190. final DB _db;
  191. public static void main( String args[] )
  192. throws Exception {
  193. (new DBCollectionTest()).runConsole();
  194. }
  195. }