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

pmd / pmd / #3787

pending completion
#3787

push

github actions

oowekyala
add a test for display name in the cache

67063 of 127681 relevant lines covered (52.52%)

0.53 hits per line

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

95.0
/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/NioTextFile.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.document;
6

7
import java.io.BufferedWriter;
8
import java.io.IOException;
9
import java.nio.charset.Charset;
10
import java.nio.file.FileSystem;
11
import java.nio.file.Files;
12
import java.nio.file.Path;
13

14
import org.checkerframework.checker.nullness.qual.NonNull;
15

16
import net.sourceforge.pmd.internal.util.BaseCloseable;
17
import net.sourceforge.pmd.lang.LanguageVersion;
18
import net.sourceforge.pmd.util.AssertionUtil;
19

20
/**
21
 * A {@link TextFile} backed by a file in some {@link FileSystem}.
22
 */
23
class NioTextFile extends BaseCloseable implements TextFile {
24

25
    private final Path path;
26
    private final Charset charset;
27
    private final LanguageVersion languageVersion;
28
    private final String displayName;
29
    private final String pathId;
30
    private boolean readOnly;
31

32
    NioTextFile(Path path,
33
                Charset charset,
34
                LanguageVersion languageVersion,
35
                String displayName,
36
                boolean readOnly) {
1✔
37
        AssertionUtil.requireParamNotNull("path", path);
1✔
38
        AssertionUtil.requireParamNotNull("charset", charset);
1✔
39
        AssertionUtil.requireParamNotNull("language version", languageVersion);
1✔
40
        AssertionUtil.requireParamNotNull("display name", displayName);
1✔
41

42
        this.displayName = displayName;
1✔
43
        this.readOnly = readOnly;
1✔
44
        this.path = path;
1✔
45
        this.charset = charset;
1✔
46
        this.languageVersion = languageVersion;
1✔
47
        // using the URI here, that handles files inside zip archives automatically (schema "jar:file:...!/path/inside/zip")
48
        // normalization ensures cannonical paths
49
        this.pathId = path.normalize().toUri().toString();
1✔
50
    }
1✔
51

52
    @Override
53
    public @NonNull LanguageVersion getLanguageVersion() {
54
        return languageVersion;
1✔
55
    }
56

57
    @Override
58
    public @NonNull String getDisplayName() {
59
        return displayName;
1✔
60
    }
61

62
    @Override
63
    public String getPathId() {
64
        return pathId;
1✔
65
    }
66

67
    @Override
68
    public boolean isReadOnly() {
69
        return readOnly || !Files.isWritable(path);
1✔
70
    }
71

72
    @Override
73
    public void writeContents(TextFileContent content) throws IOException {
74
        ensureOpen();
1✔
75
        if (isReadOnly()) {
1✔
76
            throw new ReadOnlyFileException(this);
1✔
77
        }
78
        try (BufferedWriter bw = Files.newBufferedWriter(path, charset)) {
1✔
79
            if (TextFileContent.NORMALIZED_LINE_TERM.equals(content.getLineTerminator())) {
1✔
80
                content.getNormalizedText().writeFully(bw);
1✔
81
            } else {
82
                for (Chars line : content.getNormalizedText().lines()) {
1✔
83
                    line.writeFully(bw);
1✔
84
                    bw.write(content.getLineTerminator());
1✔
85
                }
1✔
86
            }
87
        }
88
    }
1✔
89

90
    @Override
91
    public TextFileContent readContents() throws IOException {
92
        ensureOpen();
1✔
93

94
        if (!Files.isRegularFile(path)) {
1✔
95
            throw new IOException("Not a regular file: " + path);
×
96
        }
97

98
        return TextFileContent.fromInputStream(Files.newInputStream(path), charset);
1✔
99
    }
100

101

102
    @Override
103
    protected void doClose() throws IOException {
104
        // nothing to do.
105
    }
1✔
106

107
    @Override
108
    public boolean equals(Object o) {
109
        if (this == o) {
1✔
110
            return true;
×
111
        }
112
        if (o == null || getClass() != o.getClass()) {
1✔
113
            return false;
1✔
114
        }
115
        @SuppressWarnings("PMD.CloseResource")
116
        NioTextFile that = (NioTextFile) o;
1✔
117
        return path.equals(that.path);
1✔
118
    }
119

120
    @Override
121
    public int hashCode() {
122
        return path.hashCode();
1✔
123
    }
124

125
    @Override
126
    public String toString() {
127
        return "NioTextFile[charset=" + charset + ", path=" + path + ']';
1✔
128
    }
129
}
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