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

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

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

92.19
cli/src/main/java/com/devonfw/tools/ide/environment/VariableLine.java
1
package com.devonfw.tools.ide.environment;
2

3
import com.devonfw.tools.ide.log.IdeLogger;
4

5
/**
6
 * Container that represents a line from a properties (ide.properties) file. We do not use {@link java.util.Properties} as we need support for exported
7
 * variables, lists/arrays, and saving changes without loosing comments, etc.
8
 */
9
public abstract class VariableLine {
3✔
10

11
  /**
12
   * @return {@code true} if the variable is exported (e.g. "export MAVEN_OPTS=-Xmx20248m"), {@code false} otherwise.
13
   */
14
  public boolean isExport() {
15

16
    return false;
2✔
17
  }
18

19
  /**
20
   * @return the name of the variable. Will be {@code null} if not a regular variable line (e.g. a comment or empty line).
21
   */
22
  public String getName() {
23

24
    return null;
2✔
25
  }
26

27
  /**
28
   * @return the value of the variable. Will be {@code null} if not a regular variable line (e.g. a comment or empty line).
29
   */
30
  public String getValue() {
31

32
    return null;
2✔
33
  }
34

35
  /**
36
   * @return the comment line (including the '#' character). Will be {@code null} if not a comment line.
37
   */
38
  public String getComment() {
39

40
    return null;
2✔
41
  }
42

43
  /**
44
   * @param newName the new variable {@link #getName() name}.
45
   * @return the new {@link VariableLine} with the modified {@link #getName() name}.
46
   */
47
  public VariableLine withName(String newName) {
48

49
    throw new UnsupportedOperationException();
×
50
  }
51

52
  /**
53
   * @param newValue the new variable {@link #getValue() value}.
54
   * @return the new {@link VariableLine} with the modified {@link #getValue() value}.
55
   */
56
  public VariableLine withValue(String newValue) {
57

58
    throw new UnsupportedOperationException();
×
59
  }
60

61
  /**
62
   * @param newExport the new {@link #isExport() export} flag.
63
   * @return the new {@link VariableLine} with the modified {@link #isExport() export} flag.
64
   */
65
  public VariableLine withExport(boolean newExport) {
66

67
    throw new UnsupportedOperationException();
×
68
  }
69

70
  static final class Variable extends VariableLine {
71

72
    private boolean export;
73

74
    private final String name;
75

76
    private final String value;
77

78
    private final String line;
79

80
    Variable(boolean export, String name, String value) {
81

82
      this(export, name, value, null);
6✔
83
    }
1✔
84

85
    private Variable(boolean export, String name, String value, String line) {
86

87
      super();
2✔
88
      this.export = export;
3✔
89
      this.name = name;
3✔
90
      this.value = value;
3✔
91
      if (line == null) {
2✔
92
        StringBuilder sb = new StringBuilder();
4✔
93
        if (export) {
2✔
94
          sb.append("export ");
4✔
95
        }
96
        sb.append(this.name);
5✔
97
        sb.append('=');
4✔
98
        sb.append(this.value);
5✔
99
        this.line = sb.toString();
4✔
100
      } else {
1✔
101
        this.line = line;
3✔
102
      }
103
    }
1✔
104

105
    @Override
106
    public boolean isExport() {
107

108
      return this.export;
3✔
109
    }
110

111
    @Override
112
    public String getName() {
113

114
      return this.name;
3✔
115
    }
116

117
    @Override
118
    public String getValue() {
119

120
      return this.value;
3✔
121
    }
122

123
    @Override
124
    public VariableLine withName(String newName) {
125

126
      if (newName.equals(this.name)) {
5!
127
        return this;
×
128
      }
129
      return new Variable(this.export, newName, this.value);
9✔
130
    }
131

132
    @Override
133
    public VariableLine withValue(String newValue) {
134

135
      if (newValue.equals(this.value)) {
5✔
136
        return this;
2✔
137
      }
138
      return new Variable(this.export, this.name, newValue);
9✔
139
    }
140

141
    @Override
142
    public VariableLine withExport(boolean newExport) {
143

144
      if (newExport == this.export) {
4!
145
        return this;
×
146
      }
147
      return new Variable(newExport, this.name, this.value);
9✔
148
    }
149

150
    @Override
151
    public String toString() {
152

153
      return this.line;
3✔
154
    }
155

156
  }
157

158
  static final class Comment extends VariableLine {
159

160
    private final String line;
161

162
    private Comment(String line) {
163

164
      super();
2✔
165
      this.line = line;
3✔
166
    }
1✔
167

168
    @Override
169
    public String getComment() {
170

171
      return this.line;
3✔
172
    }
173

174
    @Override
175
    public String toString() {
176

177
      return this.line;
3✔
178
    }
179

180
  }
181

182
  static final class Garbage extends VariableLine {
183

184
    private final String line;
185

186
    Garbage(String line) {
187

188
      super();
2✔
189
      this.line = line;
3✔
190
    }
1✔
191

192
    @Override
193
    public String toString() {
194

195
      return this.line;
3✔
196
    }
197

198
  }
199

200
  static final class Empty extends VariableLine {
201

202
    private final String line;
203

204
    Empty(String line) {
205

206
      super();
2✔
207
      this.line = line;
3✔
208
    }
1✔
209

210
    @Override
211
    public String toString() {
212

213
      return this.line;
3✔
214
    }
215

216
  }
217

218
  /**
219
   * Parses a {@link VariableLine} from {@link String}.
220
   *
221
   * @param line the {@link VariableLine} as {@link String} to parse.
222
   * @param logger the {@link IdeLogger}.
223
   * @param source the source where the given {@link String} to parse is from (e.g. the file path).
224
   * @return the parsed {@link VariableLine}.
225
   */
226
  public static VariableLine of(String line, IdeLogger logger, Object source) {
227

228
    int len = line.length();
3✔
229
    int start = 0;
2✔
230
    while (start < len) {
3✔
231
      char c = line.charAt(start);
4✔
232
      if (c == ' ') {
3✔
233
        start++; // ignore leading spaces
2✔
234
      } else if (c == '#') {
3✔
235
        return new Comment(line);
5✔
236
      } else {
237
        break;
238
      }
239
    }
1✔
240
    if (start >= len) {
3✔
241
      return new Empty(line);
5✔
242
    }
243
    boolean export = false;
2✔
244
    int end = start;
2✔
245
    int space = -1;
2✔
246
    while (end < len) {
3✔
247
      char c = line.charAt(end);
4✔
248
      if (c == ' ') {
3✔
249
        if (space == -1) {
3✔
250
          space = end;
3✔
251
        }
252
      } else if (c == '=') {
3✔
253
        // .0123456789
254
        // "export ...="
255
        String name;
256
        if ((space == start + 6) && (end > start + 7) && line.substring(start, space).equals("export")) {
17!
257
          name = line.substring(space + 1, end).trim();
8✔
258
          if (name.isEmpty()) {
3✔
259
            name = line.substring(start, end).trim();
7✔
260
          } else {
261
            export = true;
3✔
262
          }
263
        } else {
264
          name = line.substring(start, end).trim();
6✔
265
        }
266
        String value = line.substring(end + 1).trim();
7✔
267
        if (value.isEmpty()) {
3✔
268
          value = null;
3✔
269
        } else if (value.startsWith("\"") && value.endsWith("\"")) {
8!
270
          value = value.substring(1, value.length() - 1);
8✔
271
        }
272
        return new Variable(export, name, value, line);
8✔
273
      }
274
      end++;
1✔
275
    }
1✔
276
    logger.warning("Ignoring corrupted line '{}' in {}", line, source);
13✔
277
    return new Garbage(line);
5✔
278
  }
279

280
  /**
281
   * @param export the {@link #isExport() export flag}.
282
   * @param name the {@link #getName() name}.
283
   * @param value the {@link #getValue() value}.
284
   * @return the {@link VariableLine} for the given values.
285
   */
286
  public static VariableLine of(boolean export, String name, String value) {
287

288
    return new Variable(export, name, value);
7✔
289
  }
290

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