TestResource.st
author Stefan Vogel <sv@exept.de>
Tue, 23 Apr 2013 12:33:43 +0200
changeset 577 3dea1e941af7
parent 569 edb941b33667
child 590 c13008404767
permissions -rw-r--r--
class: TestResource added: #safeTearDown changed: #makeAvailable Take care of AbortOperationRequest being raised in Debugger in tearDown after an errornous test case.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:goodies/sunit' }"
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
     3
TestAsserter subclass:#TestResource
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:'name description'
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:''
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'SUnit-Base'
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
TestResource class instanceVariableNames:'current'
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
 No other class instance variables are inherited by this class.
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    17
TestResource comment:'Normally a test will set up all the objects it needs and tear them down again after it has run.  This self-containedness makes a test more robust.  Use TestResources only for objects that are needed by several tests and that are too ''expensive'' (in time or otherwise) to recreate and destroy for each test.  A viable approach is to develop the code in MyTestCase''s #setUp and #tearDown methods, then at some point refactor the code into the #setUp and #tearDown of a TestResource whose class is added to MyTestCase class>>resource method.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    18
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    19
TestResource uses the singleton pattern.  A TestResource class will set up a single instance of itself when first requested and tear it down again at the end of TestSuite>>run (or TestCase>>run, >>debug and >>debugAsFailure).  Normally, a TestResource, once setUp, remains active during the running of all remaining tests and is #reset after all tests have run.  For an exception, see subclass CompetingResource in SUnitResourcePatterns.  Users can choose to #reset a resource in the #tearDown of a test that alters it, sacrificing the performance gain of having a single #setUp of the resource for the certainty that other tests using it will not see the alterations.  Generally however, this should be the exception:  if you need to reset the resource for every test that uses it, its code should just be part of your test''s #setUp and #tearDown code.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    20
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    21
To use, create a subclass of TestResource and override the following:
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    22
	- TestCase class>>resources, to return a collection including the TestResource class, for all test case classes that need it
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    23
		* a TestCase'' resources are set up in the order returned and torn down in the reverse order
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    24
	- TestResource class>>resources, if the resource itself always needs some other resource to be present before it can set up
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    25
		* a TestResource''s resource are set up before it and torn down after it, and are set up in the order returned and torn down in the reverse order
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    26
	- TestResource>>setUp and tearDown, to define initial and final behaviour (just like a test)
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    27
	- TestResource>>isAvailable, to return true if it is and false if it isn''t (the framework calls this after setUp);  ideally, this call should not change the resource'' state - that should be done in setUp
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    28
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    29
TestResource implements the singleton pattern in its class-side #isAvailable and #reset methods.  Do not override these when creating specific resources;  unless you are developing a whole new pattern of use, it will always be correct to override instance-side #setUp, #tearDown and #isAvailable, and dangerous to override class>>isAvailable, class>>isAlreadyAvailable and class>>reset.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    30
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    31
Generally, users do not code interactions with a test''s resources during the running of a test.  Code that reads a resource'' values while leaving its state strictly alone is safe enough.  A test must leave a resource in a clean state:  always use #reset if a test must protect later-running tests from unsafe changes (and review whether in such a case a resource is the right thing to use in the first place).
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    32
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    33
See my superclass'' comment for assertion and logging information.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    34
'
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    35
!
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    36
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    37
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    38
!TestResource class methodsFor:'instance creation'!
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    39
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    40
new
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    41
	"Use #current to get the valid current instance.  Use of #new to get an instance (that should never be the current one) could be done in bizarre circumstances, so is not blocked, but will usually be inappropriate."
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    42
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    43
	^super new initialize
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    44
!
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    45
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    46
reset
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    47
	[self isAlreadyAvailable ifTrue: [current tearDown]]
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    48
		sunitEnsure: [current := nil].
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    49
! !
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
    51
