Wednesday, March 30, 2016

Handling non-functional requirements in scrum

Product owners defines application functionalities and requirements about what application should do. Such requirements are called functional requirements.

There use to be some inherent requirements of a system like performance,scalability, manageability, which the software architect needs to identify and define.These are called non-functional requirements of a system.Non functional requirements defines the quality or constraints of a system.If these are not taken care during software architecture phase, application ends up with technical debt, which leads to high cost of maintenance, enhancement and support.

The distinction between functional and non-functional requirement can be understood from a ticket booking application. The functional requirement would be that users should be able to book the tickets between two locations. Whether the application should remain available all the time or can be brought down for some time weekly or monthly for maintenance would be a non-functional requirement.

Some examples of non-functional requirements of an application are as following -
  1. Scalability (Capability to handle growing amount of work)
  2. Performance
  3. Usability (Easy to use, learn and fit for purpose)
  4. Manageability (Easy to configured, deployed, controlled and supervised)
  5. Security
  6. Documentation
  7. Extensibility (Ability to be extended with minimal or no change in existing code/behaviour)
  8. Portability (Usability in different environment)
  9. Reliability (Ability to function under stated condition for specified period of time)
  10. Robustness (Ability to cope up with error or erroneous input during execution)
  11. Availability (Ratio of time of application at operable/committed level to total time)
  12. Resilience (Ability to provide accepted level on service in case of faults, natural disasters or targeted attacks)
  13. Configuration management (Track and control changes in software)
  14. Disaster recovery (Procedures to recover from natural or human made disasters)
In scrum, such non functional requirements, can be handled in following ways-

Creating the stories and getting the product-owners buy-in for prioritization -In this approach, we talk to product owner to get the non-functional requirements considered as user stories. The example of such story with acceptance criteria might be "As a user, I want the application to remain available 24*7. It should not need to be brought down for upgrades" . The limitation of this approach is that some of the times, such stories might not be prioritized by product owners for a long time. The positive side of this approach is that whenever its picked up, we would have complete buy-in from product-owners.

Considering the non-functional requirements part of functional story itself - In this approach, the non-functional requirements gets implemented along with the story itself, but it would have impact on the estimation of stories. While following this approach, the estimation would be higher than it would have been without non-functional requirements

In this article, I explained the way to understand and manage the non-functional requirements. Please let us know,your experience as well about managing and handling non-functional requirements

Monday, March 7, 2016

Direct vs Seda vs Direct-Vm vs VM end points in Apache Camel

Apache camel is a rule based routing and mediation engine. It provides facility to perform seamless integration of various type of components via routes and endpoints.

Camel comes up with four camel aware end points.These end points have following similarities-
1. All of these end points accept camel exchange message only.
2. All of these end points are in-memory. Message in these queues would get lost in case of abrupt shutdown of system.

All of these queues provide better performance than other end points because no camel message transformation is performed by the end point.

Despite having multiple similarities, the purpose of each and every end point is different.The exact nature of difference can be noticed from the following matrix -


With In Same Camel Context
Across Camel contexts in a Single JVM
Synchronous
direct
direct-vm
Asynchronous
seda
vm

Synchronous communication refers to the communication, where the sender sends the message and proceed ahead only when response is retrieved back. In this case, all further processing happens on the received message.

Asynchronous communication refers to the kind of communication, where sender sends a copy of message to the receiver and proceeds without waiting for the processing at receiver to be completed.

In the following example, I will show you the behavior of direct end point and let you use the same example to test and understand the behavior of seda, direct-vm and vm end points. In case of any difficulty, you can download the complete project with the example of all of the end points from the following git hub location.-


Technology used -
java version "1.8.0_73"
Apache Camel 2.16.2
Apache Maven 3.0.4

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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>camel</groupId>
 <artifactId>CamelAwareEndPoints</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
   <version>2.16.2</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version>1.6.4</version>
  </dependency>
 </dependencies>
</project>
Project Structure

DirectEndPointRouter.java-
package com.salil.camelawareendpoints.router;

import org.apache.camel.builder.RouteBuilder;

public class DirectEndPointRouter extends RouteBuilder  {

 public static final String DIRECT_END = "direct:End";
 public static final String DIRECT_START = "direct:start";

 @Override
 public void configure() throws Exception {
  from(DIRECT_START).routeId("StartRouteId")
  .setBody().simple("Start Message")
  .to(DIRECT_END)
  .log("Message at start route completion = ${body}");
  
  
  from(DIRECT_END).routeId("EndRouteId")
  .setBody().simple("End Message")
        .log("message after end-route completion = ${body}");
 }

}

