Pages

Sorting of objects based on their fields | Java

We experience the problem of sorting certain arrays or values quiet often in the world of programming. Well, When it is the inbuilt data types like int or char or even string, there are inbuilt methods that will solve the problem.
int[] c={1,5,3,7,3,9,100,50,40,30,20,10,11,9,12};
        
        //sorting of normal array
        Arrays.sort(c);
        System.out.print(Arrays.toString(c));

Now the problem is that, how do we solve a problem of sorting a list of custom objects like this one.

public class CustomSortingTest{
 
    int value=0;

    public CustomSortingTest() {
     }
    public CustomSortingTest(int a){
    value=a;
    }
}

The solution is pretty simple actually. I remember my effort some years back trying to create a separate utility method and passing the list of this object and writing whole logic there to sort the list and return another one.

Java when sorting a collection, looks for the method "compareTo(T..)" which is the function for an inbuilt interface which most of the inbuilt classes happens to be implementing. When we write a custom class, we normally do not implement this interface, and so while trying to sort the list, java never finds the interface method. Now you could simply implement this interface and override the method and write your custom logic of sorting there. Below is an example of this one.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author Rajan Prasad Upadhyay
 *
 */
public class CustomSortingTest implements Comparable<CustomSortingTest> {
 
 int value=0;

    public CustomSortingTest() {
    }
    public CustomSortingTest(int a){
        value=a;
    }
    public static void main(String[] args) {
        List <CustomSortingTest> a=new ArrayList();
        int[] b={1,5,3,7,3,9,100,50,40,30,20,10,11,9,12};
        int[] c={1,5,3,7,3,9,100,50,40,30,20,10,11,9,12};
        
        //sorting of normal array
        Arrays.sort(c);
        System.out.print(Arrays.toString(c));
        
        //sorting a list of objects based on their certain fields
        for(int i=0;i<b.length;i++){
            a.add(new CustomSortingTest(b[i]));
        }
        Collections.sort(a);
        for(int i=0;i<a.size();i++){
            System.out.print(" "+a.get(i).value);
        }
    }

    @Override
    public int compareTo(CustomSortingTest o) {
        if(value<o.value){
            return -1;
        }else if(value==o.value){
            return 0;
        }else{
            return 1;
        }
    }
}
The logic is preety simple. Just determine when to return -1 and when to return 1 or 0.

Best of luck

External table cheat sheet


 /*
  *  The directory should have full 777 permission
  *  The directory in which db is trying to create log file, should have full permission
  *  The reject limit unlimited should be given so that it can reject wrong rows
  *
  */
--Creation of directory

CREATE OR REPLACE DIRECTORY raj_directory1 AS '/oracle_data';

--Creation of the external table

  DROP TABLE raj_actors;

    CREATE TABLE raj_actors (
      first_name        VARCHAR2(10),
      last_name         VARCHAR2(10),
      gender            VARCHAR2(10),
      dob               VARCHAR2(12),
      unq_num           VARCHAR2(10)
    )
    ORGANIZATION EXTERNAL (
      TYPE ORACLE_LOADER
      DEFAULT DIRECTORY raj_directory1
      ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
        FIELDS TERMINATED BY '|'
        lrtrim
        MISSING FIELD VALUES ARE NULL
        REJECT ROWS WITH ALL NULL fields
        (
          first_name      CHAR(10),
          last_name       CHAR(10),
          gender          CHAR(10) ,
          dob             CHAR(12),
          unq_num         CHAR(10)
        )
      )
      LOCATION ('datafile.txt')
    )
   reject limit UNLIMITED;
----------------------------------------------------------------Table created---


SELECT * FROM raj_actors;

CREATE TABLE raj_actors_int AS SELECT * FROM raj_actors;

SELECT * FROM raj_actors_int;
TRUNCATE TABLE raj_actors_int;

describe table raj_actors_int;




--ZZZ_
--ZTBL_
--HI_




Cheat sheet PLSQL Package

#what is a pl/sql package?
-schema object that groups logically related pl/sql TYPES, ITEMS, AND SUBPROGRAMS.
-packages have two parts
-- specification
-- body
--sometimes body is unnecessary
--specification (spec) is the interface to your application
--it declares the types, variables, constants, exceptions, cursors, and subprograms available for use.
-- body defines cursors and subprograms, implements the spec

