Added additional Unit Tests to increase code coverage (#3692)

* Added additional Unit Tests to increase code coverage

Signed-off-by: Michael Hausegger <hausegger.michael@googlemail.com>
This commit is contained in:
Michael Hausegger 2019-05-24 08:24:40 +02:00 committed by Olivier Lamy
parent 0ebc0fa9ce
commit a3b755858d
3 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,103 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
/**
* Unit tests for class {@link ArrayUtil}.
*
* @see ArrayUtil
*/
public class ArrayUtilTest
{
@Test
public void testAddToArrayWithEmptyArray()
{
String[] stringArray = new String[0];
String[] resultArray = ArrayUtil.addToArray(stringArray, "Ca?", Object.class);
assertEquals(0, stringArray.length);
assertEquals(1, resultArray.length);
assertNotSame(stringArray, resultArray);
assertNotSame(resultArray, stringArray);
assertFalse(resultArray.equals(stringArray));
assertEquals(String.class, resultArray[0].getClass());
}
@Test
public void testAddUsingNull()
{
String[] stringArray = new String[7];
String[] stringArrayTwo = ArrayUtil.add(stringArray, null);
assertEquals(7, stringArray.length);
assertEquals(7, stringArrayTwo.length);
assertSame(stringArray, stringArrayTwo);
assertSame(stringArrayTwo, stringArray);
}
@Test
public void testAddWithNonEmptyArray()
{
Object[] objectArray = new Object[3];
Object[] objectArrayTwo = ArrayUtil.add(objectArray, objectArray);
assertEquals(3, objectArray.length);
assertEquals(6, objectArrayTwo.length);
assertNotSame(objectArray, objectArrayTwo);
assertNotSame(objectArrayTwo, objectArray);
assertFalse(objectArrayTwo.equals(objectArray));
}
@Test
public void testRemoveFromNullArrayReturningNull()
{
assertNull(ArrayUtil.removeFromArray((Integer[])null, new Object()));
}
@Test
public void testRemoveNulls()
{
Object[] objectArray = new Object[2];
objectArray[0] = new Object();
Object[] resultArray = ArrayUtil.removeNulls(objectArray);
assertEquals(2, objectArray.length);
assertEquals(1, resultArray.length);
assertNotSame(objectArray, resultArray);
assertNotSame(resultArray, objectArray);
assertFalse(resultArray.equals(objectArray));
}
}

View File

@ -0,0 +1,76 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Array;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit tests for class {@link IntrospectionUtil}.
*
* @see IntrospectionUtil
*/
public class IntrospectionUtilTest
{
@Test
public void testIsTypeCompatibleWithTwoTimesString()
{
assertTrue(IntrospectionUtil.isTypeCompatible(String.class, String.class, true));
}
@Test
public void testIsSameSignatureWithNull()
{
assertFalse(IntrospectionUtil.isSameSignature(null, null));
}
@Test
public void testFindMethodWithEmptyString()
{
assertThrows(NoSuchMethodException.class,
() -> IntrospectionUtil.findMethod(Integer.class, "", null, false, false));
}
@Test
public void testFindMethodWithNullMethodParameter()
{
assertThrows(NoSuchMethodException.class,
() -> IntrospectionUtil.findMethod(String.class, null, (Class<Integer>[])Array.newInstance(Class.class, 3), true, true));
}
@Test
public void testFindMethodWithNullClassParameter() throws NoSuchMethodException
{
assertThrows(NoSuchMethodException.class,
() -> IntrospectionUtil.findMethod(null, "subSequence", (Class<Object>[])Array.newInstance(Class.class, 9), false, false));
}
@Test
public void testIsJavaBeanCompliantSetterWithNull()
{
assertFalse(IntrospectionUtil.isJavaBeanCompliantSetter(null));
}
}

View File

@ -0,0 +1,55 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import org.junit.jupiter.api.Test;
import java.util.Locale;
import java.util.MissingResourceException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Unit tests for class {@link Loader}.
*
* @see Loader
*/
public class LoaderTest
{
@Test
public void testGetResourceBundleThrowsMissingResourceException()
{
assertThrows(MissingResourceException.class, () -> Loader.getResourceBundle("nothing", true, Locale.ITALIAN));
}
@Test
public void testLoadClassThrowsClassNotFoundException()
{
assertThrows(ClassNotFoundException.class, () -> Loader.loadClass(Object.class, "String"));
}
@Test
public void testLoadClassSucceeds() throws ClassNotFoundException
{
assertEquals(LazyList.class, Loader.loadClass(Object.class, "org.eclipse.jetty.util.LazyList"));
}
}