Posts

Showing posts with the label Java

Java on a $5 per month server from Digital Ocean

I just recently came across Digital Ocean - "Simple Cloud Hosting". I was impressed by the pricing, much cheaper than my current VPS solution so I've set up an Ubuntu 12.10 server with 512MB, 20GB SSD, for $5 USD per month. Since I've moved almost everything to Google (from self hosted WordPress to Blogger and Google Docs) the only thing I really need a VPS for is to run my Java applications (there still is the option to use AppEngine with Play 1.2.x if it makes sense to use DataStore). But for $5 a month, this means I can use Grails or Play with a relational database - if it performs well enough. Setting up a server with Digital Ocean really is simple. Once you've signed up (check their Twitter posts for a coupon) and added a credit card, your server can be running in minutes. I chose Ubuntu 12.10, and then they have nice and clear instructions on what to do next to secure it: Initial Server Setup with Ubuntu 12.04 How to Set Up a Firewall Using IP T...

Sort out your digital photo collection

Image
If you're like me, you've got several years (and gigs) of digital photos that you've been copying off your cameras, your phone, your wifes phone etc. Tonnes of photos, all unsorted, and some times copied more than once. How do you go about sorting out the good photos - the ones you want to share with your family - from the ones that just aren't worth sharing? This is why I started working on vsPhotoSorter - at the moment it is minimalistic, just enough features to provide me with value - but its working for me as I steadily sort out my collection. If you have a similar problem with your photo collection, have a look and see if it helps you too. Read more here http://www.vamonossoftware.com/2013/01/sort-out-your-digital-photo-collection.html . This software is a desktop application built using  Griffon and  Groovy on the  Java platform.

When tests become hard

A friend contacted me for advice. He was having trouble writing a test for some code. I asked him to send me the code and the test so I could see what was going on. I encounter this problem quite frequently, so I thought I'd post one of my responses... Okay, here is where I think you are going wrong, and when writing tests becomes hard, it’s usually because of this. It’s important to layer your applications. Web/Services/DB – at least conceptually. What you have is a class that does everything. Web and Service. No-one can reuse your file processing logic without using the web, not even tests! A nice way to code would be to: Write a UNIT test that attempts to process a file. It passes in a file X and asserts that the result is Y. It won’t compile because you haven’t written the file processing code yet! Now you write the service which processes the file The test will compile and run now, but probably fail because you forgot something. Now you can fix your service And ...

Could not complete schema update

I recently mapped a JPA entity to a view, and encountered this error while using hibernate to update the database schema: ORA-01702: a view is not appropriate here [SchemaUpdate.execute] could not complete schema update I was using the Oracle JDBC drivers 10.1.3.3 - luckily, the solution was to upgrade the drivers - when using 11.2.0.2.0 it works fine. Download ojdbc6.jar from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html ojdbc6.jar (2,152,051 bytes) - Classes for use with JDK 1.6. It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.

On to some exciting stuff

I went to a Agile Sydney meetup recently - "Automated web tests as a team communication tool" - by John Smart of Wakaleo Consulting . It was great stuff, and the presentation is now online . There is a nice story here in that the project feature set is documented via easily readable and executable tests, and the reporting shows which features are implemented and which aren't. More information on the the Thucydides tool he references can be found at: https://github.com/thucydides-webtests/thucydides/wiki While reading that presentation online, I found more gold here in some of his other presentations: JUnit Kung Fu: Getting More Out of Your Unit Tests Introduction to Domain Driven Design They are a great read and are great pointers in the right direction to improve your software development. (If you haven't seen them before, check out Wakaleo's training courses . I haven't been on them, but based on the course content, they look to be the most relevant ...

Using the Crystal Reports Java API to generate PDF

I recently had to investigate how to generate a PDF from a Crystal Report created by another team. Without knowing anything about Crystal Reports, I had to google around for information and piece it all together. It turns out to be really simple once you know how. We were using Business Objects 4.0, and probably the most important thing was to get the Java library - download 'SAP Crystal Reports for Java runtime components - Java Reporting Component (JRC)' from http://www.businessobjects.com/campaigns/forms/downloads/crystal/eclipse/datasave.asp There are a lot of samples on the web to look at - you might find something to help here: http://wiki.sdn.sap.com/wiki/display/BOBJ/Java+Reporting+Component++SDK+Samples A good example to start with is “JRC EXPORT REPORT”: http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/40d580ce-bd66-2b10-95b0-cc4d3f2dcaef If you have an RPT file, the java to generate the report is relatively simple - : ReportClientDocument reportClie...

