Answered step by step
Verified Expert Solution
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
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 List2. (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 remainCONSTANTS = 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started