-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SentryFileReader/SentryFileWriter
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 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
26 changes: 26 additions & 0 deletions
26
sentry/src/main/java/io/sentry/instrumentation/file/SentryFileReader.java
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.sentry.instrumentation.file; | ||
|
||
import io.sentry.IHub; | ||
import java.io.File; | ||
import java.io.FileDescriptor; | ||
import java.io.FileNotFoundException; | ||
import java.io.InputStreamReader; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public final class SentryFileReader extends InputStreamReader { | ||
public SentryFileReader(final @NotNull String fileName) throws FileNotFoundException { | ||
super(new SentryFileInputStream(fileName)); | ||
} | ||
|
||
public SentryFileReader(final @NotNull File file) throws FileNotFoundException { | ||
super(new SentryFileInputStream(file)); | ||
} | ||
|
||
public SentryFileReader(final @NotNull FileDescriptor fd) { | ||
super(new SentryFileInputStream(fd)); | ||
} | ||
|
||
SentryFileReader(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException { | ||
super(new SentryFileInputStream(file, hub)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.sentry.instrumentation.file; | ||
|
||
import io.sentry.IHub; | ||
import java.io.File; | ||
import java.io.FileDescriptor; | ||
import java.io.FileNotFoundException; | ||
import java.io.OutputStreamWriter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public final class SentryFileWriter extends OutputStreamWriter { | ||
public SentryFileWriter(final @NotNull String fileName) throws FileNotFoundException { | ||
super(new SentryFileOutputStream(fileName)); | ||
} | ||
|
||
public SentryFileWriter(final @NotNull String fileName, final boolean append) | ||
throws FileNotFoundException { | ||
super(new SentryFileOutputStream(fileName, append)); | ||
} | ||
|
||
public SentryFileWriter(final @NotNull File file) throws FileNotFoundException { | ||
super(new SentryFileOutputStream(file)); | ||
} | ||
|
||
public SentryFileWriter(final @NotNull File file, final boolean append) | ||
throws FileNotFoundException { | ||
super(new SentryFileOutputStream(file, append)); | ||
} | ||
|
||
public SentryFileWriter(final @NotNull FileDescriptor fd) { | ||
super(new SentryFileOutputStream(fd)); | ||
} | ||
|
||
SentryFileWriter(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException { | ||
super(new SentryFileOutputStream(file, hub)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
sentry/src/test/java/io/sentry/instrumentation/file/SentryFileReaderTest.kt
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.sentry.instrumentation.file | ||
|
||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import io.sentry.IHub | ||
import io.sentry.SentryOptions | ||
import io.sentry.SentryTracer | ||
import io.sentry.SpanStatus.OK | ||
import io.sentry.TransactionContext | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.File | ||
import kotlin.test.assertEquals | ||
|
||
class SentryFileReaderTest { | ||
class Fixture { | ||
val hub = mock<IHub>() | ||
val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) | ||
|
||
internal fun getSut( | ||
tmpFile: File, | ||
activeTransaction: Boolean = true, | ||
): SentryFileReader { | ||
tmpFile.writeText("TEXT") | ||
whenever(hub.options).thenReturn(SentryOptions()) | ||
if (activeTransaction) { | ||
whenever(hub.span).thenReturn(sentryTracer) | ||
} | ||
return SentryFileReader(tmpFile, hub) | ||
} | ||
} | ||
|
||
@get:Rule | ||
val tmpDir = TemporaryFolder() | ||
|
||
private val fixture = Fixture() | ||
|
||
private val tmpFile: File get() = tmpDir.newFile("test.txt") | ||
|
||
@Test | ||
fun `captures a span`() { | ||
val reader = fixture.getSut(tmpFile) | ||
reader.readText() | ||
reader.close() | ||
|
||
assertEquals(fixture.sentryTracer.children.size, 1) | ||
val fileIOSpan = fixture.sentryTracer.children.first() | ||
assertEquals(fileIOSpan.spanContext.operation, "file.read") | ||
assertEquals(fileIOSpan.spanContext.description, "test.txt (4 B)") | ||
assertEquals(fileIOSpan.data["file.size"], 4L) | ||
assertEquals(fileIOSpan.throwable, null) | ||
assertEquals(fileIOSpan.isFinished, true) | ||
assertEquals(fileIOSpan.status, OK) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.sentry.instrumentation.file | ||
|
||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import io.sentry.IHub | ||
import io.sentry.SentryOptions | ||
import io.sentry.SentryTracer | ||
import io.sentry.SpanStatus.OK | ||
import io.sentry.TransactionContext | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.File | ||
import kotlin.test.assertEquals | ||
|
||
class SentryFileWriterTest { | ||
class Fixture { | ||
val hub = mock<IHub>() | ||
val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) | ||
|
||
internal fun getSut( | ||
tmpFile: File, | ||
activeTransaction: Boolean = true, | ||
): SentryFileWriter { | ||
whenever(hub.options).thenReturn(SentryOptions()) | ||
if (activeTransaction) { | ||
whenever(hub.span).thenReturn(sentryTracer) | ||
} | ||
return SentryFileWriter(tmpFile, hub) | ||
} | ||
} | ||
|
||
@get:Rule | ||
val tmpDir = TemporaryFolder() | ||
|
||
private val fixture = Fixture() | ||
|
||
private val tmpFile: File get() = tmpDir.newFile("test.txt") | ||
|
||
@Test | ||
fun `captures a span`() { | ||
val writer = fixture.getSut(tmpFile) | ||
writer.write("TEXT") | ||
writer.close() | ||
|
||
assertEquals(fixture.sentryTracer.children.size, 1) | ||
val fileIOSpan = fixture.sentryTracer.children.first() | ||
assertEquals(fileIOSpan.spanContext.operation, "file.write") | ||
assertEquals(fileIOSpan.spanContext.description, "test.txt (4 B)") | ||
assertEquals(fileIOSpan.data["file.size"], 4L) | ||
assertEquals(fileIOSpan.throwable, null) | ||
assertEquals(fileIOSpan.isFinished, true) | ||
assertEquals(fileIOSpan.status, OK) | ||
} | ||
} |