Wednesday 2 November 2011

/bin/sh^M: bad interpreter: No such file or directory

I got the following error when attempting to run a .sh file I had obtained;

bash: ./thefile.sh: /bin/sh^M: bad interpreter: No such file or directory

After a little bit of research it turns out that the file had been written on Windows and then exported thus creating a basic text format issue.

Under DOS (Windows/PC) the end of a line of text is signalled using the ASCII code sequence CarriageReturn,LineFeed (alternately written as CR,LF or the bytes 0×0D,0×0A). On the Macintosh platform, only the CR character is used. Under UNIX, the opposite is true and only the LF character is used.

To fix this I ran the following command which converts the file to UNIX format;

dos2unix thefile.sh

Information on the dos2unix command can be found here.

Monday 31 October 2011

How to start Jasper server in Linux

To start a JasperSoft server in Linux go to your Jasper server install directory, typically /usr/local/jasperserver/

cd /usr/local/jasperserver/

Then run the start command on the Jasper control shell script;

./jasperctl.sh start

This can be done in one with the command;

./usr/local/jasperserver/jasperctl.sh start

Wednesday 26 October 2011

Recompiling Virtual Box's kernel

I recently installed some updates on my openSUSE Linux machine and found that my VirtualBox machines were no longer starting. I was receiving an error similar to the image below.


To fix this I needed to recompile my VirtualBox kernel. To do this I opened up a console as root (using su and my root password) and ran the following commands;

cd /etc/init.d/
./vboxdrv setup

Tuesday 13 September 2011

Edit Glassfish JDK path

I recently downgraded my version of Java to compile code for an old project. When I started my Glassfish domain I recieved an error;

The system cannot find the path specified.

When Glassfish is installed it hard-codes its reference to your JDK location. To fix this problem I ended up having to edit a file named asenv.conf (asenv.bat in Windows). The file is located at: /usr/local/glassfish/config/ (C:\glassfish\config\ in Windows).

I 'vi'd into the file and changed the AS_JAVA attribute to my new JDK location;

vi /usr/local/glassfish/config/asenv.conf
AS_JAVA="/usr/java/jdk1.4/"

In Windows you could use these commands to comment out the old reference and add the new one;

REM set AS_JAVA=C:\Program Files\Java\jdk1.6.0_24\jre/..
set AS_JAVA=C:\Program Files\Java\jdk1.4

Tuesday 6 September 2011

Setting JAVA_HOME and PATH variables in Linux

JAVA_HOME

JAVA_HOME is the directory where your Java JDK is installed. To set this; In a Linux Terminal use the export command to set the variable.

JAVA_HOME=<path-to-java>

If your path is set to /usr/java/jdk1.6.0_24/bin/java, set it as follows:

export JAVA_HOME=/usr/java/jdk1.6.0_24/bin/java

PATH

Set this variable to run Java commands without referencing the absolute location of Java every time i.e. javac MyClass.java rather than /usr/java/jdk1.6.0_24/bin/javac MyClass.java. To do this, also in a terminal append the <path-to-java> to the PATH variable using the export command.

PATH=$PATH:/usr/java/jdk1.6.0_24/bin

To see the changes use the echo command.

echo $JAVA_HOME
echo $PATH

Tuesday 23 August 2011

Writing to a properties file with Ant

I find this useful when I want to change how the application works depending on where and why it is being deployed. This blog post assumes you have Ant installed!

In the build.xml file;

<!-- Add the property file -->
<property file="project.properties"/>

<!--  Write a property to the file with a value based on the target -->
<target name="liveExample">
     <echo message="This target example has the live properties"/>
     <propertyfile
          file="project.properties"
          comment="Setting important system procedure variable to 100">
          <entry key="importantVariable" value="100"/>
          </propertyfile>
</target>

<target name="testExample">
     <echo message="This target example has the test properties"/>
     <propertyfile
          file="project.properties"
          comment="Setting important system procedure variable to 10 for testing purposes">
          <entry key="importantVariable" value="10"/>
          </propertyfile>
</target>

How it works

The properties file (project.properties) has a key/value pair written to it. Based on what target is run, the key has a different value. This allows us to control how the application works based on how we deploy it. For further reading on Ant's PropertyFile task see their manual.

Thursday 18 August 2011

Writing a unit test using JUnit

To do JUnit tests on your code you first need to include the JUnit libraries which can be retrieved from the JUnit web site (licensed under GNU).

Now you can create your test files. These should be created in the test packages package folder to keep them separate from the actual project code.

package yourprojects.somepackage;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class ExampleTest {

    // Create objects needed to perform the test i.e. mock objects
    private static ObjectToTest testObject;

    @BeforeClass
    public static void setUpClass() throws ConnectionException, SQLException {
        testObject = new ObjectToTest(); // Initialise object to test
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
        // Any connections made (e.g. such as database) should be closed here
    }

    @Test
    public void testSomeMethod() throws ForumException {
        assertTrue(testObject.someMethod());
        assertIsEmpty(testObject.someMethod());
        assertNotNull(testObject.someMethod());
        assertEquals(true, testObject.someMethod());
        assertSame(new ObjectToTest(), testObject.someMethod());
    }

}

