-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarify the behaviour when a converter returns null (#618)
Signed-off-by: Emily Jiang <[email protected]>
- Loading branch information
1 parent
ed2cf3d
commit bc0ea99
Showing
7 changed files
with
196 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...croprofile/config/tck/converters/convertToNull/ConvertedNullValueBrokenInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2020 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.microprofile.config.tck.converters.convertToNull; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.inject.spi.DeploymentException; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.config.spi.Converter; | ||
import org.eclipse.microprofile.config.tck.converters.Pizza; | ||
import org.eclipse.microprofile.config.tck.converters.PizzaConverter; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.ShouldThrowException; | ||
import org.jboss.arquillian.testng.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* This is to test that when a converter returns null for some config value, an exception should be thrown and the | ||
* defaultValue should not be used. | ||
*/ | ||
public class ConvertedNullValueBrokenInjectionTest extends Arquillian { | ||
private @Inject Config config; | ||
private @Inject MyBean myBean; | ||
|
||
@Deployment | ||
@ShouldThrowException(DeploymentException.class) | ||
public static Archive deployment() { | ||
return ShrinkWrap.create(WebArchive.class, "ConvertedNullValueBrokenInjectionTest.war") | ||
.addClasses(ConvertedNullValueBrokenInjectionTest.class, Pizza.class, PizzaConverter.class, | ||
MyBean.class) | ||
.addAsResource( | ||
new StringAsset( | ||
"partial.pizza=cheese"), | ||
"META-INF/microprofile-config.properties") | ||
.addAsServiceProvider(Converter.class, PizzaConverter.class) | ||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
|
||
} | ||
|
||
@Test | ||
public void test() { | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
|
||
private @Inject @ConfigProperty(name = "partial.pizza", defaultValue = "medium:chicken") Pizza myPizza; | ||
|
||
public Pizza getPizza() { | ||
return myPizza; | ||
} | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
.../org/eclipse/microprofile/config/tck/converters/convertToNull/ConvertedNullValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2020 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.microprofile.config.tck.converters.convertToNull; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.config.spi.Converter; | ||
import org.eclipse.microprofile.config.tck.converters.Pizza; | ||
import org.eclipse.microprofile.config.tck.converters.PizzaConverter; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.testng.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* This is to test that when a converter returns null for some config value, null will be assigned. This means the | ||
* property still exists but the value is set to null on purpose. | ||
*/ | ||
public class ConvertedNullValueTest extends Arquillian { | ||
private @Inject Config config; | ||
private @Inject MyBean myBean; | ||
|
||
@Deployment | ||
public static Archive deployment() { | ||
return ShrinkWrap | ||
.create(WebArchive.class, "ConvertedNullValueTest.war") | ||
.addClasses(ConvertedNullValueTest.class, Pizza.class, PizzaConverter.class, MyBean.class) | ||
.addAsResource( | ||
new StringAsset( | ||
"partial.pizza=cheese"), | ||
"META-INF/microprofile-config.properties") | ||
.addAsServiceProvider(Converter.class, PizzaConverter.class) | ||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
|
||
} | ||
|
||
@Test | ||
public void testDefaultValueNotUsed() { | ||
// The converter returns null as the converter returns null if the property does not contain : | ||
// This will treat as if the property has been removed. The defaultValue will not be used. | ||
Assert.assertNull(myBean.getPizza()); | ||
} | ||
|
||
@Test | ||
public void testGetValue() { | ||
// The converter returns null as the converter returns null if the property does not contain : | ||
// Therefore, it will be treated as non-existence. | ||
Assert.assertThrows(NoSuchElementException.class, () -> config.getValue("partial.pizza", Pizza.class)); | ||
} | ||
|
||
@Test | ||
public void testGetOptionalValue() { | ||
// The converter returns null as the converter returns null if the property does not contain : | ||
// Therefore, it will be treated as non-existence. | ||
Assert.assertFalse(config.getOptionalValue("partial.pizza", Pizza.class).isPresent()); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
|
||
private @Inject @ConfigProperty(name = "partial.pizza", defaultValue = "medium:chicken") Optional<Pizza> myPizza; | ||
|
||
public Pizza getPizza() { | ||
return myPizza.isPresent() ? myPizza.get() : null; | ||
} | ||
} | ||
|
||
} |