diff options
author | Selvasundaram <selvam@gluster.com> | 2011-09-30 18:10:44 +0530 |
---|---|---|
committer | Selvasundaram <selvam@gluster.com> | 2011-09-30 18:17:11 +0530 |
commit | 4f281bf7e7b9dae9215e5c210833494efeea3c52 (patch) | |
tree | 7648c7eef878caab21e51485d313d7f737f55819 | |
parent | 72f1affcf7f1a16e35889dd3c57b315cd9f0f03e (diff) |
Junit code for util
2 files changed, 647 insertions, 0 deletions
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..dfd25b87 --- /dev/null +++ b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/DateUtilTest.java @@ -0,0 +1,255 @@ +package com.gluster.storage.management.core.utils; + +import java.text.SimpleDateFormat; +import java.util.Date; +import org.junit.*; +import org.omg.PortableInterceptor.SUCCESSFUL; + +import static org.junit.Assert.*; + +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 { + @SuppressWarnings("deprecation") + Date date = new Date(1317275258795L); + String result = DateUtil.dateToString(date); + + // add additional test code here + + // System.out.println("testDateToString_1 : " + date.getYear() + "[" + result + "]"); // 09/29/2011 11:17:38 + 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); + + // add additional test code here + 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); + + // add additional test code here + 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 + public void testStringToDate_1() + throws Exception { + try { + DateUtil.stringToDate(""); + fail("Failed to throw exception for \"\" argument in DateUtil.stringToDate()"); + } catch (Exception e) { + // TODO: handle exception + System.out.println("Exception thrown: [" + e.getMessage() + "]"); + } + } + + /** + * Run the Date stringToDate(String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 9/27/11 12:31 PM + */ + @Test + public void testStringToDate_2() + throws Exception { + try { + String testDate = "09/29/2011"; + DateUtil.stringToDate(testDate); + fail("Failed to throw exception for \"09/29/2011\" argument in DateUtil.stringToDate()"); + } catch (Exception e) { + // TODO: handle exception + System.out.println("Exception thrown: [" + e.getMessage() + "]"); + } + } + + /** + * 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 = com.gluster.storage.management.core.exceptions.GlusterRuntimeException.class) + public void testStringToDate_4() + throws Exception { + String inputDate = ""; + String dateFormat = ""; + + DateUtil.stringToDate(inputDate, dateFormat); + fail("Failed to throw exception for [" + inputDate + "," + dateFormat + + "] arguments in DateUtil.stringToDate()"); + } + + /** + * 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..1f50ffcb --- /dev/null +++ b/src/com.gluster.storage.management.core/junit/com/gluster/storage/management/core/utils/FileUtilTest.java @@ -0,0 +1,392 @@ +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.Writer; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 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 { + + /** + * 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(); + } + + /** + * 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 + public void testCreateTextFile_1() + throws Exception { + String fileName = ""; + String contents = ""; + try { + FileUtil.createTextFile(fileName, contents); + fail("Failed to throw expception! for [] file name and [] file content in FileUtil.createTextFile()"); + } catch (Exception e) { + //Nothing to do + } + } + + /** + * 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(); + + // add additional test code here + assertEquals("/tmp", result); + } + + /** + * Run the byte[] readFileAsByteArray(File) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 9/29/11 2:39 PM + */ + @Test + public void testReadFileAsByteArray_1() + throws Exception { + File file = new File(""); + try { + byte[] result = FileUtil.readFileAsByteArray(file); + assertNotNull(result); + fail("Byte array read from empty file name should throw exception!"); + } catch (Exception e) { + // Nothing to do + } + } + + /** + * 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 { + // Setup + String filePath = "/tmp/test.txt"; + String text = "Welcome to Gluster Storage Management console."; + if (!writeToFile(filePath, text) ) { + fail("Text file creation error!"); + } + + 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 + public void testReadFileAsString_1() + throws Exception { + File file = new File(""); + try { + String result = FileUtil.readFileAsString(file); + assertNotNull(result); + fail("Read from Empty file name should throw exception!"); + } catch (Exception e) { + // Nothing to do + } + } + + + /** + * 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 + String filePath = "/tmp/test.txt"; + String text = "Welcome to Gluster Storage Management console."; + if (!writeToFile(filePath, text)) { + fail("Text file creation error!"); + } + + 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 { + + if (!createNestedDir("/tmp/a/b/c/d")) { + fail("Failed to create directory [/tmp/a/b/c/d]"); + } + + File fileOrDir = new File("/tmp/a"); + try { + FileUtil.recursiveDelete(fileOrDir); + assertTrue( !fileOrDir.exists() ); + } catch (Exception e) { + // Nothing to do + } + } + + /** + * Run the void recursiveDelete(File) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 9/29/11 2:39 PM + */ + @Test + public void testRecursiveDelete_2() + throws Exception { + + if (!createNestedDir("/tmp/a/b/c/d")) { + fail("Failed to create directory [/tmp/a/b/c/d]"); + } + + File fileOrDir = new File("/tmp/x"); + try { + FileUtil.recursiveDelete(fileOrDir); + assertTrue( !fileOrDir.exists() ); + } catch (Exception e) { + // Nothing to do + } + } + + /** + * 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 { + //Setup + File fileDir = new File("/tmp/a/bc"); + fileDir.mkdirs(); + fileDir.createNewFile(); + try { + File fileOrDir = new File("/tmp/a"); + FileUtil.recursiveDelete(fileOrDir); + assertTrue(!fileOrDir.exists()); + } catch (Exception e) { + // Nothing to do + } + } + + /** + * 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 fileDir = new File("/tmp/1/2.bc/c/123.xyz"); + fileDir.mkdirs(); + fileDir.createNewFile(); + + File fileOrDir = new File("/tmp/1/*"); + try { + FileUtil.recursiveDelete(fileOrDir); + File file = new File("/tmp/1/2.bc"); + assertTrue(!file.exists()); + } catch (Exception e) { + // fail("Unable to delete the file/folders recursively."); + } + } + + /** + * Run the void renameFile(String,String) method test. + * + * @throws Exception + * + * @generatedBy CodePro at 9/29/11 2:39 PM + */ + @Test + public void testRenameFile_1() + throws Exception { + String fromPath = "/tmp/test.txt"; + String toPath = "~/abc.txt"; + new File(fromPath).createNewFile(); + + try { + FileUtil.renameFile(fromPath, toPath); + assertTrue(!new File(fromPath).exists()); + assertTrue(new File(toPath).exists()); + } catch (Exception e) { + // Nothing to do + } + } + + /** + * 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 = "/tmp/test.txt"; + String toPath = "/tmp/abc.txt"; + new File(fromPath).createNewFile(); + + try { + FileUtil.renameFile(fromPath, toPath); + assertTrue(!new File(fromPath).exists()); + assertTrue(new File(toPath).exists()); + } catch (Exception e) { + // Nothing to do + } + } + + /** + * 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 { + // 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/29/11 2:39 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/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 |