ATS : File System Snapshots - Inspect the content of some file types

Introduction

Sometimes comparing files attributes is not enough.

Imagine you have instances of an XML file with identical content. If some of the XML nodes are moved around, we will tell you the files are different in terms of size and MD5, but the meaningful content is the same indeed.

This page shows how to compare the content of XML, Properties, INI and TEXT files without worrying about the file size and MD5 checksum.


Comparing properties files

Skipped data while comparing

Following is some data that will not be taken into consideration while comparing:

  • order of the key-value pairs
  • empty spaces around keys and values
  • commented lines

Samples files to compare

Here is the first properties file:

db.parameters.host=127.0.0.1
db.parameters.port=3306
db.parameters.name=testdb
db.parameters.username=ATSUser
db.parameters.password=bestPa$$w0rd

And here is the second one:

db.parameters.host=127.0.0.1
db.parameters.port  =         3306

# this is some comment
db.parameters.password=bestPassword

db.parameters.name=testdb
db.parameters.username=ATSUser

Compare result

If both properties files are present in your snapshot comparison, you will get some output like:


Comparing [snap1] and [snap2] produced the following unexpected differences:

Different files:
[snap1] file "C:/dir1/dbconnection.properties" - [snap2] file "C:/dir2/dbconnection.properties":
    property key 'db.parameters.password': 'bestPa$$w0rd' - 'bestPassword'

Do not compare some properties

If some properties are not of interest, there are ways to skip them while checking the content of the file

You can skip properties with some keys:

// Skip a property which key equals some value. Check does not count the letter case
snapshot1.properties.skipPropertyByKeyEqualsText( "F1", "dbconnection.properties", "db.parameters.password" );

// Skip a property which key contains some value. Check does not count the letter case
snapshot1.properties.skipPropertyByKeyContainingText( "F1", "dbconnection.properties", "password" );

// Skip a property which key matches some java regular expression
snapshot1.properties.skipPropertyByKeyMatchingText( "F1", "dbconnection.properties", ".*pass.*" );

You can skip properties with some values:

// Skip a property which value equals some value. Check does not count the letter case
snapshot1.properties.skipPropertyByValueEqualsText( "F1", "dbconnection.properties", "bestPassword" );

// Skip a property which value contains some value. Check does not count the letter case
snapshot1.properties.skipPropertyByValueContainingText( "F1", "dbconnection.properties", "BEST" );

// Skip a property which value matches some java regular expression
snapshot1.properties.skipPropertyByValueMatchingText( "F1", "dbconnection.properties", "best.*" );


Comparing XML files

Skipped data while comparing

Note that the following data that will not be taken into consideration while comparing:

  • order of the same XML nodes
  • empty spaces outside of XML nodes
  • commented nodes
  • double vs single empty nodes - for example "<building></building>" vs "<building/>"

Samples files to compare

Here is the first XML file:

<?xml version="1.0" encoding="utf-8"?>
<employees>
    <employee id="007">
        <name>Will Smith</name>
        <title>Showman</title>
        <division>Hollywood</division>
        <building></building>
    </employee>
    <employee id="009">
        <name>Indiana Jones</name>
        <title>Adventurer</title>
        
        <building></building>
    </employee>
</employees>

And here is the second one:

<?xml version="1.0" encoding="utf-8"?>
<employees>
    <employee id="008">
        <name>Will Smith</name>
        <title>Showman</title>
        <division>Hollywood</division>
        <building/>
    </employee>
    <employee id="009">
        <name>Indiana Jones</name>
        <title>Adventurer</title>
        <division>Hollywood</division>
        <building></building>
    </employee>
</employees>

Compare result

If both example XML files are present in your snapshot comparison, you will get some output like:

Comparing [snap1] and [snap2] produced the following unexpected differences:

