Package com.tngtech.jgiven.attachment
Class Attachment
java.lang.Object
com.tngtech.jgiven.attachment.Attachment
Represents an attachment of a step.
Attachments must be representable as a String so that it can be stored as JSON.
For binary attachments this means that they have to be encoded with Base64.
In addition, attachments must have a media type so that reporters know
how to present an attachment.
- Since:
- 0.7.0
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAttachment(String content, MediaType mediaType) Convenience constructor, where title is set tonullprotectedAttachment(String content, MediaType mediaType, String title) Creates a new instance of this Attachment -
Method Summary
Modifier and TypeMethodDescriptionstatic AttachmentfromBase64(String base64encodedContent, MediaType mediaType) Equivalent toAttachment(String, MediaType)static AttachmentfromBinaryBytes(byte[] bytes, MediaType mediaType) Creates an attachment from a given array of bytes.static AttachmentfromBinaryFile(File file, MediaType mediaType) Creates an attachment from the given binary filefile.static AttachmentfromBinaryInputStream(InputStream inputStream, MediaType mediaType) Creates an attachment from a binary input stream.static AttachmentEquivalent toAttachment(String, MediaType)static AttachmentfromTextFile(File file, MediaType mediaType) Creates a non-binary attachment from the given file.static AttachmentfromTextInputStream(InputStream inputStream, MediaType mediaType) Creates a non-binary attachment from the given file.The content of the attachment represented as a string.An optional filename for the attachment.The type of the attachment.booleanWhether this attachment is shown showDirectly or notgetTitle()An optional title of the attachment.static AttachmentCreates a text attachment with the given content with media type application/json.static AttachmentCreates a text attachment with the given content with media type text/plain.Directly shows this attachment in the report.withFileName(String fileName) An optional filename for the attachment without the file type suffix.Sets the title and returnsthisstatic AttachmentCreates a text attachment with the given content with media type text/xml.
-
Constructor Details
-
Attachment
Convenience constructor, where title is set tonull -
Attachment
Creates a new instance of this Attachment- Parameters:
content- the content of this attachment. In case of a binary attachment, this must be Base64 encoded. Must not benullmediaType- the mediaType. Must not benulltitle- an optional title, may benull
-
-
Method Details
-
getContent
The content of the attachment represented as a string. Binary attachments must be encoded in Base64 format. -
getMediaType
The type of the attachment. It depends on the reporter how this information is used. -
getTitle
An optional title of the attachment. The title can be used by reporters, e.g. as a tooltip. Can benull. -
getFileName
An optional filename for the attachment. Can benull. -
withFileName
An optional filename for the attachment without the file type suffix. The name can be used by reporters.- Since:
- 0.9.3
-
withTitle
Sets the title and returnsthis -
showDirectly
Directly shows this attachment in the report. By default, the attachment is not directly shown in the scenario, but referenced by a link.Note: This currently works only for images!
- Returns:
this- Throws:
JGivenWrongUsageException- if the attachment is not an image- Since:
- 0.8.2
-
fromBinaryBytes
Creates an attachment from a given array of bytes. The bytes will be Base64 encoded.- Throws:
IllegalArgumentException- if mediaType is not binary
-
fromBinaryInputStream
public static Attachment fromBinaryInputStream(InputStream inputStream, MediaType mediaType) throws IOException Creates an attachment from a binary input stream. The content of the stream will be transformed into a Base64 encoded string- Throws:
IOException- if an I/O error occursIllegalArgumentException- if mediaType is not binary
-
fromBinaryFile
Creates an attachment from the given binary filefile. The content of the file will be transformed into a Base64 encoded string.- Throws:
IOException- if an I/O error occursIllegalArgumentException- if mediaType is not binary
-
fromTextFile
Creates a non-binary attachment from the given file.- Throws:
IOException- if an I/O error occursIllegalArgumentException- if mediaType is either binary or has no specified charset
-
fromTextInputStream
public static Attachment fromTextInputStream(InputStream inputStream, MediaType mediaType) throws IOException Creates a non-binary attachment from the given file.- Throws:
IOException- if an I/O error occursIllegalArgumentException- if mediaType is either binary or has no specified charset
-
fromText
Equivalent toAttachment(String, MediaType)- Throws:
IllegalArgumentException- if mediaType is binary
-
plainText
Creates a text attachment with the given content with media type text/plain.- Parameters:
content- the content of the attachment
-
xml
Creates a text attachment with the given content with media type text/xml.- Parameters:
content- the content of the attachment
-
json
Creates a text attachment with the given content with media type application/json.- Parameters:
content- the content of the attachment
-
fromBase64
Equivalent toAttachment(String, MediaType)- Throws:
IllegalArgumentException- if mediaType is not binary
-
getShowDirectly
public boolean getShowDirectly()Whether this attachment is shown showDirectly or not- Since:
- 0.8.2
- See Also:
-
showDirectly
-