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

devonfw / IDEasy / 12750410851

13 Jan 2025 03:23PM UTC coverage: 68.077% (+0.5%) from 67.541%
12750410851

Pull #820

github

web-flow
Merge b7b0d1004 into 8e971e1a8
Pull Request #820: #759: upgrade settings commandlet

2689 of 4311 branches covered (62.38%)

Branch coverage included in aggregate %.

6946 of 9842 relevant lines covered (70.58%)

3.1 hits per line

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

41.28
cli/src/main/java/com/devonfw/tools/ide/merge/PropertiesMerger.java
1
package com.devonfw.tools.ide.merge;
2

3
import java.io.IOException;
4
import java.io.Reader;
5
import java.io.Writer;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.util.Properties;
9
import java.util.Set;
10

11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.environment.EnvironmentVariables;
13
import com.devonfw.tools.ide.environment.SortedProperties;
14

15
/**
16
 * Implementation of {@link FileMerger} for {@link Properties} files.
17
 */
18
public class PropertiesMerger extends FileMerger {
19

20
  /**
21
   * The constructor.
22
   *
23
   * @param context the {@link #context}.
24
   */
25
  public PropertiesMerger(IdeContext context) {
26

27
    super(context);
3✔
28
  }
1✔
29

30
  @Override
31
  protected void doMerge(Path setup, Path update, EnvironmentVariables resolver, Path workspace) {
32

33
    SortedProperties properties = new SortedProperties();
4✔
34
    boolean updateFileExists = Files.exists(update);
5✔
35
    Path template = setup;
2✔
36
    if (Files.exists(workspace)) {
5✔
37
      if (!updateFileExists) {
2!
38
        this.context.trace("Nothing to do as update file does not exist: {}", update);
×
39
        return; // nothing to do ...
×
40
      }
41
      load(properties, workspace);
5✔
42
    } else if (Files.exists(setup)) {
5✔
43
      load(properties, setup);
4✔
44
    }
45
    if (updateFileExists) {
2!
46
      load(properties, update);
4✔
47
      template = update;
2✔
48
    }
49
    resolve(properties, resolver, template.toString());
6✔
50
    save(properties, workspace);
4✔
51
    this.context.trace("Saved merged properties to: {}", workspace);
10✔
52
  }
1✔
53

54
  /**
55
   * @param file the {@link Path} to load.
56
   * @return the loaded {@link Properties}.
57
   */
58
  public Properties load(Path file) {
59

60
    Properties properties = new Properties();
4✔
61
    load(properties, file);
4✔
62
    return properties;
2✔
63
  }
64

65
  /**
66
   * @param file the {@link Path} to load.
67
   * @return the loaded {@link Properties}.
68
   */
69
  public Properties loadIfExists(Path file) {
70

71
    Properties properties = new Properties();
×
72
    if (file != null) {
×
73
      if (Files.exists(file)) {
×
74
        load(properties, file);
×
75
      } else {
76
        this.context.trace("Properties file does not exist: {}", file);
×
77
      }
78
    }
79
    return properties;
×
80
  }
81

82
  /**
83
   * @param properties the existing {@link Properties} instance.
84
   * @param file the properties {@link Path} to load.
85
   */
86
  public void load(Properties properties, Path file) {
87

88
    this.context.trace("Loading properties file: {}", file);
10✔
89
    try (Reader reader = Files.newBufferedReader(file)) {
3✔
90
      properties.load(reader);
3✔
91
    } catch (IOException e) {
×
92
      throw new IllegalStateException("Could not load properties from file: " + file, e);
×
93
    }
1✔
94
  }
1✔
95

96
  private void resolve(Properties properties, EnvironmentVariables variables, Object src) {
97

98
    Set<Object> keys = properties.keySet();
3✔
99
    for (Object key : keys) {
9✔
100
      String value = properties.getProperty(key.toString());
5✔
101
      properties.setProperty(key.toString(), variables.resolve(value, src, this.legacySupport));
11✔
102
    }
1✔
103
  }
1✔
104

105
  /**
106
   * @param properties the {@link Properties} to save.
107
   * @param file the {@link Path} to save to.
108
   */
109
  public void save(Properties properties, Path file) {
110

111
    this.context.trace("Saving properties file: {}", file);
10✔
112
    ensureParentDirectoryExists(file);
2✔
113
    try (Writer writer = Files.newBufferedWriter(file)) {
5✔
114
      properties.store(writer, null);
4✔
115
    } catch (IOException e) {
×
116
      throw new IllegalStateException("Could not write properties to file: " + file, e);
×
117
    }
1✔
118
  }
1✔
119

120
  @Override
121
  public void inverseMerge(Path workspace, EnvironmentVariables variables, boolean addNewProperties, Path update) {
122

123
    if (!Files.exists(workspace)) {
×
124
      this.context.trace("Workspace file does not exist: {}", workspace);
×
125
      return;
×
126
    }
127
    if (!Files.exists(update)) {
×
128
      this.context.trace("Update file does not exist: {}", update);
×
129
      return;
×
130
    }
131
    Object src = workspace.getFileName();
×
132
    Properties updateProperties = load(update);
×
133
    Properties workspaceProperties = load(workspace);
×
134
    SortedProperties mergedProperties = new SortedProperties();
×
135
    mergedProperties.putAll(updateProperties);
×
136
    boolean updated = false;
×
137
    for (Object key : workspaceProperties.keySet()) {
×
138
      Object workspaceValue = workspaceProperties.get(key);
×
139
      Object updateValue = updateProperties.get(key);
×
140
      if ((updateValue != null) || addNewProperties) {
×
141
        String updateValueResolved = null;
×
142
        if (updateValue != null) {
×
143
          updateValueResolved = variables.resolve(updateValue.toString(), src, this.legacySupport);
×
144
        }
145
        if (!workspaceValue.equals(updateValueResolved)) {
×
146
          String workspaceValueInverseResolved = variables.inverseResolve(workspaceValue.toString(), src);
×
147
          mergedProperties.put(key, workspaceValueInverseResolved);
×
148
          updated = true;
×
149
        }
150
      }
151
    }
×
152
    if (updated) {
×
153
      save(mergedProperties, update);
×
154
      this.context.debug("Saved changes from: {} to: {}", workspace.getFileName(), update);
×
155
    } else {
156
      this.context.trace("No changes for: {}", update);
×
157
    }
158
  }
×
159

160
  @Override
161
  protected boolean doUpgrade(Path workspaceFile) throws Exception {
162

163
    return doUpgradeTextContent(workspaceFile);
×
164
  }
165
}
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

© 2026 Coveralls, Inc