jsemver
jsemver copied to clipboard
MetadataVersion is not immutable
When executing the increment method of MetadataVersion, the original and the returned MetadataVersion share the same idents array, as no new array is created for the new MetadataVersion object.
MetadataVersion increment() {
String[] ids = idents;
String lastId = ids[ids.length - 1];
if (isInt(lastId)) {
int intId = Integer.parseInt(lastId);
ids[ids.length - 1] = String.valueOf(++intId); // modifies original
} else {
ids = Arrays.copyOf(ids, ids.length + 1);
ids[ids.length - 1] = String.valueOf(1);
}
return new MetadataVersion(ids);
}
The test case does not reveal this error, because it only verifies that the original MetadataVersion and the returned MetadataVersion do not reference the same object.
@Test
public void shouldBeImmutable() {
MetadataVersion v1 = new MetadataVersion(
new String[] {"alpha", "1"}
);
MetadataVersion v2 = v1.increment();
assertNotSame(v1, v2); // only checks objects references
}
The shouldBeImmutable() test method in VersionTest has also some problems connected to assertNotSame.
I don't think this library is maintained any more. You may want to check out https://github.com/asarkar/jsemver.
Hello Jan! Sorry for the delay and thank you for your contribution!