Skip to content

Instantly share code, notes, and snippets.

@hertzsprung
hertzsprung / Queue.java
Created June 22, 2012 22:49
Default interface method
interface Queue {
Message read();
void delete(Message message);
void deleteAll() default {
Message message;
while ((message = read()) != null) {
delete(message);
}
}
}
@hertzsprung
hertzsprung / BatchQueue.java
Created June 22, 2012 22:50
Default method override
interface BatchQueue extends Queue {
void setBatchSize(int batchSize);
void deleteAll() default {
setBatchSize(100);
Queue.super.deleteAll();
}
}
@hertzsprung
hertzsprung / FastQueue.java
Created June 22, 2012 22:52
Removing a default method
interface FastQueue extends Queue {
void deleteAll();
}
@hertzsprung
hertzsprung / BlogAggregatorUnitTest.java
Created August 1, 2012 21:02
Unit testing with JMock
public class BlogAggregatorUnitTest {
@Rule public final JUnitRuleMockery context = new JUnitRuleMockery();
private final BlogResource alicesBlogResource = context.mock(BlogResource.class, "Alice's Blog");
private final BlogResource bobsBlogResource = context.mock(BlogResource.class, "Bob's Blog");
@Test public void providesFeedContainingMostRecentArticleFromEachBlog() {
context.checking(new Expectations() {{
allowing(alicesBlogResource).get(); will(returnValue(alicesBlog));
allowing(bobsBlogResource).get(); will(returnValue(bobsBlog));
}});
@hertzsprung
hertzsprung / BlogResource.java
Created August 2, 2012 21:19
JAX-RS blog resource
@Path("/")
public interface BlogResource {
@GET @Produces(APPLICATION_JSON) Blog get();
}
@hertzsprung
hertzsprung / BlogAggregatorIntegrationTest.java
Created August 2, 2012 21:35
Integration testing with JMock and Jersey
public class BlogAggregatorIntegrationTest {
@Rule public JUnitRuleMockery context = new JUnitRuleMockery() {{
setThreadingPolicy(new Synchroniser());
}};
private final BlogResource alicesBlogResource = context.mock(BlogResource.class, "Alice's Blog");
private final BlogResource bobsBlogResource = context.mock(BlogResource.class, "Bob's Blog");
private final JerseyTest alicesServer = aJerseyTest().withPort(9998)
.addResource(alicesBlogResource).build();
@hertzsprung
hertzsprung / gist:3722361
Created September 14, 2012 14:48
Mocking delegates
enum FilterType { ... }
interface Filter {
Response filter(Request request);
}
class RoutingFilter {
private final Map<FilterType, Filter> routes;
public RoutingFilter(Map<FilterType, Filter> routes) {
@hertzsprung
hertzsprung / gist:3766653
Created September 22, 2012 16:21
Norwegian Wood
G 3 --- 2&6 3 --- | ---------------- | 5 (*) ---------- | ---------------- |
D ----------- 2++ | 2&6 -- 3 2++ 2&6 | ---------- 2++ - | 5 -------------- |
A --------------- | ---------------- | ------ 1&5 --- 3 | ---------------- |
E --------------- | ---------------- | ---------------- | ---------------- |
All notes harmonics
2++: harmonic ~2/3rds between 2nd and 3rd fret
x&y: hold fret x, harmonic on fret y
(*) alternatively, 2++ on A string
@hertzsprung
hertzsprung / MyCollaboratorConfig.java
Created October 22, 2012 20:39
Integration testing with Spring test and JUnit
@Configuration public class MyCollaboratorConfig {
@Bean public MyCollaborator myCollaborator() {
return new MyRealCollaborator();
}
}
@hertzsprung
hertzsprung / MyServiceConfig.java
Created October 22, 2012 22:10
Wiring beans using Configuration classes
@Configuration
@Import(MyCollaboratorConfig.class)
public class MyServiceConfig {
@Autowired private MyCollaboratorConfig collaboratorConfig;
@Bean public MyService myService() {
return new MyRealService(collaboratorConfig.myCollaborator());
}
}