Sunteți pe pagina 1din 17

Preface Targeted audience Get the code Why Arquillian?

The first test Creating the archive Create the archive using ShrinkWrap Create the archive using maven Configuring Arquillian Configuring the runtime modes of arquillian Managed container config Embedded container config Including persistency (extension) Creating test data manually Creating test data by SQL script Creating test data by means of JSON/XML (or YML) Some kind of round trip

Copyright and all intellectual property belongs to Brockhaus Group

Preface
This is a paper provided by Brockhaus Group for free. All content was checked and all code was tested carefully; in case of questions of suggestions to improve we will be happy to receive you email at: getKnowledge@brockhaus-group.com

Targeted audience
People interested in using Arquillian with a sound knowledge in Java EE technologies, this paper wont explain the details of the Java EE platform. The examples have been tested using JBoss EAP 6.1.1 and the H2 database.

Get the code


All code used for this examples can be found here (zipped Maven project): ftp:www.brockhaus-gruppe.de /getKnowledge/arquillian user: ftpguest pwd: ftpguest789

Download, unpack, youre done

Copyright and all intellectual property belongs to Brockhaus Group

Why Arquillian?
Testing of complex. multi-layered Java EE applications still is pretty difficult as in many cases the services offered by the container needs to be in place. Just think of CDI, various annotations, datasources and so on. One might say, that mocking might replace these concerns and partially this is correct but still there is a gap between what is provided and what is expected. Arquillian tries to close this gap by: maintaining the life-cycle of of a container (and despite the fact Arquillian is a JBoss project there are more containers supported than just JBoss1 ) combining all resources to a deployable artifact and providing some facilities to deploy these

Several plugins are available: Drone (includes Selenium to test the UI as well) Persistence (to create test data out of various formats like XML and JSON) Jacoco (to get some metrics about code coverage)

The first test


At first sight an arquillian test doesnt differ much from a regular unit test:
@ R u n W i t h ( A r q u i l l i a n . c l a s s ) p u b l i cc l a s sC a l c u l a t o r A r q u i l l i a n T e s t{ / /m u s tb e@ E J B ,c a n ' tb e@ I n j e c t !I fy o ud o n ' tb e l i e v e ,y o uc a nt r y. . . @ E J B p r i v a t eC a l c u l a t o rc a l c ; / /t h eo n ey o un e e du n d e ra n yc i r c u m s t a n c e @ D e p l o y m e n t p u b l i cs t a t i cA r c h i v e < ? >c r e a t e T e s t A r c h i v e ( ){ / /c h e c kt h eh e l p e rf o rd e t a i l so fh o wt og e tt h et h i n g sp a c k a g e d A r c h i v e < ? >a r c h i v e=A r c h i v e H e l p e r . g e t A r c h i v e ( ) ; r e t u r na r c h i v e ; } @ T e s t p u b l i cv o i dt e s t A d d N u m b e r s ( ){ f l o a tr e s u l t=c a l c . a d d ( 1 ,2 ) ; A s s e r t . a s s e r t E q u a l s ( 3 ,r e s u l t ,0 ) ; } }

The only remarkable things are the @ R u n W i t h ( A r q u i l l i a n . c l a s s )and the @ D e p l o y m e n t annotations. Regarding the @ R u n W i t hannotation we should not spent too many words, The @ D e p l o y m e n t annotation looks much more interesting as it seems as if some groundwork is laid in there.
1

This paper will cover JBoss EAP 6.1.1 only

Copyright and all intellectual property belongs to Brockhaus Group

Creating the archive


One of the main purposes of the method annotated with @ D e p l o y m e n tis to provide the artifact to be tested.
/ /t h eo n ey o un e e du n d e ra n yc i r c u m s t a n c e @ D e p l o y m e n t p u b l i cs t a t i cA r c h i v e < ? >c r e a t e T e s t A r c h i v e ( ){ / /c h e c kt h eh e l p e rf o rd e t a i l so fh o wt og e tt h et h i n g sp a c k a g e d A r c h i v e < ? >a r c h i v e=A r c h i v e H e l p e r . g e t A r c h i v e ( ) ; r e t u r na r c h i v e ; }

We have made use of some kind of helper to create the archive as there are two options which will be explained in more detail.

Create the archive using ShrinkWrap


