[CONJ-112] Better structure of database URL for connect in JUnit test cases Created: 2014-09-10  Updated: 2014-09-23  Resolved: 2014-09-23

Status: Closed
Project: MariaDB Connector/J
Component/s: None
Affects Version/s: 1.1.7
Fix Version/s: 1.1.8

Type: Task Priority: Trivial
Reporter: Lennart Schedin Assignee: Massimo Siani (Inactive)
Resolution: Fixed Votes: 0
Labels: None


 Description   

I like JUnit tests and I frequently “messes” with them to try different things. I have MariaDB server 5.5 installed on my Windows computer as my normal database server. I have Mysql 5.1 installed on a different port. I also have a virtual Ubuntu machine with some other version. I frequently edit the database URL in BaseTest.java to change my IP, port and sometimes also to add connection properties.

I searched the source code and found 49 matches of the string “DriverManager.getConnection”. One is found in BaseTest.java and the others are found in other test files. It would be easier for me if there only was one place to change the database host and port in the test cases. I understand that some of the test cases need to create more than 1 Connection and also use other properties in the connection url.

I suggest that the connect mechanism in BaseTest.java is refactored so that the following things can be edited easily:

  1. Database host (ip)
  2. Port number (could be same configuration as host because they usually are groups together).
  3. Database name (“test” is always used)
  4. Database user and password
  5. Additional database properties that are appended

For test cases that needs a new Connection a getConnection()-method could be created in BaseTest.java. For test cases that need optional URL parameters it can be supplied as parameters to a getConnection()-method.



 Comments   
Comment by Massimo Siani (Inactive) [ 2014-09-15 ]

Does https://code.launchpad.net/~massimo-siani/mariadb-java-client/cleanup-tests represent something close to what you mean? (not complete yet)

Comment by Lennart Schedin [ 2014-09-16 ]

Yes it is something like that that I had in mind!

Even though it is not finished I’m making two small suggestions:
1. In BastTest.java make it so that the IP/port/database name/user only exist in one place (to be easy to change)
2. Create some mechanism so that a unit test can create a new Connection so that two connections can be used at the same time. This might be useful for tests that perform some sort of concurrency testing or comparison between two Connections with different properties/settings.

Comment by Lennart Schedin [ 2014-09-17 ]

One more enhancement request: would it be possible to set the database URL/host/IP/port/username/password externally? Typically you might have a Jenkins Continuous integration system that automatically runs the test. It would be good to be able to configure the URL like this:

mvn test -DdbUrl="jdbc:mysql://localhost:3306/test?user=root"

The BaseTest.java could then use System.getProperty("dbUrl") and use that (if it exists).

Comment by Vladislav Vaintroub [ 2014-09-17 ]

Massimo, in your patch you are leaking connections. the setConnection always opens a new connection, but does not close the existing one, and you have a bunch of setConnection inside the tests, in addition to "beforeTest" one.

to me, looks like it would be better not to create a new connection, but instead use connection.setClientInfo(String name, String value), and make sure it works

Comment by Massimo Siani (Inactive) [ 2014-09-18 ]

Vlad, you're right, thanks. It should be better now.

Comment by Massimo Siani (Inactive) [ 2014-09-19 ]

I think that this one should be the first to be merged, or it will be too difficult to maintain.

Generated at Thu Feb 08 03:13:15 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.