-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSmokeTests.groovy
247 lines (214 loc) · 6.6 KB
/
SmokeTests.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2010 Luke Daley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import grails.plugin.remotecontrol.RemoteControl
import io.remotecontrol.client.RemoteException
import io.remotecontrol.client.UnserializableReturnException
import io.remotecontrol.UnserializableCommandException
/**
* This test case shows how to use the remote control and some of it's limitations
* with regard to serialisation and scope.
*
* The remote control object has an exec(Closure) method, and an alias for that as call(Closure).
* The call(Closure) variant allows the use of the Groovy language feature where you can essentially
* treat an object like a method, which is how “remote { … }” works below (i.e. it's really “remote.call { … }).
* This doesn't always work though as you will see (due to Groovy), so sometimes you need to use .exec().
*
* Where we are passing a closure to the remote control object, that closure gets executed INSIDE the
* application we are functionally testing, which may be in a different JVM on a completely different machine.
* This works by sending the closure over HTTP to the application (which must have the remote-control plugin installed).
*
* An example use for this would be creating/deleting domain data inside your remote application for testing purposes.
*/
class SmokeTests extends GroovyTestCase {
def remote = new RemoteControl()
// Used in a later test
def anIvar = 2
/**
* The result of the command run on the server is sent back and is returned
*/
void testReturingValues() {
assert remote { 1 + 1 } == 2
}
/**
* The delegate of the command contains the app context under 'ctx',
* so we can access any beans defined there
*/
void testAccessTheAppContext() {
def name = remote {
ctx.grailsApplication.metadata['app.name']
}
assert name == "grails-remote-control"
}
/**
* We can create and manipulate domain data. Each command operates in a
* hibernate session that is flushed at the end of the command.
*/
void testWorkingWithDomain() {
def id = remote {
def person = new Person(name: "Me")
person.save()
person.id
}
assert remote { Person.countByName("Me") } == 1
remote {
Person.get(id).delete()
}
assert remote { Person.countByName("Me") } == 0
}
/**
* Commands can contain other closures
*/
void testWithInnerClosures() {
assert [2,3,4] == remote {
[1,2,3].collect { it + 1 }
}
}
/**
* If the command throwns an exception, we throw a RemoteException
* client side with the actual exception instance that was thrown server
* side as the cause
*/
void testThrowingException() {
def thrown = null
try {
remote { throw new Exception("bang!") }
} catch (RemoteException e) {
thrown = e.cause
assert thrown.class == Exception
assert thrown.message == "bang!"
}
assert thrown
}
/**
* If the command returns something that is unserialisable, we thrown an UnserializableReturnException
*/
void testUnserialisableReturn() {
shouldFail(UnserializableReturnException) {
remote.exec { System.out }
}
}
/**
* If the command returns an exception but does not throw it, we just return the exception
*/
void testReturningException() {
assert (remote { new Exception() }) instanceof Exception
}
/**
* We can access lexical scope (within limits)
*/
void testAccessingLexicalScope() {
def a = 1
assert remote { a + 1 } == 2
}
/**
* Anything in lexical scope we access must be serialisable
*/
void testAccessingNonSerializableLexicalScope() {
def a = System.out
shouldFail(UnserializableCommandException) {
remote.exec { a }
}
}
/**
* Owner ivars can't be accessed because they aren't really lexical
* so get treated as bean names from the app context
*/
void testAccessingIvar() {
def thrown
try {
remote { anIvar * 2 }
} catch (RemoteException e) {
thrown = e.cause
assert thrown instanceof MissingPropertyException
}
assert thrown
}
/**
* We can pass curried commands
*/
void testCurryingCommands() {
def command = { it + 2 }
assert remote.exec(command.curry(2)) == 4
}
/**
* We can curry a command as many times as we need to
*/
void testCurryingCommandsMoreThanOnce() {
def command = { a, b -> a + b }
def curry1 = command.curry(1)
def curry2 = curry1.curry(1)
assert remote.exec(curry2) == 2
}
/**
* Like everything else, currying args must be serialisable
*/
void testCurryingArgsMustBeSerializable() {
shouldFail(UnserializableCommandException) {
remote.exec({ it }.curry(System.out))
}
}
/**
* Any classes referenced have to be available in the remote app,
* and any classes defined in tests ARE NOT.
*/
void testCannotReferToClassesNotInTheApp() {
def a = new SmokeTestsLocal()
shouldFailWithCause(ClassNotFoundException) {
remote.exec { a }
}
}
/**
* Variation of above, but yields a different error.
*/
void testCannotInstantiateClassesNotInTheApp() {
shouldFailWithCause(NoClassDefFoundError) {
remote.exec { new SmokeTestsLocal() }
}
}
/**
* Multiple commands can be sent, the return value of the previous
* command is passed to the next command as it's single argument
*/
void testCommandChaining() {
remote.exec({ 1 }, { it + 1 }, { it + 1 }) == 3
}
/**
* The delegate of commands is like a map and can store properties.
*/
void testCanUseDelegateStorageAlongChain() {
remote.exec({ num = 1 }, { num = num + 1 }, { num + 1 }) == 3
}
/**
* Trying to access a property that is not existing in the delegate
* causes a MissingPropertyException
*/
void testAccessingNonExistantPropertyFromDelegateCausesMPE() {
shouldFailWithCause(MissingPropertyException) {
remote.exec { iDontExist == true }
}
}
void testCanSetProperties() {
remote.exec { ctx.theService.value = 1 }
remote.exec { ctx.theService.setValue(1) }
remote.exec { ctx.theService.setProperty('value', 1) }
}
void testCanCallMethodsDynamicaly() {
def methodName = "setValue"
remote.exec { ctx.theService."$methodName"(1) }
remote.exec { ctx.theService.invokeMethod(methodName, 1) }
}
}
class SmokeTestsLocal implements Serializable {}