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

devonfw / IDEasy / 10214921211

02 Aug 2024 11:35AM UTC coverage: 62.685% (+0.02%) from 62.663%
10214921211

push

github

web-flow
#430: Add source of env variable for debugging (#482)

2075 of 3645 branches covered (56.93%)

Branch coverage included in aggregate %.

5498 of 8436 relevant lines covered (65.17%)

2.88 hits per line

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

91.67
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
   * @return the source of the variable.
45
   */
46
  public String getSource() {
47

48
    return null;
×
49
  }
50

51
  /**
52
   * @param newName the new variable {@link #getName() name}.
53
   * @return the new {@link VariableLine} with the modified {@link #getName() name}.
54
   */
55
  public VariableLine withName(String newName) {
56

57
    throw new UnsupportedOperationException();
×
58
  }
59

60
  /**
61
   * @param newValue the new variable {@link #getValue() value}.
62
   * @return the new {@link VariableLine} with the modified {@link #getValue() value}.
63
   */
64
  public VariableLine withValue(String newValue) {
65

66
    throw new UnsupportedOperationException();
×
67
  }
68

69
  /**
70
   * @param newExport the new {@link #isExport() export} flag.
71
   * @return the new {@link VariableLine} with the modified {@link #isExport() export} flag.
72
   */
73
  public VariableLine withExport(boolean newExport) {
74

75
    throw new UnsupportedOperationException();
×
76
  }
77

78
  static final class Variable extends VariableLine {
79

80
    private boolean export;
81

82
    private final String name;
83

84
    private final String value;
85

86
    private final String line;
87

88
    private final Object source;
89

90
    Variable(boolean export, String name, String value) {
91

92
      this(export, name, value, null, null);
7✔
93
    }
1✔
94

95
    private Variable(boolean export, String name, String value, String line, Object source) {
96

97
      super();
2✔
98
      this.export = export;
3✔
99
      this.name = name;
3✔
100
      this.value = value;
3✔
101
      if (line == null) {
2✔
102
        StringBuilder sb = new StringBuilder();
4✔
103
        if (export) {
2✔
104
          sb.append("export ");
4✔
105
        }
106
        sb.append(this.name);
5✔
107
        sb.append('=');
4✔
108
        sb.append(this.value);
5✔
109
        this.line = sb.toString();
4✔
110
      } else {
1✔
111
        this.line = line;
3✔
112
      }
113
      this.source = source;
3✔
114
    }
1✔
115

116
    @Override
117
    public boolean isExport() {
118

119
      return this.export;
3✔
120
    }
121

122
    @Override
123
    public String getName() {
124

125
      return this.name;
3✔
126
    }
127

128
    @Override
129
    public String getValue() {
130

131
      return this.value;
3✔
132
    }
133

134
    @Override
135
    public String getSource() {
136
      return source.toString();
4✔
137
    }
138

139
    @Override
140
    public VariableLine withName(String newName) {
141

142
      if (newName.equals(this.name)) {
5!
143
        return this;
×
144
      }
145
      return new Variable(this.export, newName, this.value);
9✔
146
    }
147

148
    @Override
149
    public VariableLine withValue(String newValue) {
150

151
      if (newValue.equals(this.value)) {
5✔
152
        return this;
2✔
153
      }
154
      return new Variable(this.export, this.name, newValue);
9✔
155
    }
156

157
    @Override
158
    public VariableLine withExport(boolean newExport) {
159

160
      if (newExport == this.export) {
4!
161
        return this;
×
162
      }
163
      return new Variable(newExport, this.name, this.value);
9✔
164
    }
165

166
    @Override
167
    public String toString() {
168

169
      return this.line;
3✔
170
    }
171

172
  }
173

174
  static final class Comment extends VariableLine {
175

176
    private final String line;
177

178
    private Comment(String line) {
179

180
      super();
2✔
181
      this.line = line;
3✔
182
    }
1✔
183

184
    @Override
185
    public String getComment() {
186

187
      return this.line;
3✔
188
    }
189

190
    @Override
191
    public String toString() {
192

193
      return this.line;
3✔
194
    }
195

196
  }
