zafarkhaja / jsemver

Java implementation of the SemVer Specification
MIT License
429 stars 82 forks source link

MetadataVersion is not immutable #52

Closed janjoerke closed 6 months ago

janjoerke commented 5 years ago

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
}
janjoerke commented 5 years ago

The shouldBeImmutable() test method in VersionTest has also some problems connected to assertNotSame.

asarkar commented 3 years ago

I don't think this library is maintained any more. You may want to check out https://github.com/asarkar/jsemver.

zafarkhaja commented 1 year ago

Hello Jan! Sorry for the delay and thank you for your contribution!