Skipping tests when doing “mvn install”

First things first: you should NOT be skipping tests when building your projects. 9 out of 10 cases, you shouldn’t. But when you must / have to / want to do it: after spiking what it looked like a quick solution to a little problem and it turned out to be a massive refactoring during which you broke so many tests it would not be worthy to fix them unless the original problem is really flushed away… possibly the most typical scenario where you would want to do something like this. Well, in that case, you can do:

mvn -DskipTests=true install

or the more lazy version:

mvn -DskipTests install

Or you can do:

mvn -Dmaven.skip.test=true install

or the more lazy version:

mvn -Dmaven.skip.test install

Difference between the first and the second is that the first one is less horrible. Let me explain… both versions will not run any of your tests classes, but at least the first one won’t succeed if they don’t compile. So, if you’ve broken your tests but at least you changed your source code well enough to maintain compilation errors away on your test classes, you can get away with the first command. But if you incurred in compilation errors on your test classes while changing your code, you may want to go for the second command, which not only it will ignore your tests but it won’t even attempt to compile the test classes.

If having to run the first command is already a sign that you’ve not been doing the important things (TDD, for instance) right, running the second means you can’t even use your IDE correctly (refactoring, anyone?).

[Learnt at work, from a colleague]

[Reference]

Eclipse template to insert test methods should-given-when-then

(originally posted on StackOverflow)
I saw a similar version to this one recently while pair programming with a very a good developer and friend, and I think it could be a nice addition to this list.

This template will create a new test method on a class, following the Given – When – Then approach from BDD paradigm on the comments, as a guide for structuring the code. It will start the method name with “should” and let you replace the rest of the dummy method name “CheckThisAndThat” with the best possible description of the test method responsibility. After filling the name, TAB will take you straight to the // Given section so you can start typing your preconditions.

I have it mapped to the three letters “tst”, with description “Test methods should-given-when-then” 😉

I hope you find it as useful as I did when I saw it:

@Test
public void should${CheckThisAndThat}() {
    Assert.fail("Not yet implemented");
    // Given
    ${cursor}

    // When

    // Then

}${:import(org.junit.Test, org.junit.Assert)}

Removing local SVN metadata

When you control the versions of your project codebase in SVN, one of the things that SVN does is keeping some files in the local working copy of the project. As explained in the wikipedia:

Subversion stores additional copies of data on the local machine, which can become an issue with very large projects or files, or if developers work on multiple branches simultaneously. These .svn directories on the client side can become corrupted by ill-advised user activity.

If, for some reason, you want to disconnect your local copy of your project from the SVN server version, in other words, you just want your project to be local and not be linked at all with a version control system, the best way to do it is through a graphic SVN client, preferably the one you used to connect it in the first place. Two graphic ways (Eclipse plugin and Tortoise SVN) of doing this and the command line one are described here:

1.- Eclipse plugin: Subversion, Subclipse. I am using Subclipse here. Just do right click on the name of the project, go to Team -> Disconnect. You will get a prompt like this:

Make sure you change it to:

Before hitting Yes.

2.- Tortoise SVN: imagine you have your project like this:

There is no direct tool through the Tortoise SVN client to do this, but your best shot is exporting your local working copy. This will recreate the project without any .svn directory or local SVN metadata. So we do right click on the project name, TortoiseSVN -> Export:

Then:

Click OK and there you go, project in dummyExported does NOT contain any .svn folder hence it’s not connected whatsover to any SVN repository from a local point of view. The original working copy is still there, so you can remove it completely and copy the exported one to the original location is this is necessary for you.

3.- Command line: as usual, there is a much quicker and cleaner way to do this with the command line (assuming you are in a Unix-like machine or you have at least a Unix-like console at your disposal, like Cygwin). It is explained quite well here, but in case you’re in a rush:

$ rm -rf `find . -type d -name .svn`

If you type that from the current directory of the project you want to disconnect, every single .svn directory on it and on its subdirectories will be erased, hence the local working copy disconnected from the SVN repository.

Using the Tomcat Maven Plugin to run your application

If you really want to be proficient using the Tomcat Maven Plugin you should go here and have a proper read of the plugin goals and usage. I take it for granted that your Maven knowledge is already enough to understand them. If it’s not, then close this tab and start here.

Ok, if you are still here, the quickest way to run your project into an embedded tomcat 7 instance is adding this into your pom’s plugin section:

<plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.0</version>
</plugin>

Now, just do…

mvn tomcat7:run

… in your preferred Maven installation (command-line, Eclipse plugin, Netbeans plugin…) and go to http://localhost:8080/${project.artifactId}/ to check that the project is running fine. Obviously you need replace ${project.artifactId} for your project’s Maven artifactId.

If you want to use a different value for the url, instead of the ${project.artifactId}, then you just have to add a new configuration section to the plugin:

<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <path>/mycontext</path>
        </configuration>
</plugin>

The plugin uses automatically the last version of Tomcat 7 out there. If, for whatever reason, you want to use a specific version that does not match this one, you can do it, but you’ll have to specify it in the dependencies of the plugin. A bit tedious but easy:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
	  <dependencies>
             <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-util</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-coyote</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jdbc</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-dbcp</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-servlet-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jsp-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jasper</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-jasper-el</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-el-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-catalina</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-tribes</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-catalina-ha</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	                <dependency>
	                  <groupId>org.apache.tomcat</groupId>
	                  <artifactId>tomcat-annotations-api</artifactId>
	                  <version>${tomcat7Version}</version>
	                </dependency>
	       </dependencies>
</plugin>

I took the trick to specify the Tomcat version from http://nurkiewicz.blogspot.co.uk/2012/11/standalone-web-application-with.html.

varchar or nvarchar

If you got here is because you had the same problem as me… you are probably trying to define the data type for a table column in your preferred database and you saw these two possibilities for variable length text columns. I will save you time, picking the best answer out there (IMHO) from Stack Overflow:

varchar: Variable-length, non-Unicode character data. The database collation determines which code page the data is stored using.

nvarchar: Variable-length Unicode character data. Dependent on the database collation for comparisons.

Armed with this knowledge, use whichever one matches your input data (ASCII v. Unicode).

There is plenty of information on the topic, but this (in my case) convinced me of using nvarchar.

Where to start with Lucene 4?

If you are new to Lucene, like me, and you are struggling to find documentation, or, better, you’re finding too much documentation… this is what I did to find out what this is about…

1.- Read this.
2.- Do this example. I have mavenized the project.
3.- Read this.
4.- Create your first project searching files from a local directory with the classic IndexFiles and SearchFiles classes from Lucene, as advised in the previous point reading.
5.- Create your first project searching text from a local database tables content. You may want to read this before.
6.- Address any possible performance issues.
7.- Put Luke in your Lucene-life. This tool will let you query in your indexes a very friendly manner, among other things.
8.- To present results almost real time in an impressive fashion, give this a read.

For a more technical FAQ.

As any other framework, though, as much as you work with it, more confident you will feel. I am just a beginner, so I suppose that this will be only useful for you if you’re starting too. Feel free to point out the next steps that I (or others to whom this post is useful) should be doing (resources, etc…) if you are an expert.

Thanks!

About the Core Spring certification

I attended the Core Spring course last year. It took me a while to gather the necessary time and will to try the exam. I passed it with a narrow margin (78%, passing score was 76%) but I still think my preparation could have been better.

My advice: try this right after the training. I know, this is not something you did not know. But for some reason most of the people I know have not followed this advice, including myself. The reason why you should do this is clear: the exam is heavily based in the specific information contained in the slides of the course. Again, nothing you were not aware of. So: do the course, do the labs, review the slides, understand everything on them, do some mock exams. These are actually harder than the real thing but somehow they will give you hints on how much you know on the material. Again, questions in these exams that are out of scope for the exam (not in the slides of the course) will arise. Don’t get discouraged by them, make your notes, grow your Spring knowledge but don’t lose your sleep. It’s more than likely that you won’t find them in the exam.

I based my study and preparation on Selikoff’s recommended study plan. I find it very accurate and a set of very good practices when it comes to confront this exam. I can’t thank her enough for her wise advices.

On a more operative level, make sure you get to the exam with time to find the place, a good sleep the night before and don’t forget two proofs of identify yourself (credit card is ok, at least in the centre where I took the exam).

Good luck!

About unicode and character sets

Every single developer should read this article by Joel Spolsky. It is a bit old but still a must.

By the way, I got there browsing a bit after seeing this solution for a classical problem, retrieving Chinese characters from a form and storing them in a database. Thanks Spring for charsetFilter.