Skip to content

Instantly share code, notes, and snippets.

@bcalmac
bcalmac / JMockitExceptionTest.java
Created June 3, 2015 16:28
Experiment with recording expectations that throw exceptions
import java.sql.Connection;
import java.sql.SQLException;
import mockit.Expectations;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@bcalmac
bcalmac / TestApplicationConfiguration.java
Last active August 29, 2015 14:26
Spring Boot Test configuration that excludes CommandLineRunners
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
@EnableAutoConfiguration
public class TestApplicationConfiguration {
@bcalmac
bcalmac / Numbers.java
Created February 20, 2016 16:10
Round a double
/** Rounds a double value to a specified number of decimal places. */
public static double round(double value, int scale) {
return new BigDecimal(value).setScale(scale, RoundingMode.HALF_UP).doubleValue();
}
@bcalmac
bcalmac / MemoryMappedFileTest.java
Last active April 28, 2016 17:38
Write and then read back a memory mapped file
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.assertEquals;
@bcalmac
bcalmac / SwingDrawingExample.java
Last active May 16, 2016 13:51
Minimal Swing application that draws a line
package util.graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
@bcalmac
bcalmac / StackOverflowSearchExample.java
Last active July 11, 2017 22:06
Minimal Selenium example that performs a search on stackoverflow.com
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class StackOverflowSearchExample {
@Test
@bcalmac
bcalmac / DuplicateLogging.java
Last active July 28, 2017 05:40
What happens if stdout is redirected to the same file as a FileAppender?
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DuplicateLogging {
private static final Logger logger = LoggerFactory.getLogger(DuplicateLogging.class);
public static void main(String[] args) throws IOException, InterruptedException {
@bcalmac
bcalmac / WireMockTransformerExample.java
Created August 2, 2017 21:47
Simple WireMock transformer that converts the content to upper case
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.junit.Assert.assertEquals;
import wiremock.org.apache.http.client.utils.URIBuilder;
import java.net.URI;
import java.net.URISyntaxException;
@bcalmac
bcalmac / IntervalMixIn.java
Last active August 18, 2021 21:28
Jackson serialization for Interval
import java.time.Instant;
import org.threeten.extra.Interval;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
// Exclude JavaBean properties that are not part of the state
@bcalmac
bcalmac / RemaindersOfPowers.hs
Created December 14, 2019 20:25
Determine the list of remainders of powers of a divided by b
-- a^0 mod b is 1. Once we find the next power with a remainder of 1, the remainders will repeat.
-- Take from a list until the first element repeats
takeUntilFirstRepeats (x:xs) = x : takeWhile (/= x) xs
remaindersOfPowers a b = takeUntilFirstRepeats [a^i `mod` b | i <- [0..]]
-- Example
remaindersOfPowers 9 7