CREATE [OR REPLACE] PACKAGE package_name
  [AUTHID {CURRENT_USER | DEFINER}]
  {IS | AS}
  [PRAGMA SERIALLY_REUSABLE;]
  [collection_type_definition ...]
  [record_type_definition ...]
  [subtype_definition ...]
  [collection_declaration ...]
  [constant_declaration ...]
  [exception_declaration ...]
  [object_declaration ...]
  [record_declaration ...]
  [variable_declaration ...]
  [cursor_spec ...]
  [function_spec ...]
  [procedure_spec ...]
  [call_spec ...]
  [PRAGMA RESTRICT_REFERENCES(assertions) ...]
END [package_name];

[CREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}
  [PRAGMA SERIALLY_REUSABLE;]
  [collection_type_definition ...]
  [record_type_definition ...]
  [subtype_definition ...]
  [collection_declaration ...]
  [constant_declaration ...]
  [exception_declaration ...]
  [object_declaration ...]
  [record_declaration ...]
  [variable_declaration ...]
  [cursor_body ...]
  [function_spec ...]
  [procedure_spec ...]
  [call_spec ...]
[BEGIN
  sequence_of_statements]
END [package_name];]

##Advantages of PL/SQL Packages
-modularity
-easier application design
-information hiding
-added Functionality


Selenium Web Driver Downloading video

G’Day people. Well as the heading of this article suggests, I am going to share with you some of my adventures I have had in the recent months. The adventure is in the virtual world of computers and internet rather than the real world.

The Real problem

Every story begins with a problem. The problem in my case is that Internet is very expensive. And so, as magic comes with a price, so does the internet in modern age. I have to download some video tutorial series from youtube and I prefer not to do that from my home. I know a place where I could but I could not spend too much time sitting there and going to each and every video url and then downloading the video. Now, the quest starts. How to download the videos without spending much time there? I have two choices.

· Get someone to download them for me.

· Discover of an automation, that could do that for me.

It’s not that I could not go with solution number 1, but what if I have to download another set of videos again after some time. Every time finding some to do the job for me was not an option I would prefer. I am a type of person who would rather do some learning, some discovery, some hits and trials and go to solution #2.

First steps

Now I happen to know of a tool, called a webDriver. A web driver is a tool which could communicate with a browser and tell it to perform what actions you desire through programming. I have done clicks, navigations', hovers, execution of javascript etc through it, and I truly believed that I could go one step further and do file downloads as well. I started digging.

Second thought

OK, that was the beginning of the story. The overall steps and happenings of the story are not always funny or adventurous as you might have been thinking. Keeping this in mind, lets directly go to the interesting parts of findings and problem solving and skip the boring part of blood sucking labors and hopeless efforts.

The technical problem

To find some videos in youtube is not really a big problem. It is very easy to write a selenium script that will open a browser, navigate to the youtube site and search for a term in the search box. Now when the search box is available. The real problem begins. Lets say we reached the video page where the video is being played.

clip_image002

The problem are as pointed below.

1. Youtube does not have a direct link to download the video.

Luckily there are plugins which you could just install to the Firefox and it will add a download link to your youtube video page. You just have to click it and then save it. Now another problem which I later found was that, Firefox had these things called the profiles. When you install Firefox for the first time(I am using windows7), a default profile is made. When you install extensions on Firefox from default profile, it is available only for the instance of Firefox opened from default profile. Now selenium opens Firefox with a new profile by default. So the add ons are not available. I had three options.

· Configure Firefox to open with the default profile.

· Create a different profile and configure Firefox to use this one each time.

· Pass Firefox with the add-on files each time to add to itself.

Solution number was not a good idea, mixing normal browsing and development profiles as same. So I went for solution #2.

Steps.

Ø Create a new profile called selenium.

o Hit ctrl + r on windows

o Type “Firefox –profilemanager”

o Create a new profile named selenium, directory to somewhere other than the default place like E:FirefoxSeleniumProfiles

o Now while creating instance of selenium, configure it to use this profile.


