Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CNUCollegesTest.java import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import

image text in transcribed

CNUCollegesTest.java

import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import com.google.common.truth.Truth; class CNUCollegesTest { private static final String ENUM = "CNUColleges"; private static final List CONSTANTS = List.of( "AH", "NBS", "LSB" ); private static final List METHODS = Arrays.asList( new String[]{ "values", "valueOf", "toString" }); private static final Function STRINGS = constant -> { var index = CONSTANTS.indexOf( constant ); switch (index) { case 0 : return "Arts and Humanities"; case 1 : return "Natural and Behavioral Sciences"; default: return "Luter School of Business"; } }; private Class> getClass(String name) { try { Package pkg = getClass().getPackage(); String path = (pkg == null || pkg.getName().isEmpty()) ? "" : pkg.getName()+"."; return Class.forName( path + name ); } catch (ClassNotFoundException e) { fail( String.format( "'%s' doesn't exist", name )); } return null; } private void isEnum(Class> type) { Truth.assertWithMessage( String.format( "Class '%s' is not an enum type", type.getSimpleName()) ) .that ( type.isEnum() ) .isTrue(); } private Enum>[] getEnumConstants(Class> type) { isEnum( type ); return (Enum>[]) type.getEnumConstants(); } private Method getMethod(Class> clazz, Class> result, String name, Class>... args) { try { var method = clazz.getDeclaredMethod( name, args ); Truth.assertWithMessage( String.format("Method '%s' must be public", name )) .that ( Modifier.isPublic( method.getModifiers() )) .isTrue(); Class> actual = method.getReturnType(); Truth.assertWithMessage( String.format("Unexpected return type in '%s'", name )) .that ( actual ) .isEqualTo ( result ); return method; } catch (NoSuchMethodException | SecurityException e) { String str = Arrays.stream( args ).map( Class::getSimpleName ).collect( Collectors.joining( "," )); String msg = String.format( "'%s' doesn't have method '%s(%s)'", clazz.getSimpleName(), name, str ); fail( String.format( msg )); } return null; } void testResultsFromEnumMethod(Class> result, String named, Function values, Function to) { var clazz = getClass ( ENUM ); var method = getMethod( clazz, result, named ); try { // check it has all constants and corresponding data. Enum>[] constants = getEnumConstants( clazz ); List tally = new ArrayList( CONSTANTS ); for (Enum> e : constants) { var name = e.name(); var actual = method.invoke( e ); var expected = values.apply( name ); Truth.assertWithMessage( String.format("calling '%s.%s()' returned null", name, named ) ) .that( actual ) .isNotNull(); var string = to.apply( actual ); Truth.assertWithMessage( String.format("unexpected result from '%s.%s()'", name, named )) .that ( string ) .isEqualTo ( expected ); tally.remove( name ); } Truth.assertWithMessage( "constants not found: " + tally ) .that ( tally ) .isEmpty(); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); fail( "Unexpected exception thrown" ); } } @Test void testIsPublicEnum() { var clazz = getClass( ENUM ); isEnum( clazz ); Truth.assertWithMessage( String.format( "'%s' is not public", ENUM )) .that( Modifier.isPublic( clazz.getModifiers() )) .isTrue(); var actual = Arrays.stream ( getEnumConstants( clazz )) .map ( Enum::name ) .collect( Collectors.toList() ); Truth.assertWithMessage( "constants differ from expected" ) .that ( actual ) .containsExactlyElementsIn( CONSTANTS ); } @Test void testHasPrivateNonStaticFields() { Class> type = getClass( ENUM ); Arrays.stream( type.getDeclaredFields()) .filter( f -> !f.isSynthetic() && !f.isEnumConstant()).forEach( f -> { var mods = f.getModifiers(); var name = f.getName(); Truth.assertWithMessage( String.format( "field '%s' should be private", name )) .that ( Modifier.isPrivate( mods )) .isTrue(); Truth.assertWithMessage( String.format( "field '%s' cannot be static", name )) .that ( Modifier.isStatic ( mods )) .isFalse(); }); } @Test void testHasConstructor() { var type = getClass( ENUM ); try { var constructors = type.getDeclaredConstructors(); Truth.assertWithMessage( "Enum should have 1 constructor" ) .that ( constructors ) .hasLength ( 1 ); var parameters = constructors[0].getParameterTypes(); Truth.assertWithMessage( "Constructor should receive 1 parameter" ) .that ( parameters ) .hasLength ( 3 ); // has two (additional) implicit parameters Truth.assertWithMessage( "unexpected constructor parameter type: " +parameters[2].getSimpleName() ) .that ( parameters[2] ) .isAssignableTo ( String.class ); } catch (SecurityException e) { fail( "Constructor doesn't exist" ); } } @Test void testNoOtherMethodsExist() { BiConsumer, List> classHasMethods = (clazz,expected) -> Truth.assertWithMessage( String.format( "incorrect methods in class '%s'", clazz.getSimpleName() )) .that( Arrays.stream( clazz.getDeclaredMethods() ) .map ( Method::getName ) .filter( n -> !n.startsWith("$")) .collect( Collectors.toList() )) .containsExactlyElementsIn( expected ); var clazz = getClass( ENUM ); classHasMethods.accept( clazz, METHODS ); } @Test void testToString() { testResultsFromEnumMethod( String.class, "toString", STRINGS, Function.identity() ); } }
2. (40 points) Write a standalone enumerated type CNUColleges that has constants AH, NBS and LSB. Each constant has a name, which are "Arts and Humanities", "Natural and Behavioral Sciences" and "Luter School of Business", respectively. Override the method toString to return the corresponding name. Use the file CNUCollegesTest.java (downloadable from Scholar) to validate your implementation. No Javadoc needed. No compile errors must remain

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

=+ (a) Prove that I()(t)= fox'-1(log x)*e * dx.

Answered: 1 week ago

Question

Identify the elements that make up the employee reward package.

Answered: 1 week ago

Question

Understand the purpose, value and drawbacks of the interview.

Answered: 1 week ago