@Test
  public void testSpecial() {
    DBCollection c = _db.getCollection("testSpecial");
    c.insert(new BasicDBObject("x", 1));
    c.insert(new BasicDBObject("x", 2));
    c.insert(new BasicDBObject("x", 3));
    c.ensureIndex("x");

    for (DBObject o : c.find().sort(new BasicDBObject("x", 1)).addSpecial("$returnKey", 1))
      assertTrue(o.get("_id") == null);

    for (DBObject o : c.find().sort(new BasicDBObject("x", 1))) assertTrue(o.get("_id") != null);
  }
  @Test(groups = {"basic"})
  public void testGetServerAddressQuery1() {

    final DBCollection c = _db.getCollection("getServerAddress");
    c.drop();

    // Insert some data.
    for (int i = 0; i < 10; i++) c.insert(new BasicDBObject("one", i));

    final DBCursor cur = c.find(new BasicDBObject("one", 9));
    cur.hasNext();
    assertNotNull(cur.getServerAddress());
  }
  @Test(groups = {"basic"})
  public void testCount() {
    try {
      DBCollection c = _db.getCollection("test");
      c.drop();

      assertEquals(c.find().count(), 0);

      BasicDBObject obj = new BasicDBObject();
      obj.put("x", "foo");
      c.insert(obj);

      assertEquals(c.find().count(), 1);
    } catch (MongoException e) {
      assertTrue(false);
    }
  }
示例#4
0
    public TaskRead(Map map1, Map m_max, Map m_min, DBCollection coll, ModbusSlaveSet slave) {
      try {
        //// 此处只要点击开始就能保存数据
        /// 方案:利用数据源发送单个布尔型变量(1,0)交替,链接成功,先判断是否链接成功,然后在保存数据
        synchronized (slave) {
          Map map = new HashMap();
          Set set1 = map1.entrySet();
          Iterator it1 = set1.iterator();
          while (it1.hasNext()) {
            Map.Entry<String, Map<String, String>> entry =
                (Map.Entry<String, Map<String, String>>) it1.next();
            Map<String, String> map2 = entry.getValue();
            for (Iterator it2 = map2.entrySet().iterator(); it2.hasNext(); ) {
              Map.Entry<String, String> entry2 = (Map.Entry<String, String>) it2.next();
              String name = entry2.getKey().toString();
              String paramAddr = entry2.getValue().toString();
              int fun = (int) (Double.parseDouble(paramAddr));
              if (paramAddr.substring(0, 1).equals("4")) {
                Short d4 = slave.getProcessImage(1).getInputRegister(fun % 10000);
                double dmax = (Double) m_max.get(name);
                double dmin = (Double) m_min.get(name);
                double dValue = 0;
                if (d4 >= 0) {
                  dValue = dmax * d4 / 32000;
                } else {
                  dValue = dmin * d4 / (-32000);
                }
                map.put(name, dValue);
              }
              if (paramAddr.substring(0, 1).equals("3")) {
                Short d3 = slave.getProcessImage(1).getHoldingRegister(fun % 10000);
                double dmax = (Double) m_max.get(name);
                double dmin = (Double) m_min.get(name);
                double dValue = 0;

                if (d3 >= 0) {
                  dValue = dmax * d3 / 32000;
                } else {
                  dValue = dmin * d3 / (-32000);
                }
                map.put(name, dValue);
              }
              if (paramAddr.substring(0, 1).equals("2")) {
                map.put(name, slave.getProcessImage(2).getInput(fun % 10000));
              }
              if (paramAddr.substring(0, 1).equals("1")) {
                Boolean a = slave.getProcessImage(2).getCoil(fun % 10000 - 1);
                map.put(name, a);
              }
            }
          }

          Calendar calendar = Calendar.getInstance();
          Date dd = calendar.getTime();

          BasicDBObject doc = new BasicDBObject();
          doc.put("_id", dd);

          Set set = map.entrySet();
          Iterator it = set.iterator();
          while (it.hasNext()) {
            Map.Entry<String, String> entry1 = (Map.Entry<String, String>) it.next();
            doc.put(entry1.getKey(), entry1.getValue());
          }
          coll.insert(doc);
        }
      } catch (Exception ex) {
      }
    }