Using this handy tool, any JavaEE deployable artifact can be created on the fly.
/ * *d o i n gi tt h eh a r dw a y. . .g u e s sy o uw o n ' tl i k ei ta sE V E R Yc l a s sp l u sr e l a t e ds t u f fn e e d st ob e s p e c i f i e d* / p r i v a t es t a t i cA r c h i v e < ? >g e t A r c h i v e M a n u a l l y ( ){ / /c r e a t i n ga r c h i v em a n u a l l y J a v a A r c h i v ea r t i f a c t= S h r i n k W r a p . c r e a t e ( J a v a A r c h i v e . c l a s s ,A R C H I V E _ N A M E ) . a d d P a c k a g e ( C a l c u l a t o r . c l a s s . g e t P a c k a g e ( ) ) . a d d P a c k a g e ( C a l c u l a t o r S e r v i c e . c l a s s . g e t P a c k a g e ( ) ) . a d d P a c k a g e ( F o o S e r v i c e B e a n . c l a s s . g e t P a c k a g e ( ) ) . a d d P a c k a g e ( F o o . c l a s s . g e t P a c k a g e ( ) ) . a d d P a c k a g e ( F o o S e r v i c e . c l a s s . g e t P a c k a g e ( ) ) . a d d A s R e s o u r c e ( " M E T A I N F / p e r s i s t e n c e . x m l " ) . a d d A s R e s o u r c e ( " M E T A I N F / b e a n s . x m l " ) ; / /s ow em i g h tw r i t ei tf o rf u r t h e ri n s p e c t i o n i f( W R I T E _ A R C H I V E ){ a r t i f a c t . a s ( Z i p E x p o r t e r . c l a s s ) . e x p o r t T o ( n e wF i l e ( " D : / P r o j e k t e / f f z / t m p / "+A R C H I V E _ N A M E ) , t r u e ) ; } r e t u r na r t i f a c t ; }

As you can see easily, every class, deployment descriptor and so on is added on the fly. For your convenience the code to write the created archive to the file system is included. This was proven helpful just to check, whether everything is included properly. IMHO the only disadvantage might be the vast amount of classes in an average project so we were pretty confident there must be another way of getting the archive.

Create the archive using maven


Copyright and all intellectual property belongs to Brockhaus Group 4

One of the de-facto standards in build tools is maven, so why not making use of the famous m v n c l e a np a c k a g eto get the things done? The following snippet describes how to get the archive loaded into arquillian.
/ * *m a v e nd i di tf o ru s. .w ej u s th a v et or e a dt h ef i l e* / p r i v a t es t a t i cA r c h i v e < ? >g e t A r c h i v e F r o m F i l e ( ){ J a v a A r c h i v ea r t i f a c t=S h r i n k W r a p . c r e a t e ( Z i p I m p o r t e r . c l a s s ,A R C H I V E _ N A M E ) . i m p o r t F r o m ( A R C H I V E _ F I L E ) . a s ( J a v a A r c h i v e . c l a s s ) ; r e t u r na r t i f a c t ; }

Copyright and all intellectual property belongs to Brockhaus Group

Configuring Arquillian
The initial thing we should tell Arquillian is where to find JBoss, therefore a small xml file named a r q u i l l i a n . x m lneeds to put aside of your application.

< ? x m lv e r s i o n = " 1 . 0 " ? > < a r q u i l l i a nx m l n s : x s i = " h t t p : / / w w w . w 3 . o r g / 2 0 0 1 / X M L S c h e m a i n s t a n c e " x m l n s = " h t t p : / / j b o s s . o r g / s c h e m a / a r q u i l l i a n " x s i : s c h e m a L o c a t i o n = " h t t p : / / j b o s s . o r g / s c h e m a / a r q u i l l i a n h t t p : / / j b o s s . o r g / s c h e m a / a r q u i l l i a n / a r q u i l l i a n _ 1 _ 0 . x s d " > < d e f a u l t P r o t o c o lt y p e = " S e r v l e t3 . 0 "/ > < ! f o ra d d i t i o n a li n f os e e : h t t p s : / / d o c s . j b o s s . o r g / a u t h o r / d i s p l a y / A R Q / C o n t a i n e r + c o n f i g u r a t i o n > < c o n t a i n e rq u a l i f i e r = " j b o s s "d e f a u l t = " t r u e " > < c o n f i g u r a t i o n > < ! -t h i sp o i n t st oa ne x i s t i n gi n s t a l l a t i o n ,t a k ec a r ef o r e v e r y t h i n gt ob es e tu pp r o p e r l y ,e s p .t h ed a t a s o u r c e s> < p r o p e r t y n a m e = " j b o s s H o m e " > D : \ a b c \ x y z \ j b o s s e a p 6 . 1 < / p r o p e r t y > < / c o n f i g u r a t i o n > < / c o n t a i n e r > < / a r q u i l l i a n >

