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

devonfw / IDEasy / 7280603633

20 Dec 2023 08:45PM UTC coverage: 47.217% (-0.7%) from 47.873%
7280603633

Pull #160

github

web-flow
Merge af9910eb4 into 1d60d9c17
Pull Request #160: #126: Monitoring for ide-urls

1019 of 2393 branches covered (0.0%)

Branch coverage included in aggregate %.

2714 of 5513 relevant lines covered (49.23%)

2.03 hits per line

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

33.7
cli/src/main/java/com/devonfw/tools/ide/url/model/file/UrlDownloadFile.java
1
package com.devonfw.tools.ide.url.model.file;
2

3
import java.io.BufferedReader;
4
import java.io.BufferedWriter;
5
import java.io.IOException;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.nio.file.StandardOpenOption;
9
import java.util.HashSet;
10
import java.util.Set;
11

12
import com.devonfw.tools.ide.os.OperatingSystem;
13
import com.devonfw.tools.ide.os.SystemArchitecture;
14
import com.devonfw.tools.ide.url.model.folder.UrlEdition;
15
import com.devonfw.tools.ide.url.model.folder.UrlTool;
16
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

19
/**
20
 * {@link UrlFile} with the download URLs. Its {@link #getName() name} has to follow one of the following conventions:
21
 * <ul>
22
 * <li>«os»_«arch».urls</li>
23
 * <li>«os».urls</li>
24
 * <li>urls</li>
25
 * </ul>
26
 */
27
public class UrlDownloadFile extends AbstractUrlFile<UrlVersion> implements UrlDownloadFileMetadata {
1✔
28

29
  /** The name {@value} for OS- and architecture-agnostic URLs. */
30
  public static final String NAME_URLS = "urls";
31

32
  /** The extension {@value}. */
33
  public static final String EXTENSION_URLS = ".urls";
34

35
  private final Set<String> urls;
36

37
  /**
38
   * The constructor.
39
   *
40
   * @param parent the {@link #getParent() parent folder}.
41
   * @param name the {@link #getName() filename}.
42
   */
43
  public UrlDownloadFile(UrlVersion parent, String name) {
44

45
    super(parent, name);
4✔
46
    this.urls = new HashSet<>();
5✔
47
  }
1✔
48

49
  /**
50
   * @return the number of #getUrl
51
   */
52
  public int getUrlCount() {
53

54
    return this.urls.size();
×
55
  }
56

57
  /**
58
   * @param url the download URL to add.
59
   */
60
  public void addUrl(String url) {
61

62
    boolean added = this.urls.add(url);
5✔
63
    if (added) {
2!
64
      this.modified = true;
3✔
65
    }
66
  }
1✔
67

68
  /**
69
   * Avoid direct mutation of this {@link Set} and use {@link #addUrl(String)} or {@link #removeUrl(String)} instead.
70
   */
71
  @Override
72
  public Set<String> getUrls() {
73

74
    load(false);
×
75
    return this.urls;
×
76
  }
77

78
  /**
79
   * @param url the download URL to remove.
80
   */
81
  public void removeUrl(String url) {
82

83
    boolean removed = this.urls.remove(url);
×
84
    if (removed) {
×
85
      this.modified = true;
×
86
    }
87
  }
×
88

89
  @Override
90
  protected void doLoad() {
91

92
    this.urls.clear();
3✔
93
    Path path = getPath();
3✔
94
    try (BufferedReader br = Files.newBufferedReader(path)) {
3✔
95
      String line;
96
      do {
97
        line = br.readLine();
3✔
98
        if (line != null) {
2✔
99
          this.urls.add(line.trim());
6✔
100
        }
101
      } while (line != null);
2✔
102
    } catch (IOException e) {
×
103
      throw new IllegalStateException("Failed to load file " + path, e);
×
104
    }
1✔
105
  }
1✔
106

107
  @Override
108
  protected void doSave() {
109

110
    Path path = getPath();
3✔
111
    try (BufferedWriter bw = Files.newBufferedWriter(path, StandardOpenOption.TRUNCATE_EXISTING,
17✔
112
        StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
113
      for (String line : this.urls) {
11✔
114
        bw.write(line + "\n");
4✔
115
      }
1✔
116
    } catch (IOException e) {
×
117
      throw new IllegalStateException("Failed to save file " + path, e);
×
118
    }
1✔
119
  }
1✔
120

121
  @Override
122
  public OperatingSystem getOs() {
123

124
    String name = getName();
×
125
    if (name.equals(NAME_URLS)) {
×
126
      return null;
×
127
    }
128
    for (OperatingSystem os : OperatingSystem.values()) {
×
129
      if (name.startsWith(os.toString())) {
×
130
        return os;
×
131
      }
132
    }
133
    throw new IllegalStateException("Cannot derive operating-system from " + name);
×
134
  }
135

136
  @Override
137
  public SystemArchitecture getArch() {
138

139
    String name = getName();
×
140
    if (name.equals(NAME_URLS)) {
×
141
      return null;
×
142
    }
143
    int underscore = name.indexOf('_');
×
144
    if (underscore < 0) {
×
145
      return null;
×
146
    }
147
    assert (name.endsWith(EXTENSION_URLS));
×
148
    // EXTENSION_URLS.length() = ".urls".length() = 5
149
    String archString = name.substring(underscore + 1, name.length() - 5);
×
150
    for (SystemArchitecture arch : SystemArchitecture.values()) {
×
151
      if (archString.equals(arch.toString())) {
×
152
        return arch;
×
153
      }
154
    }
155
    throw new IllegalStateException("Cannot derive system-architecture from " + name);
×
156
  }
157

158
  @Override
159
  public VersionIdentifier getVersion() {
160

161
    UrlVersion version = getParent();
×
162
    return version.getVersionIdentifier();
×
163
  }
164

165
  @Override
166
  public String getEdition() {
167

168
    UrlEdition edition = getParent().getParent();
×
169
    return edition.getName();
×
170
  }
171

172
  @Override
173
  public String getTool() {
174

175
    UrlTool tool = getParent().getParent().getParent();
×
176
    return tool.getName();
×
177
  }
178

179
  @Override
180
  public String getChecksum() {
181

182
    UrlChecksum checksum = getParent().getChecksum(getName());
×
183
    if (checksum == null) {
×
184
      return null;
×
185
    }
186
    return checksum.getChecksum();
×
187
  }
188
}
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