Tag Archives: Java

Flex – Cairngorm Architecture Overview

Cairngorm is the lightweight micro-architecture for Rich Internet Applications built in Flex or AIR. A collaboration of recognized design patterns, Cairngorm exemplifies and encourages best-practices for RIA development advocated by Adobe Consulting, encourages best-practice leverage of the underlying Flex framework, while making it easier for medium to large teams of software engineers deliver medium to large scale, mission-critical Rich Internet Applications.

Cairngorm Architectural Framework

Cairngorm follows the principle of separating the view and business logic which is known as the Model-View-Controller pattern (MVC). The following list shows the different elements that form the framework.

1. View: Contains UI controls for user interaction and displays the state of the models data. Views dispatch events handled by the controller.

2. Model Locator: It acts as a centralized repository for all data needed in the application. It manages the view an is declared as [Bindable] to use all variables for data binding in the view. It must be implemented as a singleton which implements the Cairngorm IModelLocator. All data managed in the model should only be updated through a command or a responder object.

3. Value Object (VO): A value object is a class that only has properties to store values. It contains no methods or logic and acts as a pure data container. Value objects must implement the Cairngorm ValueObject interface. If they should be mapped to a corresponding server-side class it must declare the [RemoteClass (alias=”)] metatag. By doing so, Flex automatically converts an server-side object returned through remoting or data service interaction to its corresponding ActionScript object.

4. Event: In Cairngorm everything should be mapped to an event. Whenever the user interacts with the view or an action occurs an event must be dispatched. Custom events extend the CairngormEvent class an have a special property to contain the data that comes with it.

5. Command: Commands actually do the majority of an applications work. They recieve the event and its data, execute the logic an can change the workflow state of the model and view. Each command must implement the Cairngorm ICommand interface and its execute function. The revcieved event is a generic CairngormEvent and must be casted to the appropriate custom event first. The complete frontend-logic of the RIA is encapsulated within the commands.

6. Front Controller: It extends the Cairngorm FrontController class and maps the dispatched events to its corresponding commands. Therefore it has two methods: the constructor and an initialize method which will map an event to an command via the addCommand method. The controller is instantiated directly in the main.mxml via a component tag. You simply need to add the directory your class is in as an XML Namespace and then include the tag in the file.

7. Service Locator: Implemented as a singleton, it contains references to all services an application will use. Like the Front Controller, it is placed within the main.mxml via a tag. Possible services to be used are RemoteObjects, HTTPServices, WebServices or custom services.

8. Business Delegate: Business Delegates form an abstraction layer between the server-side services and the main frontend application. The locate the required service in the Service Locator, call the given method on it and route the response back to a responder object. Unlike the other elemente in the Cairngorm architecture, it does not extend a class or implement an interface, but it need to follow some guidelines: it must have at least two properties, one named reference to the ServiceLocator and one reference to a responder, both set in the constructor. And it must have implemented one method for each server-side method to be called. The delegate is created within a command object. It should only pass strongly typed value objects to a responder. If it does not recieve an appropriate object, it must create one before passing it.

9. Responder: Responders implement the mx.rpc.IResponder interface. A responder recieves the result of a service call and implements frontend-logic like a command. It directly may set values of the model and should only deal with strongly typed value objects.

10. Service: The Service is formed by an application tier, build with some server-side technology like J2EE or PHP. It Accepts the service requests from the RIA and responds back data. With a remoting technology like BlazeDS it is even possible to pass back value objects witch are transformed to their corresponding ActionScript objects in the application, so it can interact with them as if they are created by script.

Tools For Unit Testing Java Web Applications

There are a lot of ways to do unit tests, but sometimes unit testing becomes difficult. This could be because we need to test a Struts action or we do not want the unit tests to hit the database. So, the question is what tools can we use for testing java web applications? This is not meant as a complete and thorough overview, just some of the tools we know of or have looked into at some time. In many cases, you will find that the testing code is very repetitive, so we may end up building frameworks on top of these frameworks. I am sure I missed something or maybe even your favorite, so just leave me a comment.

Basic Java Testing – JUnit and TestNG are the most popular tools for basic java unit testing. JUnit typically has a small advantage as it has been around longer and is included with most packages of Eclipse. However, I do not think one is really better than the other, they are just different.

Mock Objects – EasyMock and jMock are popular mock objects frameworks. Sometimes you have to simulate the way another system works, and mock objects are perfect for this. You can even create your own mock object, but the frameworks make it easy to avoid extra code.

Database Interaction – DBUnit is really the only decent framework I have seen. It allows you to insert data before the test is run and cleans up after the test is complete. However, capturing the appropriate data for a test is not simple for anything but the most trivial applications.

User Interface Testing – UI testing is notoriously difficult and many applications skip it entirely. If you are testing your generated html, you will want to look at the following tools and determine which best suits your needs. This is a special case, as there is no “one true way” to test the user interface. These are all free tools as I am a fan of keeping things as cheap as possible if the tools are good enough.

