Skip to content

Commit c4350c8

Browse files
committed
Change the accessability of some Version methods from private to default. Cover Version with tests.
DEVSIX-3648
1 parent e2d1381 commit c4350c8

File tree

2 files changed

+220
-34
lines changed

2 files changed

+220
-34
lines changed

kernel/src/main/java/com/itextpdf/kernel/Version.java

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Version() {
102102
this.info = new VersionInfo(iTextProductName, release, producerLine, null);
103103
}
104104

105-
private Version(VersionInfo info, boolean expired) {
105+
Version(VersionInfo info, boolean expired) {
106106
this.info = info;
107107
this.expired = expired;
108108
}
@@ -255,11 +255,48 @@ public VersionInfo getInfo() {
255255
return info;
256256
}
257257

258+
static String[] parseVersionString(String version) {
259+
String splitRegex = "\\.";
260+
String[] split = version.split(splitRegex);
261+
//Guard for empty versions and throw exceptions
262+
if (split.length == 0) {
263+
throw new LicenseVersionException(LicenseVersionException.VERSION_STRING_IS_EMPTY_AND_CANNOT_BE_PARSED);
264+
}
265+
//Desired Format: X.Y.Z-....
266+
//Also catch X, X.Y-...
267+
String major = split[0];
268+
//If no minor version is present, default to 0
269+
String minor = "0";
270+
if (split.length > 1) {
271+
minor = split[1].substring(0);
272+
}
273+
//Check if both values are numbers
274+
if (!isVersionNumeric(major)) {
275+
throw new LicenseVersionException(LicenseVersionException.MAJOR_VERSION_IS_NOT_NUMERIC);
276+
}
277+
if (!isVersionNumeric(minor)) {
278+
throw new LicenseVersionException(LicenseVersionException.MINOR_VERSION_IS_NOT_NUMERIC);
279+
}
280+
return new String[] {major, minor};
281+
}
282+
283+
static boolean isVersionNumeric(String version) {
284+
//I did not want to introduce an extra dependency on apache.commons in order to use StringUtils.
285+
//This small method is not the most optimal, but it should do for release
286+
try {
287+
Double.parseDouble(version);
288+
return true;
289+
} catch (NumberFormatException e) {
290+
return false;
291+
}
292+
}
293+
258294
/**
259295
* Checks if the current object has been initialized with AGPL license.
296+
*
260297
* @return returns true if the current object has been initialized with AGPL license.
261298
*/
262-
private boolean isAGPL() {
299+
boolean isAGPL() {
263300
return getVersion().indexOf(AGPL) > 0;
264301
}
265302

@@ -318,27 +355,6 @@ private static void checkLicenseVersion(String coreVersionString, String license
318355

319356
}
320357

321-
private static String[] parseVersionString(String version){
322-
String splitRegex = "\\.";
323-
String[] split = version.split(splitRegex);
324-
//Guard for empty versions and throw exceptions
325-
if(split.length == 0){
326-
throw new LicenseVersionException(LicenseVersionException.VERSION_STRING_IS_EMPTY_AND_CANNOT_BE_PARSED);
327-
}
328-
//Desired Format: X.Y.Z-....
329-
//Also catch X, X.Y-...
330-
String major = split[0];
331-
//If no minor version is present, default to 0
332-
String minor ="0";
333-
if(split.length > 1) {
334-
minor = split[1].substring(0);
335-
}
336-
//Check if both values are numbers
337-
if(!isVersionNumeric(major)) throw new LicenseVersionException(LicenseVersionException.MAJOR_VERSION_IS_NOT_NUMERIC);
338-
if(!isVersionNumeric(minor)) throw new LicenseVersionException(LicenseVersionException.MINOR_VERSION_IS_NOT_NUMERIC);
339-
return new String[]{major,minor};
340-
}
341-
342358
private static String[] getLicenseeInfoFromLicenseKey(String validatorKey) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
343359
String licenseeInfoMethodName = "getLicenseeInfoForVersion";
344360
Class<?> klass = getLicenseKeyClass();
@@ -364,17 +380,6 @@ private static boolean isiText5licenseLoaded(){
364380
return result;
365381
}
366382

367-
private static boolean isVersionNumeric(String version){
368-
//I did not want to introduce an extra dependency on apache.commons in order to use StringUtils.
369-
//This small method is not the most optimal, but it should do for release
370-
try{
371-
Double.parseDouble(version);
372-
return true;
373-
}catch(NumberFormatException e){
374-
return false;
375-
}
376-
}
377-
378383
private static Version atomicSetVersion(Version newVersion) {
379384
synchronized (staticLock) {
380385
version = newVersion;
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package com.itextpdf.kernel;
2+
3+
import com.itextpdf.test.ExtendedITextTest;
4+
import com.itextpdf.test.annotations.type.UnitTest;
5+
6+
import org.junit.Assert;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.experimental.categories.Category;
10+
import org.junit.rules.ExpectedException;
11+
12+
@Category(UnitTest.class)
13+
public class VersionTest extends ExtendedITextTest {
14+
15+
@Rule
16+
public ExpectedException junitExpectedException = ExpectedException.none();
17+
18+
@Test
19+
public void parseCurrentVersionTest() {
20+
Version instance = Version.getInstance();
21+
22+
// expected values
23+
String release = instance.getRelease();
24+
String major = "7";
25+
String minor = release.split("\\.")[1];
26+
27+
String[] parseResults = Version.parseVersionString(release);
28+
29+
Assert.assertEquals(2, parseResults.length);
30+
Assert.assertEquals(major, parseResults[0]);
31+
Assert.assertEquals(minor, parseResults[1]);
32+
}
33+
34+
@Test
35+
public void parseCustomCorrectVersionTest() {
36+
Version customVersion = new Version(new VersionInfo("iText®", "7.5.1-SNAPSHOT",
37+
"iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV (AGPL-version)", null), false);
38+
39+
// expected values
40+
String major = "7";
41+
String minor = "5";
42+
43+
String[] parseResults = Version.parseVersionString(customVersion.getRelease());
44+
45+
Assert.assertEquals(2, parseResults.length);
46+
Assert.assertEquals(major, parseResults[0]);
47+
Assert.assertEquals(minor, parseResults[1]);
48+
}
49+
50+
@Test
51+
public void parseVersionIncorrectMajorTest() {
52+
junitExpectedException.expect(LicenseVersionException.class);
53+
junitExpectedException.expectMessage(LicenseVersionException.MAJOR_VERSION_IS_NOT_NUMERIC);
54+
55+
// the line below is expected to produce an exception
56+
String[] parseResults = Version.parseVersionString("a.9.11");
57+
}
58+
59+
@Test
60+
public void parseVersionIncorrectMinorTest() {
61+
junitExpectedException.expect(LicenseVersionException.class);
62+
junitExpectedException.expectMessage(LicenseVersionException.MINOR_VERSION_IS_NOT_NUMERIC);
63+
64+
// the line below is expected to produce an exception
65+
Version.parseVersionString("1.a.11");
66+
}
67+
68+
@Test
69+
public void isVersionNumericPositiveIntegerTest() {
70+
Assert.assertTrue(Version.isVersionNumeric("7"));
71+
}
72+
73+
@Test
74+
public void isVersionNumericNegativeIntegerTest() {
75+
Assert.assertFalse(Version.isVersionNumeric("-7"));
76+
}
77+
78+
@Test
79+
public void isVersionNumericPositiveFloatTest() {
80+
Assert.assertFalse(Version.isVersionNumeric("5.973"));
81+
}
82+
83+
@Test
84+
public void isVersionNumericNegativeFloatTest() {
85+
Assert.assertFalse(Version.isVersionNumeric("-5.973"));
86+
}
87+
88+
@Test
89+
public void isVersionNumericLetterTest() {
90+
Assert.assertFalse(Version.isVersionNumeric("a"));
91+
}
92+
93+
@Test
94+
public void isAGPLVersionTest() {
95+
Assert.assertTrue(Version.isAGPLVersion());
96+
}
97+
98+
@Test
99+
public void isAGPLTrueTest() {
100+
Version customVersion = new Version(new VersionInfo("iText®", "7.5.1-SNAPSHOT",
101+
"iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV (AGPL-version)", null), false);
102+
Assert.assertTrue(customVersion.isAGPL());
103+
}
104+
105+
@Test
106+
public void isAGPLFalseTest() {
107+
Version customVersion = new Version(
108+
new VersionInfo("iText®", "7.5.1-SNAPSHOT", "iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV", null),
109+
false);
110+
Assert.assertFalse(customVersion.isAGPL());
111+
}
112+
113+
@Test
114+
public void isExpiredTest() {
115+
Assert.assertFalse(Version.isExpired());
116+
}
117+
118+
@Test
119+
public void getInstanceTest() {
120+
Version instance = Version.getInstance();
121+
checkVersionInstance(instance);
122+
}
123+
124+
@Test
125+
public void customVersionCorrectTest() {
126+
Version customVersion = new Version(
127+
new VersionInfo("iText®", "7.5.1-SNAPSHOT", "iText® 7.5.1-SNAPSHOT ©2000-2090 iText Group NV", null),
128+
false);
129+
checkVersionInstance(customVersion);
130+
}
131+
132+
@Test
133+
public void customVersionIncorrectMajorTest() {
134+
Version customVersion = new Version(
135+
new VersionInfo("iText®", "8.5.1-SNAPSHOT", "iText® 8.5.1-SNAPSHOT ©2000-2090 iText Group NV", null),
136+
false);
137+
Assert.assertFalse(checkVersion(customVersion.getVersion()));
138+
}
139+
140+
@Test
141+
public void customVersionIncorrectMinorTest() {
142+
Version customVersion = new Version(
143+
new VersionInfo("iText®", "7.a.1-SNAPSHOT", "iText® 7.a.1-SNAPSHOT ©2000-2090 iText Group NV", null),
144+
false);
145+
Assert.assertFalse(checkVersion(customVersion.getVersion()));
146+
}
147+
148+
@Test
149+
public void customVersionIncorrectPatchTest() {
150+
Version customVersion = new Version(
151+
new VersionInfo("iText®", "7.50.a-SNAPSHOT", "iText® 7.50.a-SNAPSHOT ©2000-2090 iText Group NV", null),
152+
false);
153+
Assert.assertFalse(checkVersion(customVersion.getVersion()));
154+
}
155+
156+
private static void checkVersionInstance(Version instance) {
157+
String product = instance.getProduct();
158+
String release = instance.getRelease();
159+
String version = instance.getVersion();
160+
String key = instance.getKey();
161+
VersionInfo info = instance.getInfo();
162+
163+
Assert.assertEquals(product, info.getProduct());
164+
Assert.assertEquals("iText®", product);
165+
166+
Assert.assertEquals(release, info.getRelease());
167+
Assert.assertTrue(release.matches("[7]\\.[0-9]+\\.[0-9]+(-SNAPSHOT)?$"));
168+
169+
Assert.assertEquals(version, info.getVersion());
170+
171+
Assert.assertTrue(checkVersion(version));
172+
173+
Assert.assertNull(key);
174+
}
175+
176+
private static boolean checkVersion(String version) {
177+
String regexp = "iText\\u00ae [7]\\.[0-9]+\\.[0-9]+(-SNAPSHOT)? \\u00a92000-20([2-9][0-9]) "
178+
+ "iText Group NV( \\(AGPL-version\\))?";
179+
return version.matches(regexp);
180+
}
181+
}

0 commit comments

Comments
 (0)