Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
jeffsheets / AccountControllerTest.groovy
Created June 20, 2014 20:18
Spock test with Mocks of Spring MVC Rest Controller using standaloneSetup and mockMvc
import groovy.json.JsonSlurper
import org.springframework.test.web.servlet.MockMvc
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.http.HttpStatus.*
import spock.lang.Specification
/**
* A Spock Spring MVC Rest unit test that doesn't require a full spring context
*/
@jeffsheets
jeffsheets / SqlServerTimestampType.java
Created August 1, 2014 17:38
Hibernate Custom Type to use @Version optimistic locking for SQLServer datetime fields that are limited by 1/300th of a second precision
public class SqlServerTimestampType extends TimestampType {
private static final int SQLSERVER_PRECISION = 10;
public SqlServerTimestampType() {
super();
}
/**
* SQLServer datetime fields are accurate to 1/300th of a second.
* We floor to the nearest 1/100th of a second for simplicity.
@jeffsheets
jeffsheets / SpringPropertiesConfig.java
Created August 14, 2014 21:21
Spring 4 Properties Java Configuration with Database-backed Properties along with File properties too
/**
* Example of Spring 4 Properties Java Configuration,
* with a Database Properties table to store most values
* and a small application.properties file too.
* The Database table will take precedence over the properties file with this setup
*/
@Configuration
@PropertySource(value = { "classpath:application.properties" }, ignoreResourceNotFound=true)
public class SpringPropertiesConfig {
private static final Logger log = LoggerFactory.getLogger(SpringPropertiesConfig.class);
@jeffsheets
jeffsheets / ProfilePropertiesInitializer.java
Last active August 29, 2015 14:07
Spring Profile specific properties files, similar to Spring Boot (or Grails) properties. This registers application-*.properties for all Active (or Default) spring profiles.
/**
* Register this with the DispatcherServlet in a ServletInitializer class like:
* dispatcherServlet.setContextInitializers(new PropertiesInitializer());
*/
public class PropertiesInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final Logger log = LoggerFactory.getLogger(PropertiesInitializer.class);
/**
* Runs as appInitializer so properties are wired before spring beans
*/
@jeffsheets
jeffsheets / RestResponseEntityExceptionHandler.java
Last active October 31, 2020 11:44
Handle REST Exceptions in Spring
/**
* REST exception handlers defined at a global level for the application
*/
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(RestResponseEntityExceptionHandler.class);
/**
* Catch all for any other exceptions...
*/
@jeffsheets
jeffsheets / SpringLog4jConfig.java
Created April 2, 2015 20:17
Super Simple Spring Log4j Configuration using different files per environment
@Configuration
public class SpringLog4jConfig {
/**
* Just a property from the normal Spring property sources, like:
* log4j.properties.location=log4j-dev.properties
*/
@Value("${log4j.properties.location}")
String log4jLocation;
@jeffsheets
jeffsheets / protractorFieldSelectionTest.spec.js
Created April 8, 2015 18:13
Protractor spec showing how to test for text selection inside a form input field
/**
* Initially written as a test for IE/Chrome for a bug in ui-mask.
* https://github.com/angular-ui/ui-utils/issues/302
*
* The test verifies that all of the text is selected in an input field when the field is tabbed into
*/
describe('field selection test', function () {
beforeEach (function () {
getRoute('#/app/events');
});
@jeffsheets
jeffsheets / CrazyDoodlePrivacyPolicy.txt
Created April 19, 2015 19:48
Crazy Doodle Privacy Policy
The Crazy Doodle app available on the Amazon Kindle and Google Play stores does NOT collect any information.
@jeffsheets
jeffsheets / ContentTypeMappingJackson2HttpMessageConverter.java
Created August 5, 2015 18:02
A MappingJackson2HttpMessageConverter that forces the response Content-Type header to be application/json
public class ContentTypeMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
/**
* Always set the Content-Type response header to application/json when using the Jackson message converter
*
* Without this there appears to be a conflict with Meteor/Atmosphere servlet that causes Spring
* to leave the Content-Type blank in the response
* https://groups.google.com/forum/#!topic/atmosphere-framework/uZOrfXl3Bu8
*/
@Override
@jeffsheets
jeffsheets / sinonKarma.spec.js
Created September 1, 2015 15:17
Simple Karma spec using Sinon to mock a backend service for AngularJS testing
var someService;
beforeEach(inject(function (_someService_) {
someService = sinon.mock(_someService_);
}));
afterEach(function () {
entityServiceFactory.verify();
});