PropsPath.java

1
package fr.sii.ogham.core.builder.env.props;
2
3
import java.io.FileInputStream;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.nio.file.Paths;
8
import java.util.Properties;
9
import java.util.regex.Pattern;
10
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
14
import fr.sii.ogham.core.exception.builder.BuildException;
15
16
public class PropsPath extends AbstractProps {
17
	private static final Logger LOG = LoggerFactory.getLogger(PropsPath.class);
18
	private static final String FILE_PREFIX = "file:";
19
	private static final String CLASSPATH_PREFIX = "classpath:";
20
	private static final Pattern OPTIONAL_MARKER = Pattern.compile("[?]");
21
	private final String path;
22
	private final boolean optional;
23
24
	public PropsPath(String path, int priority, int index) {
25
		super(priority, index);
26
		this.path = OPTIONAL_MARKER.matcher(path).replaceAll("");
27
		this.optional = path.contains("?");
28
	}
29
30
	@Override
31
	public Properties getProps() {
32
		try {
33 1 1. getProps : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::getProps → RUN_ERROR
			return load();
34
		} catch (FileNotFoundException e) {
35 1 1. getProps : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::getProps → RUN_ERROR
			return failOrSkip(e, new Properties());
36
		} catch (IOException e) {
37
			throw new BuildException("Failed to load properties file " + path, e);
38
		}
39
	}
40
41
	private Properties load() throws IOException {
42 1 1. load : negated conditional → RUN_ERROR
		if (path.startsWith(CLASSPATH_PREFIX)) {
43 1 1. load : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::load → RUN_ERROR
			return loadFromClasspath(path.substring(CLASSPATH_PREFIX.length()));
44
		}
45 1 1. load : negated conditional → RUN_ERROR
		if (path.startsWith(FILE_PREFIX)) {
46 1 1. load : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::load → RUN_ERROR
			return loadFromExternalFile(path.substring(FILE_PREFIX.length()));
47
		}
48 1 1. load : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::load → RUN_ERROR
		return loadFromClasspath(path);
49
	}
50
51
	private Properties loadFromExternalFile(String path) throws IOException {
52
		Properties props = new Properties();
53
		try (FileInputStream fis = new FileInputStream(Paths.get(path).toFile())) {
54 1 1. loadFromExternalFile : removed call to java/util/Properties::load → RUN_ERROR
			props.load(fis);
55
			return props;
56
		} catch (FileNotFoundException e) {
57 1 1. loadFromExternalFile : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::loadFromExternalFile → RUN_ERROR
			return failOrSkip(e, props);
58
		}
59
	}
60
61
	private static String getClasspathPath(String path) {
62 2 1. getClasspathPath : replaced return value with "" for fr/sii/ogham/core/builder/env/props/PropsPath::getClasspathPath → RUN_ERROR
2. getClasspathPath : negated conditional → RUN_ERROR
		return path.startsWith("/") ? path.substring(1) : path;
63
	}
64
65
	private Properties loadFromClasspath(String path) throws IOException {
66
		InputStream stream = getClass().getClassLoader().getResourceAsStream(getClasspathPath(path));
67 1 1. loadFromClasspath : negated conditional → RUN_ERROR
		if (stream == null) {
68
			throw new FileNotFoundException("Properties file not found in classpath");
69
		}
70
		Properties props = new Properties();
71 1 1. loadFromClasspath : removed call to java/util/Properties::load → RUN_ERROR
		props.load(stream);
72 1 1. loadFromClasspath : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::loadFromClasspath → RUN_ERROR
		return props;
73
	}
74
75
	private Properties failOrSkip(FileNotFoundException e, Properties properties) {
76 1 1. failOrSkip : negated conditional → RUN_ERROR
		if (optional) {
77
			LOG.debug("Properties file {} is missing but marked as optional", path);
78
			LOG.trace("Original exception", e);
79 1 1. failOrSkip : replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::failOrSkip → RUN_ERROR
			return properties;
80
		}
81
		throw new BuildException("Properties file is required and missing: " + path, e);
82
	}
83
}

Mutations

33

1.1
Location : getProps
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::getProps → RUN_ERROR

35

1.1
Location : getProps
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::getProps → RUN_ERROR

42

1.1
Location : load
Killed by :
negated conditional → RUN_ERROR

43

1.1
Location : load
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::load → RUN_ERROR

45

1.1
Location : load
Killed by :
negated conditional → RUN_ERROR

46

1.1
Location : load
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::load → RUN_ERROR

48

1.1
Location : load
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::load → RUN_ERROR

54

1.1
Location : loadFromExternalFile
Killed by :
removed call to java/util/Properties::load → RUN_ERROR

57

1.1
Location : loadFromExternalFile
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::loadFromExternalFile → RUN_ERROR

62

1.1
Location : getClasspathPath
Killed by :
replaced return value with "" for fr/sii/ogham/core/builder/env/props/PropsPath::getClasspathPath → RUN_ERROR

2.2
Location : getClasspathPath
Killed by :
negated conditional → RUN_ERROR

67

1.1
Location : loadFromClasspath
Killed by :
negated conditional → RUN_ERROR

71

1.1
Location : loadFromClasspath
Killed by :
removed call to java/util/Properties::load → RUN_ERROR

72

1.1
Location : loadFromClasspath
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::loadFromClasspath → RUN_ERROR

76

1.1
Location : failOrSkip
Killed by :
negated conditional → RUN_ERROR

79

1.1
Location : failOrSkip
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/props/PropsPath::failOrSkip → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1