DirectEndPointBehaviorExample.java-
package com.salil.camelawareendpoints.ContextExample;

import com.salil.camelawareendpoints.router.DirectEndPointRouter;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;

/**
 * Created by Salil on 29-02-2016.
 */
public class DirectEndPointBehaviorExample {

    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new DirectEndPointRouter());
        camelContext.start();

        ProducerTemplate producerTemplate=camelContext.createProducerTemplate();
        producerTemplate.sendBody(DirectEndPointRouter.DIRECT_START, "Initial Message");
    }
}

Execution output-
427 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.16.2 (CamelContext: camel-1) is starting
429 [main] INFO org.apache.camel.management.ManagedManagementStrategy - JMX is enabled
843 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 182 type converters
973 [main] INFO org.apache.camel.impl.DefaultRuntimeEndpointRegistry - Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
1280 [main] INFO org.apache.camel.impl.DefaultCamelContext - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
1280 [main] INFO org.apache.camel.impl.DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
1441 [main] INFO org.apache.camel.impl.DefaultCamelContext - Route: StartRouteId started and consuming from: Endpoint[direct://start]
1451 [main] INFO org.apache.camel.impl.DefaultCamelContext - Route: EndRouteId started and consuming from: Endpoint[direct://End]
1451 [main] INFO org.apache.camel.impl.DefaultCamelContext - Total 2 routes, of which 2 is started.
1459 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.16.2 (CamelContext: camel-1) started in 1.031 seconds
1501 [main] INFO EndRouteId - message after end-route completion = End Message
1501 [main] INFO StartRouteId - Message at start route completion = End Message

In this case, we can notice following things
1- The output of end route got printed before the completion of start route
2- Both start route and end route had the same message in the body of the message at the time of printing
3- All the execution happens on the main thread only

This behavior appears because of synchronous nature of direct end point, where at the call of '.to(DIRECT_END)', the execution of first route stopped & it resumed again on receiving of the message with body 'End Message'

Let us know whether you could understand the behavior of seda, direct-vm and vm points, or faced any difficulty even after executing the examples at following git hub location

Tuesday, March 1, 2016

Lamda expression vs Anonymous class in java

Lamda expression are anonymous methods, which can be assigned to a variable, passed as an argument or returned as a value of function call.

Lamda expression in java 8 comes as a big relief from writing the boiler plate code needed to implement single method interfaces like comparable, comparator, runnable or callable etc. For writing such methods using anonymous class it use to take minimum six to seven lines of code. The same can be written in a single line while using Lamda expression.

In the following example, we are trying to print 'Salil Verma' using thread. we can notice that Lamda expression is very concise and anonymous class contains boiler plate code.

Anonymous class approach -

public class ThreadAnonymousClassExample {
   
 public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Salil Verma");
            }
        };
        new Thread(runnable).start();
    }
}

Lamda expression approach -

public class ThreadLamdaExample {
   
 public static void main(String[] args) {
        new Thread(()->System.out.println("Salil Verma")).start();
    }
}

Although Lamda expression looks very handy and concise in writing, it has got limitations. This expression can be used only on the situations where we are using single method interface. On situations where interface or abstract class has got more than one methods, Anonymous class is the way to go.

In short, Lamda expression should be preferred over anonymous class where we are implementing single method interface otherwise anonymous class is the way to go.

Tuesday, February 23, 2016

How to minimize the performance impact of locking in database

Database locks work as a solution to the problems associated with concurrency, consistency and integrity. But it comes with its own drawback of performance hit. In this article, we will talk about steps, we could take during database design to reduce the performance impact of locking.
Introduce optimum indexes - Introducing optimum indexes improves the performance impact of locking caused by those update and delete statements, which have got filter criteria associated with them. When a update or delete query with filter criteria is fired in database, it initially takes a lock on complete table to identify the records getting impacted. Once the records are identified, Sybase can take the lock on the selected rows, depending on the locking level set on the table . Implementing the index, would minimize the time required in identifying rows to be updated or deleted, which in turn would reduce the all page level locking time.
Choose the optimum locking level on server- Sybase ASE provide option to set the default locking scheme to be applied on newly created tables.It can be set as "allpages", "datapages" or "datarows "level. In "allpages" lock level, all the pages of table gets locked during any DML operation. In "datapages" lock level only the pages, which contains impacted data, gets locked. In "datarows " lock level, only the rows impacted with DML operations,gets locked.
 
