http://git-wip-us.apache.org/repos/asf/knox/blob/24156aaf/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..72bdedf --- /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 String 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/24156aaf/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/24156aaf/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/24156aaf/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/24156aaf/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/24156aaf/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/24156aaf/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/24156aaf/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/24156aaf/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..123fb7e --- /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" ), is( "beanValue" ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/24156aaf/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 284e621..aee4cf2 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> @@ -352,6 +353,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>
