1 /* <lambda>null2 * Copyright 2017 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package trebuchet.extractors 18 19 import org.junit.Test 20 import org.junit.Assert.* 21 import trebuchet.io.BufferProducer 22 import trebuchet.io.DataSlice 23 import trebuchet.io.StreamingReader 24 import trebuchet.io.asSlice 25 26 27 class SystraceExtractorTest { 28 val HTML_HEADER = "<!DOCTYPE html>\n<html>\n<head>" 29 val TRACE_BEGIN = """<script class="trace-data" type="application/text">""" 30 val TRACE_END = "</script>" 31 32 @Test 33 fun testExtractMultiWindowSingleOutput() { 34 val buffers = listOf(HTML_HEADER, TRACE_BEGIN, "one", TRACE_END) 35 val result = extract(buffers) 36 assertEquals(1, result.size) 37 assertEquals("one", result[0].toString()) 38 } 39 40 @Test 41 fun testExtractMultiWindowMultiOutput() { 42 val buffers = listOf(HTML_HEADER, TRACE_BEGIN, "first", "second", "third", TRACE_END) 43 val result = extract(buffers) 44 assertEquals(3, result.size) 45 assertEquals("first", result[0].toString()) 46 assertEquals("second", result[1].toString()) 47 assertEquals("third", result[2].toString()) 48 } 49 50 @Test 51 fun testExtractExtremeSplitting() { 52 val buffers = listOf(HTML_HEADER, TRACE_BEGIN, "12345", TRACE_END) 53 .joinToString(separator = "").asIterable().map { it.toString() } 54 val result = extract(buffers) 55 // assertEquals(5, result.size) 56 assertEquals("1", result[0].toString()) 57 assertEquals("2", result[1].toString()) 58 assertEquals("3", result[2].toString()) 59 assertEquals("4", result[3].toString()) 60 assertEquals("5", result[4].toString()) 61 } 62 63 @Test 64 fun testExtractSingleBuffer() { 65 val buffer = listOf(HTML_HEADER, TRACE_BEGIN, "one big buffer", TRACE_END) 66 .joinToString(separator = "") 67 val result = extract(listOf(buffer)) 68 assertEquals(1, result.size) 69 assertEquals("one big buffer", result[0].toString()) 70 } 71 72 fun extract(list: Iterable<String>): List<DataSlice> { 73 val extractor = SystraceExtractor() 74 var output = mutableListOf<DataSlice>() 75 extractor.extract(makeStream(list.iterator())) { outputStream -> 76 while (true) { 77 val next = outputStream.next() 78 if (next == null) { 79 outputStream.close() 80 break 81 } 82 output.add(next) 83 } 84 } 85 return output 86 } 87 88 fun makeStream(list: Iterator<String>): StreamingReader { 89 return StreamingReader(object : BufferProducer { 90 override fun next(): DataSlice? { 91 if (list.hasNext()) { 92 return list.next().asSlice() 93 } 94 return null 95 } 96 }) 97 } 98 }