!TestResource class methodsFor:'accessing'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
current
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    54
	"This is a lazy accessor:  the assert of self isAvailable does no work unless current isNil.  However this method should normally be sent only to a resource that should already have been made available, e.g. in a test whose test case class has the resource class in its #resources, so should never be able to fail the assert.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    55
	If the intent is indeed to access a possibly-unprepared or reset-in-earlier-test resource lazily, then preface the call of 'MyResource current' with 'MyResource availableFor: self'."
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
    56
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    57
	self assert: self isAvailable
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    58
		description: 'Sent #current to unavailable resource ', self name, '.  Add it to test case'' class-side #resources (recommended) or send #availableFor: beforehand'.
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
	^current
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
    62
resources
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
    63
	^#()
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
    66
!TestResource class methodsFor:'creation'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    68
signalInitializationError
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    69
	^TestResult signalErrorWith: 'Resource ' , self name , ' could not be initialized'
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    70
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    71
! !
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    72
241
47aaf6b127eb added: #version_CVS
Claus Gittinger <cg@exept.de>
parents: 222
diff changeset
    73
!TestResource class methodsFor:'others'!
47aaf6b127eb added: #version_CVS
Claus Gittinger <cg@exept.de>
parents: 222
diff changeset
    74
47aaf6b127eb added: #version_CVS
Claus Gittinger <cg@exept.de>
parents: 222
diff changeset
    75
version_CVS
577
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
    76
    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestResource.st,v 1.11 2013-04-23 10:33:43 stefan Exp $'
241
47aaf6b127eb added: #version_CVS
Claus Gittinger <cg@exept.de>
parents: 222
diff changeset
    77
! !
47aaf6b127eb added: #version_CVS
Claus Gittinger <cg@exept.de>
parents: 222
diff changeset
    78
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    79
!TestResource class methodsFor:'private'!
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
    80
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    81
makeAvailable
569
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    82
    "This method must be the _only_ way to set a notNil value for the unique instance (current).  First, obtain a candidate instance and set current to a notNil placeholder (any notNil object not an instance of me would do;  this version uses false).  Next, check any subordinate resources needed by this resource.  Lastly, setUp the candidate and put it in current if it is available, ensuring that it is torn down otherwise."
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    83
    
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    84
    |candidate didSetup|
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
    85
569
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    86
    current := false.
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    87
    candidate := self new.
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    88
    self resources do:[:each | 
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    89
        each availableFor:candidate
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    90
    ].
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    91
    [
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    92
        didSetup := false.
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    93
        candidate setUp.
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    94
        didSetup := false.
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    95
        candidate isAvailable ifTrue:[
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    96
            current := candidate
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    97
        ]
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    98
    ] sunitEnsure:[
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
    99
        didSetup ifTrue:[
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
   100
            current == candidate ifFalse:[
577
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   101
                candidate safeTearDown
569
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
   102
            ]
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
   103
        ]
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
   104
    ].
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   107
resetOrAddResourcesTo: aCollection
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   108
	"Add correctly set-up resources to the collection unless already there. Reset any imperfectly-set-up resources, so current isNil will return true if they are re-encountered via an indirectly self-prerequing resource;  circular references cannot be set up so will never reply true to isAlreadyAvailable, but may have correctly-set-up prereqs to add and/or imperfectly-set-up ones to reset, so do not abort the loop first time round."
70
2ff4508f476d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 68
diff changeset
   109
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   110
	current isNil ifTrue: [^self].
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   111
	self isAlreadyAvailable
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   112
		ifFalse:
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   113
			[self reset.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   114
			self resources do: [:each | each resetOrAddResourcesTo: aCollection]]
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   115
		ifTrue:
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   116
			[(aCollection includes: self) ifFalse:
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   117
				[self resources do: [:each | each resetOrAddResourcesTo: aCollection].
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   118
				aCollection add: self]].
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   119
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   120
"The cloned 'self resources do: ...' line in both blocks is, I think, the best way to write this method so that its logic is clear.  The first loop resets this resource immediately, before traversing its resources;  the second traverses before adding"
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   121
! !
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   122
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   123
!TestResource class methodsFor:'running'!
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   124
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   125
availableFor: aTestAsserter
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   126
	aTestAsserter
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   127
		assert: self isAvailable
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   128
		description: 'Unavailable resource ' , self name , ' requested by ', aTestAsserter printString.
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   129
!
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   130
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   131
resetResources: topLevelResources
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   132
	"Reset all imperfectly-set-up resources while gathering the rest for ordered resetting."
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   133
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   134
	| availableResources |
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   135
	availableResources := OrderedCollection new: topLevelResources size.
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   136
	topLevelResources do: [:each | each resetOrAddResourcesTo: availableResources].
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   137
	availableResources reverseDo: [:each | each reset].
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
   140
