[libvirt] [PATCH 09/15] Split JUnit tests and use a fixture for Connect.

Claudio Bley cbley at av-test.de
Fri Oct 12 14:28:47 UTC 2012


The tests have been separated into a test case requiring
a libvirt connection and a test case which does not.

Every test gets a new Connect instance by using a fixture.
This avoids duplicate code and makes tests more consistent.
---
 build.xml                                         |    9 ++--
 src/test/java/org/libvirt/TestJavaBindings.java   |   47 +++++++++++----------
 src/test/java/org/libvirt/TestLibvirtGlobals.java |   21 +++++++++
 3 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/build.xml b/build.xml
index 0b147b0..4b73562 100644
--- a/build.xml
+++ b/build.xml
@@ -49,11 +49,14 @@
 	</target>
 
 	<target name="test" depends="build" description="tests the code">
-		<junit printsummary="yes" fork="yes">
+		<junit printsummary="yes" fork="yes" forkMode="perBatch">
 			<formatter type="plain" />
 			<classpath refid="test.classpath" />
-			<test name="org.libvirt.TestJavaBindings" outfile="target/testResults">
-			</test>
+			<batchtest todir="target">
+				<fileset dir="src/test/java">
+					<include name="**/Test*.java" />
+				</fileset>
+			</batchtest>
 		</junit>
 	</target>
 
diff --git a/src/test/java/org/libvirt/TestJavaBindings.java b/src/test/java/org/libvirt/TestJavaBindings.java
index bac7a82..55ca64b 100644
--- a/src/test/java/org/libvirt/TestJavaBindings.java
+++ b/src/test/java/org/libvirt/TestJavaBindings.java
@@ -4,28 +4,35 @@ import java.util.UUID;
 
 import junit.framework.TestCase;
 