FirefoxProfile profile = new FirefoxProfile(new File("E:ProgrammingFirefox-selenium-profile"));

WebDriver driver = new FirefoxDriver(profile);
Ok, the Firefox started with this WebDriver will use that particular profile. You could install the youtube downloader add-ons on that profile by opening its instance and installing it there.

The problem to the point where there is no download button available on the video page of selenium is solved. Now when you manually click the download button and then the mp4 option, a window appears asking you about what to do with the file. Two options are whether to open it with some registered program, or to save it to some location. Now this window is part of native windows and selenium is unable to handle the window.

So now, the problem is “How to force Firefox to save the file in disk quietly instead of asking for option”. And this happen to be easy. Just while instantiating Firefox, add some more options for that profile.
FirefoxProfile profile = new FirefoxProfile(new File("E:ProgrammingFirefox-selenium-profile"));

profile.setPreference("browser.download.folderList",2);

profile.setPreference("browser.download.manager.showWhenStarting",false);

profile.setPreference("browser.download.dir","E:ProgrammingDownloads");

profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,video/mp4");

WebDriver driver = new FirefoxDriver(profile);

 
These options override the default behavior of Firefox download-manager, specify a default download directory and also the mimetypes of files for which to quietly save the files.

The problem is almost solved. The only logic remaining to write is about which keywords to search, how to find the video urls, and how to iterate through those urls. These are yours choice. I have added a sample code to iterate to the urls of the searched videos.
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Firefox.FirefoxDriver;
import org.openqa.selenium.Firefox.FirefoxProfile;
public class WithExtension {
 static WebDriver getExtendedFirefox(){
 FirefoxProfile profile = new FirefoxProfile(new File("E:ProgrammingFirefox-selenium-profile"));  profile.setPreference("browser.download.folderList",2);  profile.setPreference("browser.download.manager.showWhenStarting",false);  profile.setPreference("browser.download.dir","E:ProgrammingDownloads");  profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,video/mp4");
 WebDriver driver = new FirefoxDriver(profile);
 return driver;
}
public static void main(String[] args) {
 WebDriver driver = getExtendedFirefox();
 driver.get("http://www.youtube.com/watch?v=2V9h1aa-Cro");  driver.navigate().to("https://www.youtube.com");
 driver.findElement(By.id("masthead-search-term")).sendKeys("java tutorial advanced leveln");
 longWait(5000);
 WebElement videoSection = driver.findElement(By.id("gh-activityfeed"));
 List videos = videoSection.findElements(By.className("yt-lockup"));
 System.out.println("The number of videos is " + videos.size());
 for(WebElement vcontent : videos){
  WebElement vpanel = vcontent.findElement(By.className("yt-lockup-content"));
  WebElement vtitle = vpanel.findElement(By.className("yt-lockup-title"));
  System.out.println(vtitle.getText());
  System.out.println(vtitle.findElement(By.tagName("a")).getAttribute("href")+"n");
}
}
public static void longWait(long a){
 try { Thread.sleep(a); }
 catch (InterruptedException e) {}
}
}

Software Testing Approach

1       Abstract

Testing is one of the common tasks almost all the application and software developers out there are adopting. One who is not using automated testing is really a dumb ass and too slow to be in a fast lane. Applications have lots of errors in them when they are developed. The test after development approach cannot simply point out all of them. The purpose of testing is not to completely point out all the defects, but to keep checking the most common types of bugs that could be introduced in the application while trying to change the other features.

2       Introduction

What most of the companies out there do? They have one or two products as their main product. Most of them being analytics application, forums, portals etc . What these applications offer is some set of features and functions. There are so many modules and each click of a button in each page is expected to be linked with some form of feature. Each month or so, they have to upgrade their applications with some requests of the clients. Clients pay lots of money for the applications, and their demands are also too much. They want refinements, logic change, design change, additional features integrated in the application etc. During the process of adding these additional features, there are often change in the previous modules or they are affected in some way.

3       Types of Testing

3.1            Smoke Test

Smoke testing or sanity testing is one very basic form of testing where a software application is tested with very basic features. The approach is
Go to some particular pages
Set some values in some inputs
Click some buttons/links
Verify some features occur or not.
There is a test-plan where these things are written.
sn
Location(Page)
Expected features
Test/Result
Remarks
1
There should be a test box. Starting to type there should result in suggestions dropping down.
Pass with Comments
The expected conditions were met but these things were noticed
2
drive.google.com

