• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nulab / zxcvbn4j / #141

pending completion
#141

push

github-actions

web-flow
Merge pull request #135 from manchilop/master

Added feedback messages translated into Spanish

1392 of 1507 relevant lines covered (92.37%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.0
/src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java
1
package com.nulabinc.zxcvbn.io;
2

3
import java.io.FileNotFoundException;
4
import java.io.IOException;
5
import java.io.InputStream;
6

7
public class ClasspathResource implements Resource {
8

9
    private final String path;
10

11
    public ClasspathResource(final String path) {
1✔
12
        this.path = path;
1✔
13
    }
1✔
14

15
    @Override
16
    public InputStream getInputStream() throws IOException {
17
        InputStream in = this.getResourceAsStreamWithFallback(path);
1✔
18
        if (in == null) {
1✔
19
            throw new FileNotFoundException("Could not get resource as stream");
×
20
        }
21
        return in;
1✔
22
    }
23

24
    /**
25
     * This code base is spring-framework's ClassUtils#getDefaultClassLoader().
26
     * https://github.com/spring-projects/spring-framework/blob/dfb7ca733ad309b35040e0027fb7a2f10f3a196a/spring-core/src/main/java/org/springframework/util/ClassUtils.java#L173-L210
27
     * <p>
28
     * First, return the InputStream to use: typically the thread context ClassLoader, if available;
29
     * Next, the ClassLoader that loaded the ResourceLoader class will be used as fallback.
30
     * Finally, if even the system ClassLoader could not access resource as stream, return null.
31
     */
32
    private InputStream getResourceAsStreamWithFallback(String path) {
33
        // 0. try loading the resource from the same artifact as this class
34
        {
35
            InputStream in = getClass().getResourceAsStream(path);
1✔
36
            if (in != null) {
1✔
37
                return in;
1✔
38
            }
39
        } // no exceptions thrown
40

41
        // 1. try to get resource with thread context ClassLoader
42
        try {
43
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
×
44
            InputStream in = this.getResourceAsStream(cl, path);
×
45
            if (in != null) {
×
46
                return in;
×
47
            }
48
        } catch (Throwable ex) {
×
49
            // Cannot access thread context ClassLoader - falling back...
50
        }
×
51

52
        // 2. try to get resource with this class context ClassLoader
53
        try {
54
            ClassLoader cl = this.getClass().getClassLoader();
×
55
            InputStream in = this.getResourceAsStream(cl, path);
×
56
            if (in != null) {
×
57
                return in;
×
58
            }
59
        } catch (Throwable ex) {
×
60
            // Cannot access this class context ClassLoader - falling back...
61
        }
×
62

63
        // 3. try to get resource with this class context ClassLoader
64
        try {
65
            ClassLoader cl = ClassLoader.getSystemClassLoader();
×
66
            InputStream in = this.getResourceAsStream(cl, path);
×
67
            if (in != null) {
×
68
                return in;
×
69
            }
70
        } catch (Throwable ex) {
×
71
            // Cannot access system ClassLoader - oh well, maybe the caller can live with null...
72
        }
×
73

74
        return null;
×
75
    }
76

77
    private InputStream getResourceAsStream(ClassLoader cl, String path) {
78
        try {
79
            if (cl != null) {
×
80
                InputStream in = cl.getResourceAsStream(path);
×
81
                if (in != null) {
×
82
                    return in;
×
83
                }
84
            }
85
        } catch (Throwable ex) {
×
86
            // Cannot access resource as stream
87
        }
×
88
        return null;
×
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc