org.netbeans.junit
Class NbTestCase

java.lang.Object
  |
  +--junit.framework.Assert
        |
        +--junit.framework.TestCase
              |
              +--org.netbeans.junit.NbTestCase
All Implemented Interfaces:
NbTest, junit.framework.Test

public abstract class NbTestCase
extends junit.framework.TestCase
implements NbTest

NetBeans extension to JUnit's TestCase. This extension adds mostly possibility to compare files (assertFile).


Constructor Summary
NbTestCase(java.lang.String name)
          Constructs a test case with the given name.
 
Method Summary
static void assertFile(java.io.File test, java.io.File pass)
          Asserts that two files are the same, it just compares two files and doesn't produce any additional output.
static void assertFile(java.io.File test, java.io.File pass, java.io.File diff)
          Asserts that two files are the same, it compares two files and stores possible differencies in the output file.
static void assertFile(java.io.File test, java.io.File pass, java.io.File diff, org.netbeans.junit.diff.Diff externalDiff)
          Asserts that two files are the same, it uses specific Diff implementation to compare two files and stores possible differencies in the output file.
static void assertFile(java.lang.String message, java.io.File test, java.io.File pass, java.io.File diff)
          Asserts that two files are the same, it compares two files and stores possible differencies in the output file, the message is displayed when assertion fails.
static void assertFile(java.lang.String message, java.io.File test, java.io.File pass, java.io.File diff, org.netbeans.junit.diff.Diff externalDiff)
          Asserts that two files are the same (their content is identical), when files differ AssertionFileFailedError exception is thrown.
static void assertFile(java.lang.String test, java.lang.String pass)
          Asserts that two files are the same, it just compares two files and doesn't produce any additional output.
static void assertFile(java.lang.String test, java.lang.String pass, java.lang.String diff)
          Asserts that two files are the same, it compares two files and stores possible differencies in the output file.
static void assertFile(java.lang.String test, java.lang.String pass, java.lang.String diff, org.netbeans.junit.diff.Diff externalDiff)
          Asserts that two files are the same, it uses specific Diff implementation to compare two files and stores possible differencies in the output file.
static void assertFile(java.lang.String message, java.lang.String test, java.lang.String pass, java.lang.String diff)
          Asserts that two files are the same, it compares two files and stores possible differencies in the output file, the message is displayed when assertion fails.
static void assertFile(java.lang.String message, java.lang.String test, java.lang.String pass, java.lang.String diff, org.netbeans.junit.diff.Diff externalDiff)
          Asserts that two files are the same (their content is identical), when files differ AssertionFileFailedError exception is thrown.
 boolean canRun()
          Checks if a test isn't filtered out by the active filter.
 void clearWorkDir()
          Deletes all files including subdirectories in test's working directory.
 void compareReferenceFiles()
          Compares default golden file and default reference log.
 void compareReferenceFiles(java.lang.String testFilename, java.lang.String goldenFilename, java.lang.String diffFilename)
          Compares golden file and reference log.
static java.lang.String convertNBFSURL(java.net.URL url)
          Converts NetBeans filesystem URL to absolute path.
 java.io.File getGoldenFile()
          Get the default testmethod specific golden file from data/goldenfiles/${classname}/${testmethodname}.pass
 java.io.File getGoldenFile(java.lang.String filename)
          Get the testmethod specific golden file from data/goldenfiles/${classname} resource directory.
 java.io.PrintStream getLog()
          Return default log named as ${testmethod}.log.
 java.io.PrintStream getLog(java.lang.String logName)
          Returns named log stream.
 java.io.PrintStream getRef()
          Get PrintStream to log inteded for reference files comparision.
 java.io.File getWorkDir()
          Returns unique working directory for a test (each test method have uniq1ue dir).
 java.lang.String getWorkDirPath()
          Returns path to test method working directory as a String.
 void log(java.lang.String message)
          Simple and easy to use method for printing a message to a default log
 void log(java.lang.String log, java.lang.String message)
          Easy to use method for logging a message to a named log
 void ref(java.lang.String message)
          Easy to use logging method for printing a message to a reference log.
 void run(junit.framework.TestResult result)
          Runs the test case and collects the results in TestResult.
 void setFilter(Filter filter)
          Sets active filter.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, name, run, runBare, runTest, setName, setUp, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assert, assert, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface junit.framework.Test