Now the person doing the manual testing has to go to the location. Do what is to be done specified in the test plan. There may be a separate column for that as well. There is a test result column which has a conditional formatting. Most commonly, Green for Pass, Red for Fail and Yellow for pass-with-comments. Sometimes the user has to have a good understanding of the product (domain knowledge) to do what is instructed to be done. If there is anything worth mentioning that is noticed during the test, there is a Remarks column.

3.2            Regression Test

Regression testing is a testing where you navigate to each pages there is on the application, and check certain values or features that’s needs to be verified. It’s the QA’s responsibility to prepare a checklist for the pages to be tested and the features for each pages that are to be tested or verified one round before taking the product to production environment at verify them at least once after the release for quick bugs discovery and hot fixes.
sn
page
Feature
Result
Remark
1
Page 1
feature 1
Pass
2
Page 1
feature 2
Fail
3
Page 1
feature 3
4
Page 1
5
Page 2
6
Page2

The template seems same as the Smoke test but the main difference between Regression test and Smoke test is that it is much detailed and covers every page to be tested and every features that is to be tested. This is a very long checklist of items to be tested.

3.3            Data Validation Test

There may or may not be a data validation test based on the scenario and the scope of change and also the type of change. Generally, the general data validation test is covered by the smoke and regression test itself. While testing the general features you happen to be checking the data also that are populated in the tables or paragraphs, forms, drop-downs etc. The data in the backend may be directly affecting the items and options you see from your  account in the application. A separate data validation test is needed in some of the major changes like server-migration, transferring data from one place to another, pointing to different instance of the backend etc. Separate SQL scripts may also be made to compare the tables of the backend.

3.4            Before After Test

This is the test which is most often done each time adding new features in the existing application, or migrating data/application from one place to another etc. One module being developed, and the changes might be affecting the others as well. In the development environment, the changes and the new modules are integrated and two instances of application are run. One of the past release which is assumed to be correct(mostly) and other which is the latest application. The application should point to identical dataset which may or may not be the same instance and if there is a security framework or dashboard feature control system implemented in the application then the two accounts used for testing should have the same settings. Now unlike the regression and smoke test where main focus in on the features like you clicking some buttons and links and expecting something to happen, here you focus on the data. You could simply use the text comparison tools where you feed with string of different text and compare if they are exactly the same. Mostly the differences are found in tables, forms etc. Some custom developed automated testing applications could find which table which column in which page is not matching in the two applications.
Application
Application a on xyz1 module x, Application b on xyz2 module b
Tested by
Rajan Prasad Upadhyay


Date
20 july to 30 july 2013


sn
Column1
Feature
Result
Remark
1
Page 1 of a and page 1 of b
feature 1
Pass
2
feature 2
Fail
3
feature 3
4
5
6
I am just reusing the template for simplicity.

3.5            Load Testing

This is not a very common testing. Load testing is generally done for those applications where performance is an issue. For example a web application takes too long to load, or some feature takes whole lot of time, One module takes too long to process etc. Load testing in one sentence is the test of response time vs number of users. When the number of users simultaneously using an application increases, the throughput decreases and response time increases. On the basis of how many users might be using the system simultaneously, proper care should be taken to keep the response time to an acceptable limit.

There are some other kinds of testing also but these are the most common types of testing most of the applications are doing for their product. They are investing even millions yearly for them getting automated.

4       Tools


There are lots of tools being used for software applications testing. I don’t personally know whole lot of tools but am familiar with a few of them. The most commonly used tool to automate the web-application is Selenium-WebDriver. It is an api and is available in most common languages. What it does is it acts as a user and could open a url, make clicks, find elements and grab their texts, execute javascripts etc using a browser just as a human would do. JMeter is a free tool in java for performance testing. You create a test plan add different ready-made components, configure them and run them. You could visualize your data in different ways.

Summary

So this was my understanding of the general way it is in this world. The article's main purpose is to give a general concept of the processes followed while testing, and the way testing is done, tools used for testing, etc.