How it works

This class is testing a method (someMethod) in an object (ObjectToTest).

We start off by including all the JUnit libraries we need for the tests.

We then override the setUpClass and tearDownClass methods which organise the test environment for us. In the set up class we initialise the mock objects we need to test. In the tear down class method we close any connections we made during the testing.

Then we write the tests; I typically write one test for each result I expect e.g. testAMethodWithNullArguement(). Keeping the test methods separate makes it clearer what has failed.

The test method in the above example lists many of the more common assert methods I use depending on what kind of output I am expecting from the method being tested.

Wednesday 17 August 2011

Writing a web unit test using JWebUnit

Firstly, you will need to include the JWebUnit library in your Java project which is available to download (and is licensed under the GNU) here. You will also need the JUnit libraries as JWebUnit is built upon it which can be downloaded here (also under the GNU license).

Then in your test packages create the web test file. I recommend putting the web test file(s) in a web test package to separate them from any JUnit test files you may also have in your test packages.

package yourproject.webtest;

import net.sourceforge.jwebunit.junit.WebTestCase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class WebUnitExample extends WebTestCase {

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        // set up where JWebUnit should look
        setBaseUrl("http://localhost");
        beginAt("/webapp/thenameofyourproject/");
    }

    @After
    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void testAFeatureOfYourProject() {
        System.out.println("Testing a feature of the project");
        // Add some method calls to the JWebUnit API here then call asserts to see if
        // the output is correct      
    }

}

How it works

As you can see this file is very incomplete and will not do anything at the moment but for this example that doesn't matter!

setUp and tearDown are important methods as you can set up what is needed before each test and obviously, 'tear down' i.e. remove those conditions after.

As for the tests themselves, JWebUnit has many methods that can be used to traverse the page (e.g. clickButton, clickLink) and methods to manipulate page elements (e.g. setTextField, selectOption). Just as in JUnit, asserts are used to check that things are how they should be. For a list of JWebUnit methods see their API listings.

Wednesday 3 August 2011

Using ResourceBundle

I use ResourceBundles to hold my project's properties so that I can make widespread changes to the project by changing only one value.

Examples of what I may use this for could be the project name, email address, directory structures etc. I would not use ResourceBundlesfor constants.

Below is a code example;

File: project.properties
projectName=AnExampleProject

File: Properties.java
package example.properties;
import java.util.ResourceBundle;

public class Properties {
  
   private static ResourceBundle rb = ResourceBundle.getBundle("example.properties.project");

   public static String getProjectName() {
     return rb.getString("projectName");
   }
}

How it works

All properties are put into a .properties file which can be retrieved using ResourceBundle's API. We get a ResourceBundle object by using the ResourceBundle method getBundle, using the path to the properties file as a parameter. With that object, we can then retrieve the properties using the getString method (other types are also available e.g. getInt).

The methods are made static so the properties can be retrieved without instantiating the Properties class every time.

The project name would be retrieved using Properties.getProjectName();

Tuesday 2 August 2011

MySQL NOT IN query

I had to do this today to find all references that did not appear in a table. Here's a basic example;

SELECT player_id FROM tbl_player WHERE player_id NOT IN (SELECT player_ref FROM tbl_team);

How it works

The above example retrieves a list of all the players (player_ids) that do not appear in the team table (tbl_team) using the reference (player_ref).

Friday 29 July 2011

Basic CSS Tool Tip

There are a lot of tool tips out there but I've grown to like this implementation as it's so simple and extensible.

CSS code:

span.tool {
  position:relative;
  cursor:help;
}
span.tool span.tip {
  display: none;
}
span.tool:hover span.tip {
  display: block; 
  z-index: 100; 
  position: absolute;
}

HTML code:

<span class="tool"> 
 <label for="txtExample">Example</label> 
 <span class="tip">The tip text goes here.</span> 
</span> 
<input type="text" name="txtExample" id="txtExample"/>

How it works

There are 2 span elements at work here; tool & tip. Tool is positioned relative to the page. Tip is not displayed (display: none) on the page. On the tool hover event, the tip's attributes are changed; 
  • The tip is now displayed (display: block) i.e. visible on the page 
  • z-index is set - this ensures that the element shows in front of anything else on the page 
  • Position is set to absolute and top and left attributes are set. This allows us to put the tip wherever we like. This is relative to the containing span (tool). 
There is no styling in the code above so it's recommended to add some styles of your choosing to the span tip (at least a background-color so that the tip text does not go over other text on the page!). I'd add all styles to the span.tool:hover