-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-2618: Empty String to Integer conversion returns null
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...rc/main/java/org/jboss/forge/addon/convert/impl/generators/IntegerConverterGenerator.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,48 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.convert.impl.generators; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import org.jboss.forge.addon.convert.Converter; | ||
import org.jboss.forge.addon.convert.ConverterGenerator; | ||
import org.jboss.forge.furnace.util.Strings; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
@Singleton | ||
public class IntegerConverterGenerator implements ConverterGenerator, Converter<String, Integer> | ||
{ | ||
|
||
@Override | ||
public boolean handles(Class<?> source, Class<?> target) | ||
{ | ||
return source == String.class && target == Integer.class; | ||
} | ||
|
||
@Override | ||
public Converter<?, ?> generateConverter(Class<?> source, Class<?> target) | ||
{ | ||
return this; | ||
} | ||
|
||
@Override | ||
public Class<? extends Converter<?, ?>> getConverterType() | ||
{ | ||
return IntegerConverterGenerator.class; | ||
} | ||
|
||
@Override | ||
public Integer convert(String source) | ||
{ | ||
return (Strings.isNullOrEmpty(source)) ? null : Integer.valueOf(source); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
convert/tests/src/test/java/org/jboss/forge/addon/convert/exported/NumberConverterTest.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,59 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.addon.convert.exported; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.forge.addon.convert.Converter; | ||
import org.jboss.forge.addon.convert.ConverterFactory; | ||
import org.jboss.forge.arquillian.AddonDeployment; | ||
import org.jboss.forge.arquillian.AddonDeployments; | ||
import org.jboss.forge.arquillian.archive.AddonArchive; | ||
import org.jboss.forge.furnace.repositories.AddonDependencyEntry; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class NumberConverterTest | ||
{ | ||
@Deployment | ||
@AddonDeployments({ | ||
@AddonDeployment(name = "org.jboss.forge.addon:convert"), | ||
@AddonDeployment(name = "org.jboss.forge.furnace.container:cdi") | ||
}) | ||
public static AddonArchive getDeployment() | ||
{ | ||
AddonArchive archive = ShrinkWrap | ||
.create(AddonArchive.class) | ||
.addBeansXML() | ||
.addPackage(StringToExportedConverterTest.class.getPackage()) | ||
.addAsAddonDependencies( | ||
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"), | ||
AddonDependencyEntry.create("org.jboss.forge.addon:convert")); | ||
|
||
return archive; | ||
} | ||
|
||
@Inject | ||
private ConverterFactory converterFactory; | ||
|
||
@Test | ||
public void testEmptyStringToIntegerConversion() throws Exception | ||
{ | ||
Converter<String, Integer> converter = converterFactory.getConverter(String.class, Integer.class); | ||
Assert.assertNull(converter.convert("")); | ||
} | ||
} |
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