1
1
package dotty .tools .repl
2
2
3
- import org .junit .Assert ._
3
+ import org .junit .Assert .*
4
4
import org .junit .Test
5
5
6
6
class AbstractFileClassLoaderTest :
7
7
8
8
import dotty .tools .io .{AbstractFile , VirtualDirectory }
9
- import scala .io . Source
10
- import scala .io .Codec , Codec .UTF8
11
- import java .io .{Closeable , InputStream }
9
+ import scala .collection . mutable . ArrayBuffer
10
+ import scala .io .{ Codec , Source } , Codec .UTF8
11
+ import java .io .{BufferedInputStream , Closeable , InputStream }
12
12
import java .net .{URLClassLoader , URL }
13
13
14
- implicit def `we love utf8` : Codec = UTF8
14
+ given `we love utf8` : Codec = UTF8
15
15
16
- /** Call a function on something Closeable, finally closing it. */
17
16
def closing [T <: Closeable , U ](stream : T )(f : T => U ): U = try f(stream) finally stream.close()
18
17
19
18
extension (f : AbstractFile ) def writeContent (s : String ): Unit = closing(f.bufferedOutput)(_.write(s.getBytes(UTF8 .charSet)))
20
- def slurp (inputStream : => InputStream )(implicit codec : Codec ): String =
21
- val src = Source .fromInputStream(inputStream)(codec)
22
- try src.mkString finally src.close() // Always Be Closing
19
+ def slurp (inputStream : => InputStream )(implicit codec : Codec ): String = closing(Source .fromInputStream(inputStream)(codec))(_.mkString)
23
20
def slurp (url : URL )(implicit codec : Codec ): String = slurp(url.openStream())
24
21
22
+ extension (input : InputStream ) def bytes : Array [Byte ] =
23
+ val bis = new BufferedInputStream (input)
24
+ val it = Iterator .continually(bis.read()).takeWhile(_ != - 1 ).map(_.toByte)
25
+ new ArrayBuffer [Byte ]().addAll(it).toArray
26
+
27
+ // cf ScalaClassLoader#classBytes
28
+ extension (loader : ClassLoader )
29
+ // An InputStream representing the given class name, or null if not found.
30
+ def classAsStream (className : String ) = loader.getResourceAsStream {
31
+ if className.endsWith(" .class" ) then className
32
+ else s " ${className.replace('.' , '/' )}.class " // classNameToPath
33
+ }
34
+ // The actual bytes for a class file, or an empty array if it can't be found.
35
+ def classBytes (className : String ): Array [Byte ] = classAsStream(className) match
36
+ case null => Array ()
37
+ case stream => stream.bytes
38
+
25
39
val NoClassLoader : ClassLoader = null
26
40
27
41
// virtual dir "fuzz" and "fuzz/buzz/booz.class"
@@ -31,24 +45,21 @@ class AbstractFileClassLoaderTest:
31
45
val booz = buzz.fileNamed(" booz.class" )
32
46
(fuzz, booz)
33
47
34
- @ Test
35
- def afclGetsParent (): Unit =
48
+ @ Test def afclGetsParent (): Unit =
36
49
val p = new URLClassLoader (Array .empty[URL ])
37
50
val d = new VirtualDirectory (" vd" , None )
38
51
val x = new AbstractFileClassLoader (d, p)
39
52
assertSame(p, x.getParent)
40
53
41
- @ Test
42
- def afclGetsResource (): Unit =
54
+ @ Test def afclGetsResource (): Unit =
43
55
val (fuzz, booz) = fuzzBuzzBooz
44
56
booz.writeContent(" hello, world" )
45
57
val sut = new AbstractFileClassLoader (fuzz, NoClassLoader )
46
58
val res = sut.getResource(" buzz/booz.class" )
47
59
assertNotNull(" Find buzz/booz.class" , res)
48
60
assertEquals(" hello, world" , slurp(res))
49
61
50
- @ Test
51
- def afclGetsResourceFromParent (): Unit =
62
+ @ Test def afclGetsResourceFromParent (): Unit =
52
63
val (fuzz, booz) = fuzzBuzzBooz
53
64
val (fuzz_, booz_) = fuzzBuzzBooz
54
65
booz.writeContent(" hello, world" )
@@ -59,8 +70,7 @@ class AbstractFileClassLoaderTest:
59
70
assertNotNull(" Find buzz/booz.class" , res)
60
71
assertEquals(" hello, world" , slurp(res))
61
72
62
- @ Test
63
- def afclGetsResourceInDefaultPackage (): Unit =
73
+ @ Test def afclGetsResourceInDefaultPackage (): Unit =
64
74
val fuzz = new VirtualDirectory (" fuzz" , None )
65
75
val booz = fuzz.fileNamed(" booz.class" )
66
76
val bass = fuzz.fileNamed(" bass" )
@@ -73,8 +83,7 @@ class AbstractFileClassLoaderTest:
73
83
assertEquals(" lo tone" , slurp(sut.getResource(" bass" )))
74
84
75
85
// scala/bug#8843
76
- @ Test
77
- def afclGetsResources (): Unit =
86
+ @ Test def afclGetsResources (): Unit =
78
87
val (fuzz, booz) = fuzzBuzzBooz
79
88
booz.writeContent(" hello, world" )
80
89
val sut = new AbstractFileClassLoader (fuzz, NoClassLoader )
@@ -83,8 +92,7 @@ class AbstractFileClassLoaderTest:
83
92
assertEquals(" hello, world" , slurp(e.nextElement))
84
93
assertFalse(e.hasMoreElements)
85
94
86
- @ Test
87
- def afclGetsResourcesFromParent (): Unit =
95
+ @ Test def afclGetsResourcesFromParent (): Unit =
88
96
val (fuzz, booz) = fuzzBuzzBooz
89
97
val (fuzz_, booz_) = fuzzBuzzBooz
90
98
booz.writeContent(" hello, world" )
@@ -98,36 +106,29 @@ class AbstractFileClassLoaderTest:
98
106
assertEquals(" hello, world_" , slurp(e.nextElement))
99
107
assertFalse(e.hasMoreElements)
100
108
101
- @ Test
102
- def afclGetsResourceAsStream (): Unit =
109
+ @ Test def afclGetsResourceAsStream (): Unit =
103
110
val (fuzz, booz) = fuzzBuzzBooz
104
111
booz.writeContent(" hello, world" )
105
112
val x = new AbstractFileClassLoader (fuzz, NoClassLoader )
106
113
val r = x.getResourceAsStream(" buzz/booz.class" )
107
114
assertNotNull(r)
108
115
assertEquals(" hello, world" , closing(r)(is => Source .fromInputStream(is).mkString))
109
116
110
- /*
111
- @Test
112
- def afclGetsClassBytes(): Unit = {
117
+ @ Test def afclGetsClassBytes (): Unit =
113
118
val (fuzz, booz) = fuzzBuzzBooz
114
- booz writeContent "hello, world"
115
- val x = new AbstractFileClassLoader(fuzz, NoClassLoader)
116
- val b = x .classBytes("buzz/booz.class")
119
+ booz. writeContent( " hello, world" )
120
+ val sut = new AbstractFileClassLoader (fuzz, NoClassLoader )
121
+ val b = sut .classBytes(" buzz/booz.class" )
117
122
assertEquals(" hello, world" , new String (b, UTF8 .charSet))
118
- }
119
123
120
- @Test
121
- def afclGetsClassBytesFromParent(): Unit = {
124
+ @ Test def afclGetsClassBytesFromParent (): Unit =
122
125
val (fuzz, booz) = fuzzBuzzBooz
123
126
val (fuzz_, booz_) = fuzzBuzzBooz
124
- booz writeContent "hello, world"
125
- booz_ writeContent "hello, world_"
127
+ booz. writeContent( " hello, world" )
128
+ booz_. writeContent( " hello, world_" )
126
129
127
130
val p = new AbstractFileClassLoader (fuzz, NoClassLoader )
128
- val x = new AbstractFileClassLoader(fuzz_, p)
129
- val b = x .classBytes("buzz/booz.class")
131
+ val sut = new AbstractFileClassLoader (fuzz_, p)
132
+ val b = sut .classBytes(" buzz/booz.class" )
130
133
assertEquals(" hello, world" , new String (b, UTF8 .charSet))
131
- }
132
- */
133
134
end AbstractFileClassLoaderTest
0 commit comments