The default locking scheme can be checked by firing following query -
 sp_configure "lock scheme"
  
Default locking scheme can be changed by following query - 
sp_configure "lock scheme", 0,  {allpages | datapages | datarows}
Choose optimum lock level on tables -It might not always be possible to set least restrictive locking scheme on the database server level. For these cases, Sybase ASE provide option to set locking level on table level. This locking level can be set during table creation as a part of create statement or changed later via alter statement.
Option to set locking level in create table statement -
create table table_name (column_name_list)[lock {datarows | datapages | allpages}]

Option to set locking level in alter table statement -
alter table table_name lock {allpages | datapages | datarows}
Keeping the transaction duration to minimum- locks are taken as a part of transaction and released at the end of transaction. Keeping the transaction duration sort helps us minimizing the lock duration and its impact on performance.
Partition the table- In a non-partitioned table, with no clustered index, all the inserts happens on the last page of the table. In case of concurrent inserts, it would create performance hit as every transaction would cause lock on the last page and let another transaction wait until the completion. In case of partitioned table, there would be multiple last pages causing less impact of locking.
Creating the clustered index-Clustered index provides multiple last page insertion points, which helps in reducing the impact of locks in concurrent inserts. But it also introduces an overhead of ordering, which might again have an impact on performance.
I hope, this article would have helped you in understanding the way, we could minimize the impact of locking in our application. Let me know your experience as well, if this article could help you some way or you could find some other ways as well to minimize the performance impact of locking.

Saturday, February 20, 2016

Testing method arguments using JUnit and Mockito ArgumentCaptor with example

Multiple times, we get to a situation, where we have a  public void method in our program, whose behaviour needs to be tested using  Junit.  Mostly such void methods, take some arguments to perform some operation locally and then pass objects for remote processing.

Automated testing of such scenarios can be done in one of following two approaches. - 
  1. Write a unit test and verify that our public method is performing necessary operations accurately and sending correct object to remote resources.
  2. Write an integration test by including third party module as well and then verify result from the remote resource.


In this article, we will be focus on first approach, where we write a JUnit test to ensure that our method is performing local operations accurately and sending the right object with right values to remote resource.

we can write such test cases using Mockito framework and its Argument captor API. Argument API provides us methods  like capture, getValue and getAllValues to capture the values passed by the method under test to mock of remote resource  and retrieve those values for verification.

In this Mockito Agrument captor example, we have a class MailProcessor, which takes the employee object from database on the basis of id, transform it to dto object  and pass it to mailing service for sending mail.

Technology used -
java version "1.7.0_06"
Apache Maven 3.0.4
junit -4.12
mockito -1.10.19

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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>mockito</groupId>
 <artifactId>ArgumentCaptorExample</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-all</artifactId>
   <version>1.10.19</version>
  </dependency>
 </dependencies>
</project>

Project Structure


Employee.java

package salil.mockito.argumentcaptorexample;

public class Employee {

 private int employeeId;
 private String name;
 private String email;
 private int salarry;

 public int getEmployeeId() {
  return employeeId;
 }

 public void setEmployeeId(int employeeId) {
  this.employeeId = employeeId;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public int getSalarry() {
  return salarry;
 }

 public void setSalarry(int salarry) {
  this.salarry = salarry;
 }

}

EmployeeDao.java

package salil.mockito.argumentcaptorexample;

public interface EmployeeDao {

 Employee getEmployee(int employeeId);
}


EmpMailDTO.java

package com.salil.argumentCaptor.domain;

public class EmpMailDTO {

 private final String reciepientEmail;
 private final String sender="admin@salilstock.blogspot.com";
 private final String subject="Salary notification";
 private final static String templateMessage="Dear %s\n\n we are processing your salary amount %s. \n\n Regards\nAdmin";
 private final String message;
 
 public EmpMailDTO(final String name,final String reciepientEmail,final int salary)
 {
  this.reciepientEmail=reciepientEmail;
  this.message=String.format(templateMessage, name,salary);
 }
 
 public String getReciepientEmail() {
  return reciepientEmail;
 }

 public String getSender() {
  return sender;
 }
 public String getMessage() {
  return message;
 }

 public String getSubject() {
  return subject;
 }
}

MailingService.java

package com.salil.argumentCaptor;

import com.salil.argumentCaptor.domain.EmpMailDTO;

public interface MailingService {
 void sendEmail(EmpMailDTO employeeMail);
}

Method to be tested - MailProcessor.sendEmailToEmployee -
package salil.mockito.argumentcaptorexample;

public class MailProcessor {