Be aware of JBoss configured according to your needs, no topic or queue, no data source means no topic or queue or data source available at testing time. The runtime selection is explained in more detail here.

Copyright and all intellectual property belongs to Brockhaus Group

As we suppose youre making use of maven, here are the dependencies you will need to make the test run:
< d e p e n d e n c i e s > < d e p e n d e n c y > < g r o u p I d > j a v a x < / g r o u p I d > < a r t i f a c t I d > j a v a e e a p i < / a r t i f a c t I d > < v e r s i o n > 6 . 0 < / v e r s i o n > < / d e p e n d e n c y > < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . s p e c < / g r o u p I d > < a r t i f a c t I d > j b o s s j a v a e e 6 . 0 < / a r t i f a c t I d > < v e r s i o n > 3 . 0 . 2 . F i n a l < / v e r s i o n > < t y p e > p o m < / t y p e > < / d e p e n d e n c y > < d e p e n d e n c y > < g r o u p I d > j u n i t < / g r o u p I d > < a r t i f a c t I d > j u n i t < / a r t i f a c t I d > < v e r s i o n > 4 . 1 1 < / v e r s i o n > < / d e p e n d e n c y > < d e p e n d e n c y > < g r o u p I d > x a l a n < / g r o u p I d > < a r t i f a c t I d > x a l a n < / a r t i f a c t I d > < v e r s i o n > 2 . 7 . 1 < / v e r s i o n > < s c o p e > p r o v i d e d < / s c o p e > < / d e p e n d e n c y > < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a r q u i l l i a n . j u n i t < / g r o u p I d > < a r t i f a c t I d > a r q u i l l i a n j u n i t c o n t a i n e r < / a r t i f a c t I d > < v e r s i o n > 1 . 1 . 2 . F i n a l < / v e r s i o n > < s c o p e > t e s t < / s c o p e > < / d e p e n d e n c y > < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a r q u i l l i a n . p r o t o c o l < / g r o u p I d > < a r t i f a c t I d > a r q u i l l i a n p r o t o c o l s e r v l e t < / a r t i f a c t I d > < v e r s i o n > 1 . 1 . 2 . F i n a l < / v e r s i o n > < s c o p e > t e s t < / s c o p e > < / d e p e n d e n c y > < ! -i fn o tu s i n ga n yp r o f i l e> < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a s < / g r o u p I d > < a r t i f a c t I d > j b o s s a s a r q u i l l i a n c o n t a i n e r m a n a g e d < / a r t i f a c t I d > < v e r s i o n > 7 . 2 . 0 . F i n a l < / v e r s i o n > < / d e p e n d e n c y >

Yu might want to check the poms included in the zip files ...

Copyright and all intellectual property belongs to Brockhaus Group

Now you are almost ready to run your first Arquillian test ( m v nc l e a nt e s t/ or using eclipse).

Configuring the runtime modes of arquillian


In the very beginning we should have made the decision of how to use Arquillian as several modes are supported: Remote container Managed container Will run in a separate VM, probably on a remote machine. Arquillian starts and stops the container (which must be installed locally). Will run in the same VM, Arquillian will manager the container.

Embedded container

Using JBoss, all modes need a JBoss to be installed and localized by a r q u i l l i a n . x m l . We suggest the following approach: Make use of maven to build your archives and include the necessary dependencies within the POM. Whatever mode you might choose, put the relevant configuration into a decent profile. We have tested the managed and embedded option only.

Managed container config


within <profiles> </profiles> tags obviously:
< p r o f i l e > < i d > a r q i l l i a n j b o s s a s m a n a g e d < / i d > < d e p e n d e n c i e s > < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a s < / g r o u p I d > < a r t i f a c t I d > j b o s s a s a r q u i l l i a n c o n t a i n e r m a n a g e d < / a r t i f a c t I d > < v e r s i o n > 7 . 2 . 0 . F i n a l < / v e r s i o n > < s c o p e > t e s t < / s c o p e > < / d e p e n d e n c y > < / d e p e n d e n c i e s >

