Thursday, January 22, 2015

Java Interface- Basics

 Interface
Interface looks like class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default.
 They are used for abstraction. Since methods in interfaces does not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces, however it cannot extend more than one classes.

Declaration
Interfaces are declared by specifying a keyword “interface”. E.g.:

package com.vikas;
public interface MyInterface {
    public void Method1();
    public void Method2();
    int x=5 ;   
    int y; // Compiler will throw error
}


Also-

package com.vikas;
public interface MyInterface1 {
    public void Method3();
    int x=10;
}
Interface Implementation
It has to provide the body of all the methods that are declared in interface.
Note: Class implements interface but an interface extends another interface.

For above two interface- MyInterface and MyInterface1 implementation class will be:

package com.vikas;
public class MyImplmnt implements MyInterface,MyInterface1{
    public void Method1(){
        System.out.println("My first name is Vikas");
    }
    public static void main(String[] args) {
        MyInterface obj = new MyImplmnt();
        MyInterface1 obj1 = new MyImplmnt();
        obj.Method1();
        obj.Method2();
        obj1.Method3();
       
    }
    @Override
    public void Method2() {
        //int x;
        //x=10;
        System.out.println("My last name is Kumar");
        System.out.println("value--> "+MyInterface1.x);
    }
    @Override
    public void Method3() {
        System.out.println("I am QA Engineer");
       
    }
}

OUTPUT:

My first name is Vikas
My last name is Kumar
value--> 10
I am QA Engineer

Key Points:

1. We can’t instantiate an interface in java.
2. implements keyword is used by classes to implement an interface.
3. While providing implementation in class of any method of an interface, it needs to be mentioned as public.
4. Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”.
5. All the interface methods are by default abstract and public.
6. Variables declared in interface are public, static and final by default.
7. Interface variables must be initialized at the time of declaration otherwise compiler will through an error.
8. Any interface can extend any other interface but cannot implement it. Class implements interface and interface extends interface.
9. Variable names conflicts can be resolved by interface name(Refer the code above-> MyInterface1.x)

A class can extend only one class but can implement any number of interfaces. It saves you from Deadly Diamond of Death(DDD) problem.

 

Sunday, November 30, 2014

Passwordless SSH Login - Linux

Passwordless SSH login
Hate typing your SSH password every time you need to login remotely to your machine? Set up public key authentication, and you can securely login without ever having to type a password.
For this guide, let's say I have two machines, a laptop and a desktop. I move around a lot with my laptop (SSH client), and frequently need to access files or run commands on my desktop (SSH server) but hate typing the password to connect each time.
Ensure proper configuration
Assumption: OpenSSH is used to login remotely using a password. If not, usually all you have to do is install openssh-server on the server and openssh-client on the client. You might also have to open up TCP port 22 on the server if that's not done for you.
Pretty much all modern distros (certainly all the popular ones) will have pubkey authentication enabled in the SSH configuration. On the server, it's enabled as long as you do NOT see this line in /etc/ssh/sshd_config:
On the client, pubkey authentication is enabled as long as that line appears in neither /etc/ssh/ssh_config nor ~/.ssh/config.

Generating a public/private key pair
To begin setting up public key authentication, you'll need to first generate public and private keys on the client. Generating a key pair is simple. Just run ssh-keygen and push enter a lot to accept the defaults:

[vikas@client ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vikas/.ssh/id_rsa):
Created directory '/home/vikas/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vikas/.ssh/id_rsa.
Your public key has been saved in /home/vikas/.ssh/id_rsa.pub.
The key fingerprint is:
2b:7d:96:a2:c7:d9:67:6f:9e:d7:0d:f2:07:5a:77:18
Now, in ~/.ssh/, you'll have two files. id_rsa is the private key, which you must keep secret. id_rsa.pub is the public key, which we're going to copy to the server.

Putting the key on the server
Now we need to copy the public key to the server and add it to the list of authorized keys. First, scp the key to the server:

[vikas@client ~]$ scp ~/.ssh/id_rsa.pub vikas@serverIP
vikas@serverIP  password:
id_rsa.pub                                    100%  393     0.4KB/s   00:00
Next, log on to the server and append the public key to the server's authorized_keys file:

[vikas@client ~]$ ssh vikas@serverIP
vikas@serverIP password:
Last login: Sun Nov  30 16:04:12 2014 from client.vikas
[vikas@serverIP ~]$ cat id_rsa.pub >> ~/.ssh/authorized_keys
[vikas@serverIP ~]$ rm id_rsa.pub
Done!

Now if you try to make an SSH connection, the server will have the client's public key and the client will have its private key, and magic will happen. You should not be prompted for a password any more:

