Skip to content

Instantly share code, notes, and snippets.

Map<String, Object> model = MockMvcBuilders.standaloneSetup(controller).build()
.perform(request)
.andExpect(status().isOk())
.andReturn().getModelAndView().getModel();
assertFalse(((BeanPropertyBindingResult)model.get(ERROR_KEY)).hasErrors());
@bmchild
bmchild / AllFieldsOrNone .java
Created September 26, 2012 19:42
AllFieldsOrNone SpringMVC 3 JSR-303 Validator
/**
*
*/
package com.bmchild.validation.constraints;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
@bmchild
bmchild / MyClassUnderTestTest.java
Created September 28, 2012 14:52
Example of using ExpectedException
// imports etc...
public class MyTest {
private MyClassUnderTest myClassUnderTest = new MyClassUnderTest();
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
@bmchild
bmchild / tryCatch.jsp
Created December 3, 2012 15:37
JSTL Exeption try - catch
<c:catch var="exception">
<jsp:include page="/WEB-INF/jsp/doesntexist.jsp" />
</c:catch>
<c:if test="${ exception != null }">
included page not found: ${exception.message}
<br />
Here is the stacktrace: ${exception.stackTrace}
</c:if>
@bmchild
bmchild / loopMap.jsp
Created December 3, 2012 15:49
JSTL loop over a map
<c:forEach var="entry" items="${theMap}">
Key: <c:out value="${entry.key}"/>
Value: <c:out value="${entry.value}"/>
</c:forEach>
@bmchild
bmchild / .gitignore
Created December 4, 2012 20:07
GitIgnore for a Java Eclipse project
# Directories #
/build/
/bin/
target/
# OS Files #
.DS_Store
*.class
@bmchild
bmchild / AttributeHasFieldErrors.java
Created December 6, 2012 14:27
Example using attributeHasFieldErrors and attributeHasNoError
// Test Failed Validation - missing ssn param
DefaultRequestBuilder requestBad = post(url)
.param("isEmployee", "true");
MockMvcBuilders.standaloneSetup(controller).build()
.perform(requestBad)
.andExpect(status().isOk())
.andExpect(model().attributeHasFieldErrors("lookupBean", "ssn"));
@bmchild
bmchild / CalcPayments.java
Created December 18, 2012 19:26
Calculate Loan Payments And APR
@Override
public BigDecimal calculatePaymentAmount(BigDecimal orignalValue,
BigDecimal ratePerPeriod, Integer numberOfPeriods) {
BigDecimal pv = orignalValue.setScale(CURRENCY_SCALE, BigDecimal.ROUND_HALF_UP);
BigDecimal r = ratePerPeriod.setScale(PRECISE_SCALE, BigDecimal.ROUND_HALF_UP);
BigDecimal denom = BigDecimal.ONE.subtract( BigDecimal.ONE.add(r).pow(-numberOfPeriods, MathContext.DECIMAL64) );
BigDecimal num = r.multiply(pv);
return num.divide(denom, CURRENCY_SCALE, BigDecimal.ROUND_HALF_UP);
}
@bmchild
bmchild / CalcAPR.java
Created December 18, 2012 19:42
Calculate APR
@Override
public BigDecimal calculateApr(BigDecimal financeCharge, BigDecimal totalPrincipal, int numberOfPayments,
int periodsPerYear, int precision, int roundingMode) {
BigDecimal numOfPayments = new BigDecimal(numberOfPayments).setScale(precision);
BigDecimal periodsInYear = new BigDecimal(periodsPerYear).setScale(precision);
BigDecimal years = numOfPayments.divide(periodsInYear, roundingMode);
BigDecimal avg = totalPrincipal.setScale(precision, roundingMode).divide(numOfPayments, roundingMode);
-- Gets the table's info including columns, primary key, foreign keys, and more
exec sp_help 'MY_TABLE';