-
Notifications
You must be signed in to change notification settings - Fork 115
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 null is returned by the converters
Signed-off-by: Emily Jiang <emijiang6@googlemail.com>
- Loading branch information
1 parent
2c42cc9
commit 1c6700f
Showing
5 changed files
with
124 additions
and
28 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
93 changes: 93 additions & 0 deletions
93
tck/src/main/java/org/eclipse/microprofile/config/tck/converters/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,93 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
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.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 testDefaultValue() { | ||
// The converter returns null as the converter returns null if the property does not contain : | ||
// Therefore, the defaultValue was used. | ||
Assert.assertEquals(myBean.getPizza(), new Pizza("chicken", "medium")); | ||
} | ||
|
||
@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") Pizza myPizza; | ||
|
||
public Pizza getPizza() { | ||
return myPizza; | ||
} | ||
} | ||
|
||
} |