diff --git a/README.md b/README.md index 15136365a5f4a4b7311e53219af9d2b9dcf89c88..250dfe59c53fed0ab779ec57e1b40a195d8cd4b2 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ > - AES解密 *decryptAES* > - **文件相关→[FileUtils.java][file.java]→[Test][file.test]** +> - 关闭IO *closeIO* > - 根据文件路径获取文件 *getFileByPath* > - 判断文件是否存在 *isFileExists* > - 判断是否是目录 *isDir* @@ -214,7 +215,7 @@ *** Gradle: ``` groovy -compile 'com.blankj:utilcode:1.1.1' +compile 'com.blankj:utilcode:1.1.2' ``` ### Proguard diff --git a/update_log.md b/update_log.md index a1c16fca496c32d3e3aed7f83cd12df5a16f3171..61b090c43900078e83d8fec01432227ea62463d8 100644 --- a/update_log.md +++ b/update_log.md @@ -1,21 +1,22 @@ -### 更新Log -#### 16/08/19 更新目录,继续完善FileUtils -#### 16/08/19 继续完善FileUtils及单元测试 -#### 16/08/18 完善FileUtils及单元测试,完善ImageUtils -#### 16/08/17 完善FileUtils -#### 16/08/16 新增StringUtils及单元测试,完善正则工具类,版本更新1.1.0 -#### 16/08/15 新增3DES和AES加密及单元检测,加密解密工具类基本完善,目录更新 -#### 16/08/14 新增DES加密及单元检测 -#### 16/08/13 新增MD2,SHA224,SHA256,SHA384,SHA512加密及单元测试,正折腾DES加密 -#### 16/08/12 新增Base64和Html编码解码及他们的单元测试,新增TimeUtils单元测试,更新md -#### 16/08/11 新增SDCardUtils,UnitUtils,单元测试慢慢完善中 -#### 16/08/09 目录排版更新,新增Download,Proguard和License -#### 16/08/08 新增Shell工具类,已传jcenter()遇到好多坑,javaDoc惹的祸,注释一定要规范 -#### 16/08/07 新增6.0获取Mac地址方法,新增对HTML转义,新增编码解码工具类,新增SP工具类 -#### 16/08/06 重命名包名,新增加密相关的单元测试,MD5加密新增文件加密重载 -#### 16/08/05 加密新增MD5盐加密,完善NetworkUtils,新增判断状态栏是否存在(在此感谢tiandawu) -#### 16/08/04 新增时间工具类(在此感谢yi520000给的补充),手机正则分简单和精确(在此感谢MIkeeJY),新增判断是否锁屏,注释分段落,目录按首字母排序 -#### 16/08/03 修复在onCreate中获取view尺寸的bug,MD5和SHA的Bug修复完成(在此感谢ssyijiu) -#### 16/08/02 wifi设置界面bug修复,注释排版还在修改,获取mac地址增加判空,新增QQ群:74721490,欢迎加入,新增隐藏状态栏,注释更加全面,工具类已封装,写的时候真的是一个一个测试过去的,宝宝心里苦 -#### 16/08/01 新增获取SD卡路径,手机和设备进行分类,代码bug修改部分,小修排版,正在封装类,新增目录中显示方法名,新增获取当前App版本Code +### 更新Log +#### 16/08/21 FileUtils单元测试完毕,修复FileUtils的bug,发布版本1.1.2 +#### 16/08/20 更新目录,继续完善FileUtils单元测试,发布版本1.1.1 +#### 16/08/19 继续完善FileUtils及单元测试,及其他小修小补(在此感谢vpop的三次Pr) +#### 16/08/18 完善FileUtils及单元测试,完善ImageUtils +#### 16/08/17 完善FileUtils +#### 16/08/16 新增StringUtils及单元测试,完善正则工具类,版本更新1.1.0 +#### 16/08/15 新增3DES和AES加密及单元检测,加密解密工具类基本完善,目录更新 +#### 16/08/14 新增DES加密及单元检测 +#### 16/08/13 新增MD2,SHA224,SHA256,SHA384,SHA512加密及单元测试,正折腾DES加密 +#### 16/08/12 新增Base64和Html编码解码及他们的单元测试,新增TimeUtils单元测试,更新md +#### 16/08/11 新增SDCardUtils,UnitUtils,单元测试慢慢完善中 +#### 16/08/09 目录排版更新,新增Download,Proguard和License +#### 16/08/08 新增Shell工具类,已传jcenter()遇到好多坑,javaDoc惹的祸,注释一定要规范 +#### 16/08/07 新增6.0获取Mac地址方法,新增对HTML转义,新增编码解码工具类,新增SP工具类 +#### 16/08/06 重命名包名,新增加密相关的单元测试,MD5加密新增文件加密重载 +#### 16/08/05 加密新增MD5盐加密,完善NetworkUtils,新增判断状态栏是否存在(在此感谢tiandawu) +#### 16/08/04 新增时间工具类(在此感谢yi520000给的补充),手机正则分简单和精确(在此感谢MIkeeJY),新增判断是否锁屏,注释分段落,目录按首字母排序 +#### 16/08/03 修复在onCreate中获取view尺寸的bug,MD5和SHA的Bug修复完成(在此感谢ssyijiu) +#### 16/08/02 wifi设置界面bug修复,注释排版还在修改,获取mac地址增加判空,新增QQ群:74721490,欢迎加入,新增隐藏状态栏,注释更加全面,工具类已封装,写的时候真的是一个一个测试过去的,宝宝心里苦 +#### 16/08/01 新增获取SD卡路径,手机和设备进行分类,代码bug修改部分,小修排版,正在封装类,新增目录中显示方法名,新增获取当前App版本Code #### 16/07/31 新增点击屏幕空白区域隐藏软键盘,未能成功增加本页目录跳转功能(不支持) \ No newline at end of file diff --git a/utilcode/build.gradle b/utilcode/build.gradle index d686a95dc8d48c0b5ec06b76e157894c0c492856..68d63f0ac6c8cb7e7026c2288d131ca957f7e101 100644 --- a/utilcode/build.gradle +++ b/utilcode/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 11 targetSdkVersion 23 - versionCode 3 - versionName "1.1.1" + versionCode 4 + versionName "1.1.2" } buildTypes { release { diff --git a/utilcode/proguard-rules.pro b/utilcode/proguard-rules.pro index 81ee4c9153116707bd99a89d5fc9a586a06cb21f..c01a719c871b2a01028aee4bc9647bd339c733d9 100644 --- a/utilcode/proguard-rules.pro +++ b/utilcode/proguard-rules.pro @@ -15,3 +15,7 @@ #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} + +#-keep class com.blankj.utilcode.** { *; } +#-keepclassmembers class com.blankj.utilcode.** { *; } +#-dontwarn com.blankj.utilcode.** \ No newline at end of file diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java index a604c8a1e13c157bc8cc81f3856a88a18acd785e..b219766482a7ca38848276ef8efe70a3e33b27a7 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java @@ -34,9 +34,11 @@ public class FileUtils { } /** + * 关闭IO + * * @param closeable closeable */ - private static void closeIO(Closeable closeable) { + public static void closeIO(Closeable closeable) { if (closeable == null) return; try { closeable.close(); @@ -132,7 +134,8 @@ public class FileUtils { * @return {@code true}: 存在或创建成功
{@code false}: 不存在或创建失败 */ public static boolean createOrExistsDir(File file) { - return file != null && (file.exists() && file.isDirectory() || file.mkdirs()); + // 如果存在,是目录则返回true,是文件则返回false,不存在则返回是否创建成功 + return file != null && (file.exists() ? file.isDirectory() : file.mkdirs()); } /** @@ -153,7 +156,8 @@ public class FileUtils { */ public static boolean createOrExistsFile(File file) { if (file == null) return false; - if (file.exists() && file.isFile()) return true; + // 如果存在,是文件则返回true,是目录则返回false + if (file.exists()) return file.isFile(); if (!createOrExistsDir(file.getParentFile())) return false; try { return file.createNewFile(); @@ -215,22 +219,29 @@ public class FileUtils { */ private static boolean copyOrMoveDir(File srcDir, File destDir, boolean isMove) { if (srcDir == null || destDir == null) return false; + // 如果目标目录在源目录中则返回false,看不懂的话好好想想递归怎么结束 + // srcPath : F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res + // destPath: F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res1 + // 为防止以上这种情况出现出现误判,须分别在后面加个路径分隔符 + String srcPath = srcDir.getPath() + File.separator; + String destPath = destDir.getPath() + File.separator; + if (destPath.contains(srcPath)) return false; // 源文件不存在或者不是目录则返回false if (!srcDir.exists() || !srcDir.isDirectory()) return false; // 目标目录不存在返回false if (!createOrExistsDir(destDir)) return false; - File[] srcDirFiles = srcDir.listFiles(); - for (File srcDirFile : srcDirFiles) { - File oneDestFile = new File(destDir.getPath() + File.separator - + srcDirFile.getName()); - if (srcDirFile.isFile()) { - copyOrMoveFile(srcDirFile, oneDestFile, isMove); - } else if (srcDirFile.isDirectory()) { - copyOrMoveDir(srcDirFile, oneDestFile, isMove); + File[] files = srcDir.listFiles(); + for (File file : files) { + File oneDestFile = new File(destPath + file.getName()); + if (file.isFile()) { + // 如果操作失败返回false + if (!copyOrMoveFile(file, oneDestFile, isMove)) return false; + } else if (file.isDirectory()) { + // 如果操作失败返回false + if (!copyOrMoveDir(file, oneDestFile, isMove)) return false; } - if (isMove && !deleteFile(srcDirFile)) return false; } - return true; + return !isMove || deleteDir(srcDir); } /** @@ -262,7 +273,8 @@ public class FileUtils { // 目标目录不存在返回false if (!createOrExistsDir(destFile.getParentFile())) return false; try { - return writeFileFromIS(destFile, new FileInputStream(srcFile), false) && !(isMove && !deleteFile(srcFile)); + return writeFileFromIS(destFile, new FileInputStream(srcFile), false) + && !(isMove && !deleteFile(srcFile)); } catch (FileNotFoundException e) { e.printStackTrace(); return false; @@ -276,8 +288,8 @@ public class FileUtils { * @param destDirPath 目标目录路径 * @return {@code true}: 复制成功
{@code false}: 复制失败 */ - public boolean copyDir(String srcDirPath, String destDirPath) { - return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath)); + public static boolean copyDir(String srcDirPath, String destDirPath) { + return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath)); } /** @@ -296,7 +308,7 @@ public class FileUtils { * * @param srcFilePath 源文件路径 * @param destFilePath 目标文件路径 - * @return {@code true}: 复制拷贝成功
{@code false}: 复制失败 + * @return {@code true}: 复制成功
{@code false}: 复制失败 */ public static boolean copyFile(String srcFilePath, String destFilePath) { return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath)); @@ -320,7 +332,7 @@ public class FileUtils { * @param destDirPath 目标目录路径 * @return {@code true}: 移动成功
{@code false}: 移动失败 */ - public boolean moveDir(String srcDirPath, String destDirPath) { + public static boolean moveDir(String srcDirPath, String destDirPath) { return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath)); } @@ -363,7 +375,7 @@ public class FileUtils { * @param dirPath 目录路径 * @return {@code true}: 删除成功
{@code false}: 删除失败 */ - public boolean deleteDir(String dirPath) { + public static boolean deleteDir(String dirPath) { return deleteDir(getFileByPath(dirPath)); } @@ -373,9 +385,11 @@ public class FileUtils { * @param dir 目录 * @return {@code true}: 删除成功
{@code false}: 删除失败 */ - public boolean deleteDir(File dir) { + public static boolean deleteDir(File dir) { if (dir == null) return false; + // 目录不存在返回true if (!dir.exists()) return true; + // 不是目录返回false if (!dir.isDirectory()) return false; // 现在文件存在且是文件夹 File[] files = dir.listFiles(); @@ -436,7 +450,10 @@ public class FileUtils { try { os = new BufferedOutputStream(new FileOutputStream(file, append)); byte data[] = new byte[KB]; - while (is.read(data) != -1) os.write(data); + int len; + while ((len = is.read(data)) != -1) { + os.write(data, 0, len); + } return true; } catch (IOException e) { e.printStackTrace(); @@ -589,7 +606,8 @@ public class FileUtils { * @param charsetName 编码格式 * @return 包含制定行的list */ - public static List readFile2List(String filePath, int start, int end, String charsetName) { + public static List readFile2List(String filePath, int start, int end, String + charsetName) { return readFile2List(getFileByPath(filePath), start, end, charsetName); } @@ -653,7 +671,12 @@ public class FileUtils { BufferedReader reader = null; try { sb = new StringBuilder(); - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName)); + if (StringUtils.isSpace(charsetName)) { + reader = new BufferedReader(new FileReader(file)); + } else { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), + charsetName)); + } String line; while ((line = reader.readLine()) != null) { sb.append(line); diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java index 2f2cdf0b85ca1991eecf47773bce7895116e3e97..6a863737bb2de0af13e9e0b168529f94deeb8fcb 100644 --- a/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java +++ b/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java @@ -29,8 +29,10 @@ public class EncryptUtilsTest { String blankjSHA1 = "C606ACCB1FEB669E19D080ADDDDBB8E6CDA5F43C"; String blankjSHA224 = "F4C5C0E8CF56CAC4D06DB6B523F67621859A9D79BDA4B2AC03097D5F"; String blankjSHA256 = "8BD80AE90DFBA112786367BEBDDEE60A638EF5B82682EDF8F3D3CA8E6BFEF648"; - String blankjSHA384 = "BF831E5221FC108D6A72ACB888BA3EB0C030A5F01BA2F739856BE70681D86F992B85E0D461101C74BAEDA895BD422557"; - String blankjSHA512 = "D59D31067F614ED3586F85A31FEFDB7F33096316DA26EBE0FF440B241C8560D96650F100D78C512560C976949EFA89CB5D5589DCF68C7FAADE98F03BCFEC2B45"; + String blankjSHA384 = + "BF831E5221FC108D6A72ACB888BA3EB0C030A5F01BA2F739856BE70681D86F992B85E0D461101C74BAEDA895BD422557"; + String blankjSHA512 = + "D59D31067F614ED3586F85A31FEFDB7F33096316DA26EBE0FF440B241C8560D96650F100D78C512560C976949EFA89CB5D5589DCF68C7FAADE98F03BCFEC2B45"; @Test public void testEncryptMD2() throws Exception { @@ -92,14 +94,16 @@ public class EncryptUtilsTest { public void testEncryptDES() throws Exception { assertThat(encryptDES(bytesDataDES, bytesKeyDES)).isEqualTo(bytesResDES); assertThat(encryptDES2HexString(bytesDataDES, bytesKeyDES)).isEqualTo(resDES); - assertThat(encryptDES2Base64(bytesDataDES, bytesKeyDES)).isEqualTo(base64Encode(bytesResDES)); + assertThat(encryptDES2Base64(bytesDataDES, bytesKeyDES)).isEqualTo(base64Encode + (bytesResDES)); } @Test public void testDecryptDES() throws Exception { assertThat(decryptDES(bytesResDES, bytesKeyDES)).isEqualTo(bytesDataDES); assertThat(decryptHexStringDES(resDES, bytesKeyDES)).isEqualTo(bytesDataDES); - assertThat(decryptBase64DES(base64Encode(bytesResDES), bytesKeyDES)).isEqualTo(bytesDataDES); + assertThat(decryptBase64DES(base64Encode(bytesResDES), bytesKeyDES)).isEqualTo + (bytesDataDES); } String data3DES = "1111111111111111"; @@ -113,14 +117,16 @@ public class EncryptUtilsTest { public void testEncrypt3DES() throws Exception { assertThat(encrypt3DES(bytesDataDES3, bytesKeyDES3)).isEqualTo(bytesResDES3); assertThat(encrypt3DES2HexString(bytesDataDES3, bytesKeyDES3)).isEqualTo(res3DES); - assertThat(encrypt3DES2Base64(bytesDataDES3, bytesKeyDES3)).isEqualTo(base64Encode(bytesResDES3)); + assertThat(encrypt3DES2Base64(bytesDataDES3, bytesKeyDES3)).isEqualTo(base64Encode + (bytesResDES3)); } @Test public void testDecrypt3DES() throws Exception { assertThat(decrypt3DES(bytesResDES3, bytesKeyDES3)).isEqualTo(bytesDataDES3); assertThat(decryptHexString3DES(res3DES, bytesKeyDES3)).isEqualTo(bytesDataDES3); - assertThat(decryptBase64_3DES(base64Encode(bytesResDES3), bytesKeyDES3)).isEqualTo(bytesDataDES3); + assertThat(decryptBase64_3DES(base64Encode(bytesResDES3), bytesKeyDES3)).isEqualTo + (bytesDataDES3); } String dataAES = "11111111111111111111111111111111"; @@ -134,21 +140,23 @@ public class EncryptUtilsTest { public void testEncryptAES() throws Exception { assertThat(encryptAES(bytesDataAES, bytesKeyAES)).isEqualTo(bytesResAES); assertThat(encryptAES2HexString(bytesDataAES, bytesKeyAES)).isEqualTo(resAES); - assertThat(encryptAES2Base64(bytesDataAES, bytesKeyAES)).isEqualTo(base64Encode(bytesResAES)); + assertThat(encryptAES2Base64(bytesDataAES, bytesKeyAES)).isEqualTo(base64Encode + (bytesResAES)); } @Test public void testDecryptAES() throws Exception { assertThat(decryptAES(bytesResAES, bytesKeyAES)).isEqualTo(bytesDataAES); assertThat(decryptHexStringAES(resAES, bytesKeyAES)).isEqualTo(bytesDataAES); - assertThat(decryptBase64AES(base64Encode(bytesResAES), bytesKeyAES)).isEqualTo(bytesDataAES); + assertThat(decryptBase64AES(base64Encode(bytesResAES), bytesKeyAES)).isEqualTo + (bytesDataAES); } - String path = System.getProperty("user.dir") + "\\src\\test\\res\\"; + String path = TestUtils.BASEPATH + "encrypt" + TestUtils.SEP; String md5 = "7F138A09169B250E9DCB378140907378"; @Test public void testEncryptMD5File() throws Exception { - assertThat(encryptMD5File2String(new File(path + File.separator + "MD5.txt"))).isEqualTo(md5); + assertThat(encryptMD5File2String(new File(path + "MD5.txt"))).isEqualTo(md5); } } \ No newline at end of file diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java index d14fd7e6a9632b2cfeccfb004cffe3704df05132..97d93666762e9c8a66883910abebaf7f04134291 100644 --- a/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java +++ b/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java @@ -1,55 +1,181 @@ -package com.blankj.utilcode.utils; - -import org.junit.Test; - -import static com.blankj.utilcode.utils.FileUtils.*; -import static com.google.common.truth.Truth.assertThat; - -/** - *
- *     author: Blankj
- *     blog  : http://blankj.com
- *     time  : 2016/8/19
- *     desc  : FileUtils单元测试
- * 
- */ -public class FileUtilsTest { - - String path = System.getProperty("user.dir") + "\\src\\test\\res\\"; - - @Test - public void testIsFileExists() throws Exception { - assertThat(isFileExists(path + "UTF8.txt")).isTrue(); - assertThat(isFileExists(path + "UTF8")).isFalse(); - } - - @Test - public void testIsDirectory() throws Exception { - assertThat(isDir(path + "UTF8.txt")).isFalse(); - assertThat(isDir(path)).isTrue(); - } - - @Test - public void testIsFile() throws Exception { - assertThat(isFile(path + "UTF8.txt")).isTrue(); - assertThat(isFile(path)).isFalse(); - } - - @Test - public void testCreateOrExistsDir() throws Exception { - assertThat(createOrExistsDir(path + "new Dir")).isTrue(); - assertThat(createOrExistsDir(path)).isTrue(); - } - - @Test - public void testCreateOrExistsFile() throws Exception { - assertThat(createOrExistsFile(path + "new Dir")).isTrue(); - assertThat(createOrExistsFile(path)).isFalse(); - } - - @Test - public void testCreateFileByDeleteOldFile() throws Exception { - assertThat(createFileByDeleteOldFile(path + "new Dir")).isTrue(); - assertThat(createFileByDeleteOldFile(path)).isFalse(); - } +package com.blankj.utilcode.utils; + +import org.junit.Test; + +import java.io.FileInputStream; + +import static com.blankj.utilcode.utils.FileUtils.*; +import static com.blankj.utilcode.utils.TestUtils.BASEPATH; +import static com.blankj.utilcode.utils.TestUtils.SEP; +import static com.google.common.truth.Truth.assertThat; + +/** + *
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 2016/8/19
+ *     desc  : FileUtils单元测试
+ * 
+ */ +public class FileUtilsTest { + + + String path = BASEPATH + "file" + SEP; + + @Test + public void testGetFileByPath() throws Exception { + assertThat(getFileByPath(" ")).isNull(); + assertThat(getFileByPath("c:")).isNotNull(); + } + + @Test + public void testIsFileExists() throws Exception { + assertThat(isFileExists(path + "UTF8.txt")).isTrue(); + assertThat(isFileExists(path + "UTF8")).isFalse(); + } + + @Test + public void testIsDir() throws Exception { + assertThat(isDir(path + "UTF8.txt")).isFalse(); + assertThat(isDir(path)).isTrue(); + } + + @Test + public void testIsFile() throws Exception { + assertThat(isFile(path + "UTF8.txt")).isTrue(); + assertThat(isFile(path)).isFalse(); + } + + @Test + public void testCreateOrExistsDir() throws Exception { + assertThat(createOrExistsDir(path + "new Dir")).isTrue(); + assertThat(createOrExistsDir(path)).isTrue(); + } + + @Test + public void testCreateOrExistsFile() throws Exception { + assertThat(createOrExistsFile(path + "new File")).isTrue(); + assertThat(createOrExistsFile(path)).isFalse(); + } + + @Test + public void testCreateFileByDeleteOldFile() throws Exception { + assertThat(createFileByDeleteOldFile(path + "new File")).isTrue(); + assertThat(createFileByDeleteOldFile(path)).isFalse(); + } + + String path1 = BASEPATH + "file1" + SEP; + + @Test + public void testCopyDir() throws Exception { + assertThat(copyDir(path, path)).isFalse(); + assertThat(copyDir(path, path + "new Dir")).isFalse(); + assertThat(copyDir(path, path1)).isTrue(); + } + + @Test + public void testCopyFile() throws Exception { + assertThat(copyFile(path + "GBK.txt", path + "GBK.txt")).isFalse(); + assertThat(copyFile(path + "GBK.txt", path + "new Dir" + SEP + "GBK.txt")).isTrue(); + assertThat(copyFile(path + "GBK.txt", path1 + "GBK.txt")).isTrue(); + } + + @Test + public void testMoveDir() throws Exception { + assertThat(moveDir(path, path)).isFalse(); + assertThat(moveDir(path, path + "new Dir")).isFalse(); + assertThat(moveDir(path, path1)).isTrue(); + assertThat(moveDir(path1, path)).isTrue(); + } + + @Test + public void testMoveFile() throws Exception { + assertThat(moveFile(path + "GBK.txt", path + "GBK.txt")).isFalse(); + assertThat(moveFile(path + "GBK.txt", path1 + "GBK.txt")).isTrue(); + assertThat(moveFile(path1 + "GBK.txt", path + "GBK.txt")).isTrue(); + } + + @Test + public void testDeleteDir() throws Exception { + assertThat(deleteDir(path + "GBK.txt")).isFalse(); + assertThat(deleteDir(path + "del")).isTrue(); + } + + @Test + public void testDeleteFile() throws Exception { + assertThat(deleteFile(path)).isFalse(); + assertThat(deleteFile(path + "GBK1.txt")).isTrue(); + assertThat(deleteFile(path + "del.txt")).isTrue(); + } + + @Test + public void testWriteFileFromIS() throws Exception { + assertThat(writeFileFromIS(path + "NEW.txt", new FileInputStream(path + "UTF8.txt"), false)) + .isTrue(); + assertThat(writeFileFromIS(path + "NEW.txt", new FileInputStream(path + "UTF8.txt"), true)) + .isTrue(); + } + + @Test + public void testWriteFileFromString() throws Exception { + assertThat(writeFileFromString(path + "NEW.txt", "这是新的", false)).isTrue(); + assertThat(writeFileFromString(path + "NEW.txt", "\r\n这是追加的", true)).isTrue(); + } + + @Test + public void testGetFileCharsetSimple() throws Exception { + assertThat(getFileCharsetSimple(path + "GBK.txt")).isEqualTo("GBK"); + assertThat(getFileCharsetSimple(path + "Unicode.txt")).isEqualTo("Unicode"); + assertThat(getFileCharsetSimple(path + "UTF8.txt")).isEqualTo("UTF-8"); + assertThat(getFileCharsetSimple(path + "UTF16BE.txt")).isEqualTo("UTF-16BE"); + } + + @Test + public void testGetFileLines() throws Exception { + assertThat(getFileLines(path + "UTF8.txt")).isEqualTo(7); + } + + @Test + public void testReadFile2List() throws Exception { + System.out.println(readFile2List(path + "UTF8.txt", "").toString()); + System.out.println(readFile2List(path + "UTF8.txt", "UTF-8").toString()); + System.out.println(readFile2List(path + "UTF8.txt", "GBK").toString()); + } + + @Test + public void testReadFile2SB() throws Exception { + System.out.println(readFile2SB(path + "UTF8.txt", "").toString()); + System.out.println(readFile2SB(path + "UTF8.txt", "UTF-8").toString()); + System.out.println(readFile2SB(path + "UTF8.txt", "GBK").toString()); + } + + @Test + public void testByte2Unit() throws Exception { + assertThat(byte2Unit(ConstUtils.GB, ConstUtils.MB) - 1024).isWithin(0.001); + } + + @Test + public void testGetFileSize() throws Exception { + assertThat(getFileSize(path + "UTF8.txt", ConstUtils.BYTE) - 25).isWithin(0.001); + } + + @Test + public void testGetDirName() throws Exception { + assertThat(getDirName(path + "UTF8.txt")).isEqualTo(path); + } + + @Test + public void testGetFileName() throws Exception { + assertThat(getFileName(path + "UTF8.txt")).isEqualTo("UTF8.txt"); + } + + @Test + public void testGetFileNameNoExtension() throws Exception { + assertThat(getFileNameNoExtension(path + "UTF8.txt")).isEqualTo("UTF8"); + } + + @Test + public void testGetFileExtension() throws Exception { + assertThat(getFileExtension(path + "UTF8.txt")).isEqualTo("txt"); + } } \ No newline at end of file diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java b/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..dcde22e7bc33d6a13c7ae1dc5aefc1f98e5949b2 --- /dev/null +++ b/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java @@ -0,0 +1,23 @@ +package com.blankj.utilcode.utils; + +import java.io.File; + +/** + *
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 2016/8/21
+ *     desc  : 单元测试工具类
+ * 
+ */ +public class TestUtils { + + private TestUtils() { + throw new UnsupportedOperationException("u can't fuck me..."); + } + + public static final char SEP = File.separatorChar; + + public static final String BASEPATH = System.getProperty("user.dir") + + SEP + "src" + SEP + "test" + SEP + "res" + SEP; +} diff --git a/utilcode/src/test/res/MD5.txt b/utilcode/src/test/res/encrypt/MD5.txt similarity index 100% rename from utilcode/src/test/res/MD5.txt rename to utilcode/src/test/res/encrypt/MD5.txt diff --git a/utilcode/src/test/res/GBK.txt b/utilcode/src/test/res/file/GBK.txt similarity index 100% rename from utilcode/src/test/res/GBK.txt rename to utilcode/src/test/res/file/GBK.txt diff --git a/utilcode/src/test/res/UTF16BE.txt b/utilcode/src/test/res/file/UTF16BE.txt similarity index 100% rename from utilcode/src/test/res/UTF16BE.txt rename to utilcode/src/test/res/file/UTF16BE.txt diff --git a/utilcode/src/test/res/UTF8.txt b/utilcode/src/test/res/file/UTF8.txt similarity index 100% rename from utilcode/src/test/res/UTF8.txt rename to utilcode/src/test/res/file/UTF8.txt diff --git a/utilcode/src/test/res/Unicode.txt b/utilcode/src/test/res/file/Unicode.txt similarity index 100% rename from utilcode/src/test/res/Unicode.txt rename to utilcode/src/test/res/file/Unicode.txt diff --git a/utilcode/src/test/res/new Dir b/utilcode/src/test/res/new Dir deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/utilcode/utilcode.iml b/utilcode/utilcode.iml index cb03c4980a69c6e8a12821817a8a7c631b0927b1..01c5fe7a6c51772f4c58644cfad729da68f11533 100644 --- a/utilcode/utilcode.iml +++ b/utilcode/utilcode.iml @@ -65,14 +65,6 @@ - - - - - - - - @@ -81,6 +73,14 @@ + + + + + + + +