197

198
  static final class Garbage extends VariableLine {
199

200
    private final String line;
201

202
    Garbage(String line) {
203

204
      super();
2✔
205
      this.line = line;
3✔
206
    }
1✔
207

208
    @Override
209
    public String toString() {
210

211
      return this.line;
3✔
212
    }
213

214
  }
215

216
  static final class Empty extends VariableLine {
217

218
    private final String line;
219

220
    Empty(String line) {
221

222
      super();
2✔
223
      this.line = line;
3✔
224
    }
1✔
225

226
    @Override
227
    public String toString() {
228

229
      return this.line;
3✔
230
    }
231

232
  }
233

234
  /**
235
   * Parses a {@link VariableLine} from {@link String}.
236
   *
237
   * @param line the {@link VariableLine} as {@link String} to parse.
238
   * @param logger the {@link IdeLogger}.
239
   * @param source the source where the given {@link String} to parse is from (e.g. the file path).
240
   * @return the parsed {@link VariableLine}.
241
   */
242
  public static VariableLine of(String line, IdeLogger logger, Object source) {
243

244
    int len = line.length();
3✔
245
    int start = 0;
2✔
246
    while (start < len) {
3✔
247
      char c = line.charAt(start);
4✔
248
      if (c == ' ') {
3✔
249
        start++; // ignore leading spaces
2✔
250
      } else if (c == '#') {
3✔
251
        return new Comment(line);
5✔
252
      } else {
253
        break;
254
      }
255
    }
1✔
256
    if (start >= len) {
3✔
257
      return new Empty(line);
5✔
258
    }
259
    boolean export = false;
2✔
260
    int end = start;
2✔
261
    int space = -1;
2✔
262
    while (end < len) {
3✔
263
      char c = line.charAt(end);
4✔
264
      if (c == ' ') {
3✔
265
        if (space == -1) {
3✔
266
          space = end;
3✔
267
        }
268
      } else if (c == '=') {
3✔
269
        // .0123456789
270
        // "export ...="
271
        String name;
272
        if ((space == start + 6) && (end > start + 7) && line.substring(start, space).equals("export")) {
17!
273
          name = line.substring(space + 1, end).trim();
8✔
274
          if (name.isEmpty()) {
3✔
275
            name = line.substring(start, end).trim();
7✔
276
          } else {
277
            export = true;
3✔
278
          }
279
        } else {
280
          name = line.substring(start, end).trim();
6✔
281
        }
282
        String value = line.substring(end + 1).trim();
7✔
283
        if (value.isEmpty()) {
3✔
284
          value = null;
3✔
285
        } else if (value.startsWith("\"") && value.endsWith("\"")) {
8!
286
          value = value.substring(1, value.length() - 1);
8✔
287
        }
288
        return new Variable(export, name, value, line, source);
9✔
289
      }
290
      end++;
1✔
291
    }
1✔
292
    logger.warning("Ignoring corrupted line '{}' in {}", line, source);
13✔
293
    return new Garbage(line);
5✔
294
  }
295

296
  /**
297
   * @param export the {@link #isExport() export flag}.
298
   * @param name the {@link #getName() name}.
299
   * @param value the {@link #getValue() value}.
300
   * @return the {@link VariableLine} for the given values.
301
   */
302
  public static VariableLine of(boolean export, String name, String value) {
303

304
    return new Variable(export, name, value);
7✔
305
  }
306

307
  /**
308
   * @param export the {@link #isExport() export flag}.
309
   * @param name the {@link #getName() name}.
310
   * @param value the {@link #getValue() value}.
311
   * @param source the {@link #getSource() source} of the variable.
312
   * @return the {@link VariableLine} for the given values.
313
   */
314
  public static VariableLine of(boolean export, String name, String value, Object source) {
315

316
    return new Variable(export, name, value, null, source);
9✔
317
  }
318

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