Saturday, May 7, 2016

Continues Inspection with SonarQube / Sonar plugins Part 4

Sonar Plugin Installation and Configuration for Eclipse IDE

Part 1 Part 2 > Part 3  >  Part 4

It is important to have at-least one project appearing in the SonarQube server's dashboard before staring this section.
(If you do not have projects in your SonarQube server, please follow the Part 2 - Continues Inspection With Sonar of this Article)

SonarLint Plugin



  • This plugin is also available on the eclipse market place
  • You can visit market place and drag/drop the "install" button to  your eclipse IDE. (It will start the installation process for you)

  • Or else inside the eclipse IDE goto "Help" > "Eclipse Marketplace"



  • Type "sonarlint" in the Find: section of the Eclipse Market place and hit enter. 
  • When search result appears, click on the "Install" button.

  • When "Confirm Selected Features screen" appears, select "SonarLint for Eclipse" and "SonarLint for Eclipse Java Configuration Helper" 
  • Then click the Confirm button.



  • When "Review Licence" screen appears select "I accept the terms of the license agreement" radio button and click on the Finish button.

  • Eclipse will continue the SonarLint plugin installation.
  • When it asks to restart the IDE to finish installation, allow it to do so.
  • Once eclipse restarts, create a sample Java project and a sample project as shown below.
CoreJavaDemo (Project)
     |--MySonar (Package)
              |--HelloWorld.java (Class)


  • Right click on the project and select "SonarLint" > "Bind to a SonarQube project"


  • When "Bind Eclipse projects to SonarQube projects" screen opens, select Eclipse project (CoreJavaDemos) and Type a SonarQube project name (which is already existing in SonarQube Dashboard)


  • If "Select a SonarQube server:" dropdown list does not have the localhost as a value, you should configure it with following values. To check whether the server is accessible, click on "Test server" button after providing the below details.
Server name : http://localhost:9000/
User : admin
Password : admin

  • Finally goto "Window" > "Show View" and select "SonarLint Issues" view


  • Now you SonarLint plugin will analyze the source-code which is being developed in the eclipse project and notify issues in real-time. (A Summary of issues will be displayed in SonarLint issues view)





Continues Inspection with SonarQube / Sonar plugins Part 3

Continues Inspection With Sonar (The Sonar Way)

Part 1 Part 2 > Part 3 Part 4

Analyzing JAVA Projects with Maven

(Please note that in-order to perform this step in a simple manner, SonarQube server should be running in the default port  - localhost:9000. If it is not, additional maven configurations needs to be done as well)

Analyzing maven project for SonarQube is easy. Only a few simple steps need to be followed in order to perform this analysis. 
(If you do not have a maven project, please download following test project and extract to a preferred location)

  • Open a command line (Run > Type "cmd")
  • Goto the java maven project folder. (Where the "pom.xml" is located)

  • Type "mvn sonar:sonar" and hit enter. (Maven will start analyzing the project)



("Build Success" message at the end of analysis confirms the analysis has completed successfully)

You should now be able to see your java project in the SonarQube dashboard.



Analyzing Unit Test Results/Code Coverage for JAVA Maven Projects

(Please note that you must include a set of plugins in your pom.xml to analyze the code coverage. 
E.g : "junit", "maven-surefire-plugin", "sonar-jacoco-listeners")

If you do not have the "junit", "maven-surefire-plugin", "sonar-jacoco-listeners" plugins in your pom.xml, simply copy and paste the same plugins from the following pom.xml. 

  pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>com.mitrai</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <name>Java :: Mitrai-TestProject :: UT Coverage with JaCoCo</name>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <!-- Minimal supported version is 4.7 -->
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>  
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <!-- BEGIN: Specific to mapping unit tests and covered code -->
  <profiles>
    <profile>
      <id>coverage-per-test</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <!-- Minimal supported version is 2.4 -->
            <version>2.13</version>
            <configuration>
              <properties>
                <property>
                  <name>listener</name>
                  <value>org.sonar.java.jacoco.JUnitListener</value>
                </property>
              </properties>
            </configuration>
          </plugin>
        </plugins>
      </build>

      <dependencies>
        <dependency>
          <groupId>org.sonarsource.java</groupId>
          <artifactId>sonar-jacoco-listeners</artifactId>
          <version>3.8</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
  <!-- END: Specific to mapping unit tests and covered code -->
</project>

Also note that having the above plugins will analyze 0 unit tests, if you do not have any unit tests written in your code. (If you are using the test Java Maven project, you shall note there is one Unit test case while its being analyzed.)
  • Open a command line (Run > Type "cmd")
  • Goto the java maven project folder. (Where the "pom.xml" is located)
  • Type "mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install" and hit enter. (Maven will Prepare jacoco agent to allow coverage report generation, build the project, and execute the unit tests)


  • Type "mvn sonar:sonar" and hit enter. (Maven will start analyzing the project along with the unit test results created from the previous step and publish results in SonarQube dashboard)

SonarQube dashboard should now consist your java project's analysis results along with the the unit test results / code coverage. 





Things to be learned before learning Load Balancing

Network Layers (Keep in mind when working with diffrent protocols) Difference between HTTPS and SSL SSL (Secure Socket Layer...