Thursday, September 21, 2017

zip to gzip


Assume you are in a directory which has few zip files. Each zip file has a large text file in it. You need to convert the zip file to gzip using unix command. Below is the command. You put all of them in1 line. For readability statements are in their own line

for f in *.zip; 
do 
unzip $f; 
mv ./*.txt `basename $f .zip`.txt;
gzip ./*.txt; 
done

Wednesday, May 31, 2017

Power of XOR

Power of XOR

if (b1 != b2) //XOR
if (b1 == b2) //XNOR

1) Any number xor'd with itself will give zero.
2) Any number xor'd with zero will give the number.

Wednesday, May 24, 2017

Java Maven Code Coverage

Sure Fire - The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats (.txt, .xml)

Fail Safe - The Failsafe Plugin is designed to run integration tests while the Surefire Plugin is designed to run unit tests. The name (failsafe) was chosen both because it is a synonym of surefire and because it implies that when it fails, it does so in a safe way. The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute. The Failsafe Plugin generates reports in two different file formats (.txt, xml)

Jacoco - The JaCoCo Maven code coverage plug-in provides the JaCoCo runtime agent to your tests and allows basic code Coverage report creation. When using the maven-surefire-plugin or maven-failsafe-plugin you must not use a forkCount of 0 or set the forkMode to never as this would prevent the execution of the tests with the javaagent set and no coverage would be recorded.

CoberturaCobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage. It follows Bytecode Instrumentation.

Clover - Clover is designed to measure code coverage in a way that fits seamlessly with your current development environment and practices, whatever they may be. Clover's IDE Plugins provide developers with a way to quickly measure code coverage without having to leave the IDE. Clover's Ant and Maven integrations allow coverage measurement to be performed in Automated Build and Continuous Integration systems, and reports generated to be shared by the team

SonarQubeSonarQube software (previously called Sonar) is an open source quality management platform, dedicated to continuously analyze and measure technical quality, from project portfolio to method

Monday, May 22, 2017

Spring Profiles

If your project has 2 profiles

  1. local
  2. prod
you can choose profile when you run using property

spring.profiles.active

create 2 application.properties files one for each profile

  1. application-local.properties
  2. application-prod.properties
when you run the application pass the profile property like below

  1. mvn spring-boot:run -Dspring.profiles.active=local
  2. mvn spring-boot:run -Dspring.profiles.active=prod

Wednesday, May 10, 2017

Unit Testing Polymer Components

Requirements

  1. Polymer CLI
  2. Web Component Tester - WCT

Overview

  1. Install Polymer CLI
  2. Create An Element Project
  3. polymer test

Install Polymer CLI

  • npm install -g polymer-cli

Create An Element Project

  • mkdir my-el && cd my-el
  • polymer init

Run Test non-interactive

  • polymer test

Run test interactive

  • polymer serve
  • go to localhost:8080/components/my-el/test/my-el_test.html in your browser

Tuesday, May 9, 2017

Hello World Polymer

What is Polymer ?

  1. Library On top of WebComponent API
  2. Bulding custom component is easier with polymer
  3. Provides 2 way data binding
  4. Components are just HTML files

Creating Hello World Component 


helloworld.html

Using Hello World Component


index.html

Open in Browser


index.html

Creating myName Property in hello-world component


hello-world.html

Using myName Property


index.html

Open in Browser


index.html




Friday, May 5, 2017

CSV epoc to Human Readable Date

sample.csv 
apple,red,1494005258000
banana,yellow,1494005306000
lemon,green,1494005640000

Unix Command

grep -Eo '[0-9]{13}' ~/sample.csv | xargs -L1 date -r

Output
Tue Feb 21 04:33:20 PST 49313
Tue Feb 21 17:53:20 PST 49313
Sat Feb 25 14:40:00 PST 49313

Thursday, March 23, 2017

Postgres - Generate Series of dates

Below SQL generate rows of first day of months between 2010 and 2012

select (yr || ''-'' || mnt || ''-'' || 1) ::date as dt from(
(SELECT t.z  AS yr FROM generate_series(2010,2015,1) AS t(z)) z
cross join
(SELECT u.y  AS mnt FROM generate_series(1,12,1) AS u(y)) y
)p

Tuesday, March 14, 2017

QueryDSL

What is QueryDSL?

Querydsl is an extensive Java framework, which allows for the generation of type-safe queries in a syntax similar to SQL

Why QueryDSL?

  1. Code Completion
  2. Type Safety
  3. No Strings Involved
  4. Adapts to Refactoring

Example QueryDSLs

query.from(employee).where(employee.name.eq("auhuman"));
query.from(employee).where(employee.age.gt(35)).and(employee.gender.eq(“Male”);

Maven Dependencies

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>3.7.4</version>
</dependency>
<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>3.7.4</version>
</dependency>

Maven Build Plugin

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources/java</outputDirectory>
                <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

Entity

@Entity
public class Employee {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName, lastName, description;

    private Long count;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }
}

Sample Code

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBodyList<Company> queryDSL(@RequestParam(value = "name", required = false) String name) {
    JPAQuery query = new JPAQuery(entityManager);    
    QCompany company = QCompany.company;    
   if (name == null) {
        return query.from(company).list(company);    
   } else {
        return query.from(company).where(company.name.eq(name)).list(company);    
   }
}

@RequestMapping(value = "/queryDSLGroupByCount", method = RequestMethod.GET)
@ResponseBody
List<Employee> queryDSLGroupByCount() {
    JPAQuery jpaQuery = new JPAQuery(entityManager);
    QEmployee employee = QEmployee.employee;
    return jpaQuery.from(employee).groupBy(employee.lastName).list(Projections.constructor(
            Employee.class,
            employee.lastName,
            employee.count()
    ));
}