-public class TestJavaBindings extends TestCase {
-
-    int UUIDArray[] = { Integer.decode("0x00"), Integer.decode("0x4b"), Integer.decode("0x96"), Integer.decode("0xe1"),
+public final class TestJavaBindings extends TestCase {
+    final int UUIDArray[] = { Integer.decode("0x00"), Integer.decode("0x4b"), Integer.decode("0x96"), Integer.decode("0xe1"),
             Integer.decode("0x2d"), Integer.decode("0x78"), Integer.decode("0xc3"), Integer.decode("0x0f"),
             Integer.decode("0x5a"), Integer.decode("0xa5"), Integer.decode("0xf0"), Integer.decode("0x3c"),
             Integer.decode("0x87"), Integer.decode("0xd2"), Integer.decode("0x1e"), Integer.decode("0x67") };
 
-    public void testErrorCallback() throws Exception {
+    private Connect conn;
+
+    protected void setUp() throws LibvirtException {
+        conn = new Connect("test:///default", false);
+    }
+
+    protected void tearDown() throws LibvirtException {
+        conn.close();
+        conn = null;
+    }
+
+    public void testConnectionErrorCallback() throws LibvirtException {
         DummyErrorCallback cb = new DummyErrorCallback();
-        Connect.setErrorCallback(cb);
+        conn.setConnectionErrorCallback(cb);
+
         try {
-            Connect conn = new Connect("test:///someUrl", false);
-        } catch (Exception e) {
-            // eat it
-        }
-        assertTrue("We should have caught an error", cb.error);
-        Connect conn = new Connect("test:///default", false);
-        conn.setConnectionErrorCallback(cb) ;
+            conn.domainDefineXML("fail, miserably");
+        } catch (LibvirtException e) {} // ignore
+
+        assertTrue("Error callback was not called", cb.error);
     }
 
     public void testConnection() throws Exception {
-        Connect conn = new Connect("test:///default", false);
         assertEquals("conn.getType()", "Test", conn.getType());
         assertEquals("conn.getURI()", "test:///default", conn.getURI());
         assertEquals("conn.getMaxVcpus(xen)", 32, conn.getMaxVcpus("xen"));
@@ -39,7 +46,6 @@ public class TestJavaBindings extends TestCase {
     }
 
     public void testNodeInfo() throws Exception {
-        Connect conn = new Connect("test:///default", false);
         NodeInfo nodeInfo = conn.nodeInfo();
         assertEquals("nodeInfo.model", "i686", nodeInfo.model);
         assertEquals("nodeInfo.memory", 3145728, nodeInfo.memory);
@@ -56,8 +62,6 @@ public class TestJavaBindings extends TestCase {
     }
 
     public void testNetworkCreate() throws Exception {
-        Connect conn = new Connect("test:///default", false);
-
         Network network1 = conn.networkCreateXML("<network>" + "  <name>createst</name>"
                 + "  <uuid>004b96e1-2d78-c30f-5aa5-f03c87d21e68</uuid>" + "  <bridge name='createst'/>"
                 + "  <forward dev='eth0'/>" + "  <ip address='192.168.66.1' netmask='255.255.255.0'>" + "    <dhcp>"
@@ -103,8 +107,6 @@ public class TestJavaBindings extends TestCase {
     }
 
     public void testDomainCreate() throws Exception {
-        Connect conn = new Connect("test:///default", false);
-
         Domain dom1 = conn.domainDefineXML("<domain type='test' id='2'>" + "  <name>deftest</name>"
                 + "  <uuid>004b96e1-2d78-c30f-5aa5-f03c87d21e70</uuid>" + "  <memory>8388608</memory>"
                 + "  <vcpu>2</vcpu>" + "  <os><type arch='i686'>hvm</type></os>" + "  <on_reboot>restart</on_reboot>"
@@ -129,7 +131,7 @@ public class TestJavaBindings extends TestCase {
         this.validateDomainData(conn.domainLookupByUUID(UUID.fromString("004b96e1-2d78-c30f-5aa5-f03c87d21e67")));
     }
 
-    public void validateDomainData(Domain dom) throws Exception {
+    private void validateDomainData(Domain dom) throws Exception {
         assertEquals("dom.getName()", "createst", dom.getName());
         assertEquals("dom.getMaxMemory()", 8388608, dom.getMaxMemory());
         // Not supported by the test driver
@@ -162,7 +164,6 @@ public class TestJavaBindings extends TestCase {
     }
 
     public void testInterfaces() throws Exception {
-        Connect conn = new Connect("test:///default", false);
         assertEquals("numOfInterfaces:", 1, conn.numOfInterfaces());
         assertEquals("numOfInterfaces:", 0, conn.numOfDefinedInterfaces());
         assertEquals("listDefinedInterfaces:", "eth1", conn.listInterfaces()[0]);
@@ -188,18 +189,18 @@ public class TestJavaBindings extends TestCase {
     }
 
     public void testAccessAfterClose() throws Exception {
-        Connect conn = new Connect("test:///default", false);
         conn.close();
         assertTrue("conn.isConnected should be false", !conn.isConnected());
+        LibvirtException virException = null;
         try {
             conn.getHostName();
         } catch (LibvirtException e) {
-            // eat it
+            virException = e;
         }
+        assertNotNull(virException);
     }
     
     public void testStoragePool() throws Exception {
-        Connect conn = new Connect("test:///default", false);           
         StoragePool pool1 = conn.storagePoolDefineXML("<pool type='dir'>"
                 + "  <name>pool1</name>"
                 + "  <target>"
diff --git a/src/test/java/org/libvirt/TestLibvirtGlobals.java b/src/test/java/org/libvirt/TestLibvirtGlobals.java
new file mode 100644
index 0000000..b2b0d9f
--- /dev/null
+++ b/src/test/java/org/libvirt/TestLibvirtGlobals.java
@@ -0,0 +1,21 @@
+package org.libvirt;
+
+import java.util.UUID;
+
+import junit.framework.TestCase;
+
+/**
+ * libvirt tests not requiring an active connection
+ */
+public class TestLibvirtGlobals extends TestCase {
+    public void testErrorCallback() throws Exception {
+        DummyErrorCallback cb = new DummyErrorCallback();
+        Connect.setErrorCallback(cb);
+        try {
+            Connect conn = new Connect("test:///someUrl", false);
+        } catch (LibvirtException e) {
+            // eat it
+        }
+        assertTrue("We should have caught an error", cb.error);
+    }
+}
-- 
1.7.9.5

-- 
AV-Test GmbH, Henricistraße 20, 04155 Leipzig, Germany
Phone: +49 341 265 310 19
Web:<http://www.av-test.org>

Eingetragen am / Registered at: Amtsgericht Stendal (HRB 114076)
Geschaeftsfuehrer (CEO): Andreas Marx, Guido Habicht, Maik Morgenstern




More information about the libvir-list mailing list