@SuppressWarnings("unchecked") @Test public void testSimpleReadWrite() throws Throwable { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, WorksConstraint.class); List<? extends Constraint> constraints = Constraints.getConstraints(desc, this.getClass().getClassLoader()); assertEquals(1, constraints.size()); assertEquals(WorksConstraint.class, constraints.get(0).getClass()); // Check that we can add more than 1 constraint and that ordering is // preserved Constraints.add(desc, AlsoWorks.class, NameConstraint.class); constraints = Constraints.getConstraints(desc, this.getClass() .getClassLoader()); assertEquals(3, constraints.size()); assertEquals(WorksConstraint.class, constraints.get(0).getClass()); assertEquals(AlsoWorks.class, constraints.get(1).getClass()); assertEquals(NameConstraint.class, constraints.get(2).getClass()); }
@Test public void testConfigurationPreserved() throws Throwable { Configuration conf = new Configuration(); conf.setBoolean("_ENABLED", false); conf.setLong("_PRIORITY", 10); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, AlsoWorks.class, conf); Constraints.add(desc, WorksConstraint.class); assertFalse(Constraints.enabled(desc, AlsoWorks.class)); List<? extends Constraint> constraints = Constraints.getConstraints(desc, this.getClass().getClassLoader()); for (Constraint c : constraints) { Configuration storedConf = c.getConf(); if (c instanceof AlsoWorks) assertEquals(10, storedConf.getLong("_PRIORITY", -1)); // its just a worksconstraint else assertEquals(2, storedConf.getLong("_PRIORITY", -1)); } }
@SuppressWarnings("unchecked") @Test public void testReadWriteWithConf() throws Throwable { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, new Pair<>(CheckConfigurationConstraint.class, CheckConfigurationConstraint.getConfiguration())); List<? extends Constraint> c = Constraints.getConstraints(desc, this .getClass().getClassLoader()); assertEquals(1, c.size()); assertEquals(CheckConfigurationConstraint.class, c.get(0).getClass()); // check to make sure that we overwrite configurations Constraints.add(desc, new Pair<>( CheckConfigurationConstraint.class, new Configuration(false))); try { Constraints.getConstraints(desc, this.getClass().getClassLoader()); fail("No exception thrown - configuration not overwritten"); } catch (IllegalArgumentException e) { // expect to have the exception, so don't do anything } }
/** * Test that when we update a constraint the ordering is not modified. * * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testUpdateConstraint() throws Exception { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, CheckConfigurationConstraint.class, CheckWasRunConstraint.class); Constraints.setConfiguration(desc, CheckConfigurationConstraint.class, CheckConfigurationConstraint.getConfiguration()); List<? extends Constraint> constraints = Constraints.getConstraints(desc, this.getClass().getClassLoader()); assertEquals(2, constraints.size()); // check to make sure the order didn't change assertEquals(CheckConfigurationConstraint.class, constraints.get(0) .getClass()); assertEquals(CheckWasRunConstraint.class, constraints.get(1).getClass()); }
/** * Test that we run a passing constraint * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testConstraintPasses() throws Exception { // create the table // it would be nice if this was also a method on the util HTableDescriptor desc = new HTableDescriptor(tableName); for (byte[] family : new byte[][] { dummy, test }) { desc.addFamily(new HColumnDescriptor(family)); } // add a constraint Constraints.add(desc, CheckWasRunConstraint.class); util.getAdmin().createTable(desc); Table table = util.getConnection().getTable(tableName); try { // test that we don't fail on a valid put Put put = new Put(row1); byte[] value = Bytes.toBytes(Integer.toString(10)); byte[] qualifier = new byte[0]; put.addColumn(dummy, qualifier, value); table.put(put); } finally { table.close(); } assertTrue(CheckWasRunConstraint.wasRun); }
/** * Test that constraints will fail properly * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testConstraintFails() throws Exception { // create the table // it would be nice if this was also a method on the util HTableDescriptor desc = new HTableDescriptor(tableName); for (byte[] family : new byte[][] { dummy, test }) { desc.addFamily(new HColumnDescriptor(family)); } // add a constraint that is sure to fail Constraints.add(desc, AllFailConstraint.class); util.getAdmin().createTable(desc); Table table = util.getConnection().getTable(tableName); // test that we do fail on violation Put put = new Put(row1); byte[] qualifier = new byte[0]; put.addColumn(dummy, qualifier, Bytes.toBytes("fail")); LOG.warn("Doing put in table"); try { table.put(put); fail("This put should not have suceeded - AllFailConstraint was not run!"); } catch (ConstraintException e) { // expected } table.close(); }
Constraints.add(desc, CheckWasRunConstraint.class);
/** * Test that Constraints are properly enabled, disabled, and removed * * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testEnableDisableRemove() throws Exception { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); // check general enabling/disabling of constraints // first add a constraint Constraints.add(desc, AllPassConstraint.class); // make sure everything is enabled assertTrue(Constraints.enabled(desc, AllPassConstraint.class)); assertTrue(desc.hasCoprocessor(ConstraintProcessor.class.getName())); // check disabling Constraints.disable(desc); assertFalse(desc.hasCoprocessor(ConstraintProcessor.class.getName())); // make sure the added constraints are still present assertTrue(Constraints.enabled(desc, AllPassConstraint.class)); // check just removing the single constraint Constraints.remove(desc, AllPassConstraint.class); assertFalse(Constraints.has(desc, AllPassConstraint.class)); // Add back the single constraint Constraints.add(desc, AllPassConstraint.class); // and now check that when we remove constraints, all are gone Constraints.remove(desc); assertFalse(desc.hasCoprocessor(ConstraintProcessor.class.getName())); assertFalse(Constraints.has(desc, AllPassConstraint.class)); }
@SuppressWarnings("unchecked") @Test public void testSimpleReadWrite() throws Throwable { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, WorksConstraint.class); List<? extends Constraint> constraints = Constraints.getConstraints(desc, this.getClass().getClassLoader()); assertEquals(1, constraints.size()); assertEquals(WorksConstraint.class, constraints.get(0).getClass()); // Check that we can add more than 1 constraint and that ordering is // preserved Constraints.add(desc, AlsoWorks.class, NameConstraint.class); constraints = Constraints.getConstraints(desc, this.getClass() .getClassLoader()); assertEquals(3, constraints.size()); assertEquals(WorksConstraint.class, constraints.get(0).getClass()); assertEquals(AlsoWorks.class, constraints.get(1).getClass()); assertEquals(NameConstraint.class, constraints.get(2).getClass()); }
@Test public void testConfigurationPreserved() throws Throwable { Configuration conf = new Configuration(); conf.setBoolean("_ENABLED", false); conf.setLong("_PRIORITY", 10); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, AlsoWorks.class, conf); Constraints.add(desc, WorksConstraint.class); assertFalse(Constraints.enabled(desc, AlsoWorks.class)); List<? extends Constraint> constraints = Constraints.getConstraints(desc, this.getClass().getClassLoader()); for (Constraint c : constraints) { Configuration storedConf = c.getConf(); if (c instanceof AlsoWorks) assertEquals(10, storedConf.getLong("_PRIORITY", -1)); // its just a worksconstraint else assertEquals(2, storedConf.getLong("_PRIORITY", -1)); } }
@SuppressWarnings("unchecked") @Test public void testReadWriteWithConf() throws Throwable { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, new Pair<>(CheckConfigurationConstraint.class, CheckConfigurationConstraint.getConfiguration())); List<? extends Constraint> c = Constraints.getConstraints(desc, this .getClass().getClassLoader()); assertEquals(1, c.size()); assertEquals(CheckConfigurationConstraint.class, c.get(0).getClass()); // check to make sure that we overwrite configurations Constraints.add(desc, new Pair<>( CheckConfigurationConstraint.class, new Configuration(false))); try { Constraints.getConstraints(desc, this.getClass().getClassLoader()); fail("No exception thrown - configuration not overwritten"); } catch (IllegalArgumentException e) { // expect to have the exception, so don't do anything } }
/** * Test that when we update a constraint the ordering is not modified. * * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testUpdateConstraint() throws Exception { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); Constraints.add(desc, CheckConfigurationConstraint.class, CheckWasRunConstraint.class); Constraints.setConfiguration(desc, CheckConfigurationConstraint.class, CheckConfigurationConstraint.getConfiguration()); List<? extends Constraint> constraints = Constraints.getConstraints(desc, this.getClass().getClassLoader()); assertEquals(2, constraints.size()); // check to make sure the order didn't change assertEquals(CheckConfigurationConstraint.class, constraints.get(0) .getClass()); assertEquals(CheckWasRunConstraint.class, constraints.get(1).getClass()); }
/** * Test that we run a passing constraint * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testConstraintPasses() throws Exception { // create the table // it would be nice if this was also a method on the util HTableDescriptor desc = new HTableDescriptor(tableName); for (byte[] family : new byte[][] { dummy, test }) { desc.addFamily(new HColumnDescriptor(family)); } // add a constraint Constraints.add(desc, CheckWasRunConstraint.class); util.getAdmin().createTable(desc); Table table = util.getConnection().getTable(tableName); try { // test that we don't fail on a valid put Put put = new Put(row1); byte[] value = Bytes.toBytes(Integer.toString(10)); byte[] qualifier = new byte[0]; put.addColumn(dummy, qualifier, value); table.put(put); } finally { table.close(); } assertTrue(CheckWasRunConstraint.wasRun); }
/** * Test that constraints will fail properly * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testConstraintFails() throws Exception { // create the table // it would be nice if this was also a method on the util HTableDescriptor desc = new HTableDescriptor(tableName); for (byte[] family : new byte[][] { dummy, test }) { desc.addFamily(new HColumnDescriptor(family)); } // add a constraint that is sure to fail Constraints.add(desc, AllFailConstraint.class); util.getAdmin().createTable(desc); Table table = util.getConnection().getTable(tableName); // test that we do fail on violation Put put = new Put(row1); byte[] qualifier = new byte[0]; put.addColumn(dummy, qualifier, Bytes.toBytes("fail")); LOG.warn("Doing put in table"); try { table.put(put); fail("This put should not have suceeded - AllFailConstraint was not run!"); } catch (ConstraintException e) { // expected } table.close(); }
Constraints.add(desc, CheckWasRunConstraint.class);
/** * Test that Constraints are properly enabled, disabled, and removed * * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testEnableDisableRemove() throws Exception { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); // check general enabling/disabling of constraints // first add a constraint Constraints.add(desc, AllPassConstraint.class); // make sure everything is enabled assertTrue(Constraints.enabled(desc, AllPassConstraint.class)); assertTrue(desc.hasCoprocessor(ConstraintProcessor.class.getName())); // check disabling Constraints.disable(desc); assertFalse(desc.hasCoprocessor(ConstraintProcessor.class.getName())); // make sure the added constraints are still present assertTrue(Constraints.enabled(desc, AllPassConstraint.class)); // check just removing the single constraint Constraints.remove(desc, AllPassConstraint.class); assertFalse(Constraints.has(desc, AllPassConstraint.class)); // Add back the single constraint Constraints.add(desc, AllPassConstraint.class); // and now check that when we remove constraints, all are gone Constraints.remove(desc); assertFalse(desc.hasCoprocessor(ConstraintProcessor.class.getName())); assertFalse(Constraints.has(desc, AllPassConstraint.class)); }