grep pattern inluding new line character

13/01/2012
bawi@hp:~$ cat a.txt
aaabbb
cccddd

dddbbb
xxxxxx
cccfff
bawi@hp:~$ pcregrep -M 'b\nc' a.txt
aaabbb
cccddd
bawi@hp:~$ pcregrep -M 'b(\n|.)*c' a.txt
aaabbb
cccddd

dddbbb
xxxxxx
cccfff
bawi@hp:~$ cat b.txt
1.ERROR test1
known-pattern1
other1.1
2.DEBUG test2
some data2.1
some data2.2
3.ERROR test3
undefined
other3.1
4.ERROR test4
known-pattern2

bawi@hp:~$ pcregrep -M 'ERROR .*\n' b.txt | pcregrep -M -v '\nknown-pattern1' | pcregrep -M -v '\nknown-pattern2'
3.ERROR test3
undefined

maven deploy

07/12/2011

mvn deploy:deploy-file -DrepositoryId=upload-releases  -DgroupId=com.bawi -DartifactId=my-project -Dversion=1.0.0-20111206 -Dpackaging=jar -DgeneratePom=true -Durl=http://maven.bawi.com/content/repositories/releases  -Dfile=my-project-1.0.0-20111206.jar

 

mysql db setup

19/10/2011

sg0212148@ubuntu:~$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> create table report (student text, subject text, score int);
Query OK, 0 rows affected (0.00 sec)

insert into report (student, subject, score) values (“st1″,”sb1″,3);
insert into report (student, subject, score) values (“st2″,”sb1″,4);
insert into report (student, subject, score) values (“st3″,”sb1″,5);
insert into report (student, subject, score) values (“st4″,”sb2″,4);
insert into report (student, subject, score) values (“st5″,”sb2″,5);
insert into report (student, subject, score) values (“st6″,”sb2″,6);
insert into report (student, subject, score) values (“st7″,”sb3″,5);
insert into report (student, subject, score) values (“st8″,”sb3″,6);
insert into report (student, subject, score) values (“st9″,”sb3″,7);
mysql> select * from report;
+———+———+——-+
| student | subject | score |
+———+———+——-+
| st1      | sb1      |     3 |
| st2      | sb1      |     4 |
| st3      | sb1      |     5 |
| st4      | sb2      |     4 |
| st5      | sb2      |     5 |
| st6      | sb2      |     6 |
| st7      | sb3      |     5 |
| st8      | sb3      |     6 |
| st9      | sb3      |     7 |
+———+———+——-+
9 rows in set (0.00 sec)

mysql>  select student,subject from report where score > 4;
+———+———+
| student | subject |
+———+———+
| st3      | sb1      |
| st5      | sb2      |
| st6      | sb2      |
| st7      | sb3      |
| st8      | sb3      |
| st9      | sb3      |
+———+———+
6 rows in set (0.00 sec)

mysql> select count(subject), subject from (select subject from report where score > 4) as SS group by subject;
+—————-+———+
| count(subject) | subject |
+—————-+———+
|              1 | sb1      |
|              2 | sb2      |
|              3 | sb3      |
+—————-+———+
3 rows in set (0.00 sec)

mysql> select count(subject), subject from (select subject from report where score > 4) as SS group by subject having count(subject)  > 1;
+—————-+———+
| count(subject) | subject |
+—————-+———+
|              2 | sb2      |
|              3 | sb3      |
+—————-+———+
2 rows in set (0.00 sec)

where‘ operates on individual rows to select row that later may be use for grouping (‘where’ cannot be used after grouping)

having‘ operate on agregate functions

you cannot have ‘having’ without group by for proper results:
mysql> select count(subject), subject from (select subject from report where score > 4) as SS having count(subject)  > 1;
+—————-+———+
| count(subject) | subject |
+—————-+———+
|              6 | sb1      |
+—————-+———+

Protected: Interview questions

07/10/2011

This post is password protected. To view it please enter your password below:


change shell

28/09/2011

‘chsh’ – changes shell

Global state

07/07/2011

Class state (opposed to object state) is persistent to lifetime of JVM.

In Global State multiple executions can produce different results for new A().doSth();

Singleton – design pattern, static object in the global space, each field in a singleton is a holds a global state since it is a global variable. Everything accessible via Singleton instance field has a global variable. They cannot be garbage collected and are kept in JVM for its lifetime.

