WELCOME Abdennour : Software engineer

Showing posts with label Monde Java. Show all posts
Showing posts with label Monde Java. Show all posts

Mar 6, 2013

assertsEquals VS assertSame



What's the difference between assertsEquals and assertSame ? 



assertsEquals([String message], expected, actual):
         Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays.

assertSame([String], expected, actual)  :
                    Checks that both variables refer to the same object.






Demo : 
( not yet)

Dec 1, 2012

Reflection Java Api Best Practices


Code Source


  1. create a java class nemed TestClass containing 5 methods (m_A(), m_B(), m_C(), m_D(), m_E() ) every method return a String ("A", "B" ...) :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
    package slm.abdennour.domain;
    import slm.abdennour.reflection.AnnoteMe;
    /**
    *
    * @author عبد النور التومي Abdennour TOUMI
    * @blog http://abdennour-insat.blogspot.com
    * 28 oct. 2012
    */
    public class TestClass {
    public String m_A(){
    return "A";
    }
    public String m_B(){
    return "B";
    }
    public String m_C(){
    return "C";
    }
    public String m_D(){
    return "D";
    }
    public String m_E(){
    return "E";
    }
    }











  2. create a parameterized annotation for methods with name: annoteMe (parameter name: "returnKey") :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    package slm.abdennour.reflection;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    /**
    * @author عبد النور التومي Abdennour TOUMI
    * 28 oct. 2012
    */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AnnoteMe {
          String returnKey();
    }
    
    

  3. annotate the methods m_B(), m_C(), m_D() with annoteMe :

    @AnnoteMe(returnKey="antB")
    public String m_B(){
    return "B";
    }
    @AnnoteMe(returnKey="antC")
    public String m_C(){
    return "C";
    }
    @AnnoteMe(returnKey="antD")
    public String m_D(){
    return "D";
    }





  4. create a main-class that search classes via the Reflection API in the same package (TestClass should be found):

    public static Class[] getClasses(String packageName)
    throws ClassNotFoundException, IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
    classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
    }
    Invoke getClasses:
    Class[] cls=getClasses(Main.class.getPackage().getName());
  5. search the methods annotated with "annoteMe" in the found classes (hier TestClass) via the Reflection API and invoke them :


    public static List<Method> getMethods(Class[] classes,Class annotat){
    List<Method> methodsAnnoted=new ArrayList<Method>();
    for(int i=0;i<classes.length;i++){
    Method[] subMs=classes[i].getMethods();
    System.out.println(subMs.length);
    for(int j=0;j<subMs.length;j++){
    //System.out.println(Calendar.getInstance().ge+"mmm : "+subMs[j].getName());
    Annotation[] ans=Arrays.asList(subMs).get(j).getAnnotations();
    for(Annotation ant:ans) {
    if( ant.annotationType() == annotat ) {
    methodsAnnoted.add(subMs[j]);
    }
    }
    }
    }
    return methodsAnnoted;
    }


    Inv
    oke getMethods:
    List<Method> annotatedMethods=getMethods(cls, AnnoteMe.class);




Nov 2, 2012

Creating Spring Roo project from existing DataBase(Reverse Engineering)


                                     


Method 1 :

1. Open roo shell
 $roo

2.Create your project and set the main configuration then Enter this Command :
roo>persistence setup --provider HIBERNATE --database MYSQL --databaseName consultationmedicale --userName root --password 0000

NOTE: Don't forget to replace "root" by  your MySql UserName and "0000" by your MySql password




3.addon install bundle :
roo> bundle --bundleSymbolicName org.springframework.roo.wrapping.mysql-connector-java

4.Add MySql Connector Jar too Context :
roo> osgi start --url file:///C:/path/to/mysql-connector-java-5.1.18-bin.jar

5.Generate XML Description of Database 
roo> database introspect --schema consultationmedical
Note : Replace "consultationmedical" by your database Name .


Result
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.--><database name="deprecated">
    <option key="moduleName" value=""/>
    <option key="activeRecord" value="false"/>
    <option key="includeNonPortableAttributes" value="true"/>
    <option key="testAutomatically" value="false"/>
    <table name="consultation">
        <column name="idc" primaryKey="true" required="true" scale="0" size="10" type="4,INT"/>
        <column name="datec" primaryKey="false" required="false" scale="0" size="45" type="12,VARCHAR"/>
      .
      ..
   .. ... 

    </table>
</database>

Sep 9, 2012

Cloud IDE(Online IDE for Java)



www.cloud-ide.com now displays a Java doc tooltip on objects and methods !!

Will be available for everybody on Monday 10th إن شاء الله. Stay tuned!




eclipse online , netbeans online , ide java online 

Apr 17, 2012

How to install Mockito to do TDD(Test Driven Development ) (with Eclipse)



Eclipse Plugins:
1-MoreUnit : 
   >> interest of Use: 
               a.It allows to manage tests by keyboard shortcuts (Ctrl + R = run the     test;    Ctrl + J = Jump) .
               b.drew a diagram of tests (failed, success, improved) [PULSE]
   >>How install it :
                a. Help>Install new Software>
                 b. put this URL link in "Work With" "http://moreunit.sourceforge.net/update-site/"
2-Infinitest : 
     >> interest of Use: 
               .it is a black bar at the bottom of the editor. it makes the automatic launching test.


   >>How install it :
                a. Help>Install new Software>
                 b. put this URL link in "Work With" "http://infinitest.github.com/"
3-Eclemma :
       >> interest of Use: 
               .it is a test coverage


   >>How install it :
                a. Help>Install new Software>
                 b. put this URL link in "Work With" "http://update.eclemma.org/"


Maven Dependencies:
-We have 4 dependencies :
<dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>3.8.1</version>
                        <scope>test</scope>
 </dependency>


  <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit-dep</artifactId>
                        <version>4.10</version>
                        <scope>test</scope>

 </dependency>

  <dependency>
                        <groupId>org.hamcrest</groupId>
                        <artifactId>hamcrest-all</artifactId>
                        <version>1.1</version>
                        <scope>test</scope>
  </dependency>


   <dependency>
                        <groupId>org.mockito</groupId>
                        <artifactId>mockito-all</artifactId>
                        <version>1.9.0</version>
                        <scope>test</scope>
    </dependency>


Apr 9, 2012

JTxtIOEasy : API to manage Java OI Stream Easly

Peace Upon To You : 

many  applications require the manipulation of IO Stream,

finally, You will get rid of flow's problems,
With this product you can 
process files easly.

Indeed, the production of this َAPI has been started for 3 years. And every time I add features. So I think I suicite your curiosity ,
Soon there will be a tuto which contains an explanation of the main advantages of this API . 
The most important is to keep in mind the name of this product. JTxtIOEasy