Class Attachment


  • public class Attachment
    extends java.lang.Object
    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 Detail

      • Attachment

        protected Attachment​(java.lang.String content,
                             MediaType mediaType)
        Convenience constructor, where title is set to null
      • Attachment

        protected Attachment​(java.lang.String content,
                             MediaType mediaType,
                             java.lang.String title)
        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 be null
        mediaType - the mediaType. Must not be null
        title - an optional title, may be null
    • Method Detail

      • getContent

        public java.lang.String getContent()
        The content of the attachment represented as a string. Binary attachments must be encoded in Base64 format.
      • getMediaType

        public MediaType getMediaType()
        The type of the attachment. It depends on the reporter how this information is used.
      • getTitle

        public java.lang.String getTitle()
        An optional title of the attachment. The title can be used by reporters, e.g. as a tooltip. Can be null.
      • getFileName

        public java.lang.String getFileName()
        An optional filename for the attachment. Can be null.
      • withFileName

        public Attachment withFileName​(java.lang.String fileName)
        An optional filename for the attachment without the file type suffix. The name can be used by reporters.
        Since:
        0.9.3
      • withTitle

        public Attachment withTitle​(java.lang.String title)
        Sets the title and returns this
      • showDirectly

        public Attachment 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

        public static Attachment fromBinaryBytes​(byte[] bytes,
                                                 MediaType mediaType)
        Creates an attachment from a given array of bytes. The bytes will be Base64 encoded.
        Throws:
        java.lang.IllegalArgumentException - if mediaType is not binary
      • fromBinaryInputStream

        public static Attachment fromBinaryInputStream​(java.io.InputStream inputStream,
                                                       MediaType mediaType)
                                                throws java.io.IOException
        Creates an attachment from a binary input stream. The content of the stream will be transformed into a Base64 encoded string
        Throws:
        java.io.IOException - if an I/O error occurs
        java.lang.IllegalArgumentException - if mediaType is not binary
      • fromBinaryFile

        public static Attachment fromBinaryFile​(java.io.File file,
                                                MediaType mediaType)
                                         throws java.io.IOException
        Creates an attachment from the given binary file file. The content of the file will be transformed into a Base64 encoded string.
        Throws:
        java.io.IOException - if an I/O error occurs
        java.lang.IllegalArgumentException - if mediaType is not binary
      • fromTextFile

        public static Attachment fromTextFile​(java.io.File file,
                                              MediaType mediaType)
                                       throws java.io.IOException
        Creates a non-binary attachment from the given file.
        Throws:
        java.io.IOException - if an I/O error occurs
        java.lang.IllegalArgumentException - if mediaType is either binary or has no specified charset
      • fromTextInputStream

        public static Attachment fromTextInputStream​(java.io.InputStream inputStream,
                                                     MediaType mediaType)
                                              throws java.io.IOException
        Creates a non-binary attachment from the given file.
        Throws:
        java.io.IOException - if an I/O error occurs
        java.lang.IllegalArgumentException - if mediaType is either binary or has no specified charset
      • plainText

        public static Attachment plainText​(java.lang.String content)
        Creates a text attachment with the given content with media type text/plain.
        Parameters:
        content - the content of the attachment
      • xml

        public static Attachment xml​(java.lang.String content)
        Creates a text attachment with the given content with media type text/xml.
        Parameters:
        content - the content of the attachment
      • json

        public static Attachment json​(java.lang.String content)
        Creates a text attachment with the given content with media type application/json.
        Parameters:
        content - the content of the attachment
      • getShowDirectly

        public boolean getShowDirectly()
        Whether this attachment is shown showDirectly or not
        Since:
        0.8.2
        See Also:
        showDirectly