Copyright and all intellectual property belongs to Brockhaus Group

< / p r o f i l e >

Embedded container config


< ! -f o re m b e d d e do p t i o ns e e :h t t p s : / / c o m m u n i t y . j b o s s . o r g / t h r e a d / 2 3 6 5 6 2 q u o t e d :T h eo n l yt h i n gE m b e d d e dd o e si st os t a r tu pt h eC o n t a i n e rw i t h i n t h es a m eJ V M .I ts t i l ln e e d st ok n o ww h e r et h ej a r s / c o n f i g u r a t i o ni s ,v i a j b o s s H o m e( s e ea r q u i l l i a n . x m l ) .> < p r o f i l e > < i d > a r q u i l l i a n j b o s s a s e m b e d d e d < / i d > < d e p e n d e n c i e s > < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a s < / g r o u p I d > < a r t i f a c t I d > j b o s s a s a r q u i l l i a n c o n t a i n e r e m b e d d e d < / a r t i f a c t I d > < v e r s i o n > 7 . 2 . 0 . F i n a l < / v e r s i o n > < / d e p e n d e n c y > < / d e p e n d e n c i e s > < b u i l d > < p l u g i n s > < p l u g i n > < g r o u p I d > o r g . a p a c h e . m a v e n . p l u g i n s < / g r o u p I d > < a r t i f a c t I d > m a v e n s u r e f i r e p l u g i n < / a r t i f a c t I d > < c o n f i g u r a t i o n > < s y s t e m P r o p e r t y V a r i a b l e s > < ! -n e e d st ob ed e f i n e df o rt h ee m b e d d e do p t i o n> < j a v a . u t i l . l o g g i n g . m a n a g e r > o r g . j b o s s . l o g m a n a g e r . L o g M a n a g e r < / j a v a . u t i l . l o g g i n g . m a n a g e r > < / s y s t e m P r o p e r t y V a r i a b l e s > < / c o n f i g u r a t i o n > < / p l u g i n > < / p l u g i n s > < / b u i l d > < / p r o f i l e >

See the embedded config running using maven (m v nc l e a nt e s tP a r q u i l l i a n j b o s s a s e m b e d d e d )

Copyright and all intellectual property belongs to Brockhaus Group

If you want to make an embedded test run from eclipse you have to add a system property or configure properly within the POM:

Copyright and all intellectual property belongs to Brockhaus Group

10

Copyright and all intellectual property belongs to Brockhaus Group

11

Including persistency (extension)


The full monty of integration testing is provided using persistency. In the field of testing, persistency first of all means generating test data for later use or testing the creation of data (which is almost synonymous). There are several options to get this task done, namely: create test data manually using SQL scripts and a pre-filled database. create/delete test data manually within the @Before annotated method create/delete test data by SQL script create/delete test data by means of XML / JSON

Creating test data manually using a pre-filled database


An easy task, just make use of the following:
I N S E R TI N T OF O O( I D ,C R E A T I O N T I M E,D E S C R I P T I O N )V A L U E S ( 9 9 8 ,' 2 0 1 4 0 2 1 81 2 : 3 7 : 0 6 . 7 3 ' ,' F o o B a r O n e ' ) ; I N S E R TI N T OF O O( I D ,C R E A T I O N T I M E,D E S C R I P T I O N )V A L U E S ( 9 9 9 ,' 2 0 1 4 0 2 1 81 2 : 3 7 : 0 6 . 7 3 ' ,' F o o B a r T w o ' ) ;

Creating test data manually


The most obvious way of creating (and deleting) test data during a test is doing so within the setUp() and tearDown() methods of a test2:
@ R u n W i t h ( A r q u i l l i a n . c l a s s ) p u b l i cc l a s sF o o S e r v i c e A r q u i l l i a n T e s t{ @ E J B p r i v a t eF o o S e r v i c ef o o S e r v i c e ; p r i v a t eF o of o o=n e wF o o ( ) ; @ D e p l o y m e n t p u b l i cs t a t i cA r c h i v e < ? >c r e a t e T e s t A r c h i v e ( ){ A r c h i v e < ? >a r c h i v e=A r c h i v e H e l p e r . g e t A r c h i v e ( ) ; r e t u r na r c h i v e ; }

@ B e f o r e
p u b l i cv o i ds e t U p ( ){ f o o . s e t C r e a t i o n T i m e ( n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( ) ) ) ; f o o . s e t D e s c r i p t i o n ( " F o oB a r " ) ; f o o . s e t C r e a t i o n T i m e ( n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( ) ) ) ;

