dwijnand::blog

Just my blog…

How to mock value classes

Posted by dwijnand on 2014/09/19
Posted in: Uncategorized. Tagged: coding, development, mocking, scala, testing. Leave a comment

I had an issue a couple of weeks ago while migrating some code from using bare Map[String, String] to a value class surrounding that map (HttpHeaders), and ran into issues with Mockito (even with the Specs2 helpers).

Long story short I was able to fix it with the following changes:

-any[HttpHeaders]
+Matchers.any(classOf[Map[_, _]]).asInstanceOf[HttpHeaders])
-when(request.headers)
-  .thenReturn(HttpHeaders.empty)
+when(request.headers.underlying)
+  .thenReturn(Map.empty[HttpHeader[_], Vector[String]])
Advertisement

How to sort in descending order on a field mapped to multiple database columns

Posted by dwijnand on 2014/05/20
Posted in: Uncategorized. Tagged: coding, database, hibernate, java, joda-time, rdbms, sql. Leave a comment

Last August (2013) I ran into an issue where I wanted to get the latest 10 created entities from a database table but I was getting the first 10 instead. The relevant components of the stack involved were PostgreSQL 9.3, with Hibernate 4.2 used as the EntityManager provider for Spring Data JPA 1.4. Turns out that the cause is an old, obscure (but known) issue in Hibernate: HHH-5574. Continue Reading

My perfect blog: problem and solution

Posted by dwijnand on 2012/05/09
Posted in: Uncategorized. Tagged: coding, dvcs, git, ocd, python, vcs. Leave a comment

I like blogging from time to time, but I wish I could do things the way I like. One issue I have with having my blog here on wordpress is that I don’t have a local copy of it on my machine & it isn’t versioned in a repository (which has basically become my obsession). I wanted to find a solution that gave me these two things, and the flexibility to do anything else (such as in what markup to write the posts). Continue Reading

How I do (Java) development on Mac OS X

Posted by dwijnand on 2012/05/08
Posted in: Uncategorized. Tagged: coding, development, java, ocd, programming. 1 Comment

I just want to share how I set up to do development on my Mac OS X machine, specifically, in my case, Java development, especially my use of a local Homebrew root. Continue Reading

MySQL (v5.5) setup

Posted by dwijnand on 2012/04/26
Posted in: Uncategorized. Leave a comment

This is how I setup a new MySQL database for a project (using a local HomeBrew root called sw):

unset TMPDIR
sw/bin/mysql_install_db --verbose --user=dnw \
  --basedir=sw/Cellar/mysql/5.5.20 --datadir=sw/var/mysql --tmpdir=/tmp
sw/bin/mysql.server start
create user [username]@localhost identified by '[password]';
create user [username] identified by '[password]';
grant all on *.* to [username]@localhost;
grant all on *.* to [username];

Creating a PostgreSQL database

Posted by dwijnand on 2012/04/17
Posted in: Uncategorized. Leave a comment

Postgres is my favorite RDBMS, but I always forget how to setup a database. So this is mostly to remind myself 🙂

createuser --echo --no-createdb --encrypted --pwprompt --no-createrole \
  --no-superuser [ROLENAME]
createdb --echo --owner=[ROLENAME] [DBNAME]

Which echoes something like:

CREATE ROLE [ROLENAME] ENCRYPTED PASSWORD '[encrypted-password]' \
  NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;
CREATE DATABASE [DBNAME] OWNER [ROLENAME];

To connect use:

psql [DBNAME] [USERNAME]

That’s all 😀

Seperate Repositories: Acceptance Testing and Database Migrations

Posted by dwijnand on 2011/05/04
Posted in: Uncategorized. Tagged: programming, testing, vcs. Leave a comment

I have an issue in both the projects that I’m currently involved in and it has to do with debugging issues in previous versions of the application. In essence I cannot run an older version the application as my current database schema is newer than the model objects that map to it. Essentially I need a way to rollback a few database migration scripts so that I can run the application, or alternatively recreate a fresh database. This later solution should always be available, but as of now neither of them can be done. If the version I’m trying to run is the current production’s version, fortunately I can use a SQL dump of the production database to fix my error and allow the application to run. Continue Reading

Naming OCD

Posted by dwijnand on 2011/03/18
Posted in: Uncategorized. Tagged: conventions, ocd, programming. Leave a comment

I have serious OCD about naming. Sometimes I just sit and think (or talk out loud..) about what a good/better name for whatever is.

And I really mean whatever- package names, class names, method names, variable names, project names, component names, build tasks.. Along with naming OCD, I also have convention OCD such as: singular or plural names (for database tables, packages), camelCase vs snake_case, space vs tab, 2 vs 4 vs 8 space indentation.

However, I’ve got a few preferences, only they sometimes are in conflict and I haven’t decided on how much weight each has (need to figure it out still). They are:

  • when trying to keep things ordered, put them in alphabetic order (ignores, properties, imports, fields, setters/getters)
  • when trying to find a name, given two real synonyms, use the shorter of the two (but it has to convey the same meaning)
  • avoid abbreviations and acronyms (this is in conflict with the previous preference..)
  • name it using English

For that last reason I’ll give an example. I have a project that uses assignments. Without going into detail, I sometimes deal with lists of assignments, other times maps of assignments. I, therefore, have started using the convention of writing variables such as assignmentsMap instead of assignmentMap, because the variable represents a map, a map of what? of assignments. Not of ‘assignment’. I think it’s a good convention to use.

Problems programming to an interface with java.util collections

Posted by dwijnand on 2011/03/01
Posted in: Uncategorized. Tagged: java, programming. Leave a comment

I try to program by interface. It gives the code more flexibility. More details.

I have an issue however with the Java collections classes, which are always used. Checkstyle by default sets all implementing classes of the collections as illegal types. However, I don’t agree with it.

For instance: Set.remove(Object) makes no promises, it says it could simply throw a UnsupportedOperationException if the method isn’t supported. This isn’t usually a problem as most of the time the implementation is HashSet or possibly LinkedHashSet. Sometimes, however, I use ImmutableSet, which throws a UOE on remove. What I would like to do is return something which is a Set but which can be modified with remove.

Scala collections I think have a much nicer API.

For instance, Scala’s Set defines that the – method “Creates a new set with a given element removed from this set”, which is also the functionality of the mutable version (scala.collection.mutable.Set). The mutable version, however, also adds a -= method to modify the set in-place, something that only makes sense for mutable collections. But mutable.Set doesn’t define the implementation used, so it can be used as the type of any parameter or field that you declare to be a Set (no duplicate elements) and modifiable (-=, +=, etc).

If only, if only. If only Java’s collections had interfaces for mutable collections without implementation, that way I could return a MutableSet, leaving the user able to remove object (without having to create a new Set with the contents of the original Set), without having to specify the implementation..

Commands to run after ‘rails new’

Posted by dwijnand on 2011/02/24
Posted in: Uncategorized. Tagged: dvcs, mercurial, rubyonrails, shell, vcs. Leave a comment
This is for Ruby on Rails and Mercurial users, such as myself 🙂
  • cp .gitignore .hgignore
  • .hgignore: prepend syntax:glob: sed -i ‘1i syntax: glob\n’ .hgignore
  • .hgignore: sort: sed -i ‘1,2d’ .hgignore && LC_ALL=C sort .hgignore && sed -i ‘1i syntax: glob\n’ .hgignore
  • echo “Gemfile.lock” >> .hgignore
  • (echo ‘*.swp’ >> .hgignore; echo ‘.idea/’ >> .hgignore)
  • find . -name ‘.git*’ -delete
  • find . -type d -empty -exec touch {}/.keep \;
  • hg init
  • hg ci -Am ‘Generate project, setup .hgignore & remove .git*’

(Edit: Split add header and sort, otherwise it trunks the first 3 lines of .hgignore prematurely)

(Edit: Simplify the two modification 1-liners using sed)

Posts navigation

← Older Entries
  • Twitter Updates

    • Going to try a tweak to the signature polymorphism implementation on stream, with @SethTisue. Live now twitch.tv/dwijnand 3 months ago
    • Streaming with @SethTisue on twitch.tv/dwijnand. Hacking on the Scala 3 compiler, looking at a Text/printing… twitter.com/i/web/status/1… 3 months ago
    • Going to continue the PR on "Support polymorphic signatures" in an hour, on twitch.tv/dwijnand with @SethTisue and Guillaume. 3 months ago
    Follow @dwijnand
  • ci coding continuous-integration conventions database development dvcs git hibernate idea java joda-time lift mercurial meta mocking ocd programming python rdbms ruby rubyonrails scala shell sql testing vcs
  • Recent Posts

    • How to mock value classes
    • How to sort in descending order on a field mapped to multiple database columns
    • My perfect blog: problem and solution
    • How I do (Java) development on Mac OS X
    • MySQL (v5.5) setup
  • Archives

    • September 2014 (1)
    • May 2014 (1)
    • May 2012 (2)
    • April 2012 (2)
    • May 2011 (1)
    • March 2011 (2)
    • February 2011 (8)
    • RSS - Posts
    • RSS - Comments
Blog at WordPress.com.
dwijnand::blog
Create a free website or blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • dwijnand::blog
    • Already have a WordPress.com account? Log in now.
    • dwijnand::blog
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...