@SuppressWarnings("unchecked") @Override public <T> Class<T> mockClass(final MockFeatures<T> params) { try { ClassLoader classLoader = params.mockedType.getClassLoader(); return (Class<T>) typeCache.findOrInsert(classLoader, new MockitoMockKey(params.mockedType, params.interfaces, params.serializableMode, params.stripAnnotations), new Callable<Class<?>>() { @Override public Class<?> call() throws Exception { return bytecodeGenerator.mockClass(params); } }, BOOTSTRAP_LOCK); } catch (IllegalArgumentException exception) { Throwable cause = exception.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else { throw exception; } } }
Throwable cause = e.getCause(); if (cause instanceof UnrecognizedPropertyException) { UnrecognizedPropertyException ex = (UnrecognizedPropertyException) cause;
Throwable cause = e.getCause(); if (cause instanceof UnrecognizedPropertyException) { UnrecognizedPropertyException ex = (UnrecognizedPropertyException) cause;
private void testInvalidValueToPOJO(String key) { try { new JsonObject().put(key, "1").mapTo(MyType2.class); fail(); } catch (IllegalArgumentException e) { assertThat(e.getCause(), is(instanceOf(InvalidFormatException.class))); InvalidFormatException ife = (InvalidFormatException) e.getCause(); assertEquals("1", ife.getValue()); } }
@Test public void testParseOptsWrongThreads() { Queue<String> opts = new LinkedList<>(); String cmdName = "sequentialWrite"; opts.offer(cmdName); opts.offer("qq"); try { PerformanceEvaluation.parseOpts(opts); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); assertEquals("Command " + cmdName + " does not have threads number", e.getMessage()); assertTrue(e.getCause() instanceof NumberFormatException); } }
@Test public void testThatInvalidValuesAsDefaultValueAreReported() throws CLIException { try { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class).setDefaultValue("a")); } catch (IllegalArgumentException e) { assertThat(e.getCause()).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e.getCause(); assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
@Test public void testParseOptsNoThreads() { Queue<String> opts = new LinkedList<>(); String cmdName = "sequentialWrite"; try { PerformanceEvaluation.parseOpts(opts); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); assertEquals("Command " + cmdName + " does not have threads number", e.getMessage()); assertTrue(e.getCause() instanceof NoSuchElementException); } }
public void testClassWithNoParseFrom() { try { new ProtoBufSerializer<Message>("java=" + MessageWithNoParseFrom.class.getName()); } catch(IllegalArgumentException e) { assertEquals(NoSuchMethodException.class, e.getCause().getClass()); return; } fail("IllegalArgumentException should have been thrown"); }
@Test public void testIncorrectSyntax() { Session s = openSession(); Transaction t = s.beginTransaction(); try { s.createQuery( "update Human set Human.description = 'xyz' where Human.id = 1 and Human.description is null" ); fail( "expected failure" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( QueryException expected ) { // ignore : expected behavior } t.commit(); s.close(); }
public void testNonExistentClass() { try { new ProtoBufSerializer<Message>("java=com.foo.Bar"); } catch(IllegalArgumentException e) { assertEquals(ClassNotFoundException.class, e.getCause().getClass()); return; } fail("IllegalArgumentException should have been thrown"); }
@Test public void testDeleteNonExistentEntity() { Session s = openSession(); Transaction t = s.beginTransaction(); try { s.createQuery( "delete NonExistentEntity" ).executeUpdate(); fail( "no exception thrown" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( QueryException ignore ) { } t.commit(); s.close(); }
@Test public void testUpdateNonExistentEntity() { Session s = openSession(); Transaction t = s.beginTransaction(); try { s.createQuery( "update NonExistentEntity e set e.someProp = ?" ).executeUpdate(); fail( "no exception thrown" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( QueryException e ) { } t.commit(); s.close(); }
@Test public void testTupleReturnFails() { Session s = openSession(); Transaction txn = s.beginTransaction(); try { s.createQuery( "select a, a.weight from Animal a inner join fetch a.offspring" ).scroll(); fail( "scroll allowed with collection fetch and reurning tuples" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( HibernateException e ) { // expected result... } txn.commit(); s.close(); }
@Test public void testSelectWithNamedParamProjection() { Session s = openSession(); try { s.createQuery("select :someParameter, id from Car"); fail("Should throw an unsupported exception"); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch(QueryException q) { // allright } finally { s.close(); } }
@Test public void testFetchInSubqueryFails() { Session s = openSession(); try { s.createQuery( "from Animal a where a.mother in (select m from Animal a1 inner join a1.mother as m join fetch m.mother)" ).list(); fail( "fetch join allowed in subquery" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( QueryException expected ) { // expected behavior } s.close(); }
@Test public void testIoExceptionWhenLoadingHeader() throws Exception { final HeaderCheck check = PowerMockito.spy(new HeaderCheck()); PowerMockito.doThrow(new IOException("expected exception")).when(check, "loadHeader", any()); try { check.setHeader("header"); fail("Exception expected"); } catch (IllegalArgumentException ex) { assertTrue("Invalid exception cause", ex.getCause() instanceof IOException); assertEquals("Invalid exception message", "unable to load header", ex.getMessage()); } }
@Test @TestForIssue( jiraKey = "HHH-11942" ) public void testOrderByExtraParenthesis() throws Exception { try { doInHibernate( this::sessionFactory, session -> { session.createQuery( "select a from Product a " + "where " + "coalesce(a.description, :description) = :description ) " + "order by a.description ", Product.class) .setParameter( "description", "desc" ) .getResultList(); fail("Should have thrown exception"); } ); } catch (IllegalArgumentException e) { final Throwable cause = e.getCause(); assertThat( cause, instanceOf( QuerySyntaxException.class ) ); assertTrue( cause.getMessage().contains( "expecting EOF, found ')'" ) ); } }
@Test public void testWithClauseFailsWithFetch() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction txn = s.beginTransaction(); try { s.createQuery( "from Animal a inner join fetch a.offspring as o with o.bodyWeight = :someLimit" ) .setDouble( "someLimit", 1 ) .list(); fail( "ad-hoc on clause allowed with fetched association" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch ( HibernateException e ) { // the expected response... } txn.commit(); s.close(); data.cleanup(); }
@Test public void testUpdateOnImplicitJoinFails() { inTransaction( s -> { try { s.createQuery( "update Human set mother.name.initial = :initial" ).setString( "initial", "F" ).executeUpdate(); fail( "update allowed across implicit join" ); } catch (IllegalArgumentException e) { assertTyping( QueryException.class, e.getCause() ); } catch( QueryException e ) { } } ); }
@Test public void testMultipleBagFetchesFail() { try ( final SessionImplementor s = (SessionImplementor) openSession() ) { inTransaction( s, session-> { try { s.createQuery( "from Human h join fetch h.friends f join fetch f.friends fof" ).list(); fail( "failure expected" ); } catch (IllegalArgumentException e) { assertTyping( MultipleBagFetchException.class, e.getCause() ); } catch( HibernateException e ) { assertTrue( "unexpected failure reason : " + e, e.getMessage().indexOf( "multiple bags" ) > 0 ); } } ); } }