Skip to content

Commit 9e7fdd2

Browse files
author
Samuel Nitsche
committed
Introduced new Version-class to parse version-string information
1 parent 23d3dae commit 9e7fdd2

File tree

3 files changed

+162
-4
lines changed

3 files changed

+162
-4
lines changed

src/main/java/org/utplsql/api/DBHelper.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import oracle.jdbc.OracleTypes;
44

5-
import java.sql.CallableStatement;
6-
import java.sql.Connection;
7-
import java.sql.SQLException;
8-
import java.sql.Types;
5+
import java.sql.*;
96

107
/**
118
* Database utility functions.
@@ -114,6 +111,28 @@ public static void failOnVersionCompatibilityCheckFailed( Connection conn )
114111
}
115112
}
116113

114+
/** Returns the Frameworks version string of the given connection
115+
*
116+
* @param conn Active db connection
117+
* @return
118+
* @throws SQLException
119+
*/
120+
public static Version getDatabaseFrameworkVersion( Connection conn )
121+
throws SQLException {
122+
Version result = new Version("");
123+
try (Statement stmt = conn.prepareStatement("select ut_runner.version() from dual"))
124+
{
125+
ResultSet rs = stmt.getResultSet();
126+
127+
if ( rs.next() )
128+
result = new Version(rs.getString(1));
129+
130+
rs.close();
131+
}
132+
133+
return result;
134+
}
135+
117136
/**
118137
* Enable the dbms_output buffer with unlimited size.
119138
* @param conn the connection
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.utplsql.api;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
7+
*
8+
* @author pesse
9+
*/
10+
public class Version {
11+
private String origString;
12+
private Integer major;
13+
private Integer minor;
14+
private Integer bugfix;
15+
private Integer build;
16+
private boolean valid = false;
17+
18+
public Version( String versionString ) {
19+
assert versionString != null;
20+
this.origString = versionString;
21+
parseVersionString();
22+
}
23+
24+
private void parseVersionString()
25+
{
26+
Pattern p = Pattern.compile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?\\.?([0-9]+)?");
27+
28+
Matcher m = p.matcher(origString);
29+
30+
try {
31+
if (m.find()) {
32+
if ( m.group(1) != null )
33+
major = Integer.valueOf(m.group(1));
34+
if ( m.group(2) != null )
35+
minor = Integer.valueOf(m.group(2));
36+
if ( m.group(3) != null )
37+
bugfix = Integer.valueOf(m.group(3));
38+
if ( m.group(4) != null )
39+
build = Integer.valueOf(m.group(4));
40+
41+
if ( major != null ) // We need a valid major version as minimum requirement for a Version object to be valid
42+
valid = true;
43+
}
44+
}
45+
catch ( NumberFormatException e )
46+
{
47+
valid = false;
48+
}
49+
}
50+
51+
public String getOrigString() {
52+
return origString;
53+
}
54+
55+
public Integer getMajor() {
56+
return major;
57+
}
58+
59+
public Integer getMinor() {
60+
return minor;
61+
}
62+
63+
public Integer getBugfix() {
64+
return bugfix;
65+
}
66+
67+
public Integer getBuild() {
68+
return build;
69+
}
70+
71+
public boolean isValid() {
72+
return valid;
73+
}
74+
75+
/** Returns a normalized form of the parsed version information
76+
*
77+
* @return
78+
*/
79+
public String getVersionString()
80+
{
81+
if ( isValid() ) {
82+
StringBuilder sb = new StringBuilder();
83+
sb.append(String.valueOf(major));
84+
if ( minor != null )
85+
sb.append("." + String.valueOf(minor));
86+
if ( bugfix != null )
87+
sb.append("." + String.valueOf(bugfix));
88+
if ( build != null )
89+
sb.append("." + String.valueOf(build));
90+
91+
return sb.toString();
92+
}
93+
else
94+
return "invalid";
95+
}
96+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class VersionObjectTest {
7+
8+
@Test
9+
public void versionPatternRecognitionFull() {
10+
Version v = new Version("v3.1.3.1234");
11+
12+
Assert.assertEquals(3, (long)v.getMajor());
13+
Assert.assertEquals(1, (long)v.getMinor());
14+
Assert.assertEquals(3, (long)v.getBugfix());
15+
Assert.assertEquals(1234, (long)v.getBuild());
16+
Assert.assertEquals(true, v.isValid());
17+
Assert.assertEquals("3.1.3.1234", v.getVersionString());
18+
}
19+
20+
@Test
21+
public void versionPatternRecognitionPartial() {
22+
Version v = new Version("3.1.etc");
23+
24+
Assert.assertEquals(3, (long)v.getMajor());
25+
Assert.assertEquals(1, (long)v.getMinor());
26+
Assert.assertNull(v.getBugfix());
27+
Assert.assertNull(v.getBuild());
28+
Assert.assertEquals(true, v.isValid());
29+
Assert.assertEquals("3.1", v.getVersionString());
30+
}
31+
32+
@Test
33+
public void versionPatternRecognitionInvalid() {
34+
Version v = new Version("adseef");
35+
36+
Assert.assertNull(v.getMajor());
37+
Assert.assertNull(v.getMinor());
38+
Assert.assertNull(v.getBugfix());
39+
Assert.assertNull(v.getBuild());
40+
Assert.assertEquals(false, v.isValid());
41+
Assert.assertEquals("invalid", v.getVersionString());
42+
}
43+
}

0 commit comments

Comments
 (0)