f o o=f o o S e r v i c e . c r e a t e F o o ( f o o ) ;
} @ T e s t p u b l i cv o i dt e s t F i n d A l l ( ){ L i s t < F o o >h i t s=f o o S e r v i c e . f i n d A l l F o o s ( ) ; A s s e r t . a s s e r t T r u e ( h i t s . s i z e ( )>0 ) ; }
2

more precisely: the methods annotated by @BeforeXxx and @AfterXxx


Copyright and all intellectual property belongs to Brockhaus Group 12

@ T e s t p u b l i cv o i dt e s t F i n d F o o s E a r l i e r T h a n ( ){ D a t ef u t u r e=n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( )+1 0 0 0 0 0 ) ; L i s t < F o o >h i t s=f o o S e r v i c e . f i n d F o o s E a r l i e r T h a n ( f u t u r e ) ; A s s e r t . a s s e r t T r u e ( h i t s . s i z e ( )>0 ) ; }

@ A f t e r
p u b l i cv o i dt e a r D o w n ( ){ L i s t < F o o >h i t s=f o o S e r v i c e . f i n d A l l F o o s ( ) ; f o r( F o of o o:h i t s ){

f o o S e r v i c e . d e l e t e F o o ( f o o ) ;
} } }

Obviously this approach only works well for not-so-complicated objects and associations.

Creating test data by SQL script


Maybe your individual preferences are with SQL and so you want to get the things done by a customized SQL script. Arquillian supports in doing so by annotations like @ A p p l y S c r i p t B e f o r eand @ C r e a t e S c h e m a . We will focus on the first one as we have left schema creation to JPA/Hibernate. WARNING: At the time of writing this paper, we have the Alpha6 release of the framework mentioned, there are several changes even in the names of the annotations which might drive you crazy. Even the Arquillian book available is not up to date. Dont forget to include the extensions into your POM:
< d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a r q u i l l i a n . e x t e n s i o n < / g r o u p I d > < a r t i f a c t I d > a r q u i l l i a n p e r s i s t e n c e a p i < / a r t i f a c t I d > < v e r s i o n > 1 . 0 . 0 . A l p h a 6 < / v e r s i o n > < / d e p e n d e n c y > < d e p e n d e n c y > < g r o u p I d > o r g . j b o s s . a r q u i l l i a n . e x t e n s i o n < / g r o u p I d > < a r t i f a c t I d > a r q u i l l i a n p e r s i s t e n c e i m p l < / a r t i f a c t I d > < v e r s i o n > 1 . 0 . 0 . A l p h a 6 < / v e r s i o n > < / d e p e n d e n c y >

Presume we have the following script:


I N S E R TI N T OF O O( I D ,C R E A T I O N T I M E,D E S C R I P T I O N )V A L U E S ( 9 9 8 ,' 2 0 1 4 0 2 1 81 2 : 3 7 : 0 6 . 7 3 ' , ' F o o B a r O n e ' ) ; I N S E R TI N T OF O O( I D ,C R E A T I O N T I M E,D E S C R I P T I O N )V A L U E S ( 9 9 9 ,' 2 0 1 4 0 2 1 81 2 : 3 7 : 0 6 . 7 3 ' , ' F o o B a r T w o ' ) ;

Put the script into the / s r c / t e s t / r e s o u r c e s / s c r i p t sfolder of your maven project and annotate the class accordingly:

Copyright and all intellectual property belongs to Brockhaus Group

13

@ R u n W i t h ( A r q u i l l i a n . c l a s s )

