Friday, June 24, 2011

Upcoming challenges

- Working out the solution to extract CSS style from html element in Selenium RC
- Studying Ruby
- Studying MySQL. (I'm currently pretty much familiar with T-SQL so I hope it will help me much)

Wednesday, June 22, 2011

How to hide some data in log4j log you consider to be secured

Once we get the requirement saying the customer does not want some data gets transfered to the log file since it looks secure for them. We even should have the capability of choosing whether to show such data in logs. The quick way to address the requirement is to override one method in PatternLayout class. This will look like:

import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

public class SecureLayout extends PatternLayout{

                static String pattern;
                
                public static void setPattern(String pattern){
                                SecureLayout.pattern = pattern;
                }

                @Override
                public String format(LoggingEvent event) {
                                String string = super.format(event);
                                return pattern == null ?  string : string.replaceAll(pattern, "NotForYourEyes");
                }

}

Such the way will allow us to set which data we consider to be secured from any place of the code. So you should only place the class under the classpath and use the following construction in log4j.xml

    <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="logfile.log"/>
        <param name="Append" value="true"/>
        <param name="MaxFileSize" value="1000KB"/>
        <param name="MaxBackupIndex" value="1000"/>
        <layout class="some.package.SecureLayout">
            <param name="ConversionPattern" value="%-5p [%d{ISO8601}] - %m%n"/>
        </layout>
    </appender>

However such solution of described engineering problem has some drawback. It means we are not capable to hide the data in exception description as exceptions are handled in another way. However that problem can be solved by introducing the changes in Exception class.

Friday, June 17, 2011

Selenium RC under IE9. Script error on selenium server console opening.

You should remove all the security settings. Make your browser as much vulnerable as possible and you will get your selenium scripts working :)

How to check if sub-string belongs to the string in batch script

Check new series of the articles. Review and user experience on test management systems. Functionality and usability.
-------------------

Recently I was in need to prepare the batch file taking the sting and some pattern as the parameters. They required that file to check if that string contains the certain substring and basing on the result execute either one functionality or another. So the solution is below:

rem first parameter should hold the main string
rem second one should hold the substring to be found

SETLOCAL ENABLEDELAYEDEXPANSION 
set "ol=%1"
set handledString=!ol:%2=!
echo."%handledString%"
IF %1 == %handledString% GOTO wayone
IF %1 NEQ %handledString% GOTO waytwo

:wayone
echo."substring not found"
goto end

:waytwo
echo."substring found"
goto end

:end

UPD: Here is more sophisticated way how to do this.

Tuesday, June 07, 2011

Ant custom task in application to the real purpose. Part Two.

Hi!

Here here I started the set of posts about how to apply the customized tasks in Ant to address some real requirement.

So there is a lot of information about how to create custom task in ant. But we're talking about some production requirements. As you may discover in previous post we're trying to create the framework allowing to roll back the changes which have been made anywhere in the ant script, and it doesn't matter whether they were made within one target or the changes were distributed among the several ones. So, to achieve that I decided to extend the native ant tasks performing file operations and I started from Replace task. However I still needed the infrastructure to support this functionality. So the whole workflow looks now like the following:

- We take care of reseting the storage holding the information about which file is mapped to which backup.
- Then we perform safe operations with our files
- Finally we take care of cleaning up the backups being sure all has finished successfully

The particular ANT script will look like this
<project name="Safe replace" default="do-all" basedir=".">

 <taskdef name="safereplace" classname="com.enkata.ant.tasks.SafeReplaceTask">
  <classpath>
   <pathelement path="C:/temp/safereplace.jar" />
  </classpath>
 </taskdef>

 <taskdef name="initstorage" classname="com.enkata.ant.tasks.InitializeStorageTask">  <classpath>
   <pathelement path="C:/temp/safereplace.jar" />
  </classpath>
 </taskdef>

 <taskdef name="cleartemporary" classname="com.enkata.ant.tasks.ClearTempFilesTask">  <classpath>
   <pathelement path="C:/temp/safereplace.jar" />
  </classpath>
 </taskdef>

 <target name="do-all">
  <initstorage/>
  <antcall target="valid"/>
  <antcall target="notvalid"/>
  <cleartemporary/>
 </target>

 <target name="valid">
  <safereplace dir="c:/test_safereplace" token="The" value="HRENAR">
   <include name="**/*.txt" />
  </safereplace>
 </target>

 <target name="notvalid">
  <safereplace file="abc" token="0" value="0">
  </safereplace>
 </target>

</project>

So here are several important points we should pay attention for. First of all it is seen that we use three custom tasks here. Each task has the path to the jar it is represented in. Also we have the general target where we take care of the items I have specified above and call the some valid target (not producing any errors) and some not valid one (that should always fail)

As most of us already know to create the custom task we have to extend ANT native class Task or any class that is the extension of it. The simple custom task that I use is the following:

public class InitializeStorageTask extends Task {

 @Override
 public void execute() throws BuildException {
  File storage = new File (SafeTasksUtils.BACKUP_STORAGE_FILE_NAME);
  if (storage.exists()){
   FileUtils.delete(storage);
  }
 }

}

I'm not including the default overridden methods not to waste the space. Anyway the IDE will remind you. So we should to implement our own execute() method to make it work.

In the third part I will describe the core of the example means how the extended Replace task works and then in the fourth part the step-by-step implementation workflow will be described using Eclipse IDE.

Thank you!