Skip to content

Commit

Permalink
FORGE-2618: Empty String to Integer conversion returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Mar 14, 2016
1 parent 6620b15 commit f5acca9
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
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);
}

}
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(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class InputComponentsTest
@Inject
private UIInputMany<Path> paths;

@Inject
private UIInput<Integer> integerInput;

@Inject
private ConverterFactory converterFactory;

Expand Down Expand Up @@ -80,6 +83,7 @@ public void testUIInputManyPathSingleList()
Assert.assertThat(list, hasItem(OperatingSystemUtils.getTempDirectory().toPath()));
}

@Test
public void testUISelectOneIndex()
{
values.setValueChoices(Arrays.asList("A", "B", "C"));
Expand All @@ -89,6 +93,7 @@ public void testUISelectOneIndex()
Assert.assertEquals(-1, values.getSelectedIndex());
}

@Test
public void testUISelectManyIndexes()
{
manyValues.setValueChoices(Arrays.asList("A", "B", "C"));
Expand All @@ -97,4 +102,14 @@ public void testUISelectManyIndexes()
manyValues.setValue(Collections.emptyList());
Assert.assertArrayEquals(new int[0], manyValues.getSelectedIndexes());
}

@Test
public void testUIInputIntegerWithEmptyStringValue()
{
Assert.assertNull(integerInput.getValue());
InputComponents.setValueFor(converterFactory, integerInput, "");
Assert.assertNull(integerInput.getValue());
InputComponents.setValueFor(converterFactory, integerInput, "123");
Assert.assertThat(integerInput.getValue(), equalTo(123));
}
}

0 comments on commit f5acca9

Please sign in to comment.