diff options
author | Bruno Jacques Mahe <bmahe@apache.org> | 2012-07-26 05:29:06 +0000 |
---|---|---|
committer | Bruno Jacques Mahe <bmahe@apache.org> | 2012-07-26 05:29:06 +0000 |
commit | e2a38cacb2e27c32dd6388811060fec586ffbfc5 (patch) | |
tree | 1d8c4adff5ff64bd03752540603eb94928282fd2 /bigtop-test-framework | |
parent | a51839504b40e946d55429eedfd55bacbf9805fe (diff) |
BIGTOP-685. Provide a way to specify the parameters expected by a test (Wing Yew Poon via Bruno Mahé)
git-svn-id: https://svn.apache.org/repos/asf/incubator/bigtop/trunk@1365886 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'bigtop-test-framework')
4 files changed, 178 insertions, 0 deletions
diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/ParameterSetter.java b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/ParameterSetter.java new file mode 100644 index 00000000..d42b7895 --- /dev/null +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/ParameterSetter.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.bigtop.itest; + +import java.lang.reflect.Field; + +import static org.junit.Assert.assertNotNull; + +/** + Class containing utility methods for test classes to use (in a static + setup method) for obtaining the values of parameters passed in via + environment variables or system properties. The parameters are obtained + by introspecting the {@link org.apache.bigtop.itest.Parameters [Parameters]} + annotation of the test class. + */ +public class ParameterSetter { + + public static void setEnv(Class target, String[] fieldNames) + throws NoSuchFieldException, IllegalAccessException { + Parameters params = (Parameters) target.getAnnotation(Parameters.class); + Variable[] vars = params.env(); + assert vars.length == fieldNames.length; + for (int i = 0; i < vars.length; i++) { + Variable var = vars[i]; + Field field = target.getField(fieldNames[i]); + String value = System.getenv(var.name()); + if (value == null && var.required()) { + assertNotNull(var.name() + " is not set", value); + } + field.set(target, value); + } + } + + public static void setProperties(Class target, String[] fieldNames) + throws NoSuchFieldException, IllegalAccessException { + Parameters params = (Parameters) target.getAnnotation(Parameters.class); + Property[] props = params.properties(); + assert props.length == fieldNames.length; + for (int i = 0; i < props.length; i++) { + Property prop = props[i]; + Field field = target.getField(fieldNames[i]); + Object value = null; + switch (prop.type()) { + case STRING: + value = System.getProperty(prop.name(), prop.defaultValue()); + break; + case INT: + value = Integer.getInteger(prop.name(), prop.intValue()); + break; + case LONG: + value = Long.getLong(prop.name(), prop.longValue()); + break; + case BOOLEAN: + value = Boolean.getBoolean(prop.name()); + } + field.set(target, value); + } + } +} diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Parameters.java b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Parameters.java new file mode 100644 index 00000000..7eb55dfa --- /dev/null +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Parameters.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.bigtop.itest; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Documented +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Parameters { + Variable[] env(); + Property[] properties(); +} diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Property.java b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Property.java new file mode 100644 index 00000000..edd106f2 --- /dev/null +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Property.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.bigtop.itest; + +/** + Specifies a parameter to be passed into a test via a system property. + The parameter may be a String, an int, a long, or a boolean. If the type + of the parameter is not specified, it defaults to String. + A default value (String value, int value, long value) may be specified + for the parameter if its type is not boolean; the default value of a + boolean parameter is false. +*/ +public @interface Property { + public static enum Type { + STRING, INT, LONG, BOOLEAN; + } + String name(); + Type type() default Type.STRING; + String defaultValue(); + int intValue(); + long longValue(); +} diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Variable.java b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Variable.java new file mode 100644 index 00000000..6a11fa6c --- /dev/null +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/Variable.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.bigtop.itest; + +/** + Specifies a parameter to be passed into a test via an environment variable. + The parameter is a String. + By default, the parameter is required. If it is required and a non-null value + cannot be found for it, an exception may be thrown. +*/ +public @interface Variable { + String name(); + boolean required() default true; +} |