 private final EmployeeDao employeeDao;
 private final MailingService mailingService;
 
 public MailProcessor(EmployeeDao employeeDao, MailingService mailer) {
  this.employeeDao = employeeDao;
  this.mailingService = mailer;
 }
 
 public void sendEmailToEmployee(int employeeId)
 {
  Employee employee = employeeDao.getEmployee(employeeId);
  if(isEmployeeMailValid(employee.getEmail()))
  {
  EmpMailDTO empMailDTO = new EmpMailDTO(employee.getName(), employee.getEmail(), employee.getSalarry());
  mailingService.sendEmail(empMailDTO);
  }
 }
 
 private boolean isEmployeeMailValid(String email)
 {
  return email!=null;
 }
}

Corresponding test case would be as following -

package salil.mockito.argumentcaptorexample;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MailProcessorTest {

 @Mock
 private EmployeeDao employeeDao;
 @Mock
 private MailingService mailingService;
 
 private int employeeId=1;
 private String name="Salil";
 private String email="admin@salilstock.blogspot.com";
 private int salarry=5000;
 private Employee employee;
 
 @Before
 public void setup() {
  employee = new Employee();
  employee.setEmployeeId(employeeId);
  employee.setName(name);
  employee.setEmail(email);
  employee.setSalarry(salarry);
 }
 
 
 @Test
 public void testSendEmailToEmployee() {
  MailProcessor mailProcessor = new MailProcessor(employeeDao, mailingService);
  Mockito.when(employeeDao.getEmployee(1)).thenReturn(employee);
  
//  Instantiate argument captor to capture the argument of EmployeeMail type  
  ArgumentCaptorargumentCaptor =ArgumentCaptor.forClass(EmpMailDTO.class);
  
//  Perform operation
  mailProcessor.sendEmailToEmployee(1);
  
//  Capture the argument
  Mockito.verify(mailingService).sendEmail(argumentCaptor.capture());
  
//  verify the correctness of the receiving object
  EmpMailDTO empMailDTO=argumentCaptor.getValue();
  assertEquals(empMailDTO.getReciepientEmail(), this.email);
  assertEquals(empMailDTO.getMessage(),"Dear Salil\n\n we are processing your salary amount 5000. \n\n Regards\nAdmin");
 }

}

This complete project can be accessed from the following git hub location.

Please let us know whether you found this article useful and your experience on using argument captor in your project.

Friday, August 30, 2013

Generating the DDL of Oracle Schema Objects


set serveroutput on size unlimited
begin
dbms_output.put_line('--');
dbms_output.put_line('-- DROP TABLES --');
dbms_output.put_line('--');
    for rt in (select table_name tname from user_tables order by tname) loop
        dbms_output.put_line('DROP TABLE '||rt.tname||' CASCADE CONSTRAINTS;');
    end loop;
end;
/        

declare 
    v_tname  varchar2(30);
    v_cname  char(32);
    v_type     char(20);
    v_null   varchar2(10);
    v_maxcol number;
    v_virg     varchar2(1);
begin
dbms_output.put_line('--');
dbms_output.put_line('-- CREATE TABLES --');
dbms_output.put_line('--');
    for rt in (select table_name from user_tables order by 1) loop
        v_tname:=rt.table_name;
        v_virg:=',';
        dbms_output.put_line('CREATE TABLE '||v_tname||' (');
        for rc in (select table_name,column_name,data_type,data_length,
                            data_precision,data_scale,nullable,column_id
                from user_tab_columns tc
                where tc.table_name=rt.table_name
                order by table_name,column_id) loop
                    v_cname:=rc.column_name;
                    if rc.data_type='VARCHAR2' then
                        v_type:='VARCHAR2('||rc.data_length||')';
                    elsif rc.data_type='VARCHAR' THEN
                        v_type:='VARCHAR('||rc.data_length||')';
                    elsif rc.data_type='NVARCHAR2' THEN
                        v_type:='NVARCHAR2('||rc.data_length||')';
                    elsif rc.data_type='NUMBER' and rc.data_precision is null and
                                         rc.data_scale=0 then
                        v_type:='INTEGER';
                    elsif rc.data_type='NUMBER' and rc.data_precision is null and
                                     rc.data_scale is null then
                        v_type:='NUMBER';
                    elsif rc.data_type='NUMBER' and rc.data_scale='0' then
                        v_type:='NUMBER('||rc.data_precision||')';
                    elsif rc.data_type='NUMBER' and rc.data_scale<>'0' then
                        v_type:='NUMBER('||rc.data_precision||','||rc.data_scale||')';
                    elsif rc.data_type='CHAR' then
                         v_type:='CHAR('||rc.data_length||')';
                    else v_type:=rc.data_type;
                    end if;
                    
