classSearchTest{ @Test funtestSearch() { try { val pageContent = ("There are certain queer times and occasions " + "in this strange mixed affair we call life when a man " + "takes this whole universe for a vast practical joke, " + "though the wit thereof he but dimly discerns, and more " + "than suspects that the joke is at nobody's expense but " + "his own.") val bytes = pageContent.toByteArray() val stream = ByteArrayInputStream(bytes) // search var search = Search(stream, "practical joke", "1") Search.LOGGER.level = Level.OFF search.setSurroundingCharacterCount(10) search.execute() Assert.assertFalse(search.errored()) val matches: List<Match> = search.getMatches() Assert.assertThat<List<Match>>(matches, CoreMatchers.`is`(CoreMatchers.notNullValue())) Assert.assertTrue(matches.size >= 1) val match: Match = matches[0] Assert.assertThat(match.searchString, CoreMatchers.equalTo("practical joke")) Assert.assertThat(match.surroundingContext, CoreMatchers.equalTo("or a vast practical joke, though t")) stream.close()
classSearchTest{ @Test @Throws(IOException::class) funtestSearch() { val pageContent = ("There are certain queer times and occasions " + "in this strange mixed affair we call life when a man " + "takes this whole universe for a vast practical joke, " + "though the wit thereof he but dimly discerns, and more " + "than suspects that the joke is at nobody's expense but " + "his own.") val bytes = pageContent.toByteArray() val stream = ByteArrayInputStream(bytes) // search var search = Search(stream, "practical joke", "1") Search.LOGGER.level = Level.OFF search.setSurroundingCharacterCount(10) search.execute() Assert.assertFalse(search.errored()) val matches: List<Match> = search.getMatches() Assert.assertTrue(matches.size >= 1) val match: Match = matches[0] Assert.assertThat(match.searchString, CoreMatchers.equalTo("practical joke")) Assert.assertThat(match.surroundingContext, CoreMatchers.equalTo("or a vast practical joke, though t")) stream.close()
classSearchTest{ @Test @Throws(IOException::class) funtestSearch() { val pageContent = ("There are certain queer times and occasions " + "in this strange mixed affair we call life when a man " + "takes this whole universe for a vast practical joke, " + "though the wit thereof he but dimly discerns, and more " + "than suspects that the joke is at nobody's expense but " + "his own.") val bytes = pageContent.toByteArray() val stream = ByteArrayInputStream(bytes) // search var search = Search(stream, "practical joke", "1") Search.LOGGER.level = Level.OFF search.setSurroundingCharacterCount(10) search.execute() assertFalse(search.errored()) assertThat(search.getMatches(), containsMatches<Match>(arrayOf( Match("1", "practical joke", "or a vast practical joke, though t" )))) stream.close()
// negative val connection = URL("http://bit.ly/15sYPA7").openConnection() val inputStream = connection.getInputStream() search = Search(inputStream, "smelt", "http://bit.ly/15sYPA7") search.execute() assertTrue(search.getMatches().isEmpty()) stream.close() } }
search.getMatches() 호출에서 반환된 매칭 목록에 대한 구현 세부 사항 5줄을 커스텀 Matcher Class로 구현 (ContainsMatches)
overridefunmatchesSafely(actual: List<Match>): Boolean { if (actual.size != expected.size) returnfalse for (i in expected.indices) if (!equals(expected[i], actual[i])) returnfalse returntrue }