Skip to content

Commit c45573f

Browse files
ars18wrwiText-CI
authored andcommitted
Change the accessability of some Version methods from private to default. Cover Version with tests.
DEVSIX-3648 Autoported commit. Original commit hash: [c4350c80a]
1 parent ef4c42f commit c45573f

File tree

3 files changed

+195
-40
lines changed

3 files changed

+195
-40
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System;
2+
using iText.Test;
3+
4+
namespace iText.Kernel {
5+
public class VersionTest : ExtendedITextTest {
6+
[NUnit.Framework.Test]
7+
public virtual void ParseCurrentVersionTest() {
8+
iText.Kernel.Version instance = iText.Kernel.Version.GetInstance();
9+
// expected values
10+
String release = instance.GetRelease();
11+
String major = "7";
12+
String minor = iText.IO.Util.StringUtil.Split(release, "\\.")[1];
13+
String[] parseResults = iText.Kernel.Version.ParseVersionString(release);
14+
NUnit.Framework.Assert.AreEqual(2, parseResults.Length);
15+
NUnit.Framework.Assert.AreEqual(major, parseResults[0]);
16+
NUnit.Framework.Assert.AreEqual(minor, parseResults[1]);
17+
}
18+
19+
[NUnit.Framework.Test]
20+
public virtual void ParseCustomCorrectVersionTest() {
21+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "7.5.1-SNAPSHOT",
22+
"iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV (AGPL-version)", null), false);
23+
// expected values
24+
String major = "7";
25+
String minor = "5";
26+
String[] parseResults = iText.Kernel.Version.ParseVersionString(customVersion.GetRelease());
27+
NUnit.Framework.Assert.AreEqual(2, parseResults.Length);
28+
NUnit.Framework.Assert.AreEqual(major, parseResults[0]);
29+
NUnit.Framework.Assert.AreEqual(minor, parseResults[1]);
30+
}
31+
32+
[NUnit.Framework.Test]
33+
public virtual void ParseVersionIncorrectMajorTest() {
34+
NUnit.Framework.Assert.That(() => {
35+
// the line below is expected to produce an exception
36+
String[] parseResults = iText.Kernel.Version.ParseVersionString("a.9.11");
37+
}
38+
, NUnit.Framework.Throws.InstanceOf<LicenseVersionException>().With.Message.EqualTo(LicenseVersionException.MAJOR_VERSION_IS_NOT_NUMERIC))
39+
;
40+
}
41+
42+
[NUnit.Framework.Test]
43+
public virtual void ParseVersionIncorrectMinorTest() {
44+
NUnit.Framework.Assert.That(() => {
45+
// the line below is expected to produce an exception
46+
iText.Kernel.Version.ParseVersionString("1.a.11");
47+
}
48+
, NUnit.Framework.Throws.InstanceOf<LicenseVersionException>().With.Message.EqualTo(LicenseVersionException.MINOR_VERSION_IS_NOT_NUMERIC))
49+
;
50+
}
51+
52+
[NUnit.Framework.Test]
53+
public virtual void IsVersionNumericPositiveIntegerTest() {
54+
NUnit.Framework.Assert.IsTrue(iText.Kernel.Version.IsVersionNumeric("7"));
55+
}
56+
57+
[NUnit.Framework.Test]
58+
public virtual void IsVersionNumericNegativeIntegerTest() {
59+
NUnit.Framework.Assert.IsFalse(iText.Kernel.Version.IsVersionNumeric("-7"));
60+
}
61+
62+
[NUnit.Framework.Test]
63+
public virtual void IsVersionNumericPositiveFloatTest() {
64+
NUnit.Framework.Assert.IsFalse(iText.Kernel.Version.IsVersionNumeric("5.973"));
65+
}
66+
67+
[NUnit.Framework.Test]
68+
public virtual void IsVersionNumericNegativeFloatTest() {
69+
NUnit.Framework.Assert.IsFalse(iText.Kernel.Version.IsVersionNumeric("-5.973"));
70+
}
71+
72+
[NUnit.Framework.Test]
73+
public virtual void IsVersionNumericLetterTest() {
74+
NUnit.Framework.Assert.IsFalse(iText.Kernel.Version.IsVersionNumeric("a"));
75+
}
76+
77+
[NUnit.Framework.Test]
78+
public virtual void IsAGPLVersionTest() {
79+
NUnit.Framework.Assert.IsTrue(iText.Kernel.Version.IsAGPLVersion());
80+
}
81+
82+
[NUnit.Framework.Test]
83+
public virtual void IsAGPLTrueTest() {
84+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "7.5.1-SNAPSHOT",
85+
"iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV (AGPL-version)", null), false);
86+
NUnit.Framework.Assert.IsTrue(customVersion.IsAGPL());
87+
}
88+
89+
[NUnit.Framework.Test]
90+
public virtual void IsAGPLFalseTest() {
91+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "7.5.1-SNAPSHOT",
92+
"iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV", null), false);
93+
NUnit.Framework.Assert.IsFalse(customVersion.IsAGPL());
94+
}
95+
96+
[NUnit.Framework.Test]
97+
public virtual void IsExpiredTest() {
98+
NUnit.Framework.Assert.IsFalse(iText.Kernel.Version.IsExpired());
99+
}
100+
101+
[NUnit.Framework.Test]
102+
public virtual void GetInstanceTest() {
103+
iText.Kernel.Version instance = iText.Kernel.Version.GetInstance();
104+
CheckVersionInstance(instance);
105+
}
106+
107+
[NUnit.Framework.Test]
108+
public virtual void CustomVersionCorrectTest() {
109+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "7.5.1-SNAPSHOT",
110+
"iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV", null), false);
111+
CheckVersionInstance(customVersion);
112+
}
113+
114+
[NUnit.Framework.Test]
115+
public virtual void CustomVersionIncorrectMajorTest() {
116+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "8.5.1-SNAPSHOT",
117+
"iText® 8.5.1-SNAPSHOT ©2000-2090 iText Group NV", null), false);
118+
NUnit.Framework.Assert.IsFalse(CheckVersion(customVersion.GetVersion()));
119+
}
120+
121+
[NUnit.Framework.Test]
122+
public virtual void CustomVersionIncorrectMinorTest() {
123+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "7.a.1-SNAPSHOT",
124+
"iText® 7.a.1-SNAPSHOT ©2000-2090 iText Group NV", null), false);
125+
NUnit.Framework.Assert.IsFalse(CheckVersion(customVersion.GetVersion()));
126+
}
127+
128+
[NUnit.Framework.Test]
129+
public virtual void CustomVersionIncorrectPatchTest() {
130+
iText.Kernel.Version customVersion = new iText.Kernel.Version(new VersionInfo("iText®", "7.50.a-SNAPSHOT",
131+
"iText® 7.50.a-SNAPSHOT ©2000-2090 iText Group NV", null), false);
132+
NUnit.Framework.Assert.IsFalse(CheckVersion(customVersion.GetVersion()));
133+
}
134+
135+
private static void CheckVersionInstance(iText.Kernel.Version instance) {
136+
String product = instance.GetProduct();
137+
String release = instance.GetRelease();
138+
String version = instance.GetVersion();
139+
String key = instance.GetKey();
140+
VersionInfo info = instance.GetInfo();
141+
NUnit.Framework.Assert.AreEqual(product, info.GetProduct());
142+
NUnit.Framework.Assert.AreEqual("iText®", product);
143+
NUnit.Framework.Assert.AreEqual(release, info.GetRelease());
144+
NUnit.Framework.Assert.IsTrue(release.Matches("[7]\\.[0-9]+\\.[0-9]+(-SNAPSHOT)?$"));
145+
NUnit.Framework.Assert.AreEqual(version, info.GetVersion());
146+
NUnit.Framework.Assert.IsTrue(CheckVersion(version));
147+
NUnit.Framework.Assert.IsNull(key);
148+
}
149+
150+
private static bool CheckVersion(String version) {
151+
String regexp = "iText\\u00ae [7]\\.[0-9]+\\.[0-9]+(-SNAPSHOT)? \\u00a92000-20([2-9][0-9]) " + "iText Group NV( \\(AGPL-version\\))?";
152+
return version.Matches(regexp);
153+
}
154+
}
155+
}