!TestResource class methodsFor:'testing'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
isAbstract
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   143
	"Override to true if a TestResource subclass is Abstract and should not have
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   144
	TestCase instances built from it"
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   145
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   146
	^ self == TestResource
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   147
!
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   148
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   149
isAlreadyAvailable
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   150
	^current class == self
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
isAvailable
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   154
	"This is (and must be) a lazy method.  If my current has a value, an attempt to make me available has already been made:  trust its result.  If not, try to make me available."
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   155
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   156
	current isNil ifTrue: [self makeAvailable].
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   157
	^self isAlreadyAvailable
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
isUnavailable
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   161
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
	^self isAvailable not
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   163
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
   166
!TestResource methodsFor:'accessing'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
description
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   169
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   170
	description isNil
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   171
		ifTrue: [^''].
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   172
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
	^description
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
description: aString
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   177
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
	description := aString
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
name
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   182
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   183
	name isNil
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   184
		ifTrue: [^self printString].
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   185
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
	^name
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
name: aString
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   190
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
	name := aString
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   192
!
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   193
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   194
resources
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   195
	^self class resources
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   198
!TestResource methodsFor:'initialize-release'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
initialize
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   201
	"This method used to call setUp but now does nothing;  setUp is called by the framework at the appropriate point.  Subclasses may override to set the object to its default state."
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
   204
!TestResource methodsFor:'printing'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
printOn: aStream
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   207
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
	aStream nextPutAll: self class printString
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
577
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   211
!TestResource methodsFor:'private'!
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   212
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   213
safeTearDown
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   214
    "Have to handle Abort. When tearDown is called as inside an ensure block after
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   215
     an abort in the debugger of an errornous test case and raises an error with a debugger
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   216
     itself."
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   217
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   218
    AbortOperationRequest handle:[:ex| ] do:[self tearDown].
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   219
! !
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   220
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
   221
!TestResource methodsFor:'running'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
setUp
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   224
	"Does nothing. Subclasses should override this to initialize their resource"
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   225
!
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   226
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   227
signalInitializationError
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   228
	^self class signalInitializationError
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   229
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
tearDown
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   233
	"Does nothing. Subclasses should override this to tear down their resource"
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
68
9fd111438d60 category renames (lower case)
Claus Gittinger <cg@exept.de>
parents: 44
diff changeset
   236
!TestResource methodsFor:'testing'!
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
isAvailable
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   239
	"Override to provide information on the readiness of the resource.  Put state-changing behaviour in setUp and keep this a state-preserving check as far as possible.  Where setUp is guaranteed to provide a valid resource if it completes, there is no need to override this."
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   240
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
	^true
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
isUnavailable
103
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   245
	"override to provide information on the
ad6897ce99e0 Merge SUnit 3.1 changes
Stefan Vogel <sv@exept.de>
parents: 70
diff changeset
   246
	readiness of the resource"
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   247
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
	^self isAvailable not
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   249
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
! !
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
!TestResource class methodsFor:'documentation'!
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
version
577
3dea1e941af7 class: TestResource
Stefan Vogel <sv@exept.de>
parents: 569
diff changeset
   255
    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestResource.st,v 1.11 2013-04-23 10:33:43 stefan Exp $'
217
2033d47baf43 changed: #new
Claus Gittinger <cg@exept.de>
parents: 180
diff changeset
   256
!
2033d47baf43 changed: #new
Claus Gittinger <cg@exept.de>
parents: 180
diff changeset
   257
222
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   258
version_SVN
8e6f482297fa Jan's 4.1 version
Claus Gittinger <cg@exept.de>
parents: 217
diff changeset
   259
    ^ '§Id: TestResource.st 204 2010-09-11 15:21:51Z vranyj1 §'
44
63d3c94197da initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
! !
569
edb941b33667 class: TestResource
Claus Gittinger <cg@exept.de>
parents: 448
diff changeset
   261