+
+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)
+ }
+}
+
+private const val IMAGE_ID = "KSK@gpl.png"
+private val freenetURI = FreenetURI(IMAGE_ID)
+private const val decomposedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/fru%CC%88hstu%CC%88ck.jpg"
+private const val normalizedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/frühstück.jpg"
+private const val textKey = "KSK@gpl.html"
+private const val sizeOkay = 2097152L
+private const val sizeNotOkay = sizeOkay + 1