itext/itext.kernel/itext/kernel/Version.cs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public Version() {
9999
this.info = new VersionInfo(iTextProductName, release, producerLine, null);
100100
}
101101

102-
private Version(VersionInfo info, bool expired) {
102+
internal Version(VersionInfo info, bool expired) {
103103
this.info = info;
104104
this.expired = expired;
105105
}
@@ -260,9 +260,46 @@ public VersionInfo GetInfo() {
260260
return info;
261261
}
262262

263+
internal static String[] ParseVersionString(String version) {
264+
String splitRegex = "\\.";
265+
String[] split = iText.IO.Util.StringUtil.Split(version, splitRegex);
266+
//Guard for empty versions and throw exceptions
267+
if (split.Length == 0) {
268+
throw new LicenseVersionException(LicenseVersionException.VERSION_STRING_IS_EMPTY_AND_CANNOT_BE_PARSED);
269+
}
270+
//Desired Format: X.Y.Z-....
271+
//Also catch X, X.Y-...
272+
String major = split[0];
273+
//If no minor version is present, default to 0
274+
String minor = "0";
275+
if (split.Length > 1) {
276+
minor = split[1].Substring(0);
277+
}
278+
//Check if both values are numbers
279+
if (!IsVersionNumeric(major)) {
280+
throw new LicenseVersionException(LicenseVersionException.MAJOR_VERSION_IS_NOT_NUMERIC);
281+
}
282+
if (!IsVersionNumeric(minor)) {
283+
throw new LicenseVersionException(LicenseVersionException.MINOR_VERSION_IS_NOT_NUMERIC);
284+
}
285+
return new String[] { major, minor };
286+
}
287+
288+
internal static bool IsVersionNumeric(String version) {
289+
//I did not want to introduce an extra dependency on apache.commons in order to use StringUtils.
290+
//This small method is not the most optimal, but it should do for release
291+
try {
292+
Double.Parse(version, System.Globalization.CultureInfo.InvariantCulture);
293+
return true;
294+
}
295+
catch (FormatException) {
296+
return false;
297+
}
298+
}
299+
263300
/// <summary>Checks if the current object has been initialized with AGPL license.</summary>
264301
/// <returns>returns true if the current object has been initialized with AGPL license.</returns>
265-
private bool IsAGPL() {
302+
internal bool IsAGPL() {
266303
return GetVersion().IndexOf(AGPL, StringComparison.Ordinal) > 0;
267304
}
268305

@@ -315,31 +352,6 @@ private static void CheckLicenseVersion(String coreVersionString, String license
315352
}
316353
}
317354

318-
private static String[] ParseVersionString(String version) {
319-
String splitRegex = "\\.";
320-
String[] split = iText.IO.Util.StringUtil.Split(version, splitRegex);
321-
//Guard for empty versions and throw exceptions
322-
if (split.Length == 0) {
323-
throw new LicenseVersionException(LicenseVersionException.VERSION_STRING_IS_EMPTY_AND_CANNOT_BE_PARSED);
324-
}
325-
//Desired Format: X.Y.Z-....
326-
//Also catch X, X.Y-...
327-
String major = split[0];
328-
//If no minor version is present, default to 0
329-
String minor = "0";
330-
if (split.Length > 1) {
331-
minor = split[1].Substring(0);
332-
}
333-
//Check if both values are numbers
334-
if (!IsVersionNumeric(major)) {
335-
throw new LicenseVersionException(LicenseVersionException.MAJOR_VERSION_IS_NOT_NUMERIC);
336-
}
337-
if (!IsVersionNumeric(minor)) {
338-
throw new LicenseVersionException(LicenseVersionException.MINOR_VERSION_IS_NOT_NUMERIC);
339-
}
340-
return new String[] { major, minor };
341-
}
342-
343355
private static String[] GetLicenseeInfoFromLicenseKey(String validatorKey) {
344356
String licenseeInfoMethodName = "GetLicenseeInfoForVersion";
345357
Type klass = GetLicenseKeyClass();
@@ -366,18 +378,6 @@ private static bool IsiText5licenseLoaded() {
366378
return result;
367379
}
368380

369-
private static bool IsVersionNumeric(String version) {
370-
//I did not want to introduce an extra dependency on apache.commons in order to use StringUtils.
371-
//This small method is not the most optimal, but it should do for release
372-
try {
373-
Double.Parse(version, System.Globalization.CultureInfo.InvariantCulture);
374-
return true;
375-
}
376-
catch (FormatException) {
377-
return false;
378-
}
379-
}
380-
381381
private static iText.Kernel.Version AtomicSetVersion(iText.Kernel.Version newVersion) {
382382
lock (staticLock) {
383383
version = newVersion;

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e2d13814ae06493ffa297b910060b2401eb259aa
1+
c4350c80a574cb144e5687475ccf6aaa90af4b1a

0 commit comments

Comments
 (0)