[vikas@client ~]$ ssh vikas@serverIP
Last login: Sun Nov  30 16:08:55 2014 from client.vikas
[vikas@serverIP ~]$
ssh, scp, sftp, and any GUI tools built on top of these (like KDE's fish KIOslave) will no longer prompt for a password.

One caveat

I want to point out one more thing. The most common cause for SSH public key authentication mysteriously not working is incorrect file permissions on the ~/.ssh/ files. As a general rule, your file permissions (on both machines) should be at least as restrictive as:

~/.ssh/                      rwx------ (700)
~/.ssh/authorized_keys       rw-r--r-- (644)
~/.ssh/id_rsa                rw------- (600)
~/.ssh/id_rsa.pub            rw-r--r-- (644)

Wednesday, July 2, 2014

Java Wrapper Class

Java is an object-oriented language and can view everything as an object. A simple file can be treated as an object (with java.io.File), an address of a system can be seen as an object (with java.util.URL), an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an object (with wrapper classes). Wrapper classes are used to convert any data type into an object.

The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language. For example, upto JDK1.4, the data structures accept only objects to store. A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes.

What are Wrapper classes?
As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
Observe the following conversion.
int k = 100;
Integer it1 = new Integer(k);
The int data type k is converted into an object, it1 using Integer class. The it1 object can be used in Java programming wherever k is required an object.

The following code can be used to unwrap (getting back int from Integer object) the object it1.
int m = it1.intValue();
System.out.println(m*m); // prints 10000
intValue() is a method of Integer class that returns an int data type.

List of Wrapper Class
Primitive data typeWrapper class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean


Tuesday, November 5, 2013

Testing: Strategy and Plan

Test Strategy
A Test Strategy document is a high level document and normally developed by Project Manager/Test Manager. This document defines "Software Testing Approach” to achieve testing objectives. The Test Strategy is normally derived from the Business Requirement Specification document.
The Test Strategy document is a static document meaning that it is not updated too often. It sets the standards for testing processes and activities and other documents such as the Test Plan draws its contents from those standards set in the Test Strategy Document.
Some companies include the “Test Approach” or “Strategy” inside the Test Plan, which is fine and it is usually the case for small projects. However, for larger projects, there is one Test Strategy document and different number of Test Plans for each phase or level of testing.
Components of the Test Strategy document
  • Scope and Objectives
  • Business issues
  • Roles and responsibilities
  • Communication and status reporting
  • Test deliverability
  • Industry standards to follow
  • Test automation and tools
  • Testing measurements and metrices
  • Risks and mitigation
  • Defect reporting and tracking
  • Change and configuration management
  • Training plan
Test Plan
The Test Plan document on the other hand, is derived from the Product Description, Software Requirement Specification SRS, or Use Case Documents.
The Test Plan document is usually prepared by the Test Lead or Test Manager and the focus of the document is to describe what to test, how to test, when to test and who will do what test.
It is not uncommon to have one Master Test Plan which is a common document for the test phases and each test phase have their own Test Plan documents.
There is much debate, as to whether the Test Plan document should also be a static document like the Test Strategy document mentioned above or should it be updated every often to reflect changes according to the direction of the project and activities.
My own personal view is that when a testing phase starts and the Test Manager is “controlling” the activities, the test plan should be updated to reflect any deviation from the original plan. After all, Planning and Control are continuous activities in the formal test process.
  • Test Plan id
  • Introduction
  • Test items
  • Features to be tested
  • Features not to be tested
  • Test techniques
  • Testing tasks
  • Suspension criteria
  • Features pass or fail criteria
  • Test environment (Entry criteria, Exit criteria)
  • Test delivarables
  • Staff and training needs
  • Responsibilities
  • Schedule
 This is a standard approach to prepare test plan and test strategy documents, but things can vary company-to-company

Friday, September 13, 2013

Native,mobile Web or Hybrid


Native, mobile Web, or hybrid? For companies considering tapping into the mobility of their increasingly connected workforce with enterprise mobile solutions, that’s the million dollar question.
Before you can determine which option is best for your custom mobile development project, we think it’s important to understand a few basic fundamentals on the differences between native, hybrid and mobile Web applications.
Native 
Apps developed exclusively for specific mobile platform that can leverage all device capabilities.

The inherent benefits are obvious. Native applications can leverage the full array of features and functions available through the mobile device’s core operating system. Generally, they are faster, smoother and offer a significantly more fluid user experience than either Hybrid apps or mobile Web apps.

The main disadvantage of native app is you can’t run this app in other platforms. So you need more developers to build app in different plat forms. Learning time is more when compared to web and hybrid apps. Building native apps are little expensive.
Mobile Web
Apps implemented with HTML5 and JavaScript that operate entirely inside a mobile browser. Mobile Web apps offer an attractive option for companies that are looking to get into the Mobility game but don’t want to invest in building native applications across four different mobile platforms. Whether getting a new app up and running or maintaining or updating an existing mobile solution, with Mobile Web Apps everything is simple and inexpensive.
Hybrid

Apps that wrap a mobile web interface inside a native container
Today, technology changes so rapidly that most businesses require immense flexibility and scalability to adapt content, design and even application architecture, all on the fly. By deploying applications that rely on a robust combination of HTML5 Web technologies and native OS features, you preserve a large degree of control over the content and design of the solutions we build for mobile platforms.
 The main advantage of Hybrid app is cross platform. It saves the development cost and time. But the problem is less user control when compared to native apps. The performance is less when compared to native apps and touch sensing also varies.