Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, May 24, 2007

Problem: cannot find symbol constructor TestCase()

I was struggling with a strange problem for a few days. Now I found partially the reason of the problem. A very simple test class didn't compile in my project:


import junit.framework.TestCase;

public class MyTest extends TestCase {
public void testMe() {
assertTrue(true);
}
}


The IDE (intellij) didn't compile it by saying:


cannot find symbol constructor TestCase()


This was very strange at first. After lots of trials and errors I found out that the reason for this compile error was that there was a conflict between the dependencies. Specifically the conflict was between junit.jar and repast.jar.

When repast.jar has a higher order than junit.jar then the test class doesn't compile. I didn't understand exactly why it occurred but it seems that it has a relation to the classpath declaration in the manifest.mf file of repast.jar, which states:


Class-Path: lib/junit.jar lib/log4j-1.2.8.jar lib/openmap.jar lib/plot.jar


Moreover repast's own junit has a smaller version than 3.8.

Read More...

Saturday, May 19, 2007

Introduction to AspectJ 5 with Spring and JavaConfig

Using the articles about JavaConfig in Guice vs. Spring JavaConfig: A comparison of DI styles and Simplifying Enterprise Applications with Spring 2.0 and AspectJ I managed to use aspectj configured with spring in java code only, that is without using xml files.

Here is the annotated aspectj class:


import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class HelloFromAspectJ {
@Pointcut("execution(* main(..))")
public void mainMethod() {}

@AfterReturning("mainMethod()")
public void sayHello() {
System.out.println("Hello from AspectJ!");
}
}

Here is the ordinary java class which is adviced by the above aspect:

public class HelloService {
public void main() {
System.out.println("Hello World!");
}
}

And here is the configuration class, which replaced the spring's configuration xml file:

import org.springframework.config.java.annotation.Configuration;
import org.springframework.config.java.annotation.Bean;
import org.springframework.config.java.context.AnnotationApplicationContext;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.ApplicationContext;

@Configuration
public class SpringConfig {
@Bean
public HelloService helloService() {
return new HelloService();
}
@Bean
public HelloFromAspectJ helloFromAspectJ() {
return new HelloFromAspectJ();
}
@Bean
public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {
return new AnnotationAwareAspectJAutoProxyCreator();
}

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationApplicationContext(SpringConfig.class);
HelloService helloService = (HelloService) ctx.getBean("helloService");
helloService.main();
}
}

Note that the bean AnnotationAwareAspectJAutoProxyCreator is the enabler of annotated aspects in spring. It has the same function as the
<aop:aspectj-autoproxy/>
element in the spring's xml configuration.

Read More...

Wednesday, March 28, 2007

Generate RTF in Java

I was googling for an open source library to generate RTF text in java. After navigating through lots of forums, I couldn't find a satisfactory result. Then I searched in Google Blogs and voila :)

Here is the link to the rtf generator in java.

http://it.newinstance.it/2007/03/23/msword-generation-made-easyer/

It is a very small jar file of 5 KB size, but it depends on Freemarker.

Read More...

Friday, March 24, 2006

Using Standalone Ant Tasks in Java Code

Ant is a very good build management and automation tool. Moreover Ant tasks are very easy to use in java code as well. I mean, you don't have to write build.xml files in order to benefit from Ant tasks. You can as well use Ant tasks from java code directly.

Here is an example of code:


Project project = new Project();
project.setBasedir(".");
project.setName("DbUnit Test");
SQLExec sqlExec = new SQLExec();
sqlExec.setProject(project);
sqlExec.setDriver(driverName);
sqlExec.setUrl(url);
sqlExec.setUserid(username);
sqlExec.setPassword(password);
File createTable = new File("db/create-table.sql");
sqlExec.setSrc(createTable);
sqlExec.execute();
sqlExec.setSrc(null);
sqlExec.addText("" +
" INSERT INTO app_user (id, first_name, last_name) \n" +
" values (5, 'Julie', 'Raible');\n" +
" INSERT INTO app_user (id, first_name, last_name) \n" +
" values (6, 'Abbie', 'Raible');\n");
sqlExec.execute();


This is the same as the following build.xml excerpt:


<sql driver=\"${jdbc.driverClassName}\" url=\"${jdbc.url}\"
userid=\"${jdbc.username}\" password=\"${jdbc.password}\">
<classpath refid=\"compile.classpath\"/>
<![CDATA[
INSERT INTO app_user (id, first_name, last_name)
values (5, \'Julie\', \'Raible\');
INSERT INTO app_user (id, first_name, last_name)
values (6, \'Abbie\', \'Raible\');
]]>
</sql>


Although xml code is shorter, java code has some adequate use cases. For example, you want to reuse SQLExec's functions in your own application. You can reuse any of the Ant tasks which is a large library full with utilities.

But beware that all Ant tasks depend on a wrapping Project object. Therefore the first line in the above java code instantiates a dummy Project object.

Read More...