                    if rc.nullable='Y' then
                        v_null:='NULL';
                    else
                        v_null:='NOT NULL';
                    end if;
                    select max(column_id)
                        into v_maxcol
                        from user_tab_columns c
                        where c.table_name=rt.table_name;
                    if rc.column_id=v_maxcol then
                        v_virg:='';
                    end if;
                    dbms_output.put_line (v_cname||v_type||v_null||v_virg);
        end loop;
        dbms_output.put_line(');');
    end loop;
end;  
/

declare 
    v_virg        varchar2(1);
    v_maxcol    number;
begin
dbms_output.put_line('--');
dbms_output.put_line('-- PRIMARY KEYS --');
dbms_output.put_line('--');
    for rcn in (select table_name,constraint_name 
            from user_constraints 
            where constraint_type='P' 
            order by table_name) loop
        dbms_output.put_line ('ALTER TABLE '||rcn.table_name||' ADD (');
        dbms_output.put_line ('CONSTRAINT '||rcn.constraint_name);
        dbms_output.put_line ('PRIMARY KEY (');
        v_virg:=',';
        for rcl in (select column_name,position 
                from user_cons_columns cl 
                where cl.constraint_name=rcn.constraint_name
                order by position) loop
            select max(position)
                into v_maxcol
                from user_cons_columns c
                where c.constraint_name=rcn.constraint_name;
            if rcl.position=v_maxcol then
                v_virg:='';
            end if;
            dbms_output.put_line (rcl.column_name||v_virg);
        end loop;
        dbms_output.put_line(')');
        dbms_output.put_line('USING INDEX );');
    end loop;
end;
/

declare
    v_virg        varchar2(1);
    v_maxcol    number;
    v_tname        varchar2(30);
begin
dbms_output.put_line('--');
dbms_output.put_line('-- FOREIGN KEYS --');
dbms_output.put_line('--');
    for rcn in (select table_name,constraint_name,r_constraint_name 
            from user_constraints 
            where constraint_type='R'
            order by table_name) loop
        dbms_output.put_line ('ALTER TABLE '||rcn.table_name||' ADD (');
        dbms_output.put_line ('CONSTRAINT '||rcn.constraint_name);
        dbms_output.put_line ('FOREIGN KEY (');
        v_virg:=',';
        for rcl in (select column_name,position 
                from user_cons_columns cl 
                where cl.constraint_name=rcn.constraint_name
                order by position) loop
            select max(position)
                into v_maxcol
                from user_cons_columns c
                where c.constraint_name=rcn.constraint_name;
            if rcl.position=v_maxcol then
                v_virg:='';
            end if;
            dbms_output.put_line (rcl.column_name||v_virg);
        end loop;
        select table_name 
            into v_tname
            from user_constraints c
            where c.constraint_name=rcn.r_constraint_name;
        dbms_output.put_line(') REFERENCES '||v_tname||' (');

