tag:blogger.com,1999:blog-22820181407943707752024-03-08T21:54:46.955+01:00Software Development and JavaMy thoughts about java, software development and other things that matters.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-2282018140794370775.post-29199168862004152992011-05-24T23:13:00.008+01:002011-05-31T15:58:14.814+01:00a TDD framework for EJB3<span class="Apple-style-span" style="font-size: large;">In Container Test Driven Development for EJB3</span><br />
<br />
Want to write code TDD style but using JEE? Well now you can!<br />
<br />
The blog post I did many months ago has now found its way into a useful tool (hopefully).<br />
<br />
<b><u>Background</u></b><br />
As most time I write code in Spring or JEE5 environments I have had the opportunity to become partially spoiled by the relative ease of testing Spring "wired" code. The only trouble I have had is basically connected to transactions (the test committed unwanted data) or that I need a small variation of the configuration and the easiest way to do this was do create a new copy of the spring-config (config-hell-yuck).<br />
<br />
<u>My experience with TDD </u><br />
My experience with the TDD "in-container" approach is aprox 4 years and have written about thousand tests of various types. I think I have a decent experience in the subject, but still have a lot to learn.<br />
<br />
<u>TDD, the test driven development.</u><br />
I have during my years as a developer started to classify the way my tests are written into four categories.<br />
<ol><li><b><u>Unit Test</u></b>, a single class with no relations. (Hard to do in real life for "service layered applications" but its the way to start with TDD). This always just works as no related classes are loaded during its test i.e. no In-Container tooling is needed.</li>
<li><b><u>Functional Test</u></b>, the entire application is "started" somehow and all internal code can be executed without exception. (This is how the Spring-JUnit extensions works). This requires some sort of In-Container testing.</li>
<li><u><b>Mocking(Partially functioning) Test</b>,</u> The test can be started as a functional test or as a regular unit test but some parts are in need of mocking. I usually do this for third-party dependencies that I can not do In-Container testing of. Not easy in any tool, in Spring a new config must be created for each type of test.</li>
<li><b><u>UseCase Test</u>, </b>this test is done to verify the specification. Its a playback of the intended software usage, for these tests to be easy to maintain and keep in control its a good thing to try to minimize the amount to something manageable. Like max 100 or so. The software I personally tried this "test-type" first time had about 60 (for a peace of software that was about 100k line of code). The interesting thing is that the requirements that we created it from had only 15 use case scenarios. According to me each test here hould have a corresponding use case scenario. The tip is to try to code the test in way that its easy to read as a scenario even though its code. </li>
</ol><div>If I look at my personal experience the functional tests are the once where i easily can test the code i create without hideous overhead or "meaningless" test code. The second most important one is the mocking test, where i try to not execute some part of the code. This is very useful for fixing bugs in legacy systems where parts are just not easy to run In-Container and for excluding a third party software from the test assertions.</div><div>Once the software is "done" i start creating the use-case scenario test and make it "really work". This use case tests can be written earlier to understand all parts of the software in creation.</div><div><br />
</div><div>- Unit Tests are used to verify utilities or classes that have no dependencies. Very good as long as they can be used.<br />
- Function/Mocking test is short and concise and have focus on assertions. There are large amounts of these test. </div><div>- The Use case test is complex and large flows are executed, only key elements are asserted. Never assert any business logic details, just big picture flows.</div><div><br />
</div><div>This is what i learned when coding Spring code. And 2 years ago I went back to JEE .... OMG! Its like stepping back to stoneages, TDD wise. The "TDD bug" had already stung me, I could no longer code without testing. It felt dirty and I felt like i did not have control over the software I was suppose to code.</div><div><br />
</div><div><b><u>A possible solution?</u></b></div><div><br />
</div><div>It's time to announce a working version of the TDD Tooling for EJB3 :)</div><div><br />
</div><div>In an effort to try to create a tool that can support the four different types of tests that are described above a tooling framework was created that makes it easy to test JEE code. At least it seems to work as intended, i need more to use to prove it though :)</div><div><br />
</div><div>I am personally using it for two separate customers with very good results. The framework is adapted for one of them and used as is for the other. I have personally educated the first customers in TDD coding and shown how we can code in a mix of EJB2/3 and Factories with this tool.<br />
<br />
<a href="http://www.squeed.com/sites/default/files/A%20Junit%20utility%20tool.pdf"><span class="Apple-style-span" style="color: blue;">See this presentation</span></a> from the Javaforum in Gothenburg.<br />
This explains how you can perform Functional and Mocking tests.</div><div><br />
</div><div><u><b>Introducing TDD tooling for JEE based code. </b></u></div><div><br />
</div><div><b>Pros</b></div><div>- Loose coupling to your test code, and no coupling to your business code.</div><div>- Automated lifecycle handling of underlying Container and DB transactions.</div><div>- Possible partial mocking of any EJB3 service (recommend Mockito or manual mocks).</div><div><b>Semi-pro (proven concept)</b><br />
- Has been in use by two development teams since january 2011 and over 300 tests are executed with this tool for both projects (at this precise moment). </div><div><br />
</div><div><b>Cons</b></div><div>- Only supports part of the JEE specification.<br />
@Stateless, @Stateful, @EJB and @PostConstruct are verified all other part are unverified and might work, but not intentionally.</div><div>- Not supported by any companies. (So far)</div><div><br />
</div><div>Thats it from me for now, hope that someone start using this and gives some feedback to me soon :)</div><div><br />
</div><div>Maven dependency example:</div><br />
<code><br />
<dependency><br />
<groupid>org.hrodberaht.inject.extension</groupid><br />
<artifactid>tdd</artifactid><br />
<version>1.0_RC2</version><br />
<scope>test</scope> <br />
</dependency><br />
<dependency><br />
<groupid>org.mockito</groupid><br />
<artifactid>mockito-all</artifactid><br />
<version>1.8.5</version><br />
<scope>test</scope><br />
</dependency><br />
</code><br />
<br />
<br />
<div>Assemply JAR with same dependencies included as the maven dependencies above.<br />
<a href="http://code.google.com/p/injection-extensions/downloads/list"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/injection-extensions/downloads/list</span></a><br />
<br />
</div><div><br />
</div><div><br />
</div>Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-82458871260123173592010-10-22T19:47:00.011+01:002010-10-23T15:09:24.440+01:00Performing Unit Tests with the @Inject Container as a RunnerAs a response to the following post about the "Perfect Integration Test" i would like to give my point of view of what a perfect test with active connection to a database is. <a href="http://community.jboss.org/en/arquillian/blog/2010/10/04/the-perfect-recipe-for-testing-jpa-2-revisited"><span class="Apple-style-span" style="color: blue;">Jboss Blogg entry</span></a><br />
<br />
The following has a big difference to the Arquillian project, it never intends to be able to execute the same testsuites on different container vendors. Its more focused on creating the fastest and simplest possible UnitTest for your EJB code.<br />
<br />
But I must admit that a lot of my inspiration to do this came from Arquillian, i just thought they had the wrong focus.<br />
<br />
I will explain how its possible to perform a UnitTest where the following rules are enforced.<br />
<br />
<ol><li>The services are "wired" for EJB3 (Stateless/Stateful Sessions Beans supported) automatically with a scanning path configuration.</li>
<li>For each test the "container" is automatically reset</li>
<li>For each test the internal database is rolled back</li>
<li>Its possible to register mocking services by overriding existing configuration, to make it easier to vary the test-suite without reconfoguring the entire software with a new "config class".</li>
</ol><br />
<br />
All these feature's are enabled with the help of the hrodberaht injection container and an extension to it called ejbunit (might be renamed to just junit as i am thinking about adding spring support as well, as it feels easy enough).<br />
<a href="http://code.google.com/p/java-simple-utils/wiki/Injection"><span class="Apple-style-span" style="color: blue;">Injection Wiki</span></a> (the 1.2-SNAPSHOT version though is needed, as its in this version that the cloning support is added)<br />
<br />
This is done using the followign techniques.<br />
<br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">1. The services are "wired" for EJB3 (Stateless/Stateful Sessions Beans supported) automatically.</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">- All that is needed is a JUnitRunner and a Project Configuration (scanners are supported)</div><br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">2. For each test the "container" is automatically reset</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">- The container is resets to its original state before the test is executed. Original is whatever the config found in the class Project Configuration context.</div><br />
3. For each test the internal database is rolled back at the end<br />
- All calls to close/commit are prevented (silently) and a rollback is executed once the testsuite is over.<br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">- Works as long as the datasource is created by the Project Configuration</div><br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">4. Its possible to register mocking services by overriding existing configuration, to make it easier to vary the testsuite without reconfoguring the entire software with a new "config class".</div><div>- Is done using a utility that can reach the running @Inject Container for the active test. (supports several active test threads if needed)</div><div><br />
</div><div>So how does this look code wise:</div><div>The test<br />
<code>@EJBContainerContext(EJBContainerConfigExample.class)<br />
@RunWith(EJBJUnitRunner.class)<br />
public class TestEJB3ServiceContext {<br />
<br />
@EJB<br />
private EJB3ServiceInterface ejb3ServiceInterface;<br />
<br />
@Test<br />
public void testEJBWiring(){<br />
String something = ejb3ServiceInterface.findSomething(12L);<br />
assertEquals("Something", something);<br />
<br />
String somethingDeep = ejb3ServiceInterface.findSomethingDeep(12L);<br />
assertEquals("Something Deep", somethingDeep);<br />
}<br />
<br />
@Test<br />
public void testEJBResourceInjectionAndUpdate(){<br />
String something = anInterface.findSomethingDeepWithDataSource(12L);<br />
assertEquals("The Name", something);<br />
anInterface.updateSomethingInDataSource(12L, "A new Name");<br />
something = anInterface.findSomethingDeepWithDataSource(12L);<br />
assertEquals("A new Name", something);<br />
}<br />
<br />
@Test<br />
public void testEJBWiringWithMockito(){<br />
<br />
EJB3InnerServiceInterface anInterface = <br />
Mockito.mock(EJB3InnerServiceInterface.class);<br />
Mockito.when(anInterface.findSomething(12L))<br />
.thenReturn("Something Deep FormMock");<br />
EJBResourceHandler<br />
.registerServiceInstance(EJB3InnerServiceInterface.class, anInterface);<br />
<br />
EJB3ServiceInterface serviceInterface = <br />
EJBResourceHandler.getService(EJB3ServiceInterface.class);<br />
String something = serviceInterface.findSomething(12L);<br />
assertEquals("Something", something);<br />
String somethingDeep = serviceInterface.findSomethingDeep(12L);<br />
assertEquals("Something Deep FormMock", somethingDeep);<br />
}<br />
}<br />
</code></div><div><br />
</div><div>So what is all this:<br />
1. The config @EJBContainerContext(EJBContainerConfigExample.class)<br />
- This contains the code that "scans" for all EJB's in the software, using package scanning. This is similar to the annotation @Deployment used by Arquillian.<br />
<code><br />
public class EJBContainerConfigExample extends EJBContainerConfigBase {<br />
<br />
public EJBContainerConfigExample() {<br />
String dataSourceName = "DataSource";<br />
addResource(dataSourceName, <br />
ResourceCreator.createDataSource(dataSourceName));<br />
addSQLSchemas(dataSourceName, <br />
"test/org/hrodberaht/inject/extension/ejbunit");<br />
}<br />
<br />
@Override<br />
public InjectContainer createContainer() {<br />
return createAutoScanEJBContainer(<br />
"test.org.hrodberaht.inject.extension.ejbunit.ejb3.service");<br />
}<br />
</code><br />
<br />
- "addResource" Will register a datasource for the application named "DataSource", this works as any datasource and can be injected with @Resource as regular with EJB3.<br />
- "addSQLSchemas" Will try to execute all SQL files found in this directory names with "create_schema*" once all those are executed all file with the name "insert_data*" will be executed. This execution results will be commited to the datasource selected in the method call.<br />
- "createContainer" A container will be created and returned (this is used by the Runner)<br />
<br />
2. @RunWith(EJBJUnitRunner.class)<br />
This will take care of all the gritty details with rollback of the active connection and reset of the "container".<br />
Before a testsuite is executed a clone of the container is made and registered as the active container for the running test (after the original has been created from the Configuration).<br />
<br />
3. @EJB private EJB3ServiceInterface ejb3ServiceInterface;<br />
All testclasses that have the RunWith can have injected Service (both @EJB and @Inject works)<br />
<br />
4. The tests themselves<br />
Are not that special, only that they can be be executed without any before/after handling for rollback or cleanup of the database.<br />
4.1 testEJBWiring<br />
The test for this will only verify that the injection have done what they are suppose to.<br />
4.2 testEJBResourceInjectionAndUpdate<br />
The test will verify that<br />
- the SQL scripts have been executed (first row is a selection of the id 12)<br />
- the Datasource has been injected correctly to the inner service bean<br />
- that the update code works as intended and can be refetched from the running database.<br />
<br />
<b>Note:</b> In the real test suite a second test is execute to verify that the update is automatically rolled<br />
back by the testrunner.<br />
<br />
All this code can be found in extensions to the hrodberaht @Inject. At the google code projects.<br />
<a href="http://code.google.com/p/java-simple-utils/"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/java-simple-utils/</span></a><br />
<a href="http://code.google.com/p/injection-extensions/"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/injection-extensions/</span></a><br />
<br />
The code for the test case used as example here can be found at:<br />
<a href="http://code.google.com/p/injection-extensions/source/browse/trunk/ejbunit/src/test/java/test/org/hrodberaht/inject/extension/ejbunit/ejb3/TestEJB3ServiceContext.java"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/injection-extensions/source/browse/trunk/ejbunit/src/test/java/test/org/hrodberaht/inject/extension/ejbunit/ejb3/TestEJB3ServiceContext.java</span></a><br />
<br />
A simple example POM file for anyone that want to test this has been committed to the test suite.<br />
<a href="http://code.google.com/p/injection-extensions/source/browse/trunk/ejbunit/example-pom/pom.xml"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/injection-extensions/source/browse/trunk/ejbunit/example-pom/pom.xml</span></a><br />
PS: I have not verified the POM on another computer so please report any problems if you try it.<br />
<br />
</div>Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-22082211132342072012010-09-19T22:18:00.041+01:002010-09-27T13:04:15.767+01:00JSR 330 and @Inject<span style="font-weight: bold;">The background</span> (why the hell create a new IoC DI framework)<br />
First of i would like to explain that the initial intention from my side was to create a very simple InjectionContainer that could extend Spring/Guice when needed and be replaces as soon as Spring/Guice released their version that supports JSR-330.<br />
<br />
After waiting and not understanding how and when Spring and Guice was actually going to do this I started to play with the JSR 330 TCK. Once the TCK was implemented with the @Inject IoC I realized that this is quite powerful and that I wanted and needed extensions for it to be useful in a real world application.<br />
<div><br />
<div>The primary reason for this research from my side was to get a deep understanding of IoC DI and to finally implement a generic solution of the many "simplifications" I have always created in a application when I start to develop for a customer.</div><br />
<b>Basic project info/note</b><br />
I am a Maven2 addicted developer (its not all good but its worth it in the end), so all projects are Maven'ized and all releases are deployed to central Maven repository (thanks to Sonatype).<br />
<div><br />
Release 1.1.0 of Simple Java (with Config/i18n and Inject implementations bundled)</div><div>Maven Site report: <a href="http://java-simple-utils.googlecode.com/svn/reporting/index.html"><span class="Apple-style-span" style="color: blue;">http://java-simple-utils.googlecode.com/svn/reporting/index.html</span></a></div><div>Maven Cobertura Coverage report:<br />
<a href="http://java-simple-utils.googlecode.com/svn/reporting/inject/cobertura/index.html"><span class="Apple-style-span" style="color: blue;">http://java-simple-utils.googlecode.com/svn/reporting/inject/cobertura/index.html</span></a></div><div><br />
<b><span class="Apple-style-span" style="font-size: x-large;">The Different projects</span></b><br />
<br />
</div><div><b>- Config</b></div><div>Java properties has been the same for ages and are very not very useful. This is an extension to those properties with Generics and override (via System parameters) support.</div><div></div><div><b>- i18n</b></div><div>Java Format packages has a lot of features but they are sometimes hard to "find" and use in a good way. This package does that, it gathers the formatters and configures them in a more useful way. And it also fixes an irritating bug that Sun refuses to solve with the wrong blank-space being used as a thousand separator.</div><div><br />
</div><div><b><span class="Apple-style-span" style="font-weight: normal;"><span style="font-weight: bold;">- @Inject (the org.hrodberath.inject)</span><br />
The Injection framework implementation is a certified (not official yet though) version of the JSF 330 specification. The TCK test suite can be found in the code base.</span></b></div><div>For latest release: <a href="http://code.google.com/p/java-simple-utils/source/browse/tags/1.1.0/inject/src/test/java/test/org/hrodberaht/inject/SuiteJsr330TckUnitT.java"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/java-simple-utils/source/browse/tags/1.1.0/inject/src/test/java/test/org/hrodberaht/inject/SuiteJsr330TckUnitT.java</span></a></div><div><br />
</div><div>The @Inject framework today implements a small but similar feature set to Guice</div><div><br />
</div><div>Current feature set (in release 1.1.0)</div><div><ul><li>support for new and singelton scope<br />
- also thread and inheritable-thread but these are custom<br />
</li>
<li>support for javax.inject.Provider (very useful interface)<br />
</li>
<li>support for configuration of older non @inject friendly factories via the InjectionFactory<br />
</li>
<li>Has extensive registering support (with "inheritable" modules), this is the intended strong point of this framework.<br />
- Has scanning support<br />
- Has module "extends" support (like inheritance)<br />
- Simple System.out printing of active configuration<br />
</li>
</ul>Planned additions (in release 1.2.0)</div><div><ul><li>support for attaching older non @inject friendly variable factory via a VariableInjectionFactory<br />
</li>
<li>SPI support for adding custom @Inject and @PostConstruct and @Scope annotations</li>
<li>Service Configuration live exporting to XML<br />
</li>
<li>More and better support for multi module configuration "mixing" where the exact results of these mixes can be seen and understood easily.<br />
- Implement the @Extends annotation that was added in 1.1<br />
- Support multiple "extends" (multi inheritance)<br />
- Support advance configuration where support for standard / customer / custom variations are supported and easy to understand how to configure. Add some way to switch between these config variations.</li>
</ul><b>- Extensions for @Inject </b><b><span class="Apple-style-span" style="font-weight: normal;"><span style="font-weight: bold;">(the org.hrodberath.inject.extension)</span></span></b></div><div>I have planned the following extensions for release soon.</div><div><br />
</div><div><b>Ongoing features </b>(Has SNAPSHOT code that is close to working as intended)</div><div><br />
</div><div>Transaction (JDBC and JPA support, not JTA or XA)</div><div><ul><li>Uses EJB annotations with AspectJ<br />
</li>
<li>All transaction types from EJB implemented and supported<br />
</li>
<li>JPA --> JDBC shared connection support implemented<br />
</li>
<li>JDBC Helper package included<br />
</li>
<li>Junit Runner<br />
- Transacted and Container manager runner implemented<br />
</li>
<li>TODO: create more test suites for mixed transactions<br />
</li>
</ul><div>JSF (@Inject and Transaction supported JSF with no-config setup)</div><div><ul><li>JSF Injection Provider for @Inject implemented<br />
</li>
<li>OpenTransactionInView via a Filter is implemented<br />
</li>
<li>TODO: no config, need inspiration here: use a Annotation? naming?</li>
</ul></div><div><b>Planned features </b>(has no SNAPSHOTS or source code at this moment)</div><div><br />
</div><div>EJB 3.0 JUnit Framework (with Mocking help connected to mockito)<br />
<ul><li>Extreme bootstrap speed (less than 1s for a standard project with 100 services or so)</li>
<li>@Inject and Transaction support for an EJBJunitRunner built on @Runwith</li>
<li>Support for @EJB and @Resource</li>
<ul><li>Not entitys, use hypersonic with JPA for this, its loads simpler.</li>
</ul></ul>NetBeans RCP</div><div><ul><li>@Inject and Transaction support</li>
</ul></div><div>Eclipse RPC</div></div><div><ul><li>@Inject and Transaction support<br />
</li>
</ul>GWT (still unsure if this will be supported, as I don't "get it" GWT at this moment)</div><div><ul><li>@Inject and Transaction support<br />
</li>
</ul>Spring<br />
<div><br />
Connect the @Inject container to Spring managed beans to support the usage of any Spring bean inside the container.</div><div><ul><li>JMS Bean usage support verified<br />
</li>
<li>WebFlow beans support verified<br />
</li>
<li>RMI Bean usage support verified</li>
</ul></div><div><b>Example Application</b> for JSF (built on the superb JSF framework PrimeFaces)</div></div><div><br />
</div><div>This is intended as the showcase to prove that the coding can be done very fast and simple.</div><div>Points to sonatypes snapshot repository for the snapshot dependencies.</div><div><a href="http://code.google.com/p/web-showcases/"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/web-showcases/</span></a><br />
<br />
</div><div><b><u><span class="Apple-style-span" style="font-size: x-large;">Usage examples</span></u></b></div><div><br />
All projects are syncronized to central Maven and can be used without a extra addition of repository. Snapshots need the sonatype repository though,<br />
<a href="https://oss.sonatype.org/content/repositories/snapshots/"><span class="Apple-style-span" style="color: blue;">https://oss.sonatype.org/content/repositories/snapshots/</span></a><br />
<br />
See <a href="http://code.google.com/p/java-simple-utils/"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/java-simple-utils/</span></a><br />
<br />
<b>Simple Java Config</b><br />
<div>Wiki Guide: <a href="http://code.google.com/p/java-simple-utils/wiki/ConfigGuide"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/java-simple-utils/wiki/ConfigGuide</span></a></div><br />
<code><br />
public class AnyApplicationConfig extends ConfigBase {<br />
private static final String DEFAULT_CONFIG = "classpath:/basicConfig.properties";<br />
<br />
public static void initConfig() throws ParseException {<br />
initConfig(DEFAULT_CONFIG);<br />
}<br />
public static void initConfig(String resource) throws ParseException {<br />
String externalConfig = System.getProperty("config.externalfile");<br />
if(!StringUtil.isBlank(externalConfig)){<br />
// This initilized (reads) the config<br />
new AnyApplicationConfig(resource, externalConfig);<br />
}else{<br />
// This initilized (reads) the config<br />
new AnyApplicationConfig(resource, null);<br />
}<br />
}<br />
<br />
public interface ApplicationState {<br />
ConfigItem<String> A_STRING = new ConfigItem<String>(String.class, "anyapp.astring");<br />
ConfigItem<Date> A_DATE = new ConfigItem<Date>(Date.class, "anyapp.adate");<br />
ConfigItem<Integer> A_INTEGER = new ConfigItem<Integer>(Integer.class, "anyapp.ainteger");<br />
ConfigItem<Long> A_LONG = new ConfigItem<Long>(Long.class, "anyapp.along");<br />
}<br />
</code><br />
<br />
<b>Simple Java i18n</b><br />
<b><span class="Apple-style-span" style="font-weight: normal;">Wiki Guide:</span><span class="Apple-style-span" style="font-weight: normal;"> </span><span class="Apple-style-span" style="font-weight: normal;"><a href="http://code.google.com/p/java-simple-utils/wiki/i18n"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/java-simple-utils/wiki/i18n</span></a></span></b><br />
<br />
<code>// This mimics what can be done in a software using the profile provider.<br />
Locale testProviderLocale = new Locale("en", "US");<br />
LocaleProvider.setThreadLocaleProvider();<br />
// This mimics what can be done in a RequestFilter for example<br />
LocaleProvider.setProfile(new LocaleProfile(testProviderLocale));<br />
// All calls to the provider from now on in the current thread will return this locale<br />
LocaleProvider.getProfile().getLocale();<br />
<br />
// The following code would now follow this locale<br />
String testDate = "2010-01-01";<br />
Formatter<Date> formatter = Formatter.getFormatter(Date.class);<br />
Date aDate = formatter.convertToObject(testDate);<br />
String aStringDate = formatter.convertToString(aDate);<br />
</code><br />
<br />
<b>Simple Java Injection (@Inject)</b><br />
<div>Wiki Guide: <a href="http://code.google.com/p/java-simple-utils/wiki/Injection"><span class="Apple-style-span" style="color: blue;">http://code.google.com/p/java-simple-utils/wiki/Injection</span></a><br />
JSR 330 Annotation supported Container Configuration example:<br />
<br />
<code>InjectionRegisterJava registerJava = new InjectionRegisterJava();<br />
registerJava.register(new RegistrationModuleSimple() {<br />
public void registrations() {<br />
register(AnyService.class).annotated(DoNothing.class)<br />
.scopeAs(ScopeContainer.Scope.SINGLETON)<br />
.with(AnyServiceDoNothingImpl.class);<br />
register(AnyService.class)<br />
.registeredAs(SimpleInjection.RegisterType.FINAL)<br />
.with(AnyServiceDoSomethingImpl.class);<br />
}<br />
}<span class="Apple-style-span" style="font-family: monospace;">);</span><br />
</code></div><div><br />
JSF and JUnit example setup with Injection support<br />
<br />
<code>@InjectionContainerContext(ModuleContainerForTests.class)<br />
@RunWith(InjectionJUnitTestRunner.class)<br />
@TransactionAttribute<br />
public class TestJPATransactionManager {<br />
<br />
@Inject<br />
private TransactedApplication application;<br />
...<br />
}<br />
</code><br />
<br />
</div><div><br />
The details about this code is<br />
1. @InjectionContainerContext<br />
- This is the InjectionContainer JUnitRunner Container Context<br />
2. @RunWith<br />
- This is the JUnit annotation that connects the Container to JUnit and adds transaction support when needed.<br />
3. @TransactionAttribute<br />
- This is the EJB3 annotation and it activates transaction support for all tests in this class. InjectionJUnitTestRunner is the class that actually does this.<br />
<br />
A JSF Bean with Injected services<br />
<br />
<code>@ManagedBean(name = "personBean")<br />
@RequestScoped<br />
public class WebInjectBean {<br />
<br />
@Inject<br />
private ServiceInject serviceInject;<br />
}<br />
</code><br />
<br />
Config in web.xml for this to work<br />
<code><context-param><br />
<param-name>com.sun.faces.injectionProvider</param-name><br />
<param-value>org.hrodberaht.webexample.web.ApplicationJSFResolverImpl</param-value><br />
</context-param><br />
</code><br />
<br />
A very simple example Application with the Injected transaction manager for a JPA configured application.<br />
See the <a href="http://code.google.com/p/injection-extensions/source/browse/trunk/transactions/src/test/java/test/com/hrodberaht/inject/extension/transaction/example/JPATransactedApplication.java"><span class="Apple-style-span" style="color: blue;">Google Code link</span></a><br />
<br />
<code>public class JPATransactedApplication implements TransactedApplication {<br />
<br />
@Inject<br />
private Provider<EntityManager><br />
<br />
@TransactionAttribute<br />
public void createPerson(Person person) { <br />
EntityManager em = entityManager.get();<br />
em.persist(person);<br />
}<br />
}</code><br />
<br />
<div>The Application with the Injected transaction manager for a JDBC configured application.<br />
See the <a href="http://code.google.com/p/injection-extensions/source/browse/trunk/transactions/src/test/java/test/com/hrodberaht/inject/extension/transaction/example/JDBCHelperApplication.java"><span class="Apple-style-span" style="color: blue;">Google Code link</span></a></div><code><br />
public class JDBCHelperApplication {<br />
<br />
@Inject<br />
private JDBCService jdbcService;<br />
<br />
public void createAdvancedModel(AdvanceModel advanceModel) {<br />
Insert insert = jdbcService.createInsert("advanceModel");<br />
insert.field("id", advanceModel.getId());<br />
updateAllFields(advanceModel, insert);<br />
jdbcService.insert(insert);<br />
}<br />
<br />
public AdvanceModel findAdvancedModel(Long id) {<br />
String sql = "select * from AdvanceModel where id = ?";<br />
return jdbcService.querySingle(sql, new AdvanceModelIterator(), id);<br />
}<br />
... <br />
}<br />
</code><br />
<br />
Example configuration from the proof of concept project <br />
See <a href="http://code.google.com/p/web-showcases/source/browse/trunk/inject/src/main/java/org/hrodberaht/webexample/service/ServiceContainer.java"><span class="Apple-style-span" style="color: blue;">Google Code link</span></a><br />
<code><br />
InjectionRegisterScan registerJava = new InjectionRegisterScan();<br />
registerJava.registerBasePackageScan("org.hrodberaht.webexample.service.impl");<br />
<br />
InjectionRegisterModule register = new InjectionRegisterModule(registerJava); <br />
final TransactionManager transactionManager =<br />
new TransactionManagerJPAImpl(Persistence.createEntityManagerFactory("web-example-jpa"));<br />
register.register(new TransactionManagerModule(transactionManager, register));<br />
</code><br />
<br />
</div></div></div>Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com2tag:blogger.com,1999:blog-2282018140794370775.post-8079617143011861282010-05-30T16:54:00.009+01:002010-09-17T07:05:17.305+01:00Simple Java soon ready for 1.0 release.So I have finally found some time to code for this and have created almost all I need to feel like this is a real 1.0 release.<br /><br />Done: <br /><br />1. Changed name of simplecontainer (IoC) to SimpleInject (injection).<br />2. Divided the code into more modules (core, i18n, ejb2x, inject)<br />3. Inject: Added support for Injection (using javax.inject.*)<br />4. Inject: Added JSR 330 as a verification of the inject module (using the TCK tests)<br />- Thanks <a href="http://code.google.com/p/kouinject/">http://code.google.com/p/kouinject/</a> for the inspiration to do this.<br />5. Updated the Maven2 (pom's) to give a good report that I uploaded to <a href="http://java-simple-utils.googlecode.com/svn/reporting/index.html">google-code-svn</a> (had some issues here, might talk about those in a seperate blogpost)<br /><br />Left to do:<br /><br />1. ejb2x: Finish the ejb2x module (servicelocator) or remove it.<br />2. Core: Write tests for the Core Util packages<br />3. Injection: needs more structure for and if it will "implement" Spring and Guice extensions.<br />4. i18n: Finish the formatters<br />5. i18n: Add language services for simpler usage of Formatter/LocaleProfile<br /><br /><span style="font-weight:bold;">Comments</span><br /><br />It was a lot easier to create an injection framework than I though. The verification tests helped alot to realise how to actually perform the code and I also looked a lot on the Kouinject project (he had the same coding style as I do so it was easy to read, atleast compared to Guice and Spring IoC).Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-77104582500846941182010-03-31T15:44:00.008+01:002010-03-31T15:59:47.484+01:00Private/Public project timeWell i have been away for a long time from this blog and the reason is i have done to many interresting Java experiences the last few years but forgot to share them :)<br /><br />What have i done -- well, i have coded my first project doing it TDD. We did this using Java 1.5 SDK/Spring/JUnit/Clover. After about 3 months of struggling with the TDD concept the "click" finally came and I can no longer code without it ... and can't understand how I managed without it before.<br /><br />From the experience of the TDD and JUnit came a bigger interest in the minor areas of Java code as when doing tests i can finally found them interesting enough to care ;)<br /><br />This interest has now grown into two simplification and extension framework i will try to create as the normal way of doing this is not easy enough.<br /><br /><a href="http://code.google.com/p/java-simple-utils/">http://code.google.com/p/java-simple-utils/</a><br /><a href="http://code.google.com/p/java-simple-utils/">http://code.google.com/p/spring-simple-extensions</a><br /><br />Both these are about making a Java developers everyday easier and more effective.<br /><br />I will create a few more posts some time soon on why these projects have been started.<br /><br />This is just a notice that they exist.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-43641946672048849442007-05-14T07:43:00.000+02:002007-05-19T07:19:07.089+02:00JavaOne 2007 day 4, Hibernate SearchHi this is the last day of JavaOne and my first session is Hibernate-Search, I was interrested in this due to the fact that it I normally have trouble using different advanced search-patterns in my apps and these tend to be slow(execution-time) and hard to develop.<br /><br />The presentation covered all of this, and explained the problems with text-based search for a domainmodel. I feelt like i understood all of this and the problems with it, the solution for these mismatches between objectmodel and indexed-text-search was made available only for Hibernate users. This was my problem, i was hoping for something more generic that i could implement on the DAO model. I will mail and ask about this, or it might finnaly be time to Contribute something to a openproduct, who knows.<br /><br />The needed frameworks for this to work is Hibernate, Hibernate-Serarch and Lucene (the searchengine).<br /><br />The solution is this, its possible to markup our objectmodel with indexing annotations (yes another annotation). These indexes are then mapped to the objectmodel using the model keys. You then use the IndexedSearch (or what it was named) to execute the query to the Lucene index. The result is handeled in the same way as a normal Hibernate-query.<br /><br />see <a href="http://www.hibernate.org/410.html">Hibernate Search</a><br /><br />My thought on this are, its really elegant, but lacks integration for non Hibernate users. I would use it if possible!Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-12611699973023397212007-05-11T08:48:00.000+02:002007-05-14T22:25:21.537+02:00JavaOne 2007 day 3, Garbage Collection friendly coding.This was i nice and fun session.<br /><br />If you just want to remember one thing from this its: <span style="font-weight: bold;">DONT use finalize method for anything in java</span>. The other things are normal memmory misuse stuff.<br />1. Always declare an Collection(impl) to an approx size of what u want, it not as sheap as one thinks to resize collections.<br />2. Small, immutable objects are really fast for the GC. Dont be clever and create them outside the loop or the normal life scope of the variable. Do not create generic variables that are nullified and reused in a method or loop.<br />3. Be aware of actionlisteners these keep reference to our instance and can create memmory leaks. This kind of behaivure is not as uncommon as one might think, even others than the AWT-tam make these kinds of programs.<br />4. If unclear dont use finalize, ever. DONT use them. If something like this i really needed, no other way can solve the problem, then use Reference classes(java.lang.ref.PhantomReference) and wrap the instance that needs this.<br /><br />There were more tips, but i cant remember them all.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-75004534690776210132007-05-11T08:39:00.000+02:002007-05-14T22:26:18.396+02:00JavaOne 2007 day 3, Project Phobos.This is another new engine/framework for the java plattform. It is used so that programmers are able to run scripts on serverside and client side. Phobos supports any scripting language that the Script-engines for java does, but its created for javascript. The idea is to use javascript on the server aswell as one the client. This is suppose to be simpler, i dont agree. All the great things about java are all lost, this is just a runtime for the scripties, im not one of them.<br /><br />The session showed how easy and fast it is to create and manipulate the client and the server. This problem is that i dont like the way the server "feels" like the client. Another thing is that i normally dislike weaktyped languages, the way i like it is on client where i just use it to present the alread trongtyped java-classes. The only framework for this that i have liked so far is DWR. That i like it dones not remove the good parts of java and replace them, it just extendes them and make them visible to javascript.<br /><br /><br />Phobos would not recommended by me to solve any level of software creation. Harch but its just my view of it.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com2tag:blogger.com,1999:blog-2282018140794370775.post-14142934685089721302007-05-11T07:41:00.000+02:002007-05-14T23:07:19.346+02:00JavaOne 2007 day 3, Seam and SOA.Another session by Gavin King, i really like this guys ideas and energy. First of all there was a change of presentation, the SOA in seam did not work yet.<br /><br />So instead we got Seam development using JBoss IDE(or some similar IDE). Gavin showed how to create a projekt using seam/facelets and some faces extensions, Richfaces and ajax4jsf. Really pointed to the ability to test things in the real context. This is part of seam.<br />The demo continued creating a application that used some UI:s from Richfaces and it showed how fast and easily u can create a interactive program today.<br /><br />So what is seam?<br />I really dont know all yet but this is some of it.<br /><ol><li>A way to get a faces/jpa and some application up and runnig requiring minimum configuration.</li><li>Minimize the configuration to use jsf.</li><li>Create a common plattform for the Bean-context. Supports a new scope called conversation. Conversation is something in between Session and request, but is more useful than session. It is useful for page-flows, tabs and similar parts of the application.</li><li>A generation-tool that can easily create boiler-plate code for your new application.</li><li>A way to get out of the configuration-hell of all the new frameworks, Seam uses anotations for most of this.<br /></li><li>A Security model implementated using anotations. Make it easy to use security on your application. Can be simple or advanced using roles etc.</li><li>A validation support using Hibernate-validation through anotations.<br /></li></ol><br />There is propably more but i did not get it from this.<br /><br />I later bumped into Gavin(or i walked to Jboss and there he was) at the Pavilion and asked about the validation support and if they have any plans to support validation depending on the state of the bean that is validated. For example if a IncidentReport is filed a title is all that is needed, later when a user works on the report it suddenly have specific data that might be required and before its closed some other data might be needed.<br /><br />This gives a more complex usage for Validation, this is something that i usually have to do, i cant be alone!? A way to solve this could be to create a Validation typebound anotation. Like the one Bob Lee presented in the Webbeans session.<br /><br />@Validation @IncidentReportValidation("#incidentState")<br />Date solutionDate;<br /><br />The variable in the custom-validation-anotation is a instance variable in the current Bean.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com2tag:blogger.com,1999:blog-2282018140794370775.post-30628297275890373572007-05-11T07:36:00.000+02:002007-05-14T22:20:47.123+02:00JavaOne 2007 day 3, Glassfish V2This was a presentation of the new stuff done in Glassfish V2. A big screwup from my side, i was looking for a session that demoed and used the Glassfish AS. This just explained the clustering(35mins) and touched other new integrations. Not what the doctor ordered...Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com0tag:blogger.com,1999:blog-2282018140794370775.post-50350936009767197712007-05-11T07:21:00.000+02:002007-05-11T07:36:45.573+02:00JavaOne 2007 day 3, SuperpackageThis day started with superpackages. This is a pretty interresting way to finally use the java-packages the way alot of people, me included, though it should work. The superpackage is suppose to hide your package from other "modules", and then you are suppose to make the ones u want "public" outside the superpackage. The proposal is that a file called super-package.java is created in the classpath and this is then compiled into a sort of rights controller for your package. The package is then hidden/exported according to your wishes. The example is (cant remember syntax exactly)<br /><br />superpackage jdk{<br /> member java.net.*<br /> member sun.net.*<br /><br /> export java.net.*<br />}<br /><br /><br />this includes the java.net and sun.net in the superpackage and then exports the java.net this means that any program that uses this module can only use the java.net. The sun.net is an internal packade, not possible to use for thirdparty products.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-23587483005804802852007-05-11T07:00:00.000+02:002007-05-11T07:20:49.501+02:00JavaOne 2007 day 2, part 2The jMaki is a very interresting Ajax-portable/pluggable framework. It hides the underlying implementation of Dojo, Yahoo, and some. this makes really easy to create and change ajax features. It also has plugins for Netbeans and Eclipse. In short the jMaki is framework i would recommend for doing Ajax if you cant have the Ajax4jsf due to lack of JSF.<br /><br />Spring/<a href="http://www-128.ibm.com/developerworks/library/specification/ws-sca/">SCA</a> (Service Component Architecture), this was semi-interresting. SCA is a large framework for creating really large applications. It now has integration with Spring so that different parts of an SCA application can be done using Spring. This only feels useful for really large applications with the need for rally clean separation between different parts. Not saying this is not a useful arhchitecture, just that its a LARGE one.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-10488905242690839832007-05-10T00:24:00.000+02:002007-05-11T07:20:26.672+02:00JavaOne 2007 day 2, part 1This day started out great, a nice showcase of the Oracle Middleware suit and some demos of the way Oracle is thi8nking about Web&JEE. The demos covered JSF(using ADF) and BPEL, the demo showed great stuff that can be made easily, but don't be fooled this still requires that you have a well defined architechture and that you create and upgrade old stuff accordingly.<br /><br />The first session was WebBeans, this was done bye an overly active Gavin King and his spec-lead-teammate Bob Lee. This was some interresting work in progress, it addresses the problem of different Application Contexts in different frameworks, creating a new Application Context. But the idea is that this should replace the current ones used i Java, for example the JSF managed beans and the EJB3 beans. Thereby creating a common way of declaring contextual dependencies. Seams promising :)<br /><br />To continue later... jMaki and Spring/SCA.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com1tag:blogger.com,1999:blog-2282018140794370775.post-79190936929926833982007-05-09T08:15:00.000+02:002007-05-11T07:20:15.158+02:00JavaOne 2007 day 1The first day has finished. It been inspiring and i have been to some interresting sessions.<br />My schedule this day was<br /><ul><li>General-session, key-note.<br /></li><li>Java-SE: present and future.</li><li>General session: Java platform, future.</li><li>Java-Puzzlers.</li><li>Closures.</li><li>JPA, tips and tricks.<br /></li></ul>The most compelling yet on my schedule was closures. Its a new java language-feature planed for SE-7, read more at <a href="http://gafter.blogspot.com/2006/08/closures-for-java.html">here</a>. The Java-SE session was nice and sadly most of it was duplicated in genereal-session, java platform. Java-puzzler was nice and fun. The JPA, tips and tricks was not really well done and i was slightly bored, this was common tips that can be found on any Hibernate-community. I did not learn anything new from this.<br /><br />The key-note was really well done, some mishaps as always but all done with the style. Liked the idea of JavaFX, but really dont think that there exists anything at this moment, some Alpha libs and technerds that have a cool idea.<br /><br />Was at the Pavilion getting free drinks and walked into the developers of JMaki, this is an interresting way of approaching Ajax, asked about plugability and ease of rendering changes. This was not a priority nore thought about yet.<br /><br />That was it, im hoping for more stuff next day.. ce ya soon.Anonymoushttp://www.blogger.com/profile/13914283476707945489noreply@blogger.com0