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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
namespace Phing\Test;
use Exception;
use Phing\Exception\BuildException;
use Phing\IntrospectionHelper;
use Phing\Project;
use Phing\Test\Support\IHCreatorFail1;
use Phing\Test\Support\IHCreatorFail2;
use Phing\Test\Support\IHCreatorFail3;
use Phing\Test\Support\IHProjectComponent;
use Phing\Type\FileSet;
use PHPUnit\Framework\TestCase;
/**
* testcases for phing.IntrospectionHelper.
*
* @author Hans Lellelid <hans@xmpl.org> (Phing)
* @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
*
* @internal
*/
class IntrospectionHelperTest extends TestCase
{
/** @var Project */
private $p;
public function setUp(): void
{
$this->p = new Project();
$this->p->setBasedir(DIRECTORY_SEPARATOR);
}
/**
* @throws BuildException
*/
public function testAddText(): void
{
$ih = IntrospectionHelper::getHelper(Exception::class);
try {
$ih->addText($this->p, new Exception(), 'test');
$this->fail("Exception doesn\\'t support addText");
} catch (BuildException $be) {
}
$element = new IHProjectComponent();
$ih = IntrospectionHelper::getHelper(IHProjectComponent::class);
$ih->addText($this->p, $element, 'test');
$this->assertSame('test', $element->text);
}
public function testSupportsCharactersAdders(): void
{
$ih = IntrospectionHelper::getHelper(Exception::class);
$this->assertFalse($ih->supportsCharacters(), "String doesn\\'t support addText");
$ih = IntrospectionHelper::getHelper(IHProjectComponent::class);
$this->assertTrue($ih->supportsCharacters(), 'IHProjectComponent supports addText');
}
public function testElementCreators(): void
{
try {
IntrospectionHelper::getHelper(IHCreatorFail1::class);
$this->fail('create cannot take param');
} catch (BuildException $be) {
}
try {
IntrospectionHelper::getHelper(IHCreatorFail2::class);
$this->fail('no class hint for add');
} catch (BuildException $be) {
}
try {
IntrospectionHelper::getHelper(IHCreatorFail3::class);
$this->fail('no class hint for addconfigured');
} catch (BuildException $be) {
}
$ih = IntrospectionHelper::getHelper(IHProjectComponent::class);
$this->assertEquals('test', $ih->createElement($this->p, new IHProjectComponent(), 'one'));
$fs = new FileSet();
$fs->setProject($this->p);
$this->assertEquals($fs, $ih->createElement($this->p, new IHProjectComponent(), 'FileSet'));
}
/**
* @requires PHP >= 8
* @return void
*/
#[\PHPUnit\Framework\Attributes\RequiresPhp('>= 8')]
public function testUnionTypeOnSetterDoesNotCrashIH(): void
{
$clz = eval('return new class {
public function setSomeAttribute(bool | string $attribute) {
}
};');
$ih = IntrospectionHelper::getHelper(get_class($clz));
$this->assertContains('someattribute', $ih->getAttributes());
}
/*
public function testGetNestedElements()
{
Hashtable h = new Hashtable();
h.put("six", java.lang.String.class);
h.put("thirteen", java.lang.StringBuffer.class);
h.put("fourteen", java.lang.StringBuffer.class);
h.put("fifteen", java.lang.StringBuffer.class);
IntrospectionHelper $ih = IntrospectionHelper::getHelper(get_class($this));
Enumeration enum = ih.getNestedElements();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
Class expect = (Class) h.get(name);
assertNotNull("Support for "+name+" in IntrospectioNHelperTest?",
expect);
$this->assertEquals("Return type of "+name, expect, ih.getElementType(name));
h.remove(name);
}
$this->assertTrue("Found all", h.isEmpty());
}
public function createOne()
{
return "test";
}
/*
public function testAttributeSetters()
{
$ih = IntrospectionHelper::getHelper(get_class($this));
try {
$ih->setAttribute($p, $this, "one", "test");
$this->fail("setOne doesn't exist");
} catch (BuildException $be) {
}
try {
$ih->setAttribute($p, $this, "two", "test");
$this->fail("setTwo returns non void");
} catch (BuildException be) {
}
try {
ih.setAttribute(p, this, "three", "test");
$this->fail("setThree takes no args");
} catch (BuildException be) {
}
try {
ih.setAttribute(p, this, "four", "test");
$this->fail("setFour takes two args");
} catch (BuildException be) {
}
try {
ih.setAttribute(p, this, "five", "test");
$this->fail("setFive takes array arg");
} catch (BuildException be) {
}
try {
ih.setAttribute(p, this, "six", "test");
$this->fail("Project doesn't have a String constructor");
} catch (BuildException be) {
}
ih.setAttribute(p, this, "seven", "2");
try {
ih.setAttribute(p, this, "seven", "3");
$this->fail("2 shouldn't be equals to three");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "eight", "2");
try {
ih.setAttribute(p, this, "eight", "3");
$this->fail("2 shouldn't be equals to three - as int");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "nine", "2");
try {
ih.setAttribute(p, this, "nine", "3");
$this->fail("2 shouldn't be equals to three - as Integer");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "ten", "2");
try {
ih.setAttribute(p, this, "ten", "3");
$this->fail(projectBasedir+"2 shouldn't be equals to "+projectBasedir+"3");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "eleven", "2");
try {
ih.setAttribute(p, this, "eleven", "on");
$this->fail("on shouldn't be false");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "twelve", "2");
try {
ih.setAttribute(p, this, "twelve", "on");
$this->fail("on shouldn't be false");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project");
try {
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper");
$this->fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
try {
ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2");
$this->fail("org.apache.tools.ant.Project2 doesn't exist");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof ClassNotFoundException);
}
ih.setAttribute(p, this, "fourteen", "2");
try {
ih.setAttribute(p, this, "fourteen", "on");
$this->fail("2 shouldn't be equals to three - as StringBuffer");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "fifteen", "abcd");
try {
ih.setAttribute(p, this, "fifteen", "on");
$this->fail("o shouldn't be equal to a");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "sixteen", "abcd");
try {
ih.setAttribute(p, this, "sixteen", "on");
$this->fail("o shouldn't be equal to a");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "seventeen", "17");
try {
ih.setAttribute(p, this, "seventeen", "3");
$this->fail("17 shouldn't be equals to three");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "eightteen", "18");
try {
ih.setAttribute(p, this, "eightteen", "3");
$this->fail("18 shouldn't be equals to three");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
ih.setAttribute(p, this, "nineteen", "19");
try {
ih.setAttribute(p, this, "nineteen", "3");
$this->fail("19 shouldn't be equals to three");
} catch (BuildException be) {
$this->assertTrue(be.getException() instanceof AssertionFailedError);
}
}
public void testGetAttributes() {
Hashtable h = new Hashtable();
h.put("seven", java.lang.String.class);
h.put("eight", java.lang.Integer.TYPE);
h.put("nine", java.lang.Integer.class);
h.put("ten", java.io.File.class);
h.put("eleven", java.lang.Boolean.TYPE);
h.put("twelve", java.lang.Boolean.class);
h.put("thirteen", java.lang.Class.class);
h.put("fourteen", java.lang.StringBuffer.class);
h.put("fifteen", java.lang.Character.TYPE);
h.put("sixteen", java.lang.Character.class);
h.put("seventeen", java.lang.Byte.TYPE);
h.put("eightteen", java.lang.Short.TYPE);
h.put("nineteen", java.lang.Double.TYPE);
h.put("name", java.lang.String.class);
IntrospectionHelper $ih = IntrospectionHelper::getHelper(get_class($this));
Enumeration enum = ih.getAttributes();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
Class expect = (Class) h.get(name);
assertNotNull("Support for "+name+" in IntrospectionHelperTest?",
expect);
$this->assertEquals("Type of "+name, expect, ih.getAttributeType(name));
h.remove(name);
}
h.remove("name");
$this->assertTrue("Found all", h.isEmpty());
}
public function setTwo($s)
{
return 0;
}
public void setThree() {}
public void setFour(String s1, String s2) {}
public void setFive(String[] s) {}
public void setSix(Project p) {}
public void setSeven(String s) {
$this->assertEquals("2", s);
}
public void setEight(int i) {
$this->assertEquals(2, i);
}
public void setNine(Integer i) {
$this->assertEquals(2, i.intValue());
}
public void setTen(File f) {
if (Os.isFamily("unix")) {
$this->assertEquals(projectBasedir+"2", f.getAbsolutePath());
} elseif (Os.isFamily("netware")) {
$this->assertEquals(projectBasedir+"2", f.getAbsolutePath().toLowerCase(Locale.US));
} else {
$this->assertEquals(":"+projectBasedir+"2", f.getAbsolutePath().toLowerCase(Locale.US).substring(1));
}
}
public void setEleven(boolean b) {
$this->assertTrue(!b);
}
public void setTwelve(Boolean b) {
$this->assertTrue(!b.booleanValue());
}
public void setThirteen(Class c) {
$this->assertEquals(Project.class, c);
}
public void setFourteen(StringBuffer sb) {
$this->assertEquals("2", sb.toString());
}
public void setFifteen(char c) {
$this->assertEquals(c, 'a');
}
public void setSixteen(Character c) {
$this->assertEquals(c.charValue(), 'a');
}
public void setSeventeen(byte b) {
$this->assertEquals(17, b);
}
public void setEightteen(short s) {
$this->assertEquals(18, s);
}
public void setNineteen(double d) {
$this->assertEquals(19, d, 1e-6);
}
*/
}
|