+ private val loggedRecords = mutableListOf<LogRecord>()
+
+ init {
+ Logger.getLogger(DefaultElementLoader::class.qualifiedName)
+ .apply { level = ALL }
+ .apply {
+ addHandler(object : Handler() {
+ override fun publish(record: LogRecord) {
+ loggedRecords += record
+ }
+
+ override fun flush() = Unit
+ override fun close() = Unit
+ })
+ }
+ }
+
+}
+
+private fun runWithCallback(requestUri: String, ticker: Ticker = createTicker(), callbackAction: (elementLoader: ElementLoader, linkedElement: LinkedElement, callback: BackgroundFetchCallback, fetchedUris: List<FreenetURI>) -> Unit) {
+ val fetchedUris = mutableListOf<FreenetURI>()
+ val callback = AtomicReference<BackgroundFetchCallback>()
+ val freenetInterface = overrideStartFetch { uri, backgroundFetchCallback ->
+ fetchedUris += uri
+ callback.set(backgroundFetchCallback)
+ }
+ val elementLoader = DefaultElementLoader(freenetInterface, ticker)
+ val linkedElement = elementLoader.loadElement(requestUri)
+ callbackAction(elementLoader, linkedElement, callback.get(), fetchedUris)
+}
+
+private fun overrideStartFetch(action: (FreenetURI, BackgroundFetchCallback) -> Unit) = object : FreenetInterface(null, null, null, null, null, dummyHighLevelSimpleClientCreator) {
+ override fun startFetch(uri: FreenetURI, backgroundFetchCallback: BackgroundFetchCallback) {
+ action(uri, backgroundFetchCallback)
+ }
+}
+
+private fun createTicker(vararg times: Long = LongArray(1) { 1 }) = object : Ticker() {
+ private var counter = 0
+ override fun read() =
+ times[min(times.size - 1, counter)]
+ .also { counter++ }
+}
+
+private fun isLinkedElement(link: Matcher<String> = everything(), properties: Matcher<Map<String, Any?>> = everything(), failed: Matcher<Boolean> = everything(), loading: Matcher<Boolean> = everything()) = object : TypeSafeDiagnosingMatcher<LinkedElement>() {
+ override fun matchesSafely(item: LinkedElement, mismatchDescription: Description) =
+ handleMatcher(link, item.link, mismatchDescription) &&
+ handleMatcher(properties, item.properties, mismatchDescription) &&
+ handleMatcher(failed, item.failed, mismatchDescription) &&
+ handleMatcher(loading, item.loading, mismatchDescription)
+
+ override fun describeTo(description: Description) {
+ description.appendText("is linked element for key matching ").appendValue(link)
+ .appendText(", properties matching ").appendValue(properties)
+ .appendText(", failed matching ").appendValue(failed)
+ .appendText(", loading matching ").appendValue(loading)
+ }