* Selenium – Maybe the most complete package of the bunch and supports testing in IE, FireFox and Safari. Your unit tests are written in java, so you just need to learn the API. They also have interesting server options where you can run your tests across servers. You can even record tests using a FireFox add-on.
* Canoo WebTest – This is another framework that can record tests with a FireFox add-on. The tests for WebTest are typically written in xml files. This gives you the option to build a library of fragments of functionality to include in various tests. You can also write your tests in Groovy or use the Java API directly. These can also be run directly from within Ant.
* Watij – This is a port of the popular Ruby testing framework, Watir. The tests are written in Java using their API. They also include BeanShell so that you can interactively test your code. This is a really interesting option if you are not sure what a unit test should look like initially.
* JWebUnit – This is more of a meta-framework, where it can build upon HtmlUnit or Selenium. HtmlUnit is fairly good by itself, but sometimes a framework is just too low-level for some people or people find that they keep doing the same things and decide to build upon the framework. There is one problem with JWebUnit, the documentation is very sparse, so the learning curve ciould be problematic.
* Cactus – This is another meta-framework, but this is from Apache. It is meant as a wrapper for testing an entire application from basic java testing, servlet testing and down to the user interface testing. Due to this meta concept, the documentation can be a bit vague. However, I have heard good things from those people that spent the time to learn more. Disappointingly, I have not had the time to dive into it much.

There are a lot of other tools out there as well. FitNesse is very popular, but it takes a very distinct approach to how unit tests are built. For Struts applications, there is a framework called StrutsTestCase that is immensely helpful as well. For a big list of testing and related software, take a look at XProgramming.com. They list testing frameworks for almost every language. Lastly, it is always a good idea to know how well your software is tested. This is not a question of whether all of your tests pass, but how much of the code is actually tested. My favorite test code coverage tool is Cobertura. It is fairly simple to use and the reports are generated in a similar manner to how JUnit generates reports.

So, if you know of a good free java testing tool, drop me a note in the comments. Sometimes a good tool may not be getting enough publicity or I may not have had time to look at it. In any case, keep testing until you have good test coverage. And remember the testing mantra, “Red, Green, Refactor”

10 Questions to Ask When Choosing a Framework

When do you need a framework and when do you decide to roll your own? Ask yourself the following questions.

  1. What is my target compile and deploy version? In the java world it is very common to have a target version of 1.2. This reduces the number of java frameworks that you may use. This is quite typical of many corporate environments as they take much longer to upgrade various parts of their IT infrastructure.
  2. What functionality do I need at the minimum? If you do not need much functionality, it may not be worth the time to even research frameworks. As a consultant, I saw some firms do a full month-long framework evaluation when they only needed a servlet filter.
  3. Are there “bonus” features that a framework may give us? Some frameworks are large enough to have many more features than you are immediately looking for. Spring is a perfect example of this. You may be looking at Spring for the dependency injection portion, and as a bonus you pick up the MVC features in the framework.
  4. Do we have any knowledge of the framework? In some shops, this is the most important question to ask. If there is no knowledge of the framework, you will spend a lot of time researching its capabilities instead of actually using it. The amount of time spent maintaining code written to use the framework is also much larger than normal.
  5. What is the cost of knowledge gathering and research for the framework? This is a question for the managers reading this post. If the developer needs a month to research the frameworks and determine their suitability, then that is very expensive. This cost is increased when you realize that mostly senior level developers are going to do the evaluations.
  6. What is the cost of developing an internal framework that does the minimal feature set? If you do not need a lot of functionality, is it “simpler” and “cheaper” to roll your own? In many cases you can have a senior level developer design the framework and get help from junior developers for implementation. This also adds to the internal technical knowledge of the team.
  7. How active is the framework community? If you have a question on how to use something, the community forums or blogs of interest are invaluable. If there is a framework user mailing list, look at how many messages are posted and how quickly responses appear. This is your development support team, so you need to have fairly quick response.
  8. Are there consultants readily available if you hit a roadblock? Sometimes companies will have specific expertise in a framework. If the framework is popular, then many consulting firms will have someone with some level of knowledge.
  9. Are there time constraints on the framework decision? Do you need a choice yesterday? If there is little time to make a decision, popularity of the framework becomes important. Popularity will typically give you the answers to a few of the other questions in this list. Also, smaller frameworks are normally simpler and quicker to integrate into your code.
  10. Would multiple smaller frameworks or various best of breed fit better than one monolithic framework? Some people get concerned with a monolithic choice like Spring or .Net. There are a wealth of frameworks available and some have very targeted functionality. For example, SiteMesh is a small framework that allows you to “skin” your website very easily. You can setup a SiteMesh example site and understand what it does within an hour.

What did I miss? Are there any other questions that you think are important?

JAVA Coding Standards

Code conventions are important to programmers for a number of reasons:

  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
  • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

We can find a lot of information about the coding standards at the link below.

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html