Skip to content

Commit

Permalink
Merge pull request #2533 from sjrd/small-javalib-changes
Browse files Browse the repository at this point in the history
Small javalib changes
  • Loading branch information
nicolasstucki committed Jul 15, 2016
2 parents 684dc76 + ac1a334 commit 407b0f4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 9 deletions.
24 changes: 16 additions & 8 deletions javalib/src/main/scala/java/io/BufferedReader.scala
Expand Up @@ -17,7 +17,10 @@ class BufferedReader(in: Reader, sz: Int) extends Reader {
private[this] var validMark = false

override def close(): Unit = {
closed = true
if (!closed) {
closed = true
in.close()
}
}

override def mark(readAheadLimit: Int): Unit = {
Expand Down Expand Up @@ -105,14 +108,19 @@ class BufferedReader(in: Reader, sz: Int) extends Reader {
}

override def skip(n: Long): Long = {
if (n < 0) throw new IllegalArgumentException("n negative")
else if (pos < end) {
val count = Math.min(n, end - pos).toInt
pos += count
count.toLong
if (n < 0) {
throw new IllegalArgumentException("n negative")
} else {
validMark = false
in.skip(n)
ensureOpen()

if (pos < end) {
val count = Math.min(n, end - pos).toInt
pos += count
count.toLong
} else {
validMark = false
in.skip(n)
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions javalib/src/main/scala/java/util/EventObject.scala
@@ -0,0 +1,8 @@
package java.util

class EventObject(protected var source: AnyRef) {
def getSource(): AnyRef = source

override def toString(): String =
s"${getClass.getSimpleName}[source=$source]"
}
Expand Up @@ -114,7 +114,42 @@ class BufferedReaderTest {
val str = "line1\nline2\r\n\nline4\rline5"
def newReader: BufferedReader = new BufferedReader(new StringReader(str), 3)

@Test def should_provide_read()(): Unit = {
@Test def close(): Unit = {
class UnderlyingReader extends StringReader(str) {
var closeCount: Int = 0

override def close(): Unit = {
closeCount += 1
/* Do not call super.close(), to ensure IOExceptions come from
* BufferedReader, and not the underlying reader.
*/
}
}

val underlying = new UnderlyingReader
val r = new BufferedReader(underlying)
r.read()
assertEquals(0, underlying.closeCount)
r.close()
assertEquals(1, underlying.closeCount)

// close() actually prevents further use of the reader
assertThrows(classOf[IOException], r.mark(1))
assertThrows(classOf[IOException], r.read())
assertThrows(classOf[IOException], r.read(new Array[Char](1), 0, 1))
assertThrows(classOf[IOException], r.read(new Array[Char](1)))
assertThrows(classOf[IOException], r.readLine())
assertThrows(classOf[IOException], r.ready())
assertThrows(classOf[IOException], r.reset())
assertThrows(classOf[IOException], r.skip(1L))
assertThrows(classOf[IllegalArgumentException], r.skip(-1L))

// close() is idempotent
r.close()
assertEquals(1, underlying.closeCount)
}

@Test def should_provide_read(): Unit = {
val r = newReader

for (c <- str) {
Expand Down
@@ -0,0 +1,49 @@
/* __ *\
** ________ ___ / / ___ __ ____ Scala.js Test Suite **
** / __/ __// _ | / / / _ | __ / // __/ (c) 2013-2016, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ **
** /____/\___/_/ |_/____/_/ | |__/ /____/ **
** |/____/ **
\* */
package org.scalajs.testsuite.javalib.util

import org.junit.Test
import org.junit.Assert._

import java.util.EventObject

class EventObjectTest {
@Test def getSource(): Unit = {
val src = new AnyRef
val e = new EventObject(src)
assertSame(src, e.getSource)
}

/* #2532 This does not link, because we cannot declare a Java field
@Test def sourceField(): Unit = {
class E(s: AnyRef) extends EventObject(s) {
def setSource(s: AnyRef): Unit = source = s
def otherGetSource: AnyRef = source
}
val src1 = new AnyRef
val e = new E(src1)
assertSame(src1, e.otherGetSource)
val src2 = new AnyRef
e.setSource(src2)
assertSame(src2, e.otherGetSource)
assertSame(src2, e.getSource)
}
*/

@Test def testToString(): Unit = {
/* There is not much we can test about toString, but it should not be the
* default inherited from Object.
*/
val e = new EventObject(new AnyRef)
assertNotNull(e.toString())
val default = classOf[EventObject].getName + "@" + Integer.toHexString(e.##)
assertNotEquals(default, e.toString())
}
}

0 comments on commit 407b0f4

Please sign in to comment.