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

https://github.com/gzsombor/mongo-java-driver · Java · 204 lines · 142 code · 47 blank · 15 comment · 1 complexity · b5c8945aefd812b9640798ca25fc12f5 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.*;
  18. import java.util.*;
  19. import org.testng.annotations.*;
  20. import com.mongodb.util.*;
  21. public class DBCollectionTest extends TestCase {
  22. public DBCollectionTest()
  23. throws IOException , MongoException {
  24. super();
  25. cleanupMongo = new Mongo( "127.0.0.1" );
  26. cleanupDB = "com_mongodb_unittest_DBCollectionTest";
  27. _db = cleanupMongo.getDB( cleanupDB );
  28. }
  29. @Test(groups = {"basic"})
  30. public void testMultiInsert() {
  31. DBCollection c = _db.getCollection("testmultiinsert");
  32. c.drop();
  33. DBObject obj = c.findOne();
  34. assertEquals(obj, null);
  35. DBObject inserted1 = BasicDBObjectBuilder.start().add("x",1).add("y",2).get();
  36. DBObject inserted2 = BasicDBObjectBuilder.start().add("x",3).add("y",3).get();
  37. c.insert(inserted1,inserted2);
  38. c.insert(new DBObject[] {inserted1,inserted2});
  39. }
  40. @Test(groups = {"basic"})
  41. public void testFindOne() {
  42. DBCollection c = _db.getCollection("test");
  43. c.drop();
  44. DBObject obj = c.findOne();
  45. assertEquals(obj, null);
  46. DBObject inserted = BasicDBObjectBuilder.start().add("x",1).add("y",2).get();
  47. c.insert(inserted);
  48. c.insert(BasicDBObjectBuilder.start().add("_id", 123).add("x",2).add("z",2).get());
  49. obj = c.findOne(123);
  50. assertEquals(obj.get("_id"), 123);
  51. assertEquals(obj.get("x"), 2);
  52. assertEquals(obj.get("z"), 2);
  53. obj = c.findOne(123, new BasicDBObject("x", 1));
  54. assertEquals(obj.get("_id"), 123);
  55. assertEquals(obj.get("x"), 2);
  56. assertEquals(obj.containsField("z"), false);
  57. obj = c.findOne(new BasicDBObject("x", 1));
  58. assertEquals(obj.get("x"), 1);
  59. assertEquals(obj.get("y"), 2);
  60. obj = c.findOne(new BasicDBObject("x", 1), new BasicDBObject("y", 1));
  61. assertEquals(obj.containsField("x"), false);
  62. assertEquals(obj.get("y"), 2);
  63. }
  64. @Test
  65. public void testDropIndex(){
  66. DBCollection c = _db.getCollection( "dropindex1" );
  67. c.drop();
  68. c.save( new BasicDBObject( "x" , 1 ) );
  69. assertEquals( 1 , c.getIndexInfo().size() );
  70. c.ensureIndex( new BasicDBObject( "x" , 1 ) );
  71. assertEquals( 2 , c.getIndexInfo().size() );
  72. c.dropIndexes();
  73. assertEquals( 1 , c.getIndexInfo().size() );
  74. c.ensureIndex( new BasicDBObject( "x" , 1 ) );
  75. assertEquals( 2 , c.getIndexInfo().size() );
  76. c.ensureIndex( new BasicDBObject( "y" , 1 ) );
  77. assertEquals( 3 , c.getIndexInfo().size() );
  78. c.dropIndex( new BasicDBObject( "x" , 1 ) );
  79. assertEquals( 2 , c.getIndexInfo().size() );
  80. }
  81. @Test
  82. public void testGenIndexName(){
  83. BasicDBObject o = new BasicDBObject();
  84. o.put( "x" , 1 );
  85. assertEquals("x_1", DBCollection.genIndexName(o));
  86. o.put( "x" , "1" );
  87. assertEquals("x_1", DBCollection.genIndexName(o));
  88. o.put( "x" , "2d" );
  89. assertEquals("x_2d", DBCollection.genIndexName(o));
  90. o.put( "y" , -1 );
  91. assertEquals("x_2d_y_-1", DBCollection.genIndexName(o));
  92. o.put( "x" , 1 );
  93. o.put( "y" , 1 );
  94. o.put( "a" , 1 );
  95. assertEquals( "x_1_y_1_a_1" , DBCollection.genIndexName(o) );
  96. o = new BasicDBObject();
  97. o.put( "z" , 1 );
  98. o.put( "a" , 1 );
  99. assertEquals( "z_1_a_1" , DBCollection.genIndexName(o) );
  100. }
  101. @Test
  102. public void testDistinct(){
  103. DBCollection c = _db.getCollection( "distinct1" );
  104. c.drop();
  105. for ( int i=0; i<100; i++ ){
  106. BasicDBObject o = new BasicDBObject();
  107. o.put( "_id" , i );
  108. o.put( "x" , i % 10 );
  109. c.save( o );
  110. }
  111. List l = c.distinct( "x" );
  112. assertEquals( 10 , l.size() );
  113. l = c.distinct( "x" , new BasicDBObject( "_id" , new BasicDBObject( "$gt" , 95 ) ) );
  114. assertEquals( 4 , l.size() );
  115. }
  116. @Test
  117. public void testEnsureIndex(){
  118. DBCollection c = _db.getCollection( "ensureIndex1" );
  119. c.drop();
  120. c.save( new BasicDBObject( "x" , 1 ) );
  121. assertEquals( 1 , c.getIndexInfo().size() );
  122. c.ensureIndex( new BasicDBObject( "x" , 1 ) , new BasicDBObject( "unique" , true ) );
  123. assertEquals( 2 , c.getIndexInfo().size() );
  124. assertEquals( Boolean.TRUE , c.getIndexInfo().get(1).get( "unique" ) );
  125. }
  126. @Test
  127. public void testIndexExceptions(){
  128. DBCollection c = _db.getCollection( "indexExceptions" );
  129. c.drop();
  130. c.insert( new BasicDBObject( "x" , 1 ) );
  131. c.insert( new BasicDBObject( "x" , 1 ) );
  132. c.ensureIndex( new BasicDBObject( "y" , 1 ) );
  133. c.resetIndexCache();
  134. c.ensureIndex( new BasicDBObject( "y" , 1 ) ); // make sure this doesn't throw
  135. c.resetIndexCache();
  136. Exception failed = null;
  137. try {
  138. c.ensureIndex( new BasicDBObject( "x" , 1 ) , new BasicDBObject( "unique" , true ) );
  139. }
  140. catch ( MongoException.DuplicateKey e ){
  141. failed = e;
  142. }
  143. assertNotNull( failed );
  144. }
  145. @Test( expectedExceptions = IllegalArgumentException.class )
  146. public void testDotKeysFail() {
  147. DBCollection c = _db.getCollection("testdotkeysFail");
  148. c.drop();
  149. DBObject obj = BasicDBObjectBuilder.start().add("x",1).add("y",2).add("foo.bar","baz").get();
  150. c.insert(obj);
  151. }
  152. final DB _db;
  153. public static void main( String args[] )
  154. throws Exception {
  155. (new DBCollectionTest()).runConsole();
  156. }
  157. }