@ A p p l y S c r i p t B e f o r e ( " s c r i p t s / C r e a t e F o o . s q l " )
p u b l i cc l a s sF o o S e r v i c e A r q u i l l i a n S Q L S c r i p t T e s t{ @ E J B p r i v a t eF o o S e r v i c ef o o S e r v i c e ; @ D e p l o y m e n t p u b l i cs t a t i cA r c h i v e < ? >c r e a t e T e s t A r c h i v e ( ){ A r c h i v e < ? >a r c h i v e=A r c h i v e H e l p e r . g e t A r c h i v e ( ) ; r e t u r na r c h i v e ; } @ T e s t p u b l i cv o i dt e s t F i n d A l l ( ){ L i s t < F o o >h i t s=f o o S e r v i c e . f i n d A l l F o o s ( ) ; A s s e r t . a s s e r t T r u e ( h i t s . s i z e ( )>0 ) ; } @ T e s t p u b l i cv o i dt e s t F i n d F o o s E a r l i e r T h a n ( ){ D a t ef u t u r e=n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( )+1 0 0 0 0 0 ) ; L i s t < F o o >h i t s=f o o S e r v i c e . f i n d F o o s E a r l i e r T h a n ( f u t u r e ) ; A s s e r t . a s s e r t T r u e ( h i t s . s i z e ( )>0 ) ; } }

Everything should work fine now Obviously this model works fine for not-so-complicated objects and associations. If complex objects respective their associations exist, there might be better options.

Creating test data by means of JSON/XML (or YML)


Presumed, the Foos are described using JSON, one can import them easily using a specific notation. The whole thing is based upon DBUnit. The JSON file (preferably stored in /src/test/resources/datasets):
{ " f o o ":[{ " i d ":9 9 8 , " d e s c r i p t i o n ":" F o oO n e " , " c r e a t i o n T i m e ":" 2 0 1 4 0 2 1 90 9 : 3 1 : 3 3 " } ,{ " i d ":9 9 9 , " d e s c r i p t i o n ":" F o oT w o " , " c r e a t i o n T i m e ":" 2 0 1 4 0 2 1 90 9 : 3 1 : 3 3 " }] }

The test itself:


@ R u n W i t h ( A r q u i l l i a n . c l a s s )

@ U s i n g D a t a S e t ( " d a t a s e t s / F o o s . j s o n " )
Copyright and all intellectual property belongs to Brockhaus Group 14

p u b l i cc l a s sF o o S e r v i c e A r q u i l l i a n D a t a S e t T e s t{ @ E J B p r i v a t eF o o S e r v i c ef o o S e r v i c e ; @ D e p l o y m e n t p u b l i cs t a t i cA r c h i v e < ? >c r e a t e T e s t A r c h i v e ( ){ A r c h i v e < ? >a r c h i v e=A r c h i v e H e l p e r . g e t A r c h i v e ( ) ; r e t u r na r c h i v e ; } @ T e s t p u b l i cv o i dt e s t F i n d A l l ( ){ L i s t < F o o >h i t s=f o o S e r v i c e . f i n d A l l F o o s ( ) ; A s s e r t . a s s e r t T r u e ( h i t s . s i z e ( )>0 ) ; } @ T e s t p u b l i cv o i dt e s t F i n d F o o s E a r l i e r T h a n ( ){ D a t ef u t u r e=n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( )+1 0 0 0 0 0 ) ; L i s t < F o o >h i t s=f o o S e r v i c e . f i n d F o o s E a r l i e r T h a n ( f u t u r e ) ; A s s e r t . a s s e r t T r u e ( h i t s . s i z e ( )>0 ) ; } }

Some kind of round trip


