Skip to content

Commit

Permalink
refs: eclipse#57 eclipse#9 fix ConfigValue TCK tests
Browse files Browse the repository at this point in the history
  • Loading branch information
struberg committed May 8, 2018
1 parent db9d9c8 commit 93ba47e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 30 deletions.
51 changes: 21 additions & 30 deletions tck/src/main/java/org/eclipse/configjsr/ConfigValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.eclipse.configjsr;

import org.eclipse.configjsr.base.AbstractTest;
import org.eclipse.configjsr.configsources.ConfigurableConfigSource;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand All @@ -30,6 +31,7 @@

import javax.config.Config;
import javax.config.ConfigValue;
import javax.config.spi.ConfigSource;
import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

Expand All @@ -47,6 +49,7 @@ public static WebArchive deploy() {
.addPackage(AbstractTest.class.getPackage())
.addClass(ConfigValueTest.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsServiceProvider(ConfigSource.class, ConfigurableConfigSource.class)
.as(JavaArchive.class);

AbstractTest.addFile(testJar, "META-INF/javaconfig.properties");
Expand Down Expand Up @@ -83,21 +86,32 @@ public void testGetValueWithStringDefault() {

@Test
public void testLookupChain() {
// set the projectstage to 'Production'
ConfigurableConfigSource.configure(config, "javaconfig.projectStage", "Production");

/**
* 1 1 -> com.foo.myapp.mycorp.Production
* 1 0 -> com.foo.myapp.mycorp
* 0 1 -> com.foo.myapp.Production
* 0 0 -> com.foo.myapp
*
*/
String cv = config.access("com.foo.myapp")
.withLookupChain("mycorp", "${javaconfig.projectStage}")
.getValue();
ConfigValue<String> cv = config.access("com.foo.myapp")
.withLookupChain("mycorp", "${javaconfig.projectStage}");

Assert.assertFalse(cv.getOptionalValue().isPresent());

ConfigurableConfigSource.configure(config, "com.foo.myapp", "TheDefault");
Assert.assertEquals(cv.getValue(), "TheDefault");

//X TODO continue/finish
ConfigurableConfigSource.configure(config, "com.foo.myapp.Production", "BasicWithProjectStage");
Assert.assertEquals(cv.getValue(), "BasicWithProjectStage");

ConfigurableConfigSource.configure(config, "com.foo.myapp.mycorp", "WithTenant");
Assert.assertEquals(cv.getValue(), "WithTenant");

config.getValue("mykey", Integer[].class);
ConfigurableConfigSource.configure(config, "com.foo.myapp.mycorp.Production", "WithTenantAndProjectStage");
Assert.assertEquals(cv.getValue(), "WithTenantAndProjectStage");
}

@Test
Expand Down Expand Up @@ -162,35 +176,12 @@ public void testBooleanConverter() {
Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.y_uppercase").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.ja").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.ja_uppercase").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.ja_mixedcase").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.no_mixedcase").as(Boolean.class).getValue(),
Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.no").as(Boolean.class).getValue(),
Boolean.FALSE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.j").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.j_uppercase").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.n_uppercase").as(Boolean.class).getValue(),
Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.no_mixedcase").as(Boolean.class).getValue(),
Boolean.FALSE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.oui").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.oui_uppercase").as(Boolean.class).getValue(),
Boolean.TRUE);

Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.boolean.oui_mixedcase").as(Boolean.class).getValue(),
Boolean.TRUE);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2016-2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed 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.eclipse.configjsr.configsources;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import javax.config.Config;
import javax.config.spi.ConfigSource;

/**
* A ConfigSource which can be used to play through multiple
* different scenarios in our TCK.
*
* @author <a href="mailto:[email protected]">Mark Struberg</a>
*/
public class ConfigurableConfigSource implements ConfigSource {

private Consumer<Set<String>> reportAttributeChange;

private Map<String, String> properties = new HashMap<>();

@Override
public int getOrdinal() {
return 110;
}

@Override
public Map<String, String> getProperties() {
return properties;
}

@Override
public String getValue(String propertyName) {
return properties.get(propertyName);
}

@Override
public String getName() {
return this.getClass().getName();
}

@Override
public void setOnAttributeChange(Consumer<Set<String>> reportAttributeChange) {
this.reportAttributeChange = reportAttributeChange;
}

public static void configure(Config cfg, String propertyName, String value) {
for (ConfigSource configSource : cfg.getConfigSources()) {
if (configSource instanceof ConfigurableConfigSource) {
((ConfigurableConfigSource) configSource).properties.put(propertyName, value);
((ConfigurableConfigSource) configSource).reportAttributeChange.accept(Collections.singleton(propertyName));
return;
}
}

throw new IllegalStateException("This Config doesn't contain a ConfigurableConfigSource!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ tck.config.test.javaconfig.configvalue.boolean.yes=yes
tck.config.test.javaconfig.configvalue.boolean.yes_uppercase=YES
tck.config.test.javaconfig.configvalue.boolean.yes_mixedcase=Yes
tck.config.test.javaconfig.configvalue.boolean.no=no
tck.config.test.javaconfig.configvalue.boolean.no_mixedcase=No

tck.config.test.javaconfig.configvalue.boolean.y=y
tck.config.test.javaconfig.configvalue.boolean.y_uppercase=Y
Expand Down

0 comments on commit 93ba47e

Please sign in to comment.