summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSelvasundaram <selvam@gluster.com>2011-09-30 18:10:44 +0530
committerSelvasundaram <selvam@gluster.com>2011-10-03 18:23:24 +0530
commit1616b9329c80cd59d42dcecf870a9ec663f2b3b4 (patch)
treee350b640e29b157731425badb87dc1c6c8a5312c
parent72f1affcf7f1a16e35889dd3c57b315cd9f0f03e (diff)
Junit code for util
-rw-r--r--src/com.gluster.storage.management.core/META-INF/MANIFEST.MF1
-rw-r--r--src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/DateUtilTest.java236
-rw-r--r--src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/FileUtilTest.java482
3 files changed, 719 insertions, 0 deletions
diff --git a/src/com.gluster.storage.management.core/META-INF/MANIFEST.MF b/src/com.gluster.storage.management.core/META-INF/MANIFEST.MF
index b4431f53..9ddbca69 100644
--- a/src/com.gluster.storage.management.core/META-INF/MANIFEST.MF
+++ b/src/com.gluster.storage.management.core/META-INF/MANIFEST.MF
@@ -12,3 +12,4 @@ Export-Package: com.gluster.storage.management.core.constants,
com.gluster.storage.management.core.utils
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.6.0"
Bundle-ClassPath: .
+Import-Package: com.gluster.storage.management.core.exceptions
diff --git a/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/DateUtilTest.java b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/DateUtilTest.java
new file mode 100644
index 00000000..36c8ec54
--- /dev/null
+++ b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/DateUtilTest.java
@@ -0,0 +1,236 @@
+package com.gluster.storage.management.core.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.Date;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.gluster.storage.management.core.constants.CoreConstants;
+import com.gluster.storage.management.core.exceptions.GlusterRuntimeException;
+
+/**
+ * The class <code>DateUtilTest</code> contains tests for the class <code>{@link DateUtil}</code>.
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ * @author root
+ * @version $Revision: 1.0 $
+ */
+public class DateUtilTest {
+ /**
+ * Run the String dateToString(Date) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testDateToString_1()
+ throws Exception {
+ Date date = new Date(1317275258795L);
+ String result = DateUtil.dateToString(date);
+
+ assertEquals("09/29/2011 11:17:38", result);
+ }
+
+ /**
+ * Run the String dateToString(Date,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testDateToString_2()
+ throws Exception {
+ Date date = new Date(1317275258795L);
+ String dateFormat = "";
+
+ String result = DateUtil.dateToString(date, dateFormat);
+
+ assertEquals("", result);
+ }
+
+ /**
+ * Run the String dateToString(Date,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testDateToString_3()
+ throws Exception {
+ Date date = new Date(1317275258795L);
+ String dateFormat = CoreConstants.PURE_DATE_FORMAT;
+
+ String result = DateUtil.dateToString(date, dateFormat);
+
+ assertEquals("09/29/2011", result);
+ }
+
+ /**
+ * Run the String formatDate(Date) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testFormatDate_1()
+ throws Exception {
+ String result = DateUtil.formatDate(new Date(1317275258795L));
+
+ assertEquals("09/29/2011", result);
+ }
+
+ /**
+ * Run the String formatDate(Date) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testFormatDate_2()
+ throws Exception {
+ String result = DateUtil.formatDate(new Date(0L));
+ assertEquals("01/01/1970", result);
+ }
+
+ /**
+ * Run the String formatDate(Date) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testFormatDate_3()
+ throws Exception {
+
+ String result = DateUtil.formatDate(new Date(-156784523000L));
+ assertEquals("01/12/1965", result);
+
+ }
+
+ /**
+ * Run the String formatTime(Date) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testFormatTime_1()
+ throws Exception {
+ Date inputDate = new Date(1317275258795L);
+
+ String result = DateUtil.formatTime(inputDate);
+
+ // add additional test code here
+ assertEquals("11:17:38.795", result);
+ }
+
+ /**
+ * Run the Date stringToDate(String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testStringToDate_1()
+ throws Exception {
+ DateUtil.stringToDate("");
+ }
+
+ /**
+ * Run the Date stringToDate(String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testStringToDate_2()
+ throws Exception {
+ String testDate = "09/29/2011";
+ DateUtil.stringToDate(testDate);
+ }
+
+ /**
+ * Run the Date stringToDate(String,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test
+ public void testStringToDate_3()
+ throws Exception {
+ String dateFormat = "MM/dd/yyyy";
+ String input = "09/29/2011"; // MM/dd/yyyy HH:mm:ss
+ Date expectedDate = new Date(1317234600000L);
+
+ Date result = DateUtil.stringToDate(input, dateFormat);
+ assertEquals(expectedDate, result);
+ }
+
+ /**
+ * Run the Date stringToDate(String,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Test(expected = GlusterRuntimeException.class)
+ public void testStringToDate_4()
+ throws Exception {
+ String inputDate = "";
+ String dateFormat = "";
+ DateUtil.stringToDate(inputDate, dateFormat);
+ }
+
+ /**
+ * Perform pre-test initialization.
+ *
+ * @throws Exception
+ * if the initialization fails for some reason
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @Before
+ public void setUp()
+ throws Exception {
+ // add additional set up code here
+ }
+
+ /**
+ * Perform post-test clean-up.
+ *
+ * @throws Exception
+ * if the clean-up fails for some reason
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ @After
+ public void tearDown()
+ throws Exception {
+ // Add additional tear down code here
+ }
+
+ /**
+ * Launch the test.
+ *
+ * @param args the command line arguments
+ *
+ * @generatedBy CodePro at 9/27/11 12:31 PM
+ */
+ public static void main(String[] args) {
+ new org.junit.runner.JUnitCore().run(DateUtilTest.class);
+ }
+} \ No newline at end of file
diff --git a/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/FileUtilTest.java b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/FileUtilTest.java
new file mode 100644
index 00000000..084a8519
--- /dev/null
+++ b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/FileUtilTest.java
@@ -0,0 +1,482 @@
+package com.gluster.storage.management.core.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.gluster.storage.management.core.exceptions.GlusterRuntimeException;
+
+
+/**
+ * The class <code>FileUtilTest</code> contains tests for the class <code>{@link FileUtil}</code>.
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ * @author root
+ * @version $Revision: 1.0 $
+ */
+public class FileUtilTest {
+
+ private String filePath = FileUtil.getTempDirName() + "/test.txt";
+
+ /**
+ * To write the text into given file.
+ *
+ * @generatedBy
+ */
+ private boolean writeToFile(String fileName, String text) {
+ try {
+ Writer output = null;
+ File file = new File(fileName);
+ output = new BufferedWriter(new FileWriter(file));
+ output.write(text);
+ output.close();
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ private boolean createNestedDir(String path) {
+ File file = new File(path);
+ return file.mkdirs();
+ }
+
+
+ private boolean createEmptyFile(String fileName) {
+ File file = new File(fileName);
+ if (file.exists()) {
+ return true;
+ }
+
+ try {
+ return file.createNewFile();
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+
+ /**
+ * Run the File createTempDir() method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testCreateTempDir_1()
+ throws Exception {
+
+ File result = FileUtil.createTempDir();
+
+ assertNotNull(result);
+ assertTrue(File.class.equals(result.getClass()));
+ }
+
+ /**
+ * Run the File createTempDir() method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testCreateTempDir_2()
+ throws Exception {
+
+ File result1 = FileUtil.createTempDir();
+ File result2 = FileUtil.createTempDir();
+
+ assertNotSame(result1, result2);
+ }
+
+
+ /**
+ * Run the void createTextFile(String,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testCreateTextFile_1()
+ throws Exception {
+ String fileName = "";
+ String contents = "";
+
+ FileUtil.createTextFile(fileName, contents);
+ }
+
+ /**
+ * Run the String getTempDirName() method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testGetTempDirName_1()
+ throws Exception {
+
+ String result = FileUtil.getTempDirName();
+
+ // while running on linux
+ assertEquals("/tmp", result);
+ }
+
+ /**
+ * Run the byte[] readFileAsByteArray(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testReadFileAsByteArray_1()
+ throws Exception {
+ File file = new File("");
+
+ byte[] result = FileUtil.readFileAsByteArray(file);
+ }
+
+ /**
+ * Run the byte[] readFileAsByteArray(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testReadFileAsByteArray_2()
+ throws Exception {
+ File file = new File(filePath);
+
+ byte[] result = FileUtil.readFileAsByteArray(file);
+ assertNotNull(result);
+ assertTrue(result instanceof byte[]);
+ }
+
+
+ /**
+ * Run the String readFileAsString(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testReadFileAsString_1()
+ throws Exception {
+ File file = new File("");
+
+ String result = FileUtil.readFileAsString(file);
+ }
+
+
+ /**
+ * Run the String readFileAsString(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testReadFileAsString_2()
+ throws Exception {
+ // Setup
+ File file = new File(filePath);
+ String result = FileUtil.readFileAsString(file);
+
+ assertNotNull(result);
+ assertTrue(result instanceof String);
+ }
+
+ /**
+ * Run the void recursiveDelete(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testRecursiveDelete_1()
+ throws Exception {
+
+ //Delete empty directories recursively
+ File fileOrDir = new File(FileUtil.getTempDirName() + "/rd");
+
+ FileUtil.recursiveDelete(fileOrDir);
+ assertTrue(!fileOrDir.exists());
+ }
+
+ /**
+ * Run the void recursiveDelete(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testRecursiveDelete_2()
+ throws Exception {
+ //Delete directories recursively (with some files)
+ File fileOrDir = new File(FileUtil.getTempDirName() + "/rdx");
+
+ FileUtil.recursiveDelete(fileOrDir);
+ assertTrue(!fileOrDir.exists());
+ }
+
+ /**
+ * Run the void recursiveDelete(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testRecursiveDelete_3()
+ throws Exception {
+ File fileOrDir = new File(FileUtil.getTempDirName() + "/rd");
+ FileUtil.recursiveDelete(fileOrDir);
+ assertTrue(!fileOrDir.exists());
+ }
+
+ /**
+ * Run the void recursiveDelete(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testRecursiveDelete_4()
+ throws Exception {
+ File file = new File(FileUtil.getTempDirName() + "/rd/b/bc/mydoc.txt");
+ assertTrue(!file.exists());
+
+ file = new File(FileUtil.getTempDirName() + "/rd//b/bc");
+ assertTrue(!file.exists());
+ }
+
+ /**
+ * Run the void recursiveDelete(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testRecursiveDelete_5()
+ throws Exception {
+ File fileOrDir = new File(FileUtil.getTempDirName() + "/rd/*"); //Wild cards
+ FileUtil.recursiveDelete(fileOrDir);
+ }
+
+
+ /**
+ * Run the void recursiveDelete(File) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testRecursiveDelete_6()
+ throws Exception {
+ File fileOrDir = new File(FileUtil.getTempDirName() + "/abcxyz");
+ FileUtil.recursiveDelete(fileOrDir);
+ }
+
+ /**
+ * Run the void renameFile(String,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test(expected=GlusterRuntimeException.class)
+ public void testRenameFile_1()
+ throws Exception {
+ String fromPath = FileUtil.getTempDirName() + "/test.txt";
+ new File(fromPath).createNewFile();
+ String toPath = "~/abc.txt"; // Relative path
+
+ FileUtil.renameFile(fromPath, toPath);
+ assertTrue(!new File(fromPath).exists());
+ assertTrue(new File(toPath).exists());
+ }
+
+ /**
+ * Run the void renameFile(String,String) method test.
+ *
+ * @throws Exception
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Test
+ public void testRenameFile_2()
+ throws Exception {
+ String fromPath = FileUtil.getTempDirName() + "/test.txt";
+ new File(fromPath).createNewFile();
+
+ String toPath = FileUtil.getTempDirName() + "/abc.txt"; // Absolute path example
+ FileUtil.renameFile(fromPath, toPath);
+ assertTrue(!new File(fromPath).exists());
+ assertTrue(new File(toPath).exists());
+ }
+
+ @Test
+ public void testRenameFile_3()
+ throws Exception {
+ String fromPath = FileUtil.getTempDirName() + "/test.txt";
+ new File(fromPath).createNewFile();
+
+ String toPath = FileUtil.getTempDirName() + "/renamefile.txt";
+ FileUtil.renameFile(fromPath, toPath);
+ assertTrue(!new File(fromPath).exists());
+ assertTrue(new File(toPath).exists());
+ }
+
+
+ /**
+ * Perform pre-test initialization.
+ *
+ * @throws Exception
+ * if the initialization fails for some reason
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @Before
+ public void setUp()
+ throws Exception {
+
+ // testReadFileAsByteArray_2()
+ if (!writeToFile(filePath, "Welcome to Gluster Storage Management console.")) {
+ fail("Setup: Text file creation error!");
+ }
+
+ // testRecursiveDelete_1()
+ if (!createNestedDir(FileUtil.getTempDirName() + "/rd/b/c/d")) {
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/b/mydoc.txt");
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/b/songs.mp3");
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/b/mysetup.cfg");
+
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/b/bc/mydoc.txt");
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/songs.mp3");
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/b/bc/mysetup.cfg");
+
+ createEmptyFile(FileUtil.getTempDirName() + "/rd//b/mydoc.txt");
+ createEmptyFile(FileUtil.getTempDirName() + "/rd/b/bc/songs.mp3");
+
+ }
+
+ if (! createEmptyFile(FileUtil.getTempDirName() + "/renamefile.txt") ) {
+ fail("Failed to create file [/renamefile.txt]");
+ }
+ }
+
+ /**
+ * Perform post-test clean-up.
+ *
+ * @throws Exception
+ * if the clean-up fails for some reason
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ @After
+ public void tearDown()
+ throws Exception {
+ File file = new File(filePath);
+ file.delete();
+ file = new File(FileUtil.getTempDirName() + "/rd/b/c/d");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/rd/b/c");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/rd/b/mydoc.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File(FileUtil.getTempDirName() + "/rd/b/songs.mp3");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File(FileUtil.getTempDirName() + "/rd/b/mysetup.cfg");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/rd/b/bc/mydoc.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File(FileUtil.getTempDirName() + "/rd/b/bc/mysetup.cfg");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File(FileUtil.getTempDirName() + "/rd/b/bc/songs.mp3");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/rd/b/bc");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/rd/b");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/rd");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ file = new File(FileUtil.getTempDirName() + "/abc.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File("~/abc.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File(FileUtil.getTempDirName() + "/test.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ file = new File(FileUtil.getTempDirName() + "/renamefile.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ }
+
+ /**
+ * Launch the test.
+ *
+ * @param args the command line arguments
+ *
+ * @generatedBy CodePro at 9/29/11 2:39 PM
+ */
+ public static void main(String[] args) {
+ new org.junit.runner.JUnitCore().run(FileUtilTest.class);
+ }
+} \ No newline at end of file