SqlMapQuery implements RepositoryQuery { @Override public Object execute(Object[] parameters) { Object parameterObject = parametersParsing(parameters); if (StatementType.SELECT.equals(statementType)) { if (!queryMethod.isPageQuery() && !queryMethod.isCollectionQuery()) { return template.queryForObject(queryMethod.getNamedQueryName(), parameterObject); } else if (queryMethod.isCollectionQuery()) { return template.queryForList(queryMethod.getNamedQueryName(), parameterObject); } } else if (StatementType.INSERT.equals(statementType)) { template.insert(queryMethod.getNamedQueryName(), parameterObject); return parameterObject; } else if (StatementType.UPDATE.equals(statementType)) { return template.update(queryMethod.getNamedQueryName(), parameterObject); } else if (StatementType.DELETE.equals(statementType)) { return template.delete(queryMethod.getNamedQueryName(), parameterObject); } throw new UnsupportedOperationException(); } SqlMapQuery(QueryMethod queryMethod, SqlMapExecutorDelegate delegate, SqlMapClientTemplate sqlMapClientTemplate); @Override Object execute(Object[] parameters); @Override QueryMethod getQueryMethod();  }
@Test public void COLLECTION을_반환하는_조회쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.SELECT); when(queryMethod.isCollectionQuery()).thenReturn(true); new SqlMapQuery(queryMethod, delegate, template).execute(null); verify(template).queryForList(namedQueryName, null); }
@Test public void SINGLE_ENTITY를_반환하는_조회쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.SELECT); when(queryMethod.isCollectionQuery()).thenReturn(false); new SqlMapQuery(queryMethod, delegate, template).execute(null); verify(template).queryForObject(namedQueryName, null); }
@Test public void 등록쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.INSERT); new SqlMapQuery(queryMethod, delegate, template).execute(null); verify(template).insert(namedQueryName, null); }
@Test public void 수정쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.UPDATE); new SqlMapQuery(queryMethod, delegate, template).execute(null); verify(template).update(namedQueryName, null); }
@Test public void 삭제쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.DELETE); new SqlMapQuery(queryMethod, delegate, template).execute(null); verify(template).delete(namedQueryName, null); }
@Test(expected=UnsupportedOperationException.class) public void 알수없는_쿼리_유형을_만나면_예외발생() { new SqlMapQuery(queryMethod, delegate, template).execute(null); }
@Test public void 파라메터가_없이_쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.INSERT); new SqlMapQuery(queryMethod, delegate, template).execute(null); verify(template).insert(namedQueryName, null); }
@Test public void 하나의_파라메터로_쿼리_실행() { when(mappedStatement.getStatementType()).thenReturn(StatementType.INSERT); Object[] parameters = new Object[]{"Condition Value"}; new SqlMapQuery(queryMethod, delegate, template).execute(parameters); verify(template).insert(namedQueryName, parameters[0]); }
@Ignore @Test(expected=IllegalArgumentException.class) public void 하나_이상의_파라메터로_쿼리_실행하면_예외발생() { when(mappedStatement.getStatementType()).thenReturn(StatementType.INSERT); Object[] parameters = new Object[]{"Condition Value", "Condition Value"}; new SqlMapQuery(queryMethod, delegate, template).execute(parameters); }