RSSAmplifier

Blog

Blake Caldwell: /dev/blog

blakecaldwell.netRSS feed ↗18 posts

Latest posts

Just for Fun: Hash Table Implementation in C

Hash tables are cool. Like many developers, I used them for years without giving much thought to how they actually work. Just for fun, I decided to look into it - and while I was at it, brush up on C , which I hadn’t spent much time with since my first internship years ago. TLDR: Take a look at the code on GitHub Example: Hash Tables in Go Hash tables (also known as “hash maps”, or “maps”) are so…

JSON Encoding in Go: Dealing with Sensitive Fields

JSON marshalling in Go is pretty straight-forward , but sometimes your structs contain sensitive information that you’d like to keep out of your public JSON API. Fortunately, there are a few good solutions that don’t require you to manually copy every field from one struct type to another. Let’s start with our basic Person struct: // Person struct that includes all fields in JSON type Person…

Solving a 2014 Google I/O Secret Invite Puzzle

I didn’t win the Google I/O ticket lottery this year, but thought I’d try to find and solve one of the secret puzzles Google was hiding, to get a chance to buy a reserved ticket. I noticed that the Google Developers page header contained the text “I/O”, with different colors for each of the letters. Refreshing the page changed their colors, and occasionally removed them from the page. Looking at…

Java: Performance Testing Hibernate Query Approaches

I’ve known entities are expensive , but wanted to see for myself, so I built this test project to run some benchmarks. The tests aren’t complete - just a simple query with no joins, but with a lot of data. The point was to see how much overhead is introduced after we have the query results. About the Project This is a simple Maven Spring project that creates an in-memory HSQLDB database, populates…

Java: Exercising Caution With Hibernate Entities

I heavily rely on the Hibernate framework on all of my database-driven Java projects. It’s a full-featured cross-database ORM framework capable of managing your database schema , persisting your entities to the database, populating entities from the database, caching queries, caching entities , registering entities for lifecycle events , indexing them in a search index , and just about everything…

Asserting Exceptions With JUnit Rules: IsEqual Matcher

In my first post on asserting exceptions with JUint , I showed how to test that a specific type of exception is thrown with expected substrings in the error message. In my second post , I showed how we can write a custom [Matcher] to inspect the contents of the exception. In this post, I’ll show you how to take advantage of the stock IsEqual matcher to accomplish the same task, but with less work.…

Asserting Exceptions With JUnit Rules: Custom Matchers

In my previous post , I demonstrated how to use JUnit’s Rules feature to assert expected assertions in your unit tests. In this post, I’ll show you how to write custom Matchers that will help give you more power when inspecting your exceptions. Maven Dependencies This demo uses the following Maven dependencies: <dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 4.11…

Asserting Exception Messages With JUnit Rules

If you’re not familiar with JUnit’s @Rule feature for asserting exceptions in your tests, then read on - you’re about to start using it. Assert Exception Type It’s very simple to assert that a given type of exception is thrown in a JUnit test case with the following: public void testExceptionTypeIsThrown () { throw new RuntimeException ( "Error!" ); } Assert Exception Message (The Old Way) But,…

Conditionally Run JUnit Integration Tests with Spring

Ideally, your unit test suites require no external dependencies - those should be mocked out. However, sometimes you want extra assurance that your code works with live endpoints via integration tests. For example, you might want to make sure that your code can successfully navigate your corporate proxy and firewall to make external web requests. For this, we’ll write tests that only run when a…

Object/Relational Mapping: Know Your Frameworks

I’ve been working with Hibernate for several years now, yet I learn something new about it all the time. The more time I spend with the framework, the more concerned I am about how it will be used by developers new to it. Mirko Novakovic Alois Reitbauer nails it in a post about O/R Mapping Anti-Patterns : The simplicity of the entrance into the world of O/R mapping however gives a wrong impression…

Java: Don't Rely on EntityManager.persist() for Immediate Insert

I had always counted on EntityManager’s persist() method to immediately insert entities. I would rely on this when writing database integration tests - I’d persist some records, then test my DAO methods to find them. On my current project, I decided to add a configuration option to allow me to run my datbase integration tests on my development Oracle database rather than my embedded HSQLDB test…

Builder Pattern Instead of Error-Prone Constructors

When designing your classes, rather than following the path of the unreadable telescoping constructor , or leaving yourself open for bugs where the caller incorrectly passes a value into the wrong parameter because you have several of the same type, consider the following Builder pattern . public class Person { // required private String firstName ; // required private String lastName ; //…

Apache Camel: Powerful EIP Framework

Pradeep Elankumaran gives a high-level overview of Apache Camel From Apache Camel’s project page : Apache Camel ™ is a versatile open-source integration framework based on known Enterprise Integration Patterns. Camel empowers you to define routing and mediation rules in a variety of domain-specific languages, including a Java-based Fluent API, Spring or Blueprint XML Configuration files, and a…

PDF Emailer v2.1: Multiple Recipient Support

PDF Emailer is a iOS utility app that lets you email PDF and Office documents from Safari using a customizable email template. In version 2.1, you can now set multiple recipients in your template by separating them with spaces. PDF Emailer v2.1 is (no longer) available for iPhone, iPad, and iPod Touch

Java: Copy/Paste-Safe Logger Creation

We’ve all copied and pasted a Logger from one class to the other, forgetting to change the class name. // WRONG public class SomeNewClass { // OOPS! I forgot to change the class name! private final Logger _logger = LoggerFactory . getLogger ( SomeOtherClass . class ); // … } Here, use this smarter LoggerFactory which uses the stack trace to figure out what class you really mean. This is safe from…

Java: Embedding Spring Batch Admin Into An Existing Application

For my current project, I need to share Spring beans between my front-end web servlets and the Spring Batch jobs that I’ll launch from Spring Batch Admin (SBA). This sounds straightforward - configure the SBA servlet to listen on /batch/*, and you should be good to go. However, of course it’s not that easy. URL Path Mismatch The first problem you run into is bad links in the menus, form posts that…

Java: Maven Environment Variables on OS X

Mac OS X comes with Apache Maven preinstalled. However, you’re still going to need to set some environment variables to get up and running. Verify that maven installed in /usr/share/maven: ls /usr/share/maven Add the following line to your ~/.profile # Set the M2_HOME environment variable export M2_HOME = /usr/share/maven # Put maven on your path export PATH = $M2_HOME /bin: $PATH This won’t take…

Solving a Mobile PDF Problem

You find an important PDF document. It’s not unusual to stumble upon a PDF document while web surfing on your iPad, iPhone, or iPod Touch. You might be logged into your bank’s site to view a monthly statement, or have finally found that important tax document that took you 10 minutes of poking and prodding a confusing online submission form. You can’t email it to yourself. You don’t have time to…