java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduClient.java
@Test(timeout = 100000)
  public void testTableWithDefaults() throws Exception {
    List<ColumnSchema> cols = new ArrayList<>();
    cols.add(new ColumnSchema.ColumnSchemaBuilder("key", Type.STRING)
             .key(true)
             .build());
    // nullable with no default
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c1", Type.STRING)
             .nullable(true)
             .build());
    // nullable with default
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c2", Type.STRING)
             .nullable(true)
             .defaultValue("def")
             .build());
    // not null with no default
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c3", Type.STRING)
             .nullable(false)
             .build());
    // not null with default
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c4", Type.STRING)
             .nullable(false)
             .defaultValue("def")
             .build());
    Schema schema = new Schema(cols);
    syncClient.createTable(tableName, schema, getBasicCreateTableOptions());
    KuduSession session = syncClient.newSession();
    KuduTable table = syncClient.openTable(tableName);

    // Insert various rows. '-' indicates leaving the row unset in the insert.
    List<String> rows = ImmutableList.of(
        // Specify all columns
        "r1,a,b,c,d",
        // Specify all, set nullable ones to NULL.
        "r2,NULL,NULL,c,d",
        // Don't specify any columns except for the one that is NOT NULL
        // with no default.
        "r3,-,-,c,-",
        // Two rows which should not succeed.
        "fail_1,a,b,c,NULL",
        "fail_2,a,b,NULL,d");
    List<String> expectedStrings = ImmutableList.of(
        "STRING key=r1, STRING c1=a, STRING c2=b, STRING c3=c, STRING c4=d",
        "STRING key=r2, STRING c1=NULL, STRING c2=NULL, STRING c3=c, STRING c4=d",
        "STRING key=r3, STRING c1=NULL, STRING c2=def, STRING c3=c, STRING c4=def");
    for (String row : rows) {
      try {
        String[] fields = row.split(",", -1);
        Insert insert = table.newInsert();
        for (int i = 0; i < fields.length; i++) {
          if (fields[i].equals("-")) { // leave unset
            continue;
          }
          if (fields[i].equals("NULL")) {
            insert.getRow().setNull(i);
          } else {
            insert.getRow().addString(i, fields[i]);
          }
        }
        session.apply(insert);
      } catch (IllegalArgumentException e) {
        // We expect two of the inserts to fail when we try to set NULL values for
        // nullable columns.
        assertTrue(e.getMessage(),
                   e.getMessage().matches("c[34] cannot be set to null"));
      }
    }
    session.flush();

    // Check that we got the results we expected.
    List<String> rowStrings = scanTableToStrings(table);
    Collections.sort(rowStrings);
    assertArrayEquals(rowStrings.toArray(new String[0]),
                      expectedStrings.toArray(new String[0]));
  }
/**
   * Test creating a table with columns with different combinations of NOT NULL and
   * default values, inserting rows, and checking the results are as expected.
   * Regression test for KUDU-180.
   */
Test creating a table with columns with different combinations of NOT NULL and
default values, inserting rows, and checking the results are as expected.
Regression test for KUDU-180.
Test creating a table with columns with different combinations of NOT NULL and default values inserting rows and checking the results are as expected . Regression test for KUDU - 180 .
@Test(timeout = 100000)
  public void testTableWithDefaults() throws Exception {
    List<ColumnSchema> cols = new ArrayList<>();
    cols.add(new ColumnSchema.ColumnSchemaBuilder("key", Type.STRING)
             .key(true)
             .build());
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c1", Type.STRING)
             .nullable(true)
             .build());
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c2", Type.STRING)
             .nullable(true)
             .defaultValue("def")
             .build());
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c3", Type.STRING)
             .nullable(false)
             .build());
    cols.add(new ColumnSchema.ColumnSchemaBuilder("c4", Type.STRING)
             .nullable(false)
             .defaultValue("def")
             .build());
    Schema schema = new Schema(cols);
    syncClient.createTable(tableName, schema, getBasicCreateTableOptions());
    KuduSession session = syncClient.newSession();
    KuduTable table = syncClient.openTable(tableName);
    List<String> rows = ImmutableList.of(
        "r1,a,b,c,d",
        "r2,NULL,NULL,c,d",
        "r3,-,-,c,-",
        "fail_1,a,b,c,NULL",
        "fail_2,a,b,NULL,d");
    List<String> expectedStrings = ImmutableList.of(
        "STRING key=r1, STRING c1=a, STRING c2=b, STRING c3=c, STRING c4=d",
        "STRING key=r2, STRING c1=NULL, STRING c2=NULL, STRING c3=c, STRING c4=d",
        "STRING key=r3, STRING c1=NULL, STRING c2=def, STRING c3=c, STRING c4=def");
    for (String row : rows) {
      try {
        String[] fields = row.split(",", -1);
        Insert insert = table.newInsert();
        for (int i = 0; i < fields.length; i++) {
          if (fields[i].equals("-")) { 
            continue;
          }
          if (fields[i].equals("NULL")) {
            insert.getRow().setNull(i);
          } else {
            insert.getRow().addString(i, fields[i]);
          }
        }
        session.apply(insert);
      } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage(),
                   e.getMessage().matches("c[34] cannot be set to null"));
      }
    }
    session.flush();
    List<String> rowStrings = scanTableToStrings(table);
    Collections.sort(rowStrings);
    assertArrayEquals(rowStrings.toArray(new String[0]),
                      expectedStrings.toArray(new String[0]));
  }