Singletons are bad for test since tests cannot fully control the state of the Singletons. If Singletons have any dependecies then tests cannot control instantiation process of these fields / dependencies.
BAD: Application has method that internally calls AppSettings.getInstace.doSth(). While testing Application how do you test in doSth() was really called. When AppSettings is a Singleton (design pattern) you cannot test it.
GOOD: Instead of this, design your Appsettings to have public constructor and a non-static doSth() method so that you cannot mock it. Then the Application wiil set AppSetting in the constructor and assign as instance field. Then you can verify if a call to Application method will internally call Appsettings doSth() method.

singleton – a single instance of an object that is intantiated once (only once was the constructor called) and is not attached to global space

you should ensure that objects are instantiated in the correct order and methods are called in the correct order. You can achieve it by explicitly passing the objects referenced to constructor or a method. Then, there is no hidden conversation between the objects and it is harder to make mistakes.

Dependency injection:
- enforces the proper order of initialization at compile time
- helps to define where you want to cut the dependecies boundaries so that you can mock the rest.

Writing code hard to test

07/07/2011

How to write code that is hard to test?
- most people say: making things private, using private keyword, long methods (monolythic code)

Real issues

  1. mixing “new” operator with business logic prevents you from testing things in isolation (wrong: creating new objects directly in the business logic instead of using them)
  2. looking for things
  3. business logic in the constructor (in constructor you should ask for dependencies by constructor arguments and assign them to instance fields. Business logic should be in methods that use these dependecies but not construct them)
  4. having global state
  5. using static methods (which is essentialy procedural programming), it is easy to test leaf methods that do not call anybody else (Math.absolute(value)), but if a this static method has a bunch of collaborators then you cannot test this method in isolation, you cannot mock the db (cannot override static method). The worst thing is trying to test your application from the main method.
  6. deep inheritance hierarchy (if your class under test extends A, B, C classes you are also testing those classes if you want or not)
  7. too many conditionals (if statements – you need to prepare lots of test data to get throught the path you want)

Scope of tests

  1. Large number of unit tests – class level tested in insolation – very fast tests
  2. Many functional tests – collection of classess as subsystem – medium tests
  3. Some scenario tests – for the whole system – slow tests

The end to end tests are important but they take a lot of time and there are so many things that can go wrong so making sure every is set up correctly is hard and time consuming. It is hard to reproduce failures. Better test smaller portions of program and some happy path (all the things are wired up together correctly). Break the applicaion into subsystem and even invidual components and test the invidually.

For bad code it is hard to write good test.

In order to test classes with dependencies you should be able to have a choice to:

  1. mock dependencies or
  2. stub dependencies or
  3. instantiate a real class that has been alread well tested.

Clean code

25/05/2011

In order to test a instance method you need to instantiate the object, so do not put any business logic code into the constructor or static factory methods.

Move the dependencies to constructor parameters so that you do not need to care how they get instantiated. We can pass in a mocked dependency or a real object.

Having dependencies defined in constructor argumeters makes the API more clear.

That makes testing much more easier since mock all the dependencies.

Write many small tests for business logic and create a one happy path test that everything is wired together correctly.

In constructor we should be asking what we need but not creating it ourselves, should not create and use any Factories inside constructor/method code.

Ideally, you would have only field assignments in your constructor.

Tests about instantiating small pieces of your application.

You could use service locator (aka context or registry) to create an object for you instead of creating it yourself. However, in order to get the mocked object you would have to override the service locator methods which can be hard sometimes (looking into source code etc…)

When there are multiples constructor parameters and you want to test behavior based on the first paramter, then you can make the other null in tests, meaning that do not take part in the test.

Law of demeter: only ask for the objects that you need directly, don’t use intermediate object that will the get what you need for you.

If your child object needs a new parameter in its constructor, it should not be added to the as parameter parent constructor. Parent object need only to know about the child, not the children dependencies. Otherwise the parent would violate the law of demeter adding as its constuctor parameter something he doesn’t directly needs.

One factory plus a couple of break down factories  per object lifetime.

As a parameter of a constructor put objects of longer or the same lifetime. Put objects of a shorter lifecycle as method parameters.

For external api the will be used by other people, put validation logic into constructor. For internal api it is not suggested to use these checks since it make testing harder (need to meet extra preconditions before you really can start testing) – in this case tests are better than runtime precondition / check.

