Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 60 additions & 27 deletions
87
src/test/scala/codes/reactive/scalatime/control/CatchersSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,72 @@ | ||
/****************************************************************** | ||
* See the NOTICE file distributed with this work for additional * | ||
* information regarding Copyright ownership. The author/authors * | ||
* license this file to you under the terms of the Apache License * | ||
* Version 2.0 (the "License"); you may not use this file except * | ||
* in compliance with the License. You may obtain a copy of the * | ||
* License at: * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * | ||
* either express or implied. See the License for the specific * | ||
* language governing permissions and limitations under the * | ||
* License. * | ||
******************************************************************/ | ||
/** **************************************************************** | ||
* See the NOTICE file distributed with this work for additional * | ||
* information regarding Copyright ownership. The author/authors * | ||
* license this file to you under the terms of the Apache License * | ||
* Version 2.0 (the "License"); you may not use this file except * | ||
* in compliance with the License. You may obtain a copy of the * | ||
* License at: * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * | ||
* either express or implied. See the License for the specific * | ||
* language governing permissions and limitations under the * | ||
* License. * | ||
* *****************************************************************/ | ||
|
||
package codes.reactive.scalatime | ||
package control | ||
|
||
import java.time.{LocalTime, LocalDate} | ||
import java.time.temporal.{IsoFields, UnsupportedTemporalTypeException} | ||
import java.time.DateTimeException | ||
import java.time.format.DateTimeParseException | ||
import java.time.temporal.UnsupportedTemporalTypeException | ||
import java.time.zone.ZoneRulesException | ||
|
||
import org.scalatest.{Matchers, FunSuite} | ||
import org.scalatest.{FunSuite, Matchers} | ||
|
||
import scala.util.{Success, Try} | ||
import scala.util.{Failure, Success, Try} | ||
|
||
|
||
class CatchersSuite extends FunSuite with Matchers { | ||
|
||
test("Catcher provides 'Catcher' instances for all 'DateTimeException' instances") { | ||
val t = Try(LocalDate.parse(")(&#)(@*@&#%@#%@#%)")).recover(Catcher.all(_ => LocalDate.of(2014, 7, 13))) | ||
val t1 = Try(LocalTime.now().get(IsoFields.DAY_OF_QUARTER)).recover(Catcher.unsupportedTemporalType(throw _)) | ||
t shouldEqual Success(LocalDate.of(2014, 7, 13)) | ||
t1.isFailure shouldEqual true | ||
intercept[UnsupportedTemporalTypeException](t1.get) | ||
case class Exceptions(base: Throwable = new DateTimeException("test"), | ||
temporalType: Throwable = new UnsupportedTemporalTypeException("test"), | ||
zoneRules: Throwable = new ZoneRulesException("test"), | ||
parse: Throwable = new DateTimeParseException("test", "", 0)) | ||
|
||
|
||
test("Catcher instances do not match unrelated Throwables") { | ||
val es = Exceptions() | ||
(Try(throw new RuntimeException).recover(Catcher.all(_ ⇒ true)) orElse Success(false)) shouldBe Success(false) | ||
(Try(throw new RuntimeException).recover(Catcher.dateTimeParseException(_ ⇒ true)) orElse Success(false)) shouldBe Success(false) | ||
(Try(throw new RuntimeException).recover(Catcher.unsupportedTemporalType(_ ⇒ true)) orElse Success(false)) shouldBe Success(false) | ||
(Try(throw new RuntimeException).recover(Catcher.zoneRules(_ ⇒ true)) orElse Success(false)) shouldBe Success(false) | ||
} | ||
|
||
test("Catcher.all matches all 'DateTimeException' instances") { | ||
val es = Exceptions() | ||
Try(throw es.base).recover(Catcher.all(_ ⇒ true)) shouldBe Success(true) | ||
Try(throw es.zoneRules).recover(Catcher.all(_ ⇒ true)) shouldBe Success(true) | ||
} | ||
|
||
test("Catcher.dateTimeParseException matches 'DateTimeParseException' instances") { | ||
val es = Exceptions() | ||
Try(throw es.parse).recover(Catcher.dateTimeParseException(_ ⇒ true)) shouldBe Success(true) | ||
Try(throw es.zoneRules).recover(Catcher.dateTimeParseException(_ ⇒ true)) shouldBe Failure(es.zoneRules) | ||
} | ||
|
||
test("Catcher.unsupportedTemporalType matches 'UnsupportedTemporalTypeException' instances") { | ||
val es = Exceptions() | ||
Try(throw es.temporalType).recover(Catcher.unsupportedTemporalType(_ ⇒ true)) shouldBe Success(true) | ||
Try(throw es.zoneRules).recover(Catcher.unsupportedTemporalType(_ ⇒ true)) shouldBe Failure(es.zoneRules) | ||
} | ||
|
||
test("Catcher.zoneRules matches 'ZoneRulesException' instances") { | ||
val es = Exceptions() | ||
Try(throw es.zoneRules).recover(Catcher.zoneRules(_ ⇒ true)) shouldBe Success(true) | ||
Try(throw es.temporalType).recover(Catcher.zoneRules(_ ⇒ true)) shouldBe Failure(es.temporalType) | ||
} | ||
|
||
} |