Different files:
[snap1] file "C:/dir1/employees.xml" - [snap2] file "C:/dir2/employees.xml":
    Presence of XML node 
    <employees>
        <employee id="007"></employee>: 
    YES - NO

    Presence of XML node 
    <employees>
        <employee id="008"></employee>: 
    NO - YES

    Presence of XML node 
    <employees>
        <employee id="009">
            <division>Hollywood</division>: 
    NO - YES

You see that:

  • employee with id "007" is present in first snapshot, but not present second snapshot
  • employee with id "008" is present in second snapshot, but not present first snapshot
  • employee with id "009" has a <division> sub-node in second snapshot, but not in first snapshot

Do not compare some nodes

If some nodes are not of interest, there are ways to skip them while checking the content of the file

You can skip nodes with some specific attributes:

// Skip a node with attribute value equal to some specified value. Check does not count the letter case
snapshot1.xml.skipNodeByAttributeValueEqualsText( "F1", "employees.xml", "//employees/employee", "id", "009" );

// Skip a node with attribute value containing some specified value. Check does not count the letter case
snapshot1.xml.skipNodeByAttributeValueContainingText( "F1", "employees.xml", "//employees/employee", "id", "09" );

// Skip a node with attribute value matching some java regular expression
snapshot1.xml.skipNodeByAttributeValueMatchingText( "F1", "employees.xml", "//employees/employee", "id", ".*9" );

You can skip nodes with some values:

// Skip a node with value equal to some specified value. Check does not count the letter case
snapshot1.xml.skipNodeByValueEqualsText( "F1", "employees.xml", "//employees/employee/division", "hollyWOOD" );

// Skip a node with value containing some specified value. Check does not count the letter case
snapshot1.xml.skipNodeByValueContainingText( "F1", "employees.xml", "//employees/employee[@id=\"009\"]/division", "Hollywood" );

// Skip a node with value matching some java regular expression
snapshot1.xml.skipNodeByValueMatchingText( "F1", "employees.xml", "//division", ".*wood" );

The 3rd argument is XPATH pointing to the node to skip.

Please refer here to see how to construct xpath expressions.


Back to parent page


Comparing INI files

As Wikipedia advices the INI file format is an informal standard for configuration files for some platforms or software.

We expect that there are sections in the INI file which look in the form of [some section name] . Each section contains a number of key-value pairs in the form key=some value. Additionally each line starting the with ; sign is considered to a comment.

Skipped data while comparing

Following is some data that will not be taken into consideration while comparing:

  • order of the key-value pairs
  • empty spaces around keys and values
  • commented lines

Samples files to compare

Here is the first properties file:

[Mail]
CMCDLLNAME32=mapi32.dll
#CMC=123456
OLEMessaging=option 1

[languages]
00A=Spanish

And here is the second one:

[Mail]
#CMC=123456789
CMCDLLNAME32=mapi32.dll

[languages]
00A=Spanish

Compare result

If both example XML files are present in your snapshot comparison, you will get some output like:


Comparing [snap1] and [snap2] produced the following unexpected differences:

Different files:
[snap1] ini file "C:/dir1/simple_ini_file.ini" - [snap2] ini file "C:/dir2/simple_ini_file.ini":
    Section [Mail], presence of key 'OLEMessaging': YES - NO

Do not compare some properties

If some INI properties are not of interest, there are ways to skip them while checking the content of the file

You can skip INI properties with some keys:

// Skip a property which key equals some value. Check does not count the letter case
snapshot1.ini.skipIniPropertyByKeyEqualsText( "F1", "simple_ini_file.ini", "[Mail]", "OLEMessaging" );

// Skip a property which key contains some value. Check does not count the letter case
snapshot1.ini.skipIniPropertyByKeyContainingText( "F1", "simple_ini_file.ini", "[Mail]", "olem" );

// Skip a property which key matches some java regular expression
snapshot1.ini.skipIniPropertyByKeyMatchingText( "F1", "simple_ini_file.ini", "[Mail]", "OLE.*" );

You can skip INI properties with some values:

// Skip a property which value equals some value. Check does not count the letter case
snapshot1.properties.skipIniPropertyByValueEqualsText( "F1", "simple_ini_file.ini", "[Mail]", "option 1" );

// Skip a property which value contains some value. Check does not count the letter case
snapshot1.ini.skipIniPropertyByValueContainingText( "F1", "simple_ini_file.ini", "[Mail]", "option" );

// Skip a property which value matches some java regular expression
snapshot1.ini.skipIniPropertyByValueMatchingText( "F1", "simple_ini_file.ini", "[Mail]", "option.*" );

Or you can skip a whole section:

snapshot1.ini.skipIniSectionEqualsText( "F1", "simple_ini_file.ini", "[Mail]" );

snapshot1.ini.skipIniSectionContainingText( "F1", "simple_ini_file.ini", "Mai" );

snapshot1.ini.skipIniSectionMatchingText( "F1", "simple_ini_file.ini", "\\[Ma.*" ); 

Comparing TEXT files

This is the simplest comparison, each lines is processed as one unbreakable token.

Skipped data while comparing

Following is some data that will not be taken into consideration while comparing:

  • order of the text lines
  • empty spaces around keys and values

Samples files to compare

Here is the first properties file:

When a father gives to his son, both laugh; when a son gives to his father, both cry. 
By William 
   
   Shakespeare

And here is the second one:

When a father gives to his son, both laugh; when a son gives to his father, both cry. 

   
   Shakespeare

Compare result

If both example TEXT files are present in your snapshot comparison, you will get some output like:


Comparing [snap1] and [snap2] produced the following unexpected differences:

Different files:
[snap1] text file "C:/dir1/file1.txt" - [snap2] text file "C:/dir2/sub-dir1/file1.txt":
    Presence of line By William: YES - NO

Do not compare some lines

If some text lines are not of interest, there are ways to skip them while checking the content of the file:

// Skip specified line. Check does not count the letter case
snapshot1.text.skipTextLineEqualsText( "F1", "file1.txt", "By William" );

// Skip lines containing some value. Check does not count the letter case
snapshot1.text.skipTextLineContainingText( "F1", "file1.txt", "William" );

// Skip lines which match some java regular expression
snapshot1.text.skipTextLineMatchingText( "F1", "file1.txt", "By.*" );

Configuration

You can change the way this comparison works. You need to call some of the setter methods:

ActionLibraryConfigurator.getInstance().snapshots.setXYZ...

Specify which files to be treated as TEXT file

Based on the file extension, you can specify which files to be treated as TEXT files. By default all files with extension ".txt" are in the list:

// This code sets all files with the specified file name extensions to be treated as TEXT files
ActionLibraryConfigurator.getInstance().snapshots.setTextFileExtensions( ".txt", ".abc" );

Similarly you can work with the other supported types by calling setXmlFileExtensions() or setPropertiesFileExtensions() or setIniFileExtensions()

Disable checking the content of some file types

By default the content of all file types discussed on this page are checked. How ever, you can disable some of them:

ActionLibraryConfigurator.getInstance().snapshots.setCheckXmlFilesContent( false );
ActionLibraryConfigurator.getInstance().snapshots.setCheckPropertiesFilesContent( false );
ActionLibraryConfigurator.getInstance().snapshots.setCheckIniFilesContent( false );
ActionLibraryConfigurator.getInstance().snapshots.setCheckTextFilesContent( false )

Specify INI file special characters

The default start character of INI section is '['. Here is how to change it:

ActionLibraryConfigurator.getInstance().snapshots.setIniFilesStartSectionChar( '<' );

The default start comment character is '#'. Here is how to change it:

ActionLibraryConfigurator.getInstance().snapshots.setIniFilesStartCommentChar( ';' );

The default key-value delimiter character is '='. Here is how to change it:

ActionLibraryConfigurator.getInstance().snapshots.setIniFilesDelimeterChar( '-' );

Back to parent page

Go to Table of Contents