diff --git a/pom.xml b/pom.xml index 1297337..7485dc4 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,21 @@ 1.6 + + + + org.slf4j + slf4j-log4j12 + 1.7.26 + + + junit + junit + 4.12 + test + + + diff --git a/src/test/java/org/csource/fastdfs/FdfsTest.java b/src/test/java/org/csource/fastdfs/FdfsTest.java new file mode 100644 index 0000000..fc3eea8 --- /dev/null +++ b/src/test/java/org/csource/fastdfs/FdfsTest.java @@ -0,0 +1,99 @@ +package org.csource.fastdfs; + +import org.csource.common.NameValuePair; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.Arrays; + +/** + * @author chengdu + * @date 2019/7/13. + */ +public class FdfsTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(FdfsTest.class); + + private static final String CONF_NAME = "fdfstest.conf"; + + private StorageClient storageClient; + + private TrackerServer trackerServer; + + @Before + public void initStorageClient() throws Exception { + ClientGlobal.init(CONF_NAME); + LOGGER.info("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + LOGGER.info("charset=" + ClientGlobal.g_charset); + TrackerClient tracker = new TrackerClient(); + trackerServer = tracker.getConnection(); + StorageServer storageServer = null; + storageClient = new StorageClient(trackerServer, storageServer); + } + + @After + public void closeClient() { + LOGGER.info("close connection"); + if(trackerServer != null){ + try { + trackerServer.close(); + trackerServer.finalize(); + }catch (Exception e){ + e.printStackTrace(); + }catch (Throwable e){ + e.printStackTrace(); + } + } + } + + public void writeByteToFile(byte[] fbyte, String fileName) throws IOException { + BufferedOutputStream bos = null; + FileOutputStream fos = null; + File file = new File(fileName); + try { + fos = new FileOutputStream(file); + bos = new BufferedOutputStream(fos); + bos.write(fbyte); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (bos != null) { + bos.close(); + } + if (fos != null) { + fos.close(); + } + } + } + + @Test + public void upload() throws Exception{ + NameValuePair[] metaList = new NameValuePair[1]; + String local_filename = "build.PNG"; + metaList[0] = new NameValuePair("fileName", local_filename); + File file = new File("C:/Users/chengdu/Desktop/build.PNG"); + InputStream inputStream = new FileInputStream(file); + int length = inputStream.available(); + byte[] bytes = new byte[length]; + inputStream.read(bytes); + String[] result = storageClient.upload_file(bytes, null, metaList); + LOGGER.info("result {}", Arrays.asList(result)); + Assert.assertEquals(2, result.length); + } + + @Test + public void download() throws Exception { + String[] uploadresult = {"group1", "M00/00/00/wKgBZV0phl2ASV1nAACk1tFxwrM3814331"}; + byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]); + String local_filename = "build.PNG"; + writeByteToFile(result, local_filename); + File file = new File(local_filename); + Assert.assertTrue(file.isFile()); + } + +} diff --git a/src/test/resources/fdfs_client.conf b/src/test/resources/fdfs_client.conf index deb3ff5..2f3b351 100644 --- a/src/test/resources/fdfs_client.conf +++ b/src/test/resources/fdfs_client.conf @@ -5,4 +5,5 @@ http.tracker_http_port = 8080 http.anti_steal_token = no http.secret_key = FastDFS1234567890 -tracker_server = 192.168.1.101:22122 +tracker_server = 10.0.11.245:22122 +tracker_server = 10.0.11.246:22122 diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties new file mode 100644 index 0000000..dbcafc8 --- /dev/null +++ b/src/test/resources/log4j.properties @@ -0,0 +1,12 @@ +log4j.rootLogger=INFO, Console ,File + +#Console +log4j.appender.Console=org.apache.log4j.ConsoleAppender +log4j.appender.Console.layout=org.apache.log4j.PatternLayout +log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n + +#File +log4j.appender.File = org.apache.log4j.FileAppender +log4j.appender.File.File = log4j2.log +log4j.appender.File.layout = org.apache.log4j.PatternLayout +log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n \ No newline at end of file