Easy classpaths with Java 6

Back in the day it used to be a bit more difficult than it is now to dynamically generate your java application classpath - you'd have to write a script to loop over all the jar files in a directory, appending them to your classpath variable - like this . Since Java 6 though, its been a lot easier - you can use wildcards to specify all jars in a directory. See the "Understanding class path wildcards" in the Java SE 6 documentation . Now its as simple as: java -cp /my/lib/dir/* MyClass Simple! As it should be!

Turning off JDK logging

I've been developing a simple little command line application and one of the libraries I'm using seems to log debug information to the console. To clean up the console output, I had to turn off the JDK by using: LogManager.getLogManager().reset(); This seemed to do the trick for me, but it sounds like in some cases you may need to go a bit further and turn off logging at the global logger level: LogManager.getLogManager().reset(); Logger globalLogger = Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME); globalLogger.setLevel(java.util.logging.Level.OFF);

JUnit parameterized test with Spring autowiring AND transactions

I've been writing JUnit Parameterized tests (a nice intro here - also see Theories ) to do some data driven API testing. Now, when introducing Spring into the mix there are a couple of extra things to do . I came unstuck though because I was trying to do Transactional tests - since I'm now using @RunWith(Parameterized.class) and setting up my Spring TestContextManager manually the @Transaction annotations caused an exception: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given I couldn't find any built in solution, so I've gone with manual transaction management in my test, using doInTransaction: @RunWith(Parameterized.class) @ContextConfiguration(locations = "classpath*:/testContext.xml") public class MyTest { @Autowired PlatformTransactionManager transactionManager; private TestContextManager testContextManager; public MyTest (... parameters for test) { // store parameters in instance v...

Introducing JtSysMon

I spend a lot of time on the train travelling to work. Working from home has not really taken off in this part of the world - so, what better to do with that time than practice my craft? Its not comfortable, its hard to concentrate, but progress can be made. I have successfully used this tool to observe real performance problems, and confirm they've been resolved. JtSysMon is a very simple monitoring tool which will poll resources and time the response. The response time is logged to a database, and from there, you can see which resources are either slow or failing/erroring. I wrote this because I needed a way to make sure that various resources were available and responsive during development, so that we could respond quickly (proactively) to problems and have them fixed before developers or testers noticed or wasted time fault finding. Existing solutions (Nagios, Zenoss etc) seemed too complex for such a simple requirement, with too much of a learning c...

A simple task queue

I'm working on a little sample framework - really only to keep my sanity and practice my chosen craft - that allows you to string together tasks in a pipeline for processing. To exercise that framework - to flush out the pros and cons of the implementation - I'm writing a sample application. The basic idea is that each task is interested in an event. That event could be the arrival of a file, or the completion of another task etc. To string tasks together, I've created a simple database queue system - as a task completes, it writes to the queue and then other tasks which are interested will see that event and then begin. Tasks complete either successfully (by returning no errors) or unsuccessfully (by returning more than one error). An important concept is to be able to add (or remove) tasks from the pipeline programmatically and without changing the database schema. So, quite simply, when a task completes, that event is written to the queue - but how do we find which event...

No Scope registered for scope request

Recently I was working with some code which defined beans as request scope. Junit tests failed with the following exception while trying to initialize the application context: Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: ...; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request' I found the solution by searching around (unfortunately I didn't record the sources). Basically, you can register the required scopes as shown below: [sourcecode language='java'] import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestScope; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.SessionScope; public class SampleTest extends AbstractDependencyInjectionSpringContextTests { @Override protected v...

Trinidad and text wrapping

While using Trinidad 1.0.1 I came across a problem where text did not wrap when displaying read only text fields. This meant that everything was pushed off the right hand side of the page. I had to change the fields from: <tr:inputText label="#{msgs['message1']}" styleClass="labelRight" required="false" value="#{mybean.value}" id="mybean" columns="10" readOnly="true"/> to <tr:panelLabelAndMessage label="#{msgs['message1']}"> <tr:outputText value="#{mybean.value}" /> </tr:panelLabelAndMessage> and added the following to my style sheet: .af_panelFormLayout_content-cell { white-space: normal; } Note outputText instead of inputText. It was tricky to get it working in FF and IE at the same time.

Groovy and Grails books

The catalog of Groovy and Grails books keeps growing, so if you are in the market for some, check out these sources: Pragmatic programmer Grails Groovy Apress Grails Groovy Manning Grails in Action Groovy in Action On Amazon:

Over typing -Dskip.junit=true

I generally prefer Maven2, but I'm currently on a project using a custom ant build script - and I'm over having to type -Dskip.junit=true when I just want to generate a quick jar without running the tests. To easily switch off tests, I modified ant.bat so that it looks for a command line parameter 'st' and if found, it will substitute it with -Dskip.junit=true. In Ant 1.7.0, you just need to change line 68 in block ':setupArgs' from set ANT_CMD_LINE_ARGS=%ANT_CMD_LINE_ARGS% %1 to if ""%1""==""st"" (set ANT_CMD_LINE_ARGS=%ANT_CMD_LINE_ARGS% -Dskip.junit=true) else (set ANT_CMD_LINE_ARGS=%ANT_CMD_LINE_ARGS% %1) So, the whole block reads (from line 65): :setupArgs if ""%1""=="""" goto doneStart if ""%1""==""-noclasspath"" goto clearclasspath if ""%1""==""st"" (set ANT_CMD_LINE_ARGS=%ANT_CMD_LINE_ARGS% -Dskip...

Do you need a Grails developer in Sydney?

If you haven't heard of Grails, head on over to Grails.org and have a look. Grails breaks down the barriers to the Java platform and gives you a highly productive environment for building web applications - and you are always free to utilise plain Java seamlessly. Having used Grails on my personal projects , and written several Grails plugins, I'm sold. My time is precious and I can achieve a lot with a small amount of code, in a small amount of time - free of a lot of the bureaucracy of "Enterprise" Java applications. If you are interested in working with Grails and need a conscientious, motivated guy on your team, drop me a line: paulrule1@gmail.com . To get to know me better, see: http://www.myonlineprofile.net/profile/prule http://www.ohloh.net/stacks/27540 http://www.linkedin.com/in/paulrule

A better StringTokenizer

I've just been reminded how painful it can be splitting a string based on a token with StringTokenizer . The problem is that it gets messy when you have two tokens consecutively. Luckily commons-lang comes to the rescue with StrTokenizer - where empty tokens may be removed or returned as null.

URI "file:./" is not hierarchical

Just recently I came across the following exception when deploying a simple Grails application to Tomcat : URI "file:./" is not hierarchical The application was so simple and ran locally on tomcat-6.0.16 - it had to be something environmental. Comparing the differences between machines showed that it was the JVM that was different. The JVM that produced the error was: java version "1.5.0" gij (GNU libgcj) version 4.2.4 (Ubuntu 4.2.4-1ubuntu3) while the Sun JRE 1.6 was okay: java version "1.6.0_03" Java(TM) SE Runtime Environment (build 1.6.0_03-b05) Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode) Note that this occurred on Ubuntu 8.04 64 bit Linux: Linux dev 2.6.24-21-generic #1 SMP Mon Aug 25 16:57:51 UTC 2008 x86_64 GNU/Linux

grails-selenium-0.4

This information is out of date - please see http://www.grails.org/Selenium+plugin Version 0.4 of the grails selenium plugin is now available. This version adds: scripts to create and run tests a postResults url for displaying the final test results Details are as follows: run-selenium Runs Selenium in the specified browser. Specify the path to your browser as a command line parameter i.e. grails run-selenium /usr/bin/firefox or, if the executable is on the path you would just need grails run-selenium firefox In your application.properties, you can specify: selenium.auto=true selenium.close=true selenium.multiWindow=true selenium.highlight=true selenium.resultsUrl=/your/url/here (defaults to ${appContext}/selenium/postResults) selenium.runInterval=1000 selenium.baseUrl= See http://selenium.openqa.org/installing.html (section titled Continuous Integration) for more information on selenium and continuous integration. create-selenium-test Generates a new empty selenium test. Supply th...

grails-jsunit-0.1

This information is out of date - please see http://www.grails.org/jsUnit+plugin grails-jsunit provides an easy and convenient way to utilize the JsUnit framework to your grails application. JsUnit allows you to unit test JavaScript functions in a similar way to using JUnit for Java. To install the plugin, use: grails install-plugin http://www.javathinking.com/grails/grails-jsunit-plugin/0.1/grails-jsunit-0.1.zip This plugin adds the following new scripts: create-jsunit-test Generates a new empty jsunit test. Supply the path of the test you want to create, relative to 'test/jsunit/tests'. Example use: grails create-jsunit-test registration/mytest creates ${basedir}/test/jsunit/tests/registration/mytest.html run-jsunit Runs JsUnit in the specified browser. Specify the path to your browser as a command line parameter i.e. grails run-jsunit /usr/bin/firefox or, if the executable is on the path you would just need grails run-jsunit firefox In your application.properties, you can s...