HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / constraint / TestConstraints.java
blob2087a9819780c877b669399e138a71d9cc23ef35
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.constraint;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import java.util.List;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.HBaseClassTestRule;
28 import org.apache.hadoop.hbase.TableNameTestRule;
29 import org.apache.hadoop.hbase.client.Put;
30 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
31 import org.apache.hadoop.hbase.constraint.TestConstraint.CheckWasRunConstraint;
32 import org.apache.hadoop.hbase.constraint.WorksConstraint.NameConstraint;
33 import org.apache.hadoop.hbase.testclassification.MiscTests;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.hbase.util.Pair;
36 import org.junit.ClassRule;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
41 /**
42 * Test reading/writing the constraints into the {@link TableDescriptorBuilder}.
44 @Category({ MiscTests.class, SmallTests.class })
45 public class TestConstraints {
47 @ClassRule
48 public static final HBaseClassTestRule CLASS_RULE =
49 HBaseClassTestRule.forClass(TestConstraints.class);
51 @Rule
52 public TableNameTestRule name = new TableNameTestRule();
54 @Test
55 public void testSimpleReadWrite() throws Exception {
56 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(name.getTableName());
57 Constraints.add(builder, WorksConstraint.class);
59 List<? extends Constraint> constraints =
60 Constraints.getConstraints(builder.build(), this.getClass().getClassLoader());
61 assertEquals(1, constraints.size());
63 assertEquals(WorksConstraint.class, constraints.get(0).getClass());
65 // Check that we can add more than 1 constraint and that ordering is
66 // preserved
67 Constraints.add(builder, AlsoWorks.class, NameConstraint.class);
68 constraints = Constraints.getConstraints(builder.build(), this.getClass().getClassLoader());
69 assertEquals(3, constraints.size());
71 assertEquals(WorksConstraint.class, constraints.get(0).getClass());
72 assertEquals(AlsoWorks.class, constraints.get(1).getClass());
73 assertEquals(NameConstraint.class, constraints.get(2).getClass());
77 @Test
78 public void testReadWriteWithConf() throws Exception {
79 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(name.getTableName());
80 Constraints.add(builder, new Pair<>(CheckConfigurationConstraint.class,
81 CheckConfigurationConstraint.getConfiguration()));
83 List<? extends Constraint> c =
84 Constraints.getConstraints(builder.build(), this.getClass().getClassLoader());
85 assertEquals(1, c.size());
87 assertEquals(CheckConfigurationConstraint.class, c.get(0).getClass());
89 // check to make sure that we overwrite configurations
90 Constraints.add(builder,
91 new Pair<>(CheckConfigurationConstraint.class, new Configuration(false)));
93 try {
94 Constraints.getConstraints(builder.build(), this.getClass().getClassLoader());
95 fail("No exception thrown - configuration not overwritten");
96 } catch (IllegalArgumentException e) {
97 // expect to have the exception, so don't do anything
102 * Test that Constraints are properly enabled, disabled, and removed
104 @Test
105 public void testEnableDisableRemove() throws Exception {
106 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(name.getTableName());
107 // check general enabling/disabling of constraints
108 // first add a constraint
109 Constraints.add(builder, AllPassConstraint.class);
110 // make sure everything is enabled
111 assertTrue(Constraints.enabled(builder.build(), AllPassConstraint.class));
112 assertTrue(builder.hasCoprocessor(ConstraintProcessor.class.getName()));
114 // check disabling
115 Constraints.disable(builder);
116 assertFalse(builder.hasCoprocessor(ConstraintProcessor.class.getName()));
117 // make sure the added constraints are still present
118 assertTrue(Constraints.enabled(builder.build(), AllPassConstraint.class));
120 // check just removing the single constraint
121 Constraints.remove(builder, AllPassConstraint.class);
122 assertFalse(Constraints.has(builder.build(), AllPassConstraint.class));
124 // Add back the single constraint
125 Constraints.add(builder, AllPassConstraint.class);
127 // and now check that when we remove constraints, all are gone
128 Constraints.remove(builder);
129 assertFalse(builder.hasCoprocessor(ConstraintProcessor.class.getName()));
130 assertFalse(Constraints.has(builder.build(), AllPassConstraint.class));
135 * Test that when we update a constraint the ordering is not modified.
137 @Test
138 public void testUpdateConstraint() throws Exception {
139 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(name.getTableName());
140 Constraints.add(builder, CheckConfigurationConstraint.class, CheckWasRunConstraint.class);
141 Constraints.setConfiguration(builder, CheckConfigurationConstraint.class,
142 CheckConfigurationConstraint.getConfiguration());
144 List<? extends Constraint> constraints =
145 Constraints.getConstraints(builder.build(), this.getClass().getClassLoader());
147 assertEquals(2, constraints.size());
149 // check to make sure the order didn't change
150 assertEquals(CheckConfigurationConstraint.class, constraints.get(0).getClass());
151 assertEquals(CheckWasRunConstraint.class, constraints.get(1).getClass());
155 * Test that if a constraint hasn't been set that there are no problems with attempting to remove
156 * it.
158 @Test
159 public void testRemoveUnsetConstraint() throws Exception {
160 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(name.getTableName());
161 Constraints.remove(builder);
162 Constraints.remove(builder, AlsoWorks.class);
165 @Test
166 public void testConfigurationPreserved() throws Exception {
167 Configuration conf = new Configuration();
168 conf.setBoolean("_ENABLED", false);
169 conf.setLong("_PRIORITY", 10);
170 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(name.getTableName());
171 Constraints.add(builder, AlsoWorks.class, conf);
172 Constraints.add(builder, WorksConstraint.class);
173 assertFalse(Constraints.enabled(builder.build(), AlsoWorks.class));
174 List<? extends Constraint> constraints =
175 Constraints.getConstraints(builder.build(), this.getClass().getClassLoader());
176 for (Constraint c : constraints) {
177 Configuration storedConf = c.getConf();
178 if (c instanceof AlsoWorks) {
179 assertEquals(10, storedConf.getLong("_PRIORITY", -1));
181 // its just a worksconstraint
182 else {
183 assertEquals(2, storedConf.getLong("_PRIORITY", -1));
188 // ---------- Constraints just used for testing
191 * Also just works
193 public static class AlsoWorks extends BaseConstraint {
194 @Override
195 public void check(Put p) {
196 // NOOP