countTestCases
 

Constructor Detail

NbTestCase

public NbTestCase(java.lang.String name)
Constructs a test case with the given name.

Parameters:
name - name of the testcase
Method Detail

setFilter

public void setFilter(Filter filter)
Sets active filter.

Specified by:
setFilter in interface NbTest
Parameters:
filter - Filter to be set as active for current test, null will reset filtering.

canRun

public boolean canRun()
Checks if a test isn't filtered out by the active filter.

Specified by:
canRun in interface NbTest
Returns:
true if the test can run

run

public void run(junit.framework.TestResult result)
Runs the test case and collects the results in TestResult. overrides JUnit run, because filter check

Specified by:
run in interface junit.framework.Test
Overrides:
run in class junit.framework.TestCase

assertFile

public static void assertFile(java.lang.String message,
                              java.lang.String test,
                              java.lang.String pass,
                              java.lang.String diff,
                              org.netbeans.junit.diff.Diff externalDiff)
Asserts that two files are the same (their content is identical), when files differ AssertionFileFailedError exception is thrown. Depending on the Diff implementation additional output can be generated to the file/dir specified by the diff param.

Parameters:
message - the detail message for this assertion
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.
externalDiff - instance of class implementing the Diff interface, it has to be already initialized, when passed in this assertFile function.

assertFile

public static void assertFile(java.lang.String test,
                              java.lang.String pass,
                              java.lang.String diff,
                              org.netbeans.junit.diff.Diff externalDiff)
Asserts that two files are the same, it uses specific Diff implementation to compare two files and stores possible differencies in the output file.

Parameters:
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.
externalDiff - instance of class implementing the Diff interface, it has to be already initialized, when passed in this assertFile function.

assertFile

public static void assertFile(java.lang.String message,
                              java.lang.String test,
                              java.lang.String pass,
                              java.lang.String diff)
Asserts that two files are the same, it compares two files and stores possible differencies in the output file, the message is displayed when assertion fails.

Parameters:
message - the detail message for this assertion
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.

assertFile

public static void assertFile(java.lang.String test,
                              java.lang.String pass,
                              java.lang.String diff)
Asserts that two files are the same, it compares two files and stores possible differencies in the output file.

Parameters:
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.

assertFile

public static void assertFile(java.lang.String test,
                              java.lang.String pass)
Asserts that two files are the same, it just compares two files and doesn't produce any additional output.

Parameters:
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.

assertFile

public static void assertFile(java.lang.String message,
                              java.io.File test,
                              java.io.File pass,
                              java.io.File diff,
                              org.netbeans.junit.diff.Diff externalDiff)
Asserts that two files are the same (their content is identical), when files differ AssertionFileFailedError exception is thrown. Depending on the Diff implementation additional output can be generated to the file/dir specified by the diff param.

Parameters:
message - the detail message for this assertion
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.
externalDiff - instance of class implementing the Diff interface, it has to be already initialized, when passed in this assertFile function.

assertFile

public static void assertFile(java.io.File test,
                              java.io.File pass,
                              java.io.File diff,
                              org.netbeans.junit.diff.Diff externalDiff)
Asserts that two files are the same, it uses specific Diff implementation to compare two files and stores possible differencies in the output file.

Parameters:
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.
externalDiff - instance of class implementing the Diff interface, it has to be already initialized, when passed in this assertFile function.

assertFile

public static void assertFile(java.lang.String message,
                              java.io.File test,
                              java.io.File pass,
                              java.io.File diff)
Asserts that two files are the same, it compares two files and stores possible differencies in the output file, the message is displayed when assertion fails.

Parameters:
message - the detail message for this assertion
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.

assertFile

public static void assertFile(java.io.File test,
                              java.io.File pass,
                              java.io.File diff)
Asserts that two files are the same, it compares two files and stores possible differencies in the output file.