@ Test ( timeout = 100000 ) public void testTableWithDefaults ( ) throws Exception { List < ColumnSchema > cols = new ArrayList < > ( ) ; cols . add ( new ColumnSchema . ColumnSchemaBuilder ( "key" , Type . STRING ) . key ( true ) . build ( ) ) ; cols . add ( new ColumnSchema . ColumnSchemaBuilder ( "c1" , Type . STRING ) . nullable ( true ) . build ( ) ) ; cols . add ( new ColumnSchema . ColumnSchemaBuilder ( "c2" , Type . STRING ) . nullable ( true ) . defaultValue ( "def" ) . build ( ) ) ; cols . add ( new ColumnSchema . ColumnSchemaBuilder ( "c3" , Type . STRING ) . nullable ( false ) . build ( ) ) ; cols . add ( new ColumnSchema . ColumnSchemaBuilder ( "c4" , Type . STRING ) . nullable ( false ) . defaultValue ( "def" ) . build ( ) ) ; Schema schema = new Schema ( cols ) ; syncClient . createTable ( tableName , schema , getBasicCreateTableOptions ( ) ) ; KuduSession session = syncClient . newSession ( ) ; KuduTable table = syncClient . openTable ( tableName ) ; List < String > rows = ImmutableList . of ( "r1,a,b,c,d" , "r2,NULL,NULL,c,d" , "r3,-,-,c,-" , "fail_1,a,b,c,NULL" , "fail_2,a,b,NULL,d" ) ; List < String > expectedStrings = ImmutableList . of ( "STRING key=r1, STRING c1=a, STRING c2=b, STRING c3=c, STRING c4=d" , "STRING key=r2, STRING c1=NULL, STRING c2=NULL, STRING c3=c, STRING c4=d" , "STRING key=r3, STRING c1=NULL, STRING c2=def, STRING c3=c, STRING c4=def" ) ; for ( String row : rows ) { try { String [ ] fields = row . split ( "," , - 1 ) ; Insert insert = table . newInsert ( ) ; for ( int i = 0 ; i < fields . length ; i ++ ) { if ( fields [ i ] . equals ( "-" ) ) { continue ; } if ( fields [ i ] . equals ( "NULL" ) ) { insert . getRow ( ) . setNull ( i ) ; } else { insert . getRow ( ) . addString ( i , fields [ i ] ) ; } } session . apply ( insert ) ; } catch ( IllegalArgumentException e ) { assertTrue ( e . getMessage ( ) , e . getMessage ( ) . matches ( "c[34] cannot be set to null" ) ) ; } } session . flush ( ) ; List < String > rowStrings = scanTableToStrings ( table ) ; Collections . sort ( rowStrings ) ; assertArrayEquals ( rowStrings . toArray ( new String [ 0 ] ) , expectedStrings . toArray ( new String [ 0 ] ) ) ; }
Test creating a table with columns with different combinations of NOT NULL and
 default values, inserting rows, and checking the results are as expected.
Test creating a table with columns with different combinations of NOT NULL and default values inserting rows and checking the results are as expected .
// nullable with no default // nullable with default // not null with no default // not null with default // Insert various rows. '-' indicates leaving the row unset in the insert. // Specify all columns // Specify all, set nullable ones to NULL. // Don't specify any columns except for the one that is NOT NULL // with no default. // Two rows which should not succeed. // leave unset // We expect two of the inserts to fail when we try to set NULL values for // nullable columns. // Check that we got the results we expected.