Question: OlympicMedalTest.java import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import com.google.common.truth.Truth; class OlympicMedalTest { private static final String ENUM =

 OlympicMedalTest.java import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.Arrays; import

OlympicMedalTest.java

import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import com.google.common.truth.Truth; class OlympicMedalTest { private static final String ENUM = "OlympicMedal"; private static final String[] CONSTANTS = { "GOLD","SILVER","BRONZE" }; private static final String[] METHODS = { "values", "valueOf" }; 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( "enum '%s' doesn't exist", name )); } return null; } private static void isEnum(Class> type) { Truth.assertWithMessage( String.format( "'%s' is not an enum type", type.getSimpleName()) ) .that ( type.isEnum() ) .isTrue(); } private static Enum>[] getEnumConstants(Class> type) { isEnum( type ); return (Enum>[])type.getEnumConstants(); } @Test void testIsPublicEnum() { Class> a = getClass( ENUM ); isEnum( a ); Truth.assertWithMessage( String.format( "'%s' is not public", ENUM )) .that( Modifier.isPublic( a.getModifiers() )) .isTrue(); } @Test void testHasConstants() { var type = getClass( ENUM ); var actual = Arrays.stream ( getEnumConstants( type )) .map ( Enum::name ) .collect( Collectors.toList() ); Truth.assertWithMessage( "constants differ from expected" ) .that ( actual ) .containsExactlyElementsIn( CONSTANTS ); } @Test void testNoConstructors() { Class> type = getClass( ENUM ); try { Constructor>[] constructors = type.getDeclaredConstructors(); Truth.assertWithMessage( "Enum should have no constructor" ).that( constructors ).hasLength( 1 ); Class>[] parameters = constructors[0].getParameterTypes(); Truth.assertWithMessage( "Constructor should have no parameters" ).that( parameters.length-1 ).isEqualTo( 1 ); } catch (SecurityException e) { fail( "Constructor doesn't exist" ); } } @Test void testNoFieldsExist() { Class> type = getClass( ENUM ); Arrays.stream ( type.getDeclaredFields() ) .filter ( f -> !f.isSynthetic() && !f.isEnumConstant()) .findAny () .ifPresent( f -> fail( String.format( "unexpected field '%s' found", f.getName() ))); } @Test void testNoMethodsExist() { var type = getClass( ENUM ); var actual = Arrays.stream( type.getDeclaredMethods() ) .filter( f -> !f.isSynthetic() ) .map ( f -> f.getName() ) .collect( Collectors.toList() ); Truth.assertWithMessage( "methods differ from expected" ) .that ( actual ) .containsExactlyElementsIn( METHODS ); } }

1. (20 points) Write a standalone enumerated type OlympicMedal that has constants GOLD, SILVER and BRONZE. Use the file OlympicMedalTest.java (download from Scholar) to validate your implementation. No Javadoc needed. No compile errors must remain. Upload OlympicMedal.java to Scholar before the end of the exam

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!