Skip to content

Commit 46ad5e6

Browse files
committed
Improve server version handling
Fixed parsing, comparison and added tests.
1 parent cd67c58 commit 46ad5e6

File tree

3 files changed

+221
-73
lines changed

3 files changed

+221
-73
lines changed

src/v1/internal/server-version-util.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/v1/internal/server-version.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright (c) 2002-2017 "Neo Technology,","
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import {assertString} from './util';
21+
22+
const SERVER_VERSION_REGEX = new RegExp('^(Neo4j/)?(\\d+)\\.(\\d+)(?:\\.)?(\\d*)(\\.|-|\\+)?([0-9A-Za-z-.]*)?$');
23+
24+
class ServerVersion {
25+
26+
/**
27+
* @constructor
28+
* @param {number} major the major version number.
29+
* @param {number} minor the minor version number.
30+
* @param {number} patch the patch version number.
31+
*/
32+
constructor(major, minor, patch) {
33+
this.major = major;
34+
this.minor = minor;
35+
this.patch = patch;
36+
}
37+
38+
/**
39+
* Parse given string to a {@link ServerVersion} object.
40+
* @param versionStr the string to parse.
41+
* @return {ServerVersion} version for the given string.
42+
* @throws Error if given string can't be parsed.
43+
*/
44+
static fromString(versionStr) {
45+
if (!versionStr) {
46+
return new ServerVersion(3, 0, 0);
47+
}
48+
49+
assertString(versionStr, 'Neo4j version string');
50+
51+
const version = versionStr.match(SERVER_VERSION_REGEX);
52+
if (!version) {
53+
throw new Error(`Unparsable Neo4j version: ${versionStr}`);
54+
}
55+
56+
const major = parseIntStrict(version[2]);
57+
const minor = parseIntStrict(version[3]);
58+
const patch = parseIntStrict(version[4] || 0);
59+
60+
return new ServerVersion(major, minor, patch);
61+
}
62+
63+
/**
64+
* Compare this version to the given one.
65+
* @param {ServerVersion} other the version to compare with.
66+
* @return {number} value 0 if this version is the same as the given one, value less then 0 when this version
67+
* was released earlier than the given one and value greater then 0 when this version was released after
68+
* than the given one.
69+
*/
70+
compareTo(other) {
71+
let result = compareInts(this.major, other.major);
72+
if (result === 0) {
73+
result = compareInts(this.minor, other.minor);
74+
if (result === 0) {
75+
result = compareInts(this.patch, other.patch);
76+
}
77+
}
78+
return result;
79+
}
80+
}
81+
82+
function parseIntStrict(str, name) {
83+
const value = parseInt(str);
84+
if (!value && value !== 0) {
85+
throw new Error(`Unparsable number ${name}: '${str}'`);
86+
}
87+
return value;
88+
}
89+
90+
function compareInts(x, y) {
91+
return (x < y) ? -1 : ((x === y) ? 0 : 1);
92+
}
93+
94+
const VERSION_3_2_0 = new ServerVersion(3, 2, 0);
95+
96+
export {
97+
ServerVersion,
98+
VERSION_3_2_0
99+
};
100+
101+
102+
103+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Copyright (c) 2002-2017 "Neo Technology,","
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import {ServerVersion, VERSION_3_2_0} from '../../src/v1/internal/server-version';
21+
22+
describe('ServerVersion', () => {
23+
24+
it('should construct with correct values', () => {
25+
verifyVersion(new ServerVersion(2, 3, 10), 2, 3, 10);
26+
verifyVersion(new ServerVersion(3, 2, 0), 3, 2, 0);
27+
verifyVersion(new ServerVersion(1, 9, 12), 1, 9, 12);
28+
});
29+
30+
it('should define correct 3.2.0 constant', () => {
31+
verifyVersion(VERSION_3_2_0, 3, 2, 0);
32+
});
33+
34+
it('should parse "undefined" strings to 3.0.0 for backwards compatibility reasons', () => {
35+
verifyVersion(parse(null), 3, 0, 0);
36+
verifyVersion(parse(undefined), 3, 0, 0);
37+
verifyVersion(parse(''), 3, 0, 0);
38+
});
39+
40+
it('should fail to parse object, array and function', () => {
41+
expect(() => parse({})).toThrowError(TypeError);
42+
expect(() => parse([])).toThrowError(TypeError);
43+
expect(() => parse(() => {
44+
})).toThrowError(TypeError);
45+
});
46+
47+
it('should fail to parse illegal strings', () => {
48+
expect(() => parse('Cat')).toThrow();
49+
expect(() => parse('Dog')).toThrow();
50+
expect(() => parse('Neo4j')).toThrow();
51+
expect(() => parse('Neo4j/')).toThrow();
52+
expect(() => parse('Not-Neo4j/3.1.0')).toThrow();
53+
expect(() => parse('Neo4j/5')).toThrow();
54+
expect(() => parse('Neo4j/3.')).toThrow();
55+
expect(() => parse('Neo4j/1.A')).toThrow();
56+
expect(() => parse('Neo4j/1.Hello')).toThrow();
57+
});
58+
59+
it('should parse valid version strings', () => {
60+
verifyVersion(parse('Neo4j/3.2.1'), 3, 2, 1);
61+
verifyVersion(parse('Neo4j/1.9.10'), 1, 9, 10);
62+
verifyVersion(parse('Neo4j/7.77.777'), 7, 77, 777);
63+
64+
verifyVersion(parse('Neo4j/3.10.0-GA'), 3, 10, 0);
65+
verifyVersion(parse('Neo4j/2.5.5-RC01'), 2, 5, 5);
66+
verifyVersion(parse('Neo4j/3.1.1-SNAPSHOT'), 3, 1, 1);
67+
verifyVersion(parse('Neo4j/2.0.0-M09'), 2, 0, 0);
68+
69+
verifyVersion(parse('Neo4j/3.2'), 3, 2, 0);
70+
verifyVersion(parse('Neo4j/1.5'), 1, 5, 0);
71+
verifyVersion(parse('Neo4j/42.42'), 42, 42, 0);
72+
73+
verifyVersion(parse('Neo4j/2.2-GA'), 2, 2, 0);
74+
verifyVersion(parse('Neo4j/3.0-RC01'), 3, 0, 0);
75+
verifyVersion(parse('Neo4j/2.3-SNAPSHOT'), 2, 3, 0);
76+
verifyVersion(parse('Neo4j/2.2-M09'), 2, 2, 0);
77+
});
78+
79+
it('should compare equal versions', () => {
80+
expect(new ServerVersion(3, 1, 0).compareTo(new ServerVersion(3, 1, 0))).toEqual(0);
81+
expect(new ServerVersion(1, 9, 12).compareTo(new ServerVersion(1, 9, 12))).toEqual(0);
82+
expect(new ServerVersion(2, 3, 8).compareTo(new ServerVersion(2, 3, 8))).toEqual(0);
83+
});
84+
85+
it('should compare correctly by major', () => {
86+
expect(new ServerVersion(3, 1, 0).compareTo(new ServerVersion(2, 1, 0))).toBeGreaterThan(0);
87+
expect(new ServerVersion(2, 1, 0).compareTo(new ServerVersion(3, 1, 0))).toBeLessThan(0);
88+
89+
expect(new ServerVersion(3, 1, 4).compareTo(new ServerVersion(1, 9, 10))).toBeGreaterThan(0);
90+
expect(new ServerVersion(1, 5, 42).compareTo(new ServerVersion(10, 10, 43))).toBeLessThan(0);
91+
});
92+
93+
it('should compare correctly by minor', () => {
94+
expect(new ServerVersion(3, 3, 0).compareTo(new ServerVersion(3, 1, 0))).toBeGreaterThan(0);
95+
expect(new ServerVersion(1, 3, 5).compareTo(new ServerVersion(1, 9, 5))).toBeLessThan(0);
96+
97+
expect(new ServerVersion(3, 9, 5).compareTo(new ServerVersion(3, 1, 2))).toBeGreaterThan(0);
98+
expect(new ServerVersion(1, 5, 42).compareTo(new ServerVersion(1, 10, 11))).toBeLessThan(0);
99+
});
100+
101+
it('should compare correctly by patch', () => {
102+
expect(new ServerVersion(3, 3, 6).compareTo(new ServerVersion(3, 3, 5))).toBeGreaterThan(0);
103+
expect(new ServerVersion(1, 8, 2).compareTo(new ServerVersion(1, 8, 8))).toBeLessThan(0);
104+
expect(new ServerVersion(9, 9, 9).compareTo(new ServerVersion(9, 9, 0))).toBeGreaterThan(0);
105+
expect(new ServerVersion(3, 3, 3).compareTo(new ServerVersion(3, 3, 42))).toBeLessThan(0);
106+
});
107+
108+
});
109+
110+
function verifyVersion(serverVersion, expectedMajor, expectedMinor, expectedPatch) {
111+
expect(serverVersion.major).toEqual(expectedMajor);
112+
expect(serverVersion.minor).toEqual(expectedMinor);
113+
expect(serverVersion.patch).toEqual(expectedPatch);
114+
}
115+
116+
function parse(string) {
117+
return ServerVersion.fromString(string);
118+
}

0 commit comments

Comments
 (0)