For production code you should return non null objects (not to get NPE). For test code passing null as a parameter makes it clear that this parameter is not important for the test.

For production code do not call ‘new’ operator (do not instantiate objects directly) in constructor so that somebody can pass in you a mock in the constructor argument. You should ask for what you want instead creating it by yourself.

The ‘new’ operator should be used only in tests and factories. Exception are the leaves of the application graph, there is nothing behind them, e.g.: value objects (e.g. User) or collections (new HashMap).

Law of demeter: only ask for the thinks that you need directly, you shouldn’t know about the objects you don’t need.

Objects should be divided into two categories:

1) business logic objects that do the logic in your application (here are most of bugs so you should make them easy to instantiate and  to test)

2) Pile of factories, the ‘new’ operators, builders etc …

Then you can test objects instantiation and business logic separately, in isolation.

Cygwin

05/03/2011

~/.bash_profile:


umask 022
set -o ignoreeof
shopt -s cdspell histappend no_empty_cmd_completion

export EDITOR="vim"
#export HISTCONTROL="ignoreboth:erasedups"
export HISTFILESIZE=2500
export HISTSIZE=2500
export HISTIGNORE="*shutdown*:su:[bf]g:exit:logout:rm *:sudo rm *"
export PATH=$PATH:~/bin

export LANG="en_US.ISO8859-1"
export LC_CTYPE="pl_PL.ISO8859-2"
export LC_NUMERIC="pl_PL.ISO8859-2"
export LC_TIME="en_US.ISO8859-1"
export LC_COLLATE="pl_PL.ISO8859-2"
export LC_MONETARY="pl_PL.ISO8859-2"
export LC_MESSAGES="en_US.ISO8859-1"
export LC_PAPER="pl_PL.ISO8859-2"
export LC_NAME="pl_PL.ISO8859-2"
export LC_ADDRESS="pl_PL.ISO8859-2"
export LC_TELEPHONE="pl_PL.ISO8859-2"
export LC_MEASUREMENT="pl_PL.ISO8859-2"
export LC_IDENTIFICATION="pl_PL.ISO8859-2"
export MM_CHARSET="ISO-8859-2"
export LS_COLORS='no=00:fi=00:di=01;36:ln=01;34:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.svgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.aac=00;34:*.au=00;34:*.flac=00;34:*.mid=00;34:*.midi=00;34:*.mka=00;34:*.mp3=00;34:*.mpc=00;34:*.ogg=00;34:*.ra=00;34:*.wav=00;34:';

# Bash completion
[[ -f /etc/profile.d/bash-completion ]] && source /etc/profile.d/bash-completion
# CYGWIN startup directory
CYGWINDIR=c:/dev/
[[ ! -z `echo $CYGWINDIR | grep -v " "` ]] && cd $CYGWINDIR

#source ./setEnv.sh

# Prompt colors
PS1='\[33[01;32m\]\w \$\[33[00m\] '

# Aliases
alias console='unset DISPLAY && rxvt -e bash -login &'
alias ls='ls --color'		# colors

( cd && complete -W "$(echo `cat .bash_history | egrep '^ssh ' | sort | uniq | sed 's/^ssh //'`;)" ssh )

~/.bashrc without lines:

# CYGWIN startup directory
CYGWINDIR=c:/dev/
[[ ! -z `echo $CYGWINDIR | grep -v " "` ]] && cd $CYGWINDIR

cygwin.bat:

@echo off

C:
chdir C:\dev\environment\bin

bash --login -i

cygwin-my-dir.bat:

@echo off

C:
chdir C:\dev\environment\bin

bash --login -i -c 'cd my-dir; bash '

Eclipse plugins

10/02/2011

checkstyle http://eclipse-cs.sf.net/update/
crucible http://update.atlassian.com/atlassian-eclipse-plugin/e3.6
EclEmma Java Code Coverage http://update.eclemma.org/
eCobertura http://ecobertura.johoop.de/update/
FindBugs http://findbugs.cs.umd.edu/eclipse/
m2eclipse http://m2eclipse.sonatype.org/sites/m2e
More Unit http://moreunit.sourceforge.net/update-site/
PMD http://pmd.sourceforge.net/eclipse
Spring IDE http://springide.org/updatesite
subclipse http://subclipse.tigris.org/update_1.6.x


Follow

Get every new post delivered to your Inbox.