Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
jeffsheets / SpringAsyncListenableFutureCallbackSpec.groovy
Created September 4, 2015 17:19
Spring Async ListenableFutureCallback Spock test to validate that onFailure is handling exceptions raised in Async method
package com.sheetsj.spring.async.test
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.Async
import org.springframework.scheduling.annotation.AsyncResult
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.test.context.ContextConfiguration
import org.springframework.util.concurrent.ListenableFuture
import org.springframework.util.concurrent.ListenableFutureCallback
@jeffsheets
jeffsheets / AbstractHandlerMethodMappingTestOverride.java
Created March 24, 2016 19:49
Spring MVC unit (minimal integration) test that uses Spring config for the controller setup
package org.springframework.web.servlet.handler
import org.springframework.web.context.WebApplicationContext
/**
* Workaround to register a controller with the WebAppContext because detectHandlerMethods is protected
*
* This allows our Mvc Unit tests to use the wired up Spring Jackson converters and mappers,
* without having to specify them individually in every test
*
@jeffsheets
jeffsheets / HibernatePersistenceProviderResolver.java
Created May 20, 2016 19:51
Use JPA 2.1 and Hibernate 4.3.11 on Websphere 8.5.5.x
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;
import java.util.Collections;
import java.util.List;
@jeffsheets
jeffsheets / WorstFizzBuzzEver.java
Created July 14, 2016 19:35
Worst FizzBuzz Ever in Java
import java.util.Arrays;
public class WorstFizzBuzzEver {
public static final String BUZZ = "Fizz";
public static final String FIZZ = "Buzz";
/*
* This does Fizz Buzz
*/
@jeffsheets
jeffsheets / BinarySearch.groovy
Created August 8, 2016 13:53
BinarySearch, QuickSort, DepthFirstSearch, BreadthFirstSearch in groovy
class BinarySearch {
def bfs(def node, def result=[]) {
Queue queue = new LinkedList()
queue.add(node)
while (queue) {
def it = queue.poll()
result << it.val
@jeffsheets
jeffsheets / TreeTraversal.js
Created August 15, 2016 21:38
Javascript binary search, quick sort, depth first search, and breadth first search
//See full source with HTML at jsfiddle:
// https://jsfiddle.net/jeffsheets/ry2ns0s7/4/
function node(val, left, right) {
return {val: val, left: left, right: right, visited: false};
}
/*
@jeffsheets
jeffsheets / EMUserRepository.groovy
Last active March 23, 2017 17:53
UserRepository showing export false at type level, but export true at method level
//Filed Spring Data Rest bug
//https://jira.spring.io/browse/DATAREST-1034
//so check there for answers
@RepositoryRestResource(path='emUsers', exported = false)
interface EMUserRepository extends EnversRevisionRepository<EMUser, Long, Integer>, JpaSpecificationExecutor<EMUser> {
//Attempting, as an example, to expose only this /search/findByAuth0UserId endpoint
//but with @RepositoryRestResource(exported=false) this endpoint is not exported
//but with @RepositoryRestResource(exported=true) this endpoint is exported
@RestResource(exported = true)
@jeffsheets
jeffsheets / EmbeddedMariaDbConfig.groovy
Last active April 26, 2021 12:31
Embedded MariaDB4j Spring Boot Configuration
import ch.vorburger.mariadb4j.DBConfigurationBuilder
import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import javax.sql.DataSource
@jeffsheets
jeffsheets / ApiSecurityConfig.groovy
Created September 6, 2017 16:33
Spock Test for Spring Boot Security configuration - showing basic simple examples for unauthenticated users, role based access, and httpBasic logins
@EnableWebSecurity
class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
@Inject
void configureGlobal(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication()
.withUser('svc_acct').password('somePassword').roles('FULL_ACCESS')
}
@Override
protected void configure(HttpSecurity http) {
@jeffsheets
jeffsheets / GwtSpockWidgetSpec.groovy
Last active April 30, 2018 21:27
GWT Spock test with GwtMockito for Google Web Toolkit client testing
package com.sheetsj.myapp.client.widget.fancy
import com.google.gwt.event.dom.client.KeyUpEvent
import com.google.gwt.user.client.ui.Widget
import com.google.gwtmockito.GwtMockito
import com.sheetsj.myproj.client.AppController
import com.sheetsj.myproj.client.widget.ErrorWidget
import org.mockito.Mockito
import spock.lang.Specification