/** * Asserts that the two given schemas are equal. * * @param fieldName * name of the field owning that schema, if it's not a top-level schema * @param actual * the actual schema * @param expected * the expected schema */ public static void assertConnectSchemasAreEqual(String fieldName, Schema actual, Schema expected) { if (!areConnectSchemasEqual(actual, expected)) { // first try failing with an assertion message that shows the actual difference assertThat(SchemaUtil.asString(actual)).describedAs("field name: " + fieldName).isEqualTo(SchemaUtil.asString(expected)); // compare schema parameters assertThat(actual.parameters()).describedAs("field '" + fieldName + "' parameters").isEqualTo(expected.parameters()); // fall-back just in case (e.g. differences of element schemas of arrays) fail("field '" + fieldName + "': " + SchemaUtil.asString(actual) + " was not equal to " + SchemaUtil.asString(expected)); } }
@Test public void cannotInsertSameEntityTwice() throws Exception { em.getTransaction().begin(); // given MakeupArtist wibke = new MakeupArtist( "wibke", "halloween" ); em.persist( wibke ); em.getTransaction().commit(); em.clear(); em.getTransaction().begin(); // when MakeupArtist notWibke = new MakeupArtist( "wibke", "glamorous" ); em.persist( notWibke ); try { em.getTransaction().commit(); fail( "Expected exception wasn't raised" ); } catch ( Exception e ) { // then assertThat( e.getCause() ).isExactlyInstanceOf( PersistenceException.class ); assertThat( e.getCause().getMessage() ).matches( ".*OGM000067.*" ); } em.clear(); em.getTransaction().begin(); MakeupArtist loadedMakeupArtist = em.find( MakeupArtist.class, "wibke" ); assertThat( loadedMakeupArtist ).isNotNull(); assertThat( loadedMakeupArtist.getFavoriteStyle() ).describedAs( "Second insert should not be applied" ) .isEqualTo( "halloween" ); em.remove( loadedMakeupArtist ); em.getTransaction().commit(); }
); assertThat( loadedMakeupArtist ).isNotNull(); assertThat( loadedMakeupArtist.getFavoriteStyle() ).describedAs( "Second insert should not be applied" ) .isEqualTo( "halloween" );