        select max(position)
                into v_maxcol
                from user_cons_columns c
                where c.constraint_name=rcn.r_constraint_name;
        v_virg:=',';
        select max(position)
            into v_maxcol
            from user_cons_columns c
            where c.constraint_name=rcn.r_constraint_name;
        for rcr in (select column_name,position 
                from user_cons_columns cl
                where rcn.r_constraint_name=cl.constraint_name
                order by position) loop
            if rcr.position=v_maxcol then
                v_virg:='';
            end if;
            dbms_output.put_line (rcr.column_name||v_virg);
        end loop;
        dbms_output.put_line(') );');
    end loop;
end;
/
        
begin
dbms_output.put_line('--');
dbms_output.put_line('-- DROP SEQUENCES --');
dbms_output.put_line('--');
    for rs in (select sequence_name 
            from user_sequences
            where sequence_name like 'SQ%'
            order by sequence_name) loop
        dbms_output.put_line('DROP SEQUENCE '||rs.sequence_name||';');
    end loop;
dbms_output.put_line('--');
dbms_output.put_line('-- CREATE SEQUENCES --');
dbms_output.put_line('--');
    for rs in (select sequence_name 
            from user_sequences
            where sequence_name like 'SQ%'
            order by sequence_name) loop
        dbms_output.put_line('CREATE SEQUENCE '||rs.sequence_name||' NOCYCLE;');
    end loop;
end;
/

declare
    v_virg        varchar2(1);
    v_maxcol    number;
begin
dbms_output.put_line('--');
dbms_output.put_line('-- INDEXES --');
dbms_output.put_line('--');
    for rid in (select index_name, table_name 
            from user_indexes
            where index_name not in (select constraint_name from user_constraints) 
                and index_type<>'LOB'
            order by index_name) loop
        v_virg:=',';
        dbms_output.put_line('CREATE INDEX '||rid.index_name||' ON '||rid.table_name||' (');    
        for rcl in (select column_name,column_position 
                from user_ind_columns cl 
                where cl.index_name=rid.index_name
                order by column_position) loop
            select max(column_position)
                into v_maxcol
                from user_ind_columns c
                where c.index_name=rid.index_name;
            if rcl.column_position=v_maxcol then
                v_virg:='';
            end if;
            dbms_output.put_line (rcl.column_name||v_virg);
        end loop;
        dbms_output.put_line(');');
    end loop;
end;
/



http://www.dba-oracle.com/art_builder_get_schema_syntax.htm
http://docs.oracle.com/cd/E17781_01/server.112/e18804/impexp.htm
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_metada.htm
http://docs.oracle.com/cd/E11882_01/server.112/e22490/original_import.htm

Saturday, July 20, 2013

Preparing environment for Andiod Development

While thinking about Android application development, first question comes into our mind, that how can we get the android environment into our laptop or desktop.

There are two ways, we could deal with this challenge one installing an android simulator and another installing android operating system in the laptop. 

The example of some android simulators are as follows -
  1. BlueStacks
  2. Youwave
Installation of Android operating system directly needs vm virtual box. The steps of same can be accessed from here

Monday, May 13, 2013

Various tool and their benefits


JSHint - JSHint is a community-driven tool to detect errors and potential problems in JavaScript code and to enforce your team’s coding conventions.JSHint can be used in eclipse via it's eclipse plugin  as well as in qunit to develop test cases which will consider each validation failure as test case failure.

W3c Markup validator - This validator checks the markup validity  of Web documents in HTML, XHTML, SMIL, MathML, etc.
Stripe Generator -This is a tool to generate stripe images by few clicks, in few seconds. These images can be used as background of a web page. 
SpriteMe -Sprite me is a javascript utility, whcih can be executed from any browser. During execution, it scans all the background images in current page and suggest possible spriting. The suggestion also include the number of request reduction happening on sptiring implementation. We can export the final spriting image along with corresponding CSS to be used for applying spriting in the page, from this tool. 
Trang - Tool to generate xsd from xml file

Various techniques & their benefit


CSS Sprites:CSS sprites are a way to reduce the number of images by combining several images into one. This technique is useful in reducing the all images load time.
Regular Expression - Regular Expression - Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. They can be used to search, edit, or manipulate text and data. In java regular expression evaluation engine's evaluation process can be tweaked in any of three possible flavors using quantifiers ie Greedy, Reluctant & Possessive

Wednesday, May 8, 2013

Various frameworks and their purposes

DWR (Direct Web Remoting) -DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.

Display tag -Display tag library can just... display tables! Give it a list of objects and it will handle column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table in a customizable XHTML style.

JQuery - jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.

BlazeDS - BlazeDS is a server-based Java remoting and web messaging technology that allows you to connect to back-end distributed data and push data to Adobe Flex and Adobe Integrated Runtime (AIR) Rich Internet applications (RIA).

QUnit -QUnit is a powerful, easy-to-use JavaScript unit testing framework. It's used by the jQuery, jQuery UI and jQuery Mobile projects and is capable of testing any generic JavaScript code, including itself!

Junit - JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

JUnitParamsIts a library, which acts as an extension to write parametric test cases in an easier fashion than conventional JUnit parametric test case writing and execution.It works on JUnit version greater than 4.6. Its change log and java doc can be accessed from  git and javadocslinks respectively.

Tuesday, May 7, 2013

Challenges while developing test for spring beans

Spring provides us options to test beans in application contexts using spring-test module. The detailed explanation of the way to use it can be accessed from the following link

I had maven based application. During the development and running of these test cases, I came accross few challenges. The key ones along with their resolutions are as mentioned below.

Issue - Compilation failure - SpringJUnit4ClassRunner.class and @ContextConfiguration not found
Solution - Included below mentioned maven dependency in the project and executed mvn eclipse:eclipse

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
            <version>3.1.2.RELEASE</version>
        </dependency>

Issue - IDE displying error - The type org.junit.runners.BlockJUnit4ClassRunner cannot be resolved. It is indirectly referenced from required .class files
Solution - BlockJUnit4ClassRunner was included in Junit4.5 so added below mentioned dependency of junit4.5 and executed mvn eclipse:eclipse

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.5</version>
        </dependency>

Friday, January 18, 2013

Eliminating hibernate boiler plate code


While using Hibernate, we use below mentioned code each time, we perform any database operation.
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
 // your purposefull code goes here

