ByteResource.java

1
package fr.sii.ogham.core.resource;
2
3
import java.io.ByteArrayInputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.Arrays;
7
8
import fr.sii.ogham.core.util.EqualsBuilder;
9
import fr.sii.ogham.core.util.HashCodeBuilder;
10
import fr.sii.ogham.core.util.IOUtils;
11
12
/**
13
 * Basic implementation of a {@link NamedResource} that simply stores a
14
 * reference to the provided bytes.
15
 * 
16
 * @author Aurélien Baudet
17
 *
18
 */
19
public class ByteResource implements NamedResource {
20
	/**
21
	 * The content of the resource as array of bytes
22
	 */
23
	private byte[] bytes;
24
25
	/**
26
	 * The name of the resource
27
	 */
28
	private String name;
29
30
	/**
31
	 * Initialize the resource with the provided name and bytes that are
32
	 * immediately read from the stream. The bytes are copied into a new array
33
	 * to prevent security leaks.
34
	 * 
35
	 * @param name
36
	 *            the name of the resource
37
	 * @param stream
38
	 *            the stream that is read
39
	 * @throws IOException
40
	 *             when the input stream can't be read
41
	 */
42
	public ByteResource(String name, InputStream stream) throws IOException {
43
		this(name, IOUtils.toByteArray(stream));
44
	}
45
46
	/**
47
	 * Initialize the resource with the provided name and bytes. The bytes are
48
	 * copied into a new array to prevent security leaks.
49
	 * 
50
	 * @param name
51
	 *            the name of the resource
52
	 * @param bytes
53
	 *            the bytes of the resource
54
	 */
55
	public ByteResource(String name, byte[] bytes) {
56
		super();
57
		this.name = name;
58
		this.bytes = Arrays.copyOf(bytes, bytes.length);
59
	}
60
61
	@Override
62
	public InputStream getInputStream() {
63 7 1. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → NO_COVERAGE
2. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → TIMED_OUT
3. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED
4. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED
5. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED
6. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED
7. getInputStream : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED
		return new ByteArrayInputStream(bytes);
64
	}
65
66
	@Override
67
	public String getName() {
68 4 1. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → NO_COVERAGE
2. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → SURVIVED
3. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → KILLED
4. getName : replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → KILLED
		return name;
69
	}
70
71
	public byte[] getBytes() {
72 3 1. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → NO_COVERAGE
2. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → SURVIVED
3. getBytes : replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → KILLED
		return bytes;
73
	}
74
75
	@Override
76
	public int hashCode() {
77 3 1. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → NO_COVERAGE
2. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → SURVIVED
3. hashCode : replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → KILLED
		return new HashCodeBuilder().append(name, bytes).hashCode();
78
	}
79
80
	@Override
81
	public boolean equals(Object obj) {
82 6 1. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → NO_COVERAGE
2. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → NO_COVERAGE
3. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → SURVIVED
4. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → KILLED
5. equals : replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → KILLED
6. equals : replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → KILLED
		return new EqualsBuilder(this, obj).appendFields("name", "bytes").isEqual();
83
	}
84
}

Mutations

63

1.1
Location : getInputStream
Killed by : none
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → TIMED_OUT

2.2
Location : getInputStream
Killed by : none
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → NO_COVERAGE

3.3
Location : getInputStream
Killed by : oghamcore.it.html.translator.InlineImageTranslatorTest.oghamAttributesShoulBeRemoved(oghamcore.it.html.translator.InlineImageTranslatorTest)
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED

4.4
Location : getInputStream
Killed by : oghamjavamail.it.JavaMailStructureTest.plainTextBodyWithAttachments(oghamjavamail.it.JavaMailStructureTest)
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED

5.5
Location : getInputStream
Killed by : oghamthymeleafv3.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv3.it.ThymeleafDetectorTest)
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED

6.6
Location : getInputStream
Killed by : oghamall.it.html.translator.JsoupInlineCssTranslatorTest.externalStyles(oghamall.it.html.translator.JsoupInlineCssTranslatorTest)
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED

7.7
Location : getInputStream
Killed by : oghamthymeleafv2.it.ThymeleafDetectorTest.foundButEmpty(oghamthymeleafv2.it.ThymeleafDetectorTest)
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getInputStream → KILLED

68

1.1
Location : getName
Killed by : none
replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → NO_COVERAGE

2.2
Location : getName
Killed by : none
replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → SURVIVED

3.3
Location : getName
Killed by : oghamall.it.email.EmailSMTPDefaultsTest.attachmentLookup(oghamall.it.email.EmailSMTPDefaultsTest)
replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → KILLED

4.4
Location : getName
Killed by : oghamcore.ut.core.EqualsTest.byteSource(oghamcore.ut.core.EqualsTest)
replaced return value with "" for fr/sii/ogham/core/resource/ByteResource::getName → KILLED

72

1.1
Location : getBytes
Killed by : none
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → NO_COVERAGE

2.2
Location : getBytes
Killed by : oghamcore.ut.core.EqualsTest.byteSource(oghamcore.ut.core.EqualsTest)
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → KILLED

3.3
Location : getBytes
Killed by : none
replaced return value with null for fr/sii/ogham/core/resource/ByteResource::getBytes → SURVIVED

77

1.1
Location : hashCode
Killed by : oghamcore.ut.core.EqualsTest.byteSource(oghamcore.ut.core.EqualsTest)
replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → KILLED

2.2
Location : hashCode
Killed by : none
replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → NO_COVERAGE

3.3
Location : hashCode
Killed by : none
replaced int return with 0 for fr/sii/ogham/core/resource/ByteResource::hashCode → SURVIVED

82

1.1
Location : equals
Killed by : oghamcore.ut.html.inliner.impl.JsoupAttachImageInlinerTest.withImages(oghamcore.ut.html.inliner.impl.JsoupAttachImageInlinerTest)
replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → KILLED

2.2
Location : equals
Killed by : oghamall.it.html.translator.JsoupInlineImageTranslatorTest.skipExternalImages(oghamall.it.html.translator.JsoupInlineImageTranslatorTest)
replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → KILLED

3.3
Location : equals
Killed by : none
replaced boolean return with false for fr/sii/ogham/core/resource/ByteResource::equals → NO_COVERAGE

4.4
Location : equals
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → NO_COVERAGE

5.5
Location : equals
Killed by : oghamcore.ut.core.EqualsTest.byteSource(oghamcore.ut.core.EqualsTest)
replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → KILLED

6.6
Location : equals
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/resource/ByteResource::equals → SURVIVED

Active mutators

Tests examined


Report generated by PIT OGHAM