Skip to content

Commit d9ae7f5

Browse files
committed
Add tests
1 parent f1c3888 commit d9ae7f5

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

src/Packages/java/io/PrintStream.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public function format($a = null, $b = null, $c = null)
123123
*/
124124
public function print($methodSignature, $a = null)
125125
{
126+
/**
127+
* TODO: Rewrite here.
128+
* This method is very weak.
129+
* We need to judge parameters type with methodSignature parameter.
130+
*/
126131
// Did not pass a parameter.
127132
if (!isset($methodSignature['arguments'][0])) {
128133
return;
@@ -138,8 +143,10 @@ public function print($methodSignature, $a = null)
138143
$arg instanceof Collection ||
139144
$arg instanceof PrimitiveValueInterface
140145
) {
141-
echo $arg;
142-
return;
146+
if (((string) $arg) !== "\x00") {
147+
echo $arg;
148+
return;
149+
}
143150
}
144151

145152
if ($arg instanceof JavaClass) {

tests/Packages/JavaIoPrintStreamClassTest.php

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPJava\Tests\Packages\java\io;
33

4+
use PHPJava\Exceptions\UncaughtException;
5+
use PHPJava\Packages\java\lang\NullPointerException;
46
use PHPJava\Tests\Base;
57

68
class JavaIoPrintStreamClassTest extends Base
@@ -9,17 +11,23 @@ class JavaIoPrintStreamClassTest extends Base
911
'JavaIoPrintStreamClassTest',
1012
];
1113

14+
protected $expectedSpecialException;
15+
1216
private function call($method, ...$arguments)
1317
{
1418
ob_start();
15-
static::$initiatedJavaClasses['JavaIoPrintStreamClassTest']
16-
->getInvoker()
17-
->getStatic()
18-
->getMethods()
19-
->call(
20-
$method,
21-
...$arguments
22-
);
19+
try {
20+
static::$initiatedJavaClasses['JavaIoPrintStreamClassTest']
21+
->getInvoker()
22+
->getStatic()
23+
->getMethods()
24+
->call(
25+
$method,
26+
...$arguments
27+
);
28+
} catch (UncaughtException $e) {
29+
$this->expectedSpecialException = get_class($e->getPrevious());
30+
}
2331
return ob_get_clean();
2432
}
2533

@@ -35,6 +43,16 @@ public function testPrintlnWithStringParams()
3543
$this->assertEquals("Hello World\n", $result);
3644
}
3745

46+
public function testPrintlnWithNullStringParams()
47+
{
48+
$result = $this->call(explode('::', __METHOD__)[1]);
49+
$this->assertEquals("null\n", $result);
50+
$this->assertSame(
51+
NullPointerException::class,
52+
$this->expectedSpecialException
53+
);
54+
}
55+
3856
public function testPrintlnWithCharParams()
3957
{
4058
$result = $this->call(explode('::', __METHOD__)[1]);
@@ -47,6 +65,16 @@ public function testPrintlnWithCharArrayParams()
4765
$this->assertEquals("ABC\n", $result);
4866
}
4967

68+
public function testPrintlnWithNullCharArrayParams()
69+
{
70+
$result = $this->call(explode('::', __METHOD__)[1]);
71+
$this->assertEquals("\n", $result);
72+
$this->assertSame(
73+
NullPointerException::class,
74+
$this->expectedSpecialException
75+
);
76+
}
77+
5078
public function testPrintlnWithFloatParams()
5179
{
5280
$result = $this->call(explode('::', __METHOD__)[1]);
@@ -74,6 +102,16 @@ public function testPrintWithStringParams()
74102
$this->assertEquals('Hello World', $result);
75103
}
76104

105+
public function testPrintWithNullStringParams()
106+
{
107+
$result = $this->call(explode('::', __METHOD__)[1]);
108+
$this->assertEquals('null', $result);
109+
$this->assertSame(
110+
NullPointerException::class,
111+
$this->expectedSpecialException
112+
);
113+
}
114+
77115
public function testPrintWithCharParams()
78116
{
79117
$result = $this->call(explode('::', __METHOD__)[1]);
@@ -86,6 +124,16 @@ public function testPrintWithCharArrayParams()
86124
$this->assertEquals('ABC', $result);
87125
}
88126

127+
public function testPrintWithNullCharArrayParams()
128+
{
129+
$result = $this->call(explode('::', __METHOD__)[1]);
130+
$this->assertEquals('', $result);
131+
$this->assertSame(
132+
NullPointerException::class,
133+
$this->expectedSpecialException
134+
);
135+
}
136+
89137
public function testPrintWithFloatParams()
90138
{
91139
$result = $this->call(explode('::', __METHOD__)[1]);

tests/fixtures/java/JavaIoPrintStreamClassTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public static void testPrintlnWithStringParams()
1111
System.out.println(x);
1212
}
1313

14+
public static void testPrintlnWithNullStringParams()
15+
{
16+
String x = null;
17+
System.out.println(x);
18+
}
19+
1420
public static void testPrintlnWithCharParams()
1521
{
1622
char x = 'A';
@@ -23,6 +29,12 @@ public static void testPrintlnWithCharArrayParams()
2329
System.out.println(x);
2430
}
2531

32+
public static void testPrintlnWithNullCharArrayParams()
33+
{
34+
char[] x = null;
35+
System.out.println(x);
36+
}
37+
2638
public static void testPrintlnWithFloatParams()
2739
{
2840
float x = (float) 0.123;
@@ -53,6 +65,12 @@ public static void testPrintWithStringParams()
5365
System.out.print(x);
5466
}
5567

68+
public static void testPrintWithNullStringParams()
69+
{
70+
String x = null;
71+
System.out.print(x);
72+
}
73+
5674
public static void testPrintWithCharParams()
5775
{
5876
char x = 'A';
@@ -65,6 +83,12 @@ public static void testPrintWithCharArrayParams()
6583
System.out.print(x);
6684
}
6785

86+
public static void testPrintWithNullCharArrayParams()
87+
{
88+
char[] x = null;
89+
System.out.print(x);
90+
}
91+
6892
public static void testPrintWithFloatParams()
6993
{
7094
float x = (float) 0.123;

0 commit comments

Comments
 (0)