org.unitils.dbmaintainer.script
Class Script

java.lang.Object
  extended by org.unitils.dbmaintainer.script.Script
All Implemented Interfaces:
Comparable<Script>

public class Script
extends Object
implements Comparable<Script>

A class representing a script file and it's content.

Author:
Filip Neven, Tim Ducheyne

Constructor Summary
Script(String fileName, Long fileLastModifiedAt, ScriptContentHandle scriptContentHandle)
          Creates a script with the given script fileName, whose content is provided by the given handle.
Script(String fileName, Long fileLastModifiedAt, String checkSum)
          Creates a script with the given fileName and content checksum.
 
Method Summary
 int compareTo(Script script)
          Compares the given script to this script by comparing the versions.
protected  Version createVersion(List<Long> parentIndexes, File scriptFile)
          Creates a version for the given script file.
 boolean equals(Object obj)
           
protected  Long extractIndex(String pathPart)
          Extracts the index part out of a given file name.
 String getCheckSum()
           
 Long getFileLastModifiedAt()
           
 String getFileName()
           
 ScriptContentHandle getScriptContentHandle()
           
 Version getVersion()
           
protected  Version getVersionFromPath(String relativePath)
           
 int hashCode()
           
 boolean isIncremental()
           
 boolean isScriptContentEqualTo(Script other, boolean useLastModificationDates)
           
 String toString()
          Gets a string representation of this script.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Script

public Script(String fileName,
              Long fileLastModifiedAt,
              ScriptContentHandle scriptContentHandle)
Creates a script with the given script fileName, whose content is provided by the given handle.

Parameters:
fileName - The name of the script file, not null
fileLastModifiedAt -
scriptContentHandle - Handle providing access to the contents of the script, not null

Script

public Script(String fileName,
              Long fileLastModifiedAt,
              String checkSum)
Creates a script with the given fileName and content checksum. The contents of the scripts itself are unknown, which makes a script that is created this way unsuitable for being executed. The reason that we provide this constructor is to be able to store information of the script without having to store it's contents. The availability of a checksum enables us to find out whether it's contents are equal to another script objects whose contents are provided.

Parameters:
fileName - The name of the script file, not null
fileLastModifiedAt -
checkSum - Checksum calculated for the content of the script
Method Detail

getFileName

public String getFileName()
Returns:
The script name, not null

getFileLastModifiedAt

public Long getFileLastModifiedAt()
Returns:
The timestamp at which the file in which this script is stored on the filesystem was last modified. Be careful: This can also be the timestamp on which this file was retrieved from the sourcde control system. If the timestamp wasn't changed, we're almost 100% sure that this file has not been modified. If changed, this file is possibly modified (but the reason might also be that a fresh checkout has been made from the version control system, or that the script was applied from another workstation or from another copy of the project), not null

getCheckSum

public String getCheckSum()
Returns:
Checksum calculated for the content of the script, not null

getVersion

public Version getVersion()
Returns:
The version, not null

getScriptContentHandle

public ScriptContentHandle getScriptContentHandle()
Returns:
Handle that provides access to the content of the script. May be null! If so, this object is not suitable for being executed. The checksum however cannot be null, so we can always verify if the contents of the script are equal to another one.

isScriptContentEqualTo

public boolean isScriptContentEqualTo(Script other,
                                      boolean useLastModificationDates)
Parameters:
other - Another script, not null
useLastModificationDates - If true, this method first checks if the lastModifiedAt property of this Script is equal to the given one. If equal, we assume that the contents are also equal and we don't compare the checksums. If not equal, we compare the checksums to find out whether there is a difference. By setting this value to true, performance is heavily improved when you check for updates regularly from the same workstation (which is the case when you use unitils's automatic database maintenance for testing). This is because, to calculate a checksum, the script contents have to be read. This can take a few seconds to complete, which we want to avoid since a check for database updates is started every time a test is launched that accesses the test database. For applying changes to an environment that can only be updated incrementally (e.g. a database use by testers or even the production database), this parameter should be false, since working with last modification dates is not guaranteed to be 100% bulletproof (although unlikely, it is possible that a different version of the same file is checked out on different systems on exactly the same time).
Returns:
True if the contents of this script are equal to the given one, false otherwise

isIncremental

public boolean isIncremental()
Returns:
True if this is an incremental script, i.e. it needs to be executed in the correct order, and it can be executed only once. If an incremental script is changed, the database needs to be recreated from scratch, or an error must be reported.

compareTo

public int compareTo(Script script)
Compares the given script to this script by comparing the versions. Can be used to define the proper execution sequence of the scripts.

Specified by:
compareTo in interface Comparable<Script>
Parameters:
script - The other script, not null
Returns:
-1 when this script has a smaller version, 0 if equal, 1 when larger

createVersion

protected Version createVersion(List<Long> parentIndexes,
                                File scriptFile)
Creates a version for the given script file. The index -1 is used for files that do not have a version in the file name.

Parameters:
parentIndexes - The indexes of the parent folders, not null
scriptFile - The script file, not null
Returns:
The version of the script file, not null

getVersionFromPath

protected Version getVersionFromPath(String relativePath)

extractIndex

protected Long extractIndex(String pathPart)
Extracts the index part out of a given file name.

Parameters:
pathPart - The simple (only one part of path) directory or file name, not null
Returns:
The index, null if there is no index

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

toString

public String toString()
Gets a string representation of this script.

Overrides:
toString in class Object
Returns:
The name and version, not null


Copyright © 2011. All Rights Reserved.