Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 =

image text in transcribed

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

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_2

Step: 3

blur-text-image_3

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

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions