Repository: knox Updated Branches: refs/heads/KNOX-481 9e0e3e8ce -> a1d43564d
http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java new file mode 100755 index 0000000..6240d44 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationException; + +import java.util.Properties; + +public class PropertiesConfigurationAdapter implements ConfigurationAdapter { + + private Properties properties; + + public PropertiesConfigurationAdapter( Properties properties ) { + this.properties = properties; + } + + @Override + public Object getConfigurationValue( String name ) throws ConfigurationException { + return properties.getProperty( name ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..b93e49a --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Properties; + +public class PropertiesConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public PropertiesConfigurationAdapterDescriptor() { + add( Properties.class, PropertiesConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..5372911 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java @@ -0,0 +1,42 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.HashMap; +import java.util.Map; + +public abstract class AbstractConfigurationAdapterDescriptor implements ConfigurationAdapterDescriptor { + + private static Map<Class<?>, Class<? extends ConfigurationAdapter>> ADAPTERS = + new HashMap<Class<?>, Class<? extends ConfigurationAdapter>>(); + + protected AbstractConfigurationAdapterDescriptor() { + } + + protected void add( Class<?> configType, Class<? extends ConfigurationAdapter> adapterType ) { + ADAPTERS.put( configType, adapterType ); + } + + @Override + public Map<Class<?>, Class<? extends ConfigurationAdapter>> providedConfigurationAdapters() { + return ADAPTERS; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..3e304d4 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java @@ -0,0 +1,28 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.Map; + +public interface ConfigurationAdapterDescriptor { + + Map<Class<?>,Class<? extends ConfigurationAdapter>> providedConfigurationAdapters(); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java new file mode 100755 index 0000000..0bbd1d9 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java @@ -0,0 +1,27 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +public interface ConfigurationInjector { + + void configure(Object target, ConfigurationAdapter adapter, ConfigurationBinding binding); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor new file mode 100755 index 0000000..5a240e0 --- /dev/null +++ b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor @@ -0,0 +1,20 @@ +########################################################################## +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +########################################################################## +org.apache.hadoop.gateway.config.impl.MapConfigurationAdapterDescriptor +org.apache.hadoop.gateway.config.impl.PropertiesConfigurationAdapterDescriptor +org.apache.hadoop.gateway.config.impl.BeanConfigurationAdapterDescriptor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector new file mode 100755 index 0000000..e3a0d65 --- /dev/null +++ b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector @@ -0,0 +1,18 @@ +########################################################################## +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +########################################################################## +org.apache.hadoop.gateway.config.impl.DefaultConfigurationInjector \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java new file mode 100755 index 0000000..f3e392e --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java @@ -0,0 +1,57 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.Hashtable; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class AdapterSampleTest { + + public static class Target { + @Configure + private String username = null; + } + + public static class Adapter implements ConfigurationAdapter { + private Hashtable config; + public Adapter( Hashtable config ) { + this.config = config; + } + @Override + public Object getConfigurationValue( String name ) throws ConfigurationException { + Object value = config.get( name.toUpperCase() ); + return value == null ? null : value.toString(); + } + } + + static Hashtable config = new Hashtable(); + static{ config.put( "USERNAME", "somebody" ); } + + @Test + public void sample() { + Target target = new Target(); + Adapter adapter = new Adapter( config ); + ConfigurationInjectorBuilder.configuration().target( target ).source( adapter ).inject(); + assertThat( target.username, is( "somebody" ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java new file mode 100755 index 0000000..f4898ac --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java @@ -0,0 +1,379 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.apache.hadoop.gateway.config.impl.MappedConfigurationBinding; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.core.AllOf.allOf; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.fail; + +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.*; + +public class FuncTest { + + public static class TestBean { + @Configure + String stringMember = "stringDefault"; + + @Configure + int intMember = 1; + + @Configure + Integer integerMember = Integer.valueOf( 1 ); + + @Configure + public void setStringProp( String s ) { + stringPropField = s; + } + protected String stringPropField = "stringDefault"; + + @Configure + @Alias("altStringProp") + public void setNamedStringProp( String s ) { + stringPropFieldAlt = s; + } + protected String stringPropFieldAlt = "stringDefault"; + + @Configure + public void setNamedArgMethod( @Configure @Alias("altArgStringProp") String s ) { + stringPropFieldAltArg = s; + } + protected String stringPropFieldAltArg = "stringDefault"; + + @Configure + public void setMultiArgs( + @Configure @Alias("multiArg1") String s, + @Configure @Alias("multiArg2") Integer i, + @Configure @Alias("multiArg3") int n ) { + multiArgStringField = s; + multiArgIntegerField = i; + multiArgIntField = n; + } + String multiArgStringField = "default"; + Integer multiArgIntegerField = 0; + int multiArgIntField = 0; + + } + + @Test + public void testMapOfStrings() { + + Map<String,String> testConfig = new HashMap<String,String>(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", "2" ); + testConfig.put( "integerMember", "2" ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", "42" ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( testConfig ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 2 ) ); + assertThat( testBean.integerMember, is( new Integer(2) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + @Test + public void testProperties() { + + Properties testConfig = new Properties(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", "2" ); + testConfig.put( "integerMember", "2" ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", "42" ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( testConfig ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 2 ) ); + assertThat( testBean.integerMember, is( new Integer(2) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + public static class TestAdapter implements ConfigurationAdapter { + + private Map<String,String> config; + + public TestAdapter( Map<String,String> config ) { + this.config = config; + } + + @Override + public String getConfigurationValue( String name ) { + return config.get( name ); + } + + } + + @Test + public void testExplicitProvider() { + + Map<String,String> testConfig = new HashMap<String,String>(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", "2" ); + testConfig.put( "integerMember", "2" ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", "42" ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( new TestAdapter( testConfig ) ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 2 ) ); + assertThat( testBean.integerMember, is( new Integer(2) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + @Test + public void testMapOfObjects() { + + Map<Object,Object> testConfig = new HashMap<Object,Object>(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", 42 ); + testConfig.put( "integerMember", new Integer(42) ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", new Integer(42) ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( testConfig ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 42 ) ); + assertThat( testBean.integerMember, is( new Integer(42) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + public class Target { + @Configure @Alias("user.name") + private String user; + } + + public class Adapter implements ConfigurationAdapter { + @Override + public Object getConfigurationValue( String name ) throws ConfigurationException { + return System.getProperty( name ); + } + } + + @Test + public void testFactoryConfigurationDirect() { + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + + @Test + public void testFactoryConfigurationAdapter() { + Target target = new Target(); + configuration().target( target ).source( new Adapter() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + + @Test + public void testMissingRequiredFieldConfiguration() { + class RequiredFieldTarget { + @Configure + private String required; + } + RequiredFieldTarget target = new RequiredFieldTarget(); + try { + configuration().target( target ).source( System.getProperties() ).inject(); + fail( "Expected an exception because the configuration values could not be populated." ); + } catch ( ConfigurationException e ) { + assertThat( e.getMessage(), allOf(containsString("Failed"),containsString( "find" ),containsString( "required" )) ); + } + } + + @Test + public void testMissingOptionalFieldConfiguration() { + class OptionalFieldTarget { + @Configure + @Optional + private String optional = "default"; + } + OptionalFieldTarget target = new OptionalFieldTarget(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.optional, is("default") ); + } + + @Test + public void testMissingRequiredConfigurationParameter() { + class Target { + private String field; + @Configure + public void setRequired(String value) { + field = value; + } + } + Target target = new Target(); + try { + configuration().target( target ).source( System.getProperties() ).inject(); + fail( "Expected an exception because the configuration values could not be populated." ); + } catch ( ConfigurationException e ) { + assertThat( e.getMessage(), allOf(containsString("Failed"),containsString( "find" ),containsString( "required" )) ); + } + } + + @Test + public void testMissingRequiredConfigurationParameterWithDefault() { + class Target { + private String field; + @Configure + public void setRequired(@Default("default")String value) { + field = value; + } + } + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.field, is( "default" ) ); + } + + @Test + public void testTwoMissingRequiredConfigurationParameterWithDefault() { + class Target { + private String field1; + private String field2; + @Configure + public void setRequired(@Default("default1")String value1, @Default("default2")String value2) { + field1 = value1; + field2 = value2; + } + } + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.field1, is( "default1" ) ); + assertThat( target.field2, is("default2") ); + } + + @Test + public void testFieldBinding() { + class Target { + @Configure + private String user; + } + class Binding extends MappedConfigurationBinding { + Binding() { + bind("user","user.name"); + } + } + Target target = new Target(); + Properties source = System.getProperties(); + ConfigurationBinding binding = new Binding(); + configuration().target( target ).source( source ).binding( binding ).inject(); + assertThat( target.user, is(System.getProperty("user.name"))); + + } + + @Test + public void testFieldBindingUsingBuilderBinding() { + class Target { + @Configure + private String user; + } + Target target = new Target(); + Properties source = System.getProperties(); + configuration().target( target ).source( source ).bind( "user", "user.name" ).inject(); + assertThat( target.user, is(System.getProperty("user.name"))); + + } + + @Test + public void testFieldBindingUsingBuilderBindingFactory() { + class Target { + @Configure + private String user; + } + Target target = new Target(); + Properties source = System.getProperties(); + ConfigurationBinding binding = configuration().bind( "user", "user.name" ).binding(); + configuration().target( target ).source( source ).binding( binding ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + + } + + public static class UserBean { + public String getPrincipal() { + return "test-user"; + } + } + + @Test + public void testBeanAdapter() { + Target target = new Target(); + UserBean bean = new UserBean(); + configuration() + .target( target ) + .source( bean ) + .bind( "user.name", "principal" ) + .inject(); + assertThat( target.user, is( "test-user" ) ); + + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java new file mode 100755 index 0000000..b9336b5 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java @@ -0,0 +1,45 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MapFieldSampleTest { + + public static class Target { + @Configure + private int retryLimit = 3; + } + + static Map<String,String> config = new HashMap<String,String>(); + static { config.put( "retryLimit", "5" ); } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( config ).inject(); + assertThat( target.retryLimit, is(5) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java new file mode 100755 index 0000000..96f46ec --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java @@ -0,0 +1,49 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MapMethodSampleTest { + + public static class Target { + private int limit = 3; + + @Configure + public void setRetryLimit( int value ) { + limit = value; + } + } + + static Map<String,String> config = new HashMap<String,String>(); + static { config.put( "retryLimit", "5" ); } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( config ).inject(); + assertThat( target.limit, is( 5 ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java new file mode 100755 index 0000000..2adbe72 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java @@ -0,0 +1,40 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.*; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PropertiesFactorySampleTest { + + public static class Target { + @Configure @Alias("user.name") + private String user = "nobody"; + } + + @Test + public void sampleDirect() { + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java new file mode 100755 index 0000000..db0af61 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java @@ -0,0 +1,39 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PropertiesFieldSampleTest { + + public static class Target { + @Configure @Alias("user.name") + private String user = "nobody"; + } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java new file mode 100755 index 0000000..4b72e3b --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java @@ -0,0 +1,57 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PropertiesMethodSampleTest { + + public static class Target { + + private String user = "nobody"; + private String home = "nowhere"; + private String temp = "nowhere"; + + @Configure + @Alias("user.name") + public void setUser( String value ) { + user = value; + } + + @Configure + public void setDirs( + @Alias("user.dir") String home, + @Alias("java.io.tmpdir") String temp ) { + this.home = home; + this.temp = temp; + } + } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + assertThat( target.home, is( System.getProperty( "user.dir" ) ) ); + assertThat( target.temp, is( System.getProperty( "java.io.tmpdir" ) ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java new file mode 100755 index 0000000..2fe963a --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java @@ -0,0 +1,43 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config; + +import org.junit.Test; +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.configuration; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class UsageTest { + + class Target { + @Configure + private String user; + } + + @Test + public void usage() { + Target target = new Target(); + configuration() + .target( target ) + .source( System.getProperties() ) + .bind( "user", "user.name" ) + .inject(); + assertThat( target.user, is(System.getProperty("user.name"))); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java new file mode 100755 index 0000000..80913e2 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java @@ -0,0 +1,57 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor; +import org.junit.Test; + +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; + +import static junit.framework.TestCase.fail; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasKey; + +public class BeanConfigurationAdapterDescriptorTest { + + @Test + public void testServiceLoader() { + ServiceLoader<ConfigurationAdapterDescriptor> loader = ServiceLoader.load( ConfigurationAdapterDescriptor.class ); + Iterator<ConfigurationAdapterDescriptor> i = loader.iterator(); + while( i.hasNext() ) { + if( i.next() instanceof BeanConfigurationAdapterDescriptor ) { + return; + } + } + fail( "Failed to load BeanConfigurationAdapterDescriptor" ); + } + + @Test + public void testDescriptor() { + ConfigurationAdapterDescriptor descriptor = new BeanConfigurationAdapterDescriptor(); + Map<Class<?>,Class<? extends ConfigurationAdapter>> map = descriptor.providedConfigurationAdapters(); + assertThat( map, hasKey( (Class)Object.class ) ); + Class<? extends ConfigurationAdapter> type = map.get( Object.class ); + assertThat( + "Descriptor didn't return " + BeanConfigurationAdapter.class.getName(), + type == BeanConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java new file mode 100755 index 0000000..0d36eb6 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java @@ -0,0 +1,40 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.hadoop.gateway.config.impl; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class BeanConfigurationAdapterTest { + + public static class Bean { + public String getValue() { + return "beanValue"; + }; + } + + @Test + public void test() { + Bean bean = new Bean(); + BeanConfigurationAdapter adapter = new BeanConfigurationAdapter( bean ); + assertThat( adapter.getConfigurationValue( "value" ).toString(), is( "beanValue" ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/hsso-release/pom.xml ---------------------------------------------------------------------- diff --git a/hsso-release/pom.xml b/hsso-release/pom.xml index df85c52..c5abf8e 100644 --- a/hsso-release/pom.xml +++ b/hsso-release/pom.xml @@ -119,21 +119,13 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-oozie</artifactId> - </dependency> - <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-webhcat</artifactId> + <artifactId>gateway-service-definitions</artifactId> </dependency> <dependency> <groupId>${gateway-group}</groupId> <artifactId>gateway-service-tgs</artifactId> </dependency> <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-yarn-rm</artifactId> - </dependency> - <dependency> <groupId>${gateway-group}</groupId> <artifactId>gateway-provider-rewrite</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/knox/blob/ea28bf22/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index e121942..f980c97 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ <modules> <module>gateway-test-utils</module> <module>gateway-util-common</module> + <module>gateway-util-configinjector</module> <module>gateway-util-launcher</module> <module>gateway-util-urltemplate</module> <module>gateway-test-ldap</module> @@ -65,10 +66,8 @@ <module>gateway-service-hbase</module> <module>gateway-service-hive</module> <module>gateway-service-webhdfs</module> - <module>gateway-service-oozie</module> - <module>gateway-service-webhcat</module> <module>gateway-service-tgs</module> - <module>gateway-service-yarn-rm</module> + <module>gateway-service-definitions</module> <module>gateway-shell</module> <module>gateway-shell-launcher</module> <module>knox-cli-launcher</module> @@ -351,6 +350,11 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> + <artifactId>gateway-util-configinjector</artifactId> + <version>${gateway-version}</version> + </dependency> + <dependency> + <groupId>${gateway-group}</groupId> <artifactId>gateway-util-launcher</artifactId> <version>${gateway-version}</version> </dependency> @@ -486,17 +490,7 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-webhcat</artifactId> - <version>${gateway-version}</version> - </dependency> - <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-yarn-rm</artifactId> - <version>${gateway-version}</version> - </dependency> - <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-oozie</artifactId> + <artifactId>gateway-service-definitions</artifactId> <version>${gateway-version}</version> </dependency> <dependency> @@ -608,7 +602,7 @@ <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> - <version>4.2.5</version> + <version>4.3.6</version> </dependency> <!-- @@ -818,7 +812,11 @@ <artifactId>commons-net</artifactId> <version>1.4.1</version> </dependency> - + <dependency> + <groupId>commons-collections</groupId> + <artifactId>commons-collections</artifactId> + <version>3.2.1</version> + </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-digester3</artifactId>