 session.getTransaction().commit();
 return object;
} catch (Exception exception) {
 session.getTransaction().rollback();
 throw new RuntimeException(exception);
} finally {

 session.disconnect();
}
 We can eliminate the duplicity of this code by using the strategy, where this code will reside only  in HibernateUtil and while performing database operation, we will focus only on database operation, we want to perform.
The infrastructure code is as mentioned below - 
HibernateStrategy.java
package hibernate;

import org.hibernate.Session;

public interface HibernateStrategy {
 Object execute(Session session);
}
HibernateUtil.java
package hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

 private static SessionFactory sessionFactory;

 static {

  sessionFactory = new AnnotationConfiguration().configure()
    .buildSessionFactory();
 }

 public static Object performAction(final HibernateStrategy strategy) {
  Session session = sessionFactory.openSession();
  session.beginTransaction();
  try {
   Object object = strategy.execute(session);
   session.getTransaction().commit();
   return object;
  } catch (Exception exception) {
   session.getTransaction().rollback();
   throw new RuntimeException(exception);
  } finally {

   session.disconnect();
  }
 }
}

The sample code using HibernateUtil is as mentioned below - 
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
  <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
  <property name="hibernate.connection.url">jdbc:hsqldb:mem:InMemoryDatabase</property>
  <property name="hibernate.connection.username">sa</property>
  <property name="hibernate.connection.password"></property>


  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">2</property>

  <property name="hibernate.hbm2ddl.auto">create-drop</property>
  <property name="hibernate.show_sql">true</property>

  <mapping class="hibernate.Student"></mapping>
 </session-factory>
</hibernate-configuration> 
Student.java
package hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Student")
public class Student {

 @Id
 @GeneratedValue
 private int studentId;
 private String name;
 private int age;

 public Student(final String name, final int age) {
  this.name = name;
  this.age = age;
 }

 public Student() {
 }

 @Override
 public String toString() {
  return "Student [studentId=" + studentId + ", name=" + name + ", age="
    + age + "]";
 }
}

Maven dependency
<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
 <scope>test</scope>
</dependency>
<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate</artifactId>
 <version>3.2.7.ga</version>
</dependency>
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> 
 <version>3.2.1.ga</version> </dependency> -->
<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-annotations</artifactId>
 <version>3.2.1.ga</version>
 <exclusions>
  <exclusion>
   <artifactId>javax.transaction</artifactId>
   <groupId>jta</groupId>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>javax.transaction</groupId>
 <artifactId>jta</artifactId>
 <version>1.1</version>
</dependency>
<dependency>
 <groupId>org.hsqldb</groupId>
 <artifactId>hsqldb</artifactId>
 <version>2.2.9</version>
</dependency>
HibernateUtilTest.java  (code to test above infrastructure)
package hibernate;

import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

@SuppressWarnings("unchecked")
public class HibernateUtilTest extends TestCase {

 @Test
 public void testHibernateUtil2() {

  HibernateUtil.performAction(new HibernateStrategy() {
   @Override
   public Object execute(Session session) {
    Student student = new Student("Salil Verma-Test1", 20);
    session.saveOrUpdate(student);
    return new Object();
   }
  });

  List studentList = (List) HibernateUtil
    .performAction(new HibernateStrategy() {
     @Override
     public Object execute(Session session) {
      Criteria criteria = session.createCriteria(
        Student.class).add(
        Restrictions.eq("name", "Salil Verma-Test1"));

      return criteria.list();
     }
    });

  assertNotNull(studentList);
  assertEquals(1, studentList.size());
 }
}

Wednesday, December 26, 2012

How to create Objects in Javascript



JavaScript is an object oriented language. In-spite  of being class based , it is prototype based language, where we can create the prototype of specific objects.

