|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the LGPL. For more information please see |
|
18
|
|
|
* <http://phing.info>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Phing\Test; |
|
22
|
|
|
|
|
23
|
|
|
use Exception; |
|
24
|
|
|
use Phing\Exception\BuildException; |
|
25
|
|
|
use Phing\IntrospectionHelper; |
|
26
|
|
|
use Phing\Project; |
|
27
|
|
|
use Phing\Test\Support\IHCreatorFail1; |
|
28
|
|
|
use Phing\Test\Support\IHCreatorFail2; |
|
29
|
|
|
use Phing\Test\Support\IHCreatorFail3; |
|
30
|
|
|
use Phing\Test\Support\IHProjectComponent; |
|
31
|
|
|
use Phing\Type\FileSet; |
|
32
|
|
|
use PHPUnit\Framework\TestCase; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* testcases for phing.IntrospectionHelper. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Hans Lellelid <[email protected]> (Phing) |
|
38
|
|
|
* @author Stefan Bodewig <[email protected]> (Ant) |
|
39
|
|
|
* |
|
40
|
|
|
* @internal |
|
41
|
|
|
*/ |
|
42
|
|
|
class IntrospectionHelperTest extends TestCase |
|
43
|
|
|
{ |
|
44
|
|
|
/** @var Project */ |
|
45
|
|
|
private $p; |
|
46
|
|
|
|
|
47
|
|
|
public function setUp(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->p = new Project(); |
|
50
|
|
|
$this->p->setBasedir(DIRECTORY_SEPARATOR); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws BuildException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testAddText(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$ih = IntrospectionHelper::getHelper(Exception::class); |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$ih->addText($this->p, new Exception(), 'test'); |
|
62
|
|
|
$this->fail("Exception doesn\\'t support addText"); |
|
63
|
|
|
} catch (BuildException $be) { |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$element = new IHProjectComponent(); |
|
67
|
|
|
$ih = IntrospectionHelper::getHelper(IHProjectComponent::class); |
|
68
|
|
|
$ih->addText($this->p, $element, 'test'); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$this->assertSame('test', $element->text); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testSupportsCharactersAdders(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$ih = IntrospectionHelper::getHelper(Exception::class); |
|
76
|
|
|
$this->assertFalse($ih->supportsCharacters(), "String doesn\\'t support addText"); |
|
77
|
|
|
$ih = IntrospectionHelper::getHelper(IHProjectComponent::class); |
|
78
|
|
|
$this->assertTrue($ih->supportsCharacters(), 'IHProjectComponent supports addText'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testElementCreators(): void |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
|
|
IntrospectionHelper::getHelper(IHCreatorFail1::class); |
|
85
|
|
|
$this->fail('create cannot take param'); |
|
86
|
|
|
} catch (BuildException $be) { |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
IntrospectionHelper::getHelper(IHCreatorFail2::class); |
|
91
|
|
|
$this->fail('no class hint for add'); |
|
92
|
|
|
} catch (BuildException $be) { |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
|
|
IntrospectionHelper::getHelper(IHCreatorFail3::class); |
|
97
|
|
|
$this->fail('no class hint for addconfigured'); |
|
98
|
|
|
} catch (BuildException $be) { |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$ih = IntrospectionHelper::getHelper(IHProjectComponent::class); |
|
102
|
|
|
$this->assertEquals('test', $ih->createElement($this->p, new IHProjectComponent(), 'one')); |
|
103
|
|
|
|
|
104
|
|
|
$fs = new FileSet(); |
|
105
|
|
|
$fs->setProject($this->p); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertEquals($fs, $ih->createElement($this->p, new IHProjectComponent(), 'FileSet')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @requires PHP >= 8 |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testUnionTypeOnSetterDoesNotCrashIH(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$clz = eval('return new class { |
|
|
|
|
|
|
117
|
|
|
public function setSomeAttribute(bool | string $attribute) { |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
};'); |
|
121
|
|
|
|
|
122
|
|
|
$ih = IntrospectionHelper::getHelper(get_class($clz)); |
|
123
|
|
|
$this->assertContains('someattribute', $ih->getAttributes()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/* |
|
127
|
|
|
public function testGetNestedElements() |
|
128
|
|
|
{ |
|
129
|
|
|
Hashtable h = new Hashtable(); |
|
130
|
|
|
h.put("six", java.lang.String.class); |
|
131
|
|
|
h.put("thirteen", java.lang.StringBuffer.class); |
|
132
|
|
|
h.put("fourteen", java.lang.StringBuffer.class); |
|
133
|
|
|
h.put("fifteen", java.lang.StringBuffer.class); |
|
134
|
|
|
IntrospectionHelper $ih = IntrospectionHelper::getHelper(get_class($this)); |
|
135
|
|
|
Enumeration enum = ih.getNestedElements(); |
|
136
|
|
|
while (enum.hasMoreElements()) { |
|
137
|
|
|
String name = (String) enum.nextElement(); |
|
138
|
|
|
Class expect = (Class) h.get(name); |
|
139
|
|
|
assertNotNull("Support for "+name+" in IntrospectioNHelperTest?", |
|
140
|
|
|
expect); |
|
141
|
|
|
$this->assertEquals("Return type of "+name, expect, ih.getElementType(name)); |
|
142
|
|
|
h.remove(name); |
|
143
|
|
|
} |
|
144
|
|
|
$this->assertTrue("Found all", h.isEmpty()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function createOne() |
|
148
|
|
|
{ |
|
149
|
|
|
return "test"; |
|
150
|
|
|
} |
|
151
|
|
|
/* |
|
152
|
|
|
public function testAttributeSetters() |
|
153
|
|
|
{ |
|
154
|
|
|
$ih = IntrospectionHelper::getHelper(get_class($this)); |
|
155
|
|
|
try { |
|
156
|
|
|
$ih->setAttribute($p, $this, "one", "test"); |
|
157
|
|
|
$this->fail("setOne doesn't exist"); |
|
158
|
|
|
} catch (BuildException $be) { |
|
159
|
|
|
} |
|
160
|
|
|
try { |
|
161
|
|
|
$ih->setAttribute($p, $this, "two", "test"); |
|
162
|
|
|
$this->fail("setTwo returns non void"); |
|
163
|
|
|
} catch (BuildException be) { |
|
164
|
|
|
} |
|
165
|
|
|
try { |
|
166
|
|
|
ih.setAttribute(p, this, "three", "test"); |
|
167
|
|
|
$this->fail("setThree takes no args"); |
|
168
|
|
|
} catch (BuildException be) { |
|
169
|
|
|
} |
|
170
|
|
|
try { |
|
171
|
|
|
ih.setAttribute(p, this, "four", "test"); |
|
172
|
|
|
$this->fail("setFour takes two args"); |
|
173
|
|
|
} catch (BuildException be) { |
|
174
|
|
|
} |
|
175
|
|
|
try { |
|
176
|
|
|
ih.setAttribute(p, this, "five", "test"); |
|
177
|
|
|
$this->fail("setFive takes array arg"); |
|
178
|
|
|
} catch (BuildException be) { |
|
179
|
|
|
} |
|
180
|
|
|
try { |
|
181
|
|
|
ih.setAttribute(p, this, "six", "test"); |
|
182
|
|
|
$this->fail("Project doesn't have a String constructor"); |
|
183
|
|
|
} catch (BuildException be) { |
|
184
|
|
|
} |
|
185
|
|
|
ih.setAttribute(p, this, "seven", "2"); |
|
186
|
|
|
try { |
|
187
|
|
|
ih.setAttribute(p, this, "seven", "3"); |
|
188
|
|
|
$this->fail("2 shouldn't be equals to three"); |
|
189
|
|
|
} catch (BuildException be) { |
|
190
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
191
|
|
|
} |
|
192
|
|
|
ih.setAttribute(p, this, "eight", "2"); |
|
193
|
|
|
try { |
|
194
|
|
|
ih.setAttribute(p, this, "eight", "3"); |
|
195
|
|
|
$this->fail("2 shouldn't be equals to three - as int"); |
|
196
|
|
|
} catch (BuildException be) { |
|
197
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
198
|
|
|
} |
|
199
|
|
|
ih.setAttribute(p, this, "nine", "2"); |
|
200
|
|
|
try { |
|
201
|
|
|
ih.setAttribute(p, this, "nine", "3"); |
|
202
|
|
|
$this->fail("2 shouldn't be equals to three - as Integer"); |
|
203
|
|
|
} catch (BuildException be) { |
|
204
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
205
|
|
|
} |
|
206
|
|
|
ih.setAttribute(p, this, "ten", "2"); |
|
207
|
|
|
try { |
|
208
|
|
|
ih.setAttribute(p, this, "ten", "3"); |
|
209
|
|
|
$this->fail(projectBasedir+"2 shouldn't be equals to "+projectBasedir+"3"); |
|
210
|
|
|
} catch (BuildException be) { |
|
211
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
212
|
|
|
} |
|
213
|
|
|
ih.setAttribute(p, this, "eleven", "2"); |
|
214
|
|
|
try { |
|
215
|
|
|
ih.setAttribute(p, this, "eleven", "on"); |
|
216
|
|
|
$this->fail("on shouldn't be false"); |
|
217
|
|
|
} catch (BuildException be) { |
|
218
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
219
|
|
|
} |
|
220
|
|
|
ih.setAttribute(p, this, "twelve", "2"); |
|
221
|
|
|
try { |
|
222
|
|
|
ih.setAttribute(p, this, "twelve", "on"); |
|
223
|
|
|
$this->fail("on shouldn't be false"); |
|
224
|
|
|
} catch (BuildException be) { |
|
225
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
226
|
|
|
} |
|
227
|
|
|
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project"); |
|
228
|
|
|
try { |
|
229
|
|
|
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper"); |
|
230
|
|
|
$this->fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper"); |
|
231
|
|
|
} catch (BuildException be) { |
|
232
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
233
|
|
|
} |
|
234
|
|
|
try { |
|
235
|
|
|
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2"); |
|
236
|
|
|
$this->fail("org.apache.tools.ant.Project2 doesn't exist"); |
|
237
|
|
|
} catch (BuildException be) { |
|
238
|
|
|
$this->assertTrue(be.getException() instanceof ClassNotFoundException); |
|
239
|
|
|
} |
|
240
|
|
|
ih.setAttribute(p, this, "fourteen", "2"); |
|
241
|
|
|
try { |
|
242
|
|
|
ih.setAttribute(p, this, "fourteen", "on"); |
|
243
|
|
|
$this->fail("2 shouldn't be equals to three - as StringBuffer"); |
|
244
|
|
|
} catch (BuildException be) { |
|
245
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
246
|
|
|
} |
|
247
|
|
|
ih.setAttribute(p, this, "fifteen", "abcd"); |
|
248
|
|
|
try { |
|
249
|
|
|
ih.setAttribute(p, this, "fifteen", "on"); |
|
250
|
|
|
$this->fail("o shouldn't be equal to a"); |
|
251
|
|
|
} catch (BuildException be) { |
|
252
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
253
|
|
|
} |
|
254
|
|
|
ih.setAttribute(p, this, "sixteen", "abcd"); |
|
255
|
|
|
try { |
|
256
|
|
|
ih.setAttribute(p, this, "sixteen", "on"); |
|
257
|
|
|
$this->fail("o shouldn't be equal to a"); |
|
258
|
|
|
} catch (BuildException be) { |
|
259
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
260
|
|
|
} |
|
261
|
|
|
ih.setAttribute(p, this, "seventeen", "17"); |
|
262
|
|
|
try { |
|
263
|
|
|
ih.setAttribute(p, this, "seventeen", "3"); |
|
264
|
|
|
$this->fail("17 shouldn't be equals to three"); |
|
265
|
|
|
} catch (BuildException be) { |
|
266
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
267
|
|
|
} |
|
268
|
|
|
ih.setAttribute(p, this, "eightteen", "18"); |
|
269
|
|
|
try { |
|
270
|
|
|
ih.setAttribute(p, this, "eightteen", "3"); |
|
271
|
|
|
$this->fail("18 shouldn't be equals to three"); |
|
272
|
|
|
} catch (BuildException be) { |
|
273
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
274
|
|
|
} |
|
275
|
|
|
ih.setAttribute(p, this, "nineteen", "19"); |
|
276
|
|
|
try { |
|
277
|
|
|
ih.setAttribute(p, this, "nineteen", "3"); |
|
278
|
|
|
$this->fail("19 shouldn't be equals to three"); |
|
279
|
|
|
} catch (BuildException be) { |
|
280
|
|
|
$this->assertTrue(be.getException() instanceof AssertionFailedError); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public void testGetAttributes() { |
|
285
|
|
|
Hashtable h = new Hashtable(); |
|
286
|
|
|
h.put("seven", java.lang.String.class); |
|
287
|
|
|
h.put("eight", java.lang.Integer.TYPE); |
|
288
|
|
|
h.put("nine", java.lang.Integer.class); |
|
289
|
|
|
h.put("ten", java.io.File.class); |
|
290
|
|
|
h.put("eleven", java.lang.Boolean.TYPE); |
|
291
|
|
|
h.put("twelve", java.lang.Boolean.class); |
|
292
|
|
|
h.put("thirteen", java.lang.Class.class); |
|
293
|
|
|
h.put("fourteen", java.lang.StringBuffer.class); |
|
294
|
|
|
h.put("fifteen", java.lang.Character.TYPE); |
|
295
|
|
|
h.put("sixteen", java.lang.Character.class); |
|
296
|
|
|
h.put("seventeen", java.lang.Byte.TYPE); |
|
297
|
|
|
h.put("eightteen", java.lang.Short.TYPE); |
|
298
|
|
|
h.put("nineteen", java.lang.Double.TYPE); |
|
299
|
|
|
|
|
300
|
|
|
h.put("name", java.lang.String.class); |
|
301
|
|
|
|
|
302
|
|
|
IntrospectionHelper $ih = IntrospectionHelper::getHelper(get_class($this)); |
|
303
|
|
|
Enumeration enum = ih.getAttributes(); |
|
304
|
|
|
while (enum.hasMoreElements()) { |
|
305
|
|
|
String name = (String) enum.nextElement(); |
|
306
|
|
|
Class expect = (Class) h.get(name); |
|
307
|
|
|
assertNotNull("Support for "+name+" in IntrospectionHelperTest?", |
|
308
|
|
|
expect); |
|
309
|
|
|
$this->assertEquals("Type of "+name, expect, ih.getAttributeType(name)); |
|
310
|
|
|
h.remove(name); |
|
311
|
|
|
} |
|
312
|
|
|
h.remove("name"); |
|
313
|
|
|
$this->assertTrue("Found all", h.isEmpty()); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
public function setTwo($s) |
|
317
|
|
|
{ |
|
318
|
|
|
return 0; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public void setThree() {} |
|
322
|
|
|
|
|
323
|
|
|
public void setFour(String s1, String s2) {} |
|
324
|
|
|
|
|
325
|
|
|
public void setFive(String[] s) {} |
|
326
|
|
|
|
|
327
|
|
|
public void setSix(Project p) {} |
|
328
|
|
|
|
|
329
|
|
|
public void setSeven(String s) { |
|
330
|
|
|
$this->assertEquals("2", s); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
public void setEight(int i) { |
|
334
|
|
|
$this->assertEquals(2, i); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
public void setNine(Integer i) { |
|
338
|
|
|
$this->assertEquals(2, i.intValue()); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
public void setTen(File f) { |
|
342
|
|
|
if (Os.isFamily("unix")) { |
|
343
|
|
|
$this->assertEquals(projectBasedir+"2", f.getAbsolutePath()); |
|
344
|
|
|
} elseif (Os.isFamily("netware")) { |
|
345
|
|
|
$this->assertEquals(projectBasedir+"2", f.getAbsolutePath().toLowerCase(Locale.US)); |
|
346
|
|
|
} else { |
|
347
|
|
|
$this->assertEquals(":"+projectBasedir+"2", f.getAbsolutePath().toLowerCase(Locale.US).substring(1)); |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
public void setEleven(boolean b) { |
|
352
|
|
|
$this->assertTrue(!b); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
public void setTwelve(Boolean b) { |
|
356
|
|
|
$this->assertTrue(!b.booleanValue()); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
public void setThirteen(Class c) { |
|
360
|
|
|
$this->assertEquals(Project.class, c); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
public void setFourteen(StringBuffer sb) { |
|
364
|
|
|
$this->assertEquals("2", sb.toString()); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
public void setFifteen(char c) { |
|
368
|
|
|
$this->assertEquals(c, 'a'); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
public void setSixteen(Character c) { |
|
372
|
|
|
$this->assertEquals(c.charValue(), 'a'); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
public void setSeventeen(byte b) { |
|
376
|
|
|
$this->assertEquals(17, b); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
public void setEightteen(short s) { |
|
380
|
|
|
$this->assertEquals(18, s); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
public void setNineteen(double d) { |
|
384
|
|
|
$this->assertEquals(19, d, 1e-6); |
|
385
|
|
|
} |
|
386
|
|
|
*/ |
|
387
|
|
|
} |
|
388
|
|
|
|