Example #1
0
  public static void bind(
      final BoundStatement statement,
      final TableScheme scheme,
      final ColumnDefinitions meta,
      final Value<?> value) {
    final String name = value.getColumn().getName();
    if (statement.isSet(name)) {
      throw new IllegalStateException(); // FIXME: descriptive
    }

    if (value.getType() != Value.Type.BOUND) {
      throw new IllegalStateException(); // FIXME: descriptive
    }

    final DataType tableType = meta.getType(name);
    final DataType valueType = value.getColumn().getCassandraType().getDataType();

    if (!typesEquals(tableType, valueType)) {
      throw new IllegalArgumentException(
          MessageFormat.format("Unexpected types: {0} != {1}", tableType, valueType));
    }

    final Object converted = value.asCassandraValue();
    statement.setBytesUnsafe(
        name, tableType.serialize(converted, ProtocolVersion.NEWEST_SUPPORTED));
  }
  // The two following tests a really unit tests, but since the whole uses
  // CCMBridge.PerClassSingleNodeCluster, they'll still spawn a cluster even
  // you execute only them, so we keep them in the "long" group. We could
  // move them in another class but not sure where honestly (one could argue
  // that it would make more sense to move all the *other* tests to some
  // DataTypeIntegrationTest class).
  @Test(groups = "long")
  public void serializeDeserializeTest() {

    for (DataType dt : DataType.allPrimitiveTypes()) {
      if (exclude(dt)) continue;

      Object value = TestUtils.getFixedValue(dt);
      assertEquals(dt.deserialize(dt.serialize(value)), value);
    }

    try {
      DataType.bigint().serialize(4);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }

    try {
      ByteBuffer badValue = ByteBuffer.allocate(4);
      DataType.bigint().deserialize(badValue);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }
  }
  @Test(groups = "long")
  public void serializeDeserializeCollectionsTest() {

    List<String> l = Arrays.asList("foo", "bar");

    DataType dt = DataType.list(DataType.text());
    assertEquals(dt.deserialize(dt.serialize(l)), l);

    try {
      DataType.list(DataType.bigint()).serialize(l);
      fail("This should not have worked");
    } catch (InvalidTypeException e) {
      /* That's what we want */
    }
  }