Parameters:
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.
diff - file, where differences will be stored, when null differences will not be stored. In case it points to directory the result file name is constructed from the pass argument and placed to that directory. Constructed file name consists from the name of pass file (without extension and path) appended by the '.diff'.

assertFile

public static void assertFile(java.io.File test,
                              java.io.File pass)
Asserts that two files are the same, it just compares two files and doesn't produce any additional output.

Parameters:
test - first file to be compared, by the convention this should be the test-generated file
pass - second file to be comapred, it should be so called 'golden' file, which defines the correct content for the test-generated file.

getWorkDirPath

public java.lang.String getWorkDirPath()
                                throws java.io.IOException
Returns path to test method working directory as a String. Path is constructed as ${nbjunit.workdir}/${package}/${classname}/${testmethodname}. (nbjunit.workdir property have to be set in junit.properties file) Please note, this method does not guarantee that working directory really exist.

Returns:
path
Throws:
java.io.IOException - if nbjunit.workdir property has not been set

getWorkDir

public java.io.File getWorkDir()
                        throws java.io.IOException
Returns unique working directory for a test (each test method have uniq1ue dir). If not available, method tries to create it. This method uses getWorkDirPath() method to determine the unique path.

Returns:
file to the working directory directory
Throws:
java.io.IOException - if the directory cannot be created

clearWorkDir

public void clearWorkDir()
                  throws java.io.IOException
Deletes all files including subdirectories in test's working directory.

Throws:
java.io.IOException - if any problem has occured during deleting files/directories

getLog

public java.io.PrintStream getLog(java.lang.String logName)
Returns named log stream. If log cannot be created as a file in the testmethod working directory, PrintStream created from System.out is used. Please note, that tests shoudn't call log.close() method, unless they really don't want to use this log anymore.

Parameters:
logName - name of the log - file in the working directory
Returns:
Log PrintStream

getLog

public java.io.PrintStream getLog()
Return default log named as ${testmethod}.log. If the log cannot be created as a file in testmethod working directory, PrinterStream to System.out is returned

Returns:
log

log

public void log(java.lang.String message)
Simple and easy to use method for printing a message to a default log

Parameters:
message - meesage to log

log

public void log(java.lang.String log,
                java.lang.String message)
Easy to use method for logging a message to a named log

Parameters:
log - which log to use
message - message to log

getRef

public java.io.PrintStream getRef()
Get PrintStream to log inteded for reference files comparision. Reference log is stored as a file named ${testmethod}.ref in test method working directory. If the file cannot be created, the testcase will automatically fail.

Returns:
PrintStream to referencing log

ref

public void ref(java.lang.String message)
Easy to use logging method for printing a message to a reference log.

Parameters:
message - message to log

getGoldenFile

public java.io.File getGoldenFile(java.lang.String filename)
Get the testmethod specific golden file from data/goldenfiles/${classname} resource directory.

Parameters:
filename - filename to get from golden files resource directory
Returns:
file

getGoldenFile

public java.io.File getGoldenFile()
Get the default testmethod specific golden file from data/goldenfiles/${classname}/${testmethodname}.pass

Returns:
filename to get from golden files resource directory

compareReferenceFiles

public void compareReferenceFiles(java.lang.String testFilename,
                                  java.lang.String goldenFilename,
                                  java.lang.String diffFilename)
Compares golden file and reference log. If both files are the same, test passes. If files differ, test fails and diff file is created (diff is created only when using native diff, for details see JUnit module documentation)

Parameters:
testFilename - reference log file name
goldenFilename - golden file name
diffFilename - diff file name (optional, if null, then no diff is created)

compareReferenceFiles

public void compareReferenceFiles()
Compares default golden file and default reference log. If both files are the same, test passes. If files differ, test fails and default diff (${methodname}.diff) file is created (diff is created only when using native diff, for details see JUnit module documentation)


convertNBFSURL

public static java.lang.String convertNBFSURL(java.net.URL url)
Converts NetBeans filesystem URL to absolute path.

Parameters:
url - URL to convert
Returns:
absolute path