A. It can be done a number of different ways.
- Using regex.
- Without using regex.
Step 1: Have the right dependency jars in the pom.xml file as shown below.
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Step 2: The implementation class.
package com.mycompany.app5;
import org.apache.commons.lang.StringUtils;
public class AlphaNumeric
{
public boolean isAlphaNumeric(String input)
{
//fail fast
if (StringUtils.isEmpty(input))
{
throw new IllegalArgumentException("The input cannot be empty !!!");
}
boolean result = false;
if (input.matches("[a-zA-Z0-9]+")) // one or more alpha numeric characters
{
result = true;
}
return result;
}
}
Step 3:The JUnit test class.
package com.mycompany.app5;Using apaches commons-lang packages method directly as shown below.
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
public class AlphaNumericTest
{
AlphaNumeric an;
@Before
public void setUp()
{
an = new AlphaNumeric();
}
@Test
public void testAlphaNumericHappyPath()
{
Assert.assertEquals(true, an.isAlphaNumeric("AUD123"));
Assert.assertEquals(true, an.isAlphaNumeric("123aud"));
Assert.assertEquals(true, an.isAlphaNumeric("123"));
Assert.assertEquals(true, an.isAlphaNumeric("hgD"));
Assert.assertEquals(false, an.isAlphaNumeric("hg*D"));
Assert.assertEquals(false, an.isAlphaNumeric("@hgD"));
Assert.assertEquals(false, an.isAlphaNumeric("@hgD"));
Assert.assertEquals(false, an.isAlphaNumeric("@*#"));
}
@Test(expected = java.lang.IllegalArgumentException.class)
public void testAlphaNumericUnHappyPath1()
{
Assert.assertEquals(true, an.isAlphaNumeric(null));
}
@Test(expected = java.lang.IllegalArgumentException.class)
public void testAlphaNumericUnHappyPath2()
{
Assert.assertEquals(true, an.isAlphaNumeric(""));
}
}
package com.mycompany.app5;Using the core Java without any framework
import org.apache.commons.lang.StringUtils;
public class AlphaNumeric
{
public boolean isAlphaNumeric(String input)
{
//fail fast
if (StringUtils.isEmpty(input))
{
throw new IllegalArgumentException("The input cannot be empty !!!");
}
boolean result = false;
if (StringUtils.isAlphanumeric(input)) // one or more alpha numeric characters
{
result = true;
}
return result;
}
}
package com.mycompany.app5;Alternatively, using the Character class.
import org.apache.commons.lang.StringUtils;
public class AlphaNumeric
{
public boolean isAlphaNumeric(String input)
{
//fail fast
if (StringUtils.isEmpty(input))
{
throw new IllegalArgumentException("The input cannot be empty !!!");
}
boolean result = true;
//chack each character agaianst the ASCII table hex value
for (int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a)
{
result = false;
break;
}
}
return result;
}
}
package com.mycompany.app5;
import org.apache.commons.lang.StringUtils;
public class AlphaNumeric
{
public boolean isAlphaNumeric(String input)
{
//fail fast
if (StringUtils.isEmpty(input))
{
throw new IllegalArgumentException("The input cannot be empty !!!");
}
boolean result = true;
//chack each character agaianst the ASCII table hex value
for (int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
if (!Character.isDigit(c) && !Character.isLetter(c))
{
result = false;
break;
}
}
return result;
}
}