Alexander Wels has uploaded a new change for review.

Change subject: core: broken unit test
......................................................................

core: broken unit test

- Fixed a unit test that would break if a file didn't exist
in a certain environment.

Change-Id: I9d0627ea770a3194515b22b6d6cc5df5fe8df829
Signed-off-by: Alexander Wels <[email protected]>
---
M 
backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ServletUtilsTest.java
1 file changed, 29 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/27/14327/1

diff --git 
a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ServletUtilsTest.java
 
b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ServletUtilsTest.java
index cfd636f..583954d 100644
--- 
a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ServletUtilsTest.java
+++ 
b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/ServletUtilsTest.java
@@ -12,8 +12,10 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.io.BufferedWriter;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 
 import javax.servlet.ServletOutputStream;
@@ -174,14 +176,31 @@
        HttpServletResponse mockResponse = mock(HttpServletResponse.class);
        ServletOutputStream responseOut = mock(ServletOutputStream.class);
        when(mockResponse.getOutputStream()).thenReturn(responseOut);
-       File file = new File("/etc/favicon.png");
-       ServletUtils.sendFile(mockRequest, mockResponse, file, 
"application/xml");
-       //Check that the mime type was set to the one passed in, instead of the 
one associated with the file
-       verify(mockResponse).setContentType("application/xml");
-       //Check the file length is set right.
-       verify(mockResponse).setContentLength((int)file.length());
-       //Make sure the stream is written to.
-       verify(responseOut).write((byte[])anyObject(), eq(0), anyInt());
+       File file = File.createTempFile("favicon", "png");
+       BufferedWriter bw = null;
+       try {
+           FileWriter fw = new FileWriter(file.getAbsoluteFile());
+           bw = new BufferedWriter(fw);
+           bw.write("This is some content");
+           bw.close();
+           bw = null;
+           ServletUtils.sendFile(mockRequest, mockResponse, file, 
"application/xml");
+           //Check that the mime type was set to the one passed in, instead of 
the one associated with the file
+           verify(mockResponse).setContentType("application/xml");
+           //Check the file length is set right.
+           verify(mockResponse).setContentLength((int) file.length());
+           //Make sure the stream is written to.
+           verify(responseOut).write((byte[]) anyObject(), eq(0), anyInt());
+       } catch (IOException ioe) {
+           fail("IOException thrown: " + ioe.getMessage());
+       } finally {
+           if (bw != null) {
+               bw.close();
+           }
+           if (!file.delete()) {
+               fail("Unable to delete temporary file");
+           }
+       }
     }
 
     /**
@@ -199,8 +218,8 @@
        //Check that the mime type was set to the one passed in, instead of the 
one associated with the file
        verify(mockResponse).setContentType("image/png");
        //Check the file length is set right.
-       verify(mockResponse).setContentLength((int)file.length());
+       verify(mockResponse).setContentLength((int) file.length());
        //Make sure the stream is written to.
-       verify(responseOut).write((byte[])anyObject(), eq(0), anyInt());
+       verify(responseOut).write((byte[]) anyObject(), eq(0), anyInt());
     }
 }


--
To view, visit http://gerrit.ovirt.org/14327
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9d0627ea770a3194515b22b6d6cc5df5fe8df829
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alexander Wels <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to