Java script provides us three ways to create objects -
1- Direct instance using JSON ( Java Script Object Notation) syntax - In this way we do not create the prototype but direct object.
The sample code is as mentioned below -

var jsonObject = {
            text : "Json Object has been accessed ",
                  numberOfTimes : 0
  };


2- Direct instance using Object - In this way, we do not create prototype but direct object.
The sample code is as mentioned below -

  var directObject = new Object();
  directObject.text = "Direct created Object has been accessed ",
  directObject.numberOfTimes = 0;

3- Instance from prototype - In this way we create prototype and then create instances based on the prototype. This approach is beneficial when we want to create multiple objects based on the same prototype in the same application or want to use this prototype as utility.
The sample code  is as mentioned below -
function prototypeTest()
 {
    this.text;
    this.numberOfTimes;
 }
 var prototypeObject = new prototypeTest();
 prototypeObject.text="Object created through prototype has been accessed ";
 prototypeObject.numberOfTimes=0;


References -
http://www.w3schools.com/json/json_syntax.asp
http://www.w3schools.com/js/js_objects.asp
http://www.json.org/
 
  

Sunday, December 16, 2012

Copying the resources to and from windows network using Java



Using JCIFS - In this approach we use the implementation of CIFS (Common Internet File System) protocol, which is used for file sharing in windows.  File copy using this  approach is bit slower but suitable on the instances when the user running the program does not have direct access to the network folder and require to pass credentials to access. The sample code to copy file in network drive is as mentioned below - 

Required maven dependency -

<dependency>
 <groupId>org.samba.jcifs</groupId>
 <artifactId>jcifs</artifactId>
 <version>1.2.19</version>
</dependency> 

Sample code -

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class CopyFileUsingJCIFS {

 public static void main(String[] args) throws IOException {
  final String userName = "UserName";
  final String password = "Password";
  final String sourcePath = "pom.xml";
  final String destinationPath = "smb://SALIL-HP/Temp/pom.xml";

  copyFileUsingJcifs(userName, password, sourcePath, destinationPath);

  System.out.println("The file has been copied using JCIFS");
 }
 
 public static void copyFileUsingJcifs(final String userName,
   final String password, final String sourcePath,
   final String destinationPath) throws IOException {

  final String user = userName + ":" + password;
  final NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
    user);
  final SmbFile sFile = new SmbFile(destinationPath, auth);
  final SmbFileOutputStream smbFileOutputStream = new SmbFileOutputStream(
    sFile);
  final FileInputStream fileInputStream = new FileInputStream(new File(
    sourcePath));

  final byte[] buf = new byte[16 * 1024 * 1024];
  int len;
  while ((len = fileInputStream.read(buf)) > 0) {
   smbFileOutputStream.write(buf, 0, len);
  }
  fileInputStream.close();
  smbFileOutputStream.close();
 }
}

 

Using  pure Java IO - This hand coded approach provides better performance than JCIFS but require the user running the java program to have direct access to the network resource. In this case we do not need to import any additional library. The sample code is as mentioned below -

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyingFile {

 public static void main(String[] args) throws IOException {
  final String sourcePath = "pom.xml";
  final String destinationPath = "//SALIL-HP/Temp/pom.xml";

  copyFile(sourcePath, destinationPath);

  System.out.println("The file has been copied using java");
 }

 public static void copyFile(final String sourcePath,
   final String destinationPath) throws IOException {

  final FileOutputStream fileOutputStream = new FileOutputStream(
    destinationPath);
  final FileInputStream fileInputStream = new FileInputStream(new File(
    sourcePath));

  final byte[] buf = new byte[16 * 1024 * 1024];
  int len;
  while ((len = fileInputStream.read(buf)) > 0) {
   fileOutputStream.write(buf, 0, len);
  }
  fileInputStream.close();
  fileOutputStream.close();
 }
}
 



Using commons-io  -  Apache provides FileUtils api, which contains multiple utility functions along with multiple utility methods of copying resources from one folder to network location. In this case code to write will be very small but we will need to include commons-io jar file in the project and user running the program would need to have direct access to the network resources.
The sample code is as mentioned below -

Required maven dependency - 


<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-io</artifactId>
 <version>1.3.2</version>
</dependency>

Sample Code
import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class CopyFileUsingCommonIO {

 public static void main(String[] args) throws IOException {
  FileUtils.copyFileToDirectory(new File("pom.xml"), new File("//SALIL-HP/Temp"));
 }
}