Maybe you consider it a bright idea to serialize your object in JSON and to load these objects maybe after some changes - into the database or utilize them as test data. Well there are several options available to do so, the most popular frameworks are Googles gson and Jackson. The following explains how to do so using Jackson but keep in mind, there is still a lot to discover. To convert / serialize your objects some kind of utility class might be of help:
p u b l i cc l a s sJ S O N P a r s e r{ / * *s e l fr e f e r e n c e* / p r i v a t es t a t i cf i n a lJ S O N P a r s e rT H I S=n e wJ S O N P a r s e r ( ) ; p r i v a t eO b j e c t M a p p e ro b j e c t M a p p e r=n e wO b j e c t M a p p e r ( ) ; / /S i n g l e t o n p r i v a t eJ S O N P a r s e r ( ){ / /s e e :h t t p : / / w i k i . f a s t e r x m l . c o m / J a c k s o n F A Q D a t e H a n d l i n g D a t e F o r m a td f=n e wS i m p l e D a t e F o r m a t ( " y y y y M M d dH H : m m : s s " ) ; o b j e c t M a p p e r . s e t D a t e F o r m a t ( d f ) ; o b j e c t M a p p e r . g e t D e s e r i a l i z a t i o n C o n f i g ( ) . w i t h D a t e F o r m a t ( d f ) ; } p u b l i cs t a t i cJ S O N P a r s e rg e t I n s t a n c e ( ){ r e t u r nT H I S ; } p u b l i cS t r i n gt o J S O N ( O b j e c to ){ S t r i n gr e t=n u l l ; t r y{ r e t=o b j e c t M a p p e r . w r i t e r W i t h D e f a u l t P r e t t y P r i n t e r ( ) . w r i t e V a l u e A s S t r i n g ( o ) ;

Copyright and all intellectual property belongs to Brockhaus Group

15

}c a t c h( J s o n G e n e r a t i o n E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; }c a t c h( J s o n M a p p i n g E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; }c a t c h( I O E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; } r e t u r nr e t ; } p u b l i c< T >Tf r o m J S O N ( C l a s s < T >c l a z z ,S t r i n gj s o n ){ Tr e t=n u l l ; t r y{ r e t=o b j e c t M a p p e r . r e a d V a l u e ( j s o n ,c l a z z ) ; }c a t c h( J s o n P a r s e E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; }c a t c h( J s o n M a p p i n g E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; }c a t c h( I O E x c e p t i o ne ){ e . p r i n t S t a c k T r a c e ( ) ; } r e t u r nr e t ; } }

This class serves perfectly as long as you want to serialize just one foo, if you want to serialize more of them, you need a so-called wrapper class (note the field name of the collection, its a singular, leave it like this to get the expected result):
p u b l i cc l a s sF o o W r a p p e r{ L i s t < F o o >f o o=n e wA r r a y L i s t < F o o > ( ) ; p u b l i cL i s t < F o o >g e t F o o ( ){ r e t u r nf o o ; } p u b l i cv o i ds e t F o o ( L i s t < F o o >f o o ){ t h i s . f o o=f o o ; } }

Creating the JSON representation is simple like this (think of combining it with a findAll() method of the DAO layer):
p u b l i cc l a s sJ S O N P a r s e r T e s t{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ J S O N P a r s e r T e s tt e s t=n e wJ S O N P a r s e r T e s t ( ) ; t e s t . s e r i a l i z e F o o s ( ) ; } p r i v a t ev o i ds e r i a l i z e F o o s ( ){ F o o W r a p p e rw r a p p e r=n e wJ S O N P a r s e r T e s t . F o o W r a p p e r ( ) ; F o of o o 1=n e wF o o ( ) ; f o o 1 . s e t I d ( 9 9 8 l ) ; f o o 1 . s e t C r e a t i o n T i m e ( n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( ) ) ) ; f o o 1 . s e t D e s c r i p t i o n ( " F o oO n e " ) ;

Copyright and all intellectual property belongs to Brockhaus Group

16

F o of o o 2=n e wF o o ( ) ; f o o 2 . s e t I d ( 9 9 9 l ) ; f o o 2 . s e t C r e a t i o n T i m e ( n e wD a t e ( S y s t e m . c u r r e n t T i m e M i l l i s ( ) ) ) ; f o o 2 . s e t D e s c r i p t i o n ( " F o oT w o " ) ; w r a p p e r . g e t F o o ( ) . a d d ( f o o 1 ) ; w r a p p e r . g e t F o o ( ) . a d d ( f o o 2 ) ; S t r i n gj s o n=J S O N P a r s e r . g e t I n s t a n c e ( ) . t o J S O N ( w r a p p e r ) ; S y s t e m . o u t . p r i n t l n ( j s o n ) ; } p r i v a t ec l a s sF o o W r a p p e r{ L i s t < F o o >f o o=n e wA r r a y L i s t < F o o > ( ) ; p u b l i cL i s t < F o o >g e t F o o ( ){ r e t u r nf o o ; } p u b l i cv o i ds e t F o o ( L i s t < F o o >f o o ){ t h i s . f o o=f o o ; } } }

Copyright and all intellectual property belongs to Brockhaus Group

17

S-ar putea să vă placă și