Skip to content

Instantly share code, notes, and snippets.

View jbrains's full-sized avatar

J. B. Rainsberger jbrains

View GitHub Profile
@jbrains
jbrains / Any Name With Spaces.sh
Created October 13, 2013 17:01
Creating a gist with a filename containing whitespace, evidently for testing.
#!/bin/bash
true
#!/bin/bash
download_yuml_image() {
local file="$1"
local output_directory="$2"
instructions=$(tr "\\n" "," < <(cat $file))
image_basename=$(basename $(echo $file | sed -E "s/(.+)\.yuml$/\1/"))
image_filename="${output_directory}/${image_basename}.png"
@jbrains
jbrains / ExecuteSomething.java
Created November 19, 2012 17:18
An example of a Template Method that handles "rollback on exception" in Java
new RollbackOnExceptionAction(new JtaTransactionService())
.executeInTransaction(new Closure() {
@Override
public void execute(Object... arguments) {
// cast arguments
// perform action
// throw exception if there's a problem
}
});
@jbrains
jbrains / TestingIoFailure.java
Created November 19, 2012 16:33
A new twist on an old pattern for checking for exceptions
@Test
public void ioFailure() throws Exception {
final IOException ioFailure = new IOException("Simulating a failure writing to the file.");
try {
new WriteTextToFileActionImpl() {
@Override
protected FileWriter fileWriterOn(File path) throws IOException {
return new FileWriter(path) {
@Override
public void write(String str, int off, int len) throws IOException {
it "should render a partial for all the items" do
assign(:parent, parent_with_many_items)
# Ewww! Stubs aren't mocks!
stub_template "_item.html.erb" => %Q{<span class="item"><%= item.nil? %></span>}
render
rendered.all("span.item").map(&:text).should == ["false", "false", "false"]
end
@jbrains
jbrains / gist:1259077
Created October 3, 2011 13:14 — forked from philipschwarz/gist:1241175
Separating use from construction - Before
public class BusinessObject {
/** @deprecated */
public void actionMethod() {
actionMethod(Service.getInstance());
}
// A SEAM! A SEAM! New clients should use me!!
public void actionMethod(ServiceDelegate serviceDelegate) {
// Other things
serviceDelegate.doService();
@jbrains
jbrains / ListContract.java
Created July 15, 2011 11:45
An alternative contract test style in Java
package ca.jbrains.junit;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
class UserGestureListener
def on_barcode_scanned(barcode)
product = @catalog.find(barcode)
if product.nil?
@display.display_message(ProductNotFoundMessage.with(barcode))
else
@display.display_product(product)
accumulate_cost(product)
end
end
@jbrains
jbrains / Main.java
Created May 2, 2011 05:33
Design after introducing Auction Events.
package ca.jbrains.auction.test;
public class Main {
public interface AuctionEventListener {
void handleNewBiddingState(BiddingState biddingState);
void handleGenericEvent(Object object);
}
public static class AuctionEventSourceMessageListener implements
@jbrains
jbrains / Main.java
Created May 2, 2011 04:42
After pulling up processMessage().
package ca.jbrains.auction.test;
public class Main {
public static abstract class FooMessageListener implements MessageListener {
@Override
public void processMessage(Chat chat, Message message) {
final Object event = Messages.parse(message);
if (event instanceof BiddingState) {
BiddingState biddingState = (BiddingState) event;
handleBiddingStateEvent(chat, biddingState);