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

devonfw / IDEasy / 11060995883

26 Sep 2024 10:09PM UTC coverage: 66.053% (-0.05%) from 66.107%
11060995883

Pull #652

github

web-flow
Merge 3cdc5b3b2 into ad80f56d2
Pull Request #652: #593: #651: #564: #439: fixed bugs, refactored tool dependencies

2312 of 3848 branches covered (60.08%)

Branch coverage included in aggregate %.

6078 of 8854 relevant lines covered (68.65%)

3.03 hits per line

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

83.87
cli/src/main/java/com/devonfw/tools/ide/url/model/UrlMetadata.java
1
package com.devonfw.tools.ide.url.model;
2

3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9

10
import com.devonfw.tools.ide.cli.CliException;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.url.model.folder.UrlEdition;
13
import com.devonfw.tools.ide.url.model.folder.UrlRepository;
14
import com.devonfw.tools.ide.url.model.folder.UrlTool;
15
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
16
import com.devonfw.tools.ide.version.GenericVersionRange;
17
import com.devonfw.tools.ide.version.VersionIdentifier;
18

19
/**
20
 * Service to {@link #getEdition(String, String) load} an {@link UrlEdition} to get access to its versions.
21
 */
22
public class UrlMetadata {
23

24
  private final IdeContext context;
25

26
  private final UrlRepository repository;
27

28
  private final Map<String, List<VersionIdentifier>> toolEdition2VersionMap;
29

30
  /**
31
   * The constructor.
32
   *
33
   * @param context the owning {@link IdeContext}.
34
   */
35
  public UrlMetadata(IdeContext context) {
36

37
    super();
2✔
38
    this.context = context;
3✔
39
    this.repository = new UrlRepository(this.context.getUrlsPath());
8✔
40
    this.toolEdition2VersionMap = new HashMap<>();
5✔
41
  }
1✔
42

43
  /**
44
   * @param tool the name of the {@link UrlTool}.
45
   * @param edition the name of the {@link UrlEdition}.
46
   * @return the {@link UrlEdition}. Will be lazily loaded.
47
   */
48
  public UrlEdition getEdition(String tool, String edition) {
49

50
    UrlTool urlTool = this.repository.getOrCreateChild(tool);
6✔
51
    UrlEdition urlEdition = urlTool.getOrCreateChild(edition);
5✔
52
    return urlEdition;
2✔
53
  }
54

55
  /**
56
   * @param tool the name of the {@link UrlTool}.
57
   * @return the sorted {@link List} of {@link String editions} .
58
   */
59
  public List<String> getSortedEditions(String tool) {
60

61
    List<String> list = new ArrayList<>();
4✔
62
    UrlTool urlTool = this.repository.getChild(tool);
6✔
63
    if (urlTool == null) {
2✔
64
      this.context.warning("Can't get sorted editions for tool {} because it does not exist in {}.", tool, this.repository.getPath());
17✔
65
    } else {
66
      for (UrlEdition urlEdition : urlTool.getChildren()) {
11✔
67
        list.add(urlEdition.getName());
5✔
68
      }
1✔
69
    }
70
    Collections.sort(list);
2✔
71
    return Collections.unmodifiableList(list);
3✔
72
  }
73

74
  /**
75
   * @param tool the name of the {@link UrlTool}.
76
   * @param edition the name of the {@link UrlEdition}.
77
   * @return the {@link List} of {@link VersionIdentifier}s sorted descending so the latest version comes first and the oldest comes last.
78
   */
79
  public List<VersionIdentifier> getSortedVersions(String tool, String edition) {
80

81
    String key = tool + "/" + edition;
4✔
82
    return this.toolEdition2VersionMap.computeIfAbsent(key, k -> computeSortedVersions(tool, edition));
15✔
83
  }
84

85
  private List<VersionIdentifier> computeSortedVersions(String tool, String edition) {
86

87
    List<VersionIdentifier> list = new ArrayList<>();
4✔
88
    UrlEdition urlEdition = getEdition(tool, edition);
5✔
89
    urlEdition.load(false);
3✔
90
    for (UrlVersion urlVersion : urlEdition.getChildren()) {
11✔
91
      VersionIdentifier versionIdentifier = urlVersion.getVersionIdentifier();
3✔
92
      list.add(versionIdentifier);
4✔
93
    }
1✔
94
    Collections.sort(list, Comparator.reverseOrder());
3✔
95
    return Collections.unmodifiableList(list);
3✔
96
  }
97

98
  /**
99
   * @param tool the name of the {@link UrlTool}.
100
   * @param edition the name of the {@link UrlEdition}.
101
   * @param version the {@link GenericVersionRange} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for
102
   *     the latest version.
103
   * @return the latest matching {@link VersionIdentifier} for the given {@code tool} and {@code edition}.
104
   */
105
  public VersionIdentifier getVersion(String tool, String edition, GenericVersionRange version) {
106

107
    if (version == null) {
2!
108
      version = VersionIdentifier.LATEST;
×
109
    }
110
    if (!version.isPattern()) {
3✔
111
      return (VersionIdentifier) version;
3✔
112
    }
113
    List<VersionIdentifier> versions = getSortedVersions(tool, edition);
5✔
114
    for (VersionIdentifier vi : versions) {
10!
115
      if (version.contains(vi)) {
4!
116
        this.context.debug("Resolved version pattern {} to version {}", version, vi);
14✔
117
        return vi;
2✔
118
      }
119
    }
×
120
    throw new CliException(
×
121
        "Could not find any version matching '" + version + "' for tool '" + tool + "' - potentially there are " + versions.size() + " version(s) available in "
×
122
            + getEdition(tool, edition).getPath() + " but none matched!");
×
123
  }
124

125
  /**
126
   * @param tool the name of the {@link UrlTool}.
127
   * @param edition the name of the {@link UrlEdition}.
128
   * @param version the {@link GenericVersionRange} to match. May be a {@link VersionIdentifier#isPattern() pattern}, a specific version or {@code null} for
129
   *     the latest version.
130
   * @return the latest matching {@link UrlVersion} for the given {@code tool} and {@code edition}.
131
   */
132
  public UrlVersion getVersionFolder(String tool, String edition, GenericVersionRange version) {
133

134
    VersionIdentifier resolvedVersion = getVersion(tool, edition, version);
6✔
135
    UrlVersion urlVersion = getEdition(tool, edition).getChild(resolvedVersion.toString());
9✔
136
    if (urlVersion == null) {
2!
137
      throw new CliException("Version " + version + " for tool " + tool + " does not exist in edition " + edition + ".");
×
138
    }
139
    return urlVersion;
2✔
140
  }
141

142
}
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