From 615c5a8f42805c43ded62c37e5b702d8b81cc1c5 Mon Sep 17 00:00:00 2001 From: ddaa2000 <2470872965@qq.com> Date: Sun, 12 Apr 2020 18:07:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86AppendixFileNotFoundException=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=BA=86IOException=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=BA=9B=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/component/activity/FileSystem.java | 2 +- .../AppendixFileNotFoundException.java | 4 +- .../codesdream/ase/test/FileSystemTest.java | 40 ++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/codesdream/ase/component/activity/FileSystem.java b/src/main/java/com/codesdream/ase/component/activity/FileSystem.java index 659437d..f0fab48 100644 --- a/src/main/java/com/codesdream/ase/component/activity/FileSystem.java +++ b/src/main/java/com/codesdream/ase/component/activity/FileSystem.java @@ -137,7 +137,7 @@ public class FileSystem { * 根据id获取一个文件的条目,其中包含文件信息 * @param id 要寻找条目的id */ - public AppendixFile getFileData(int id) + public AppendixFile getFileData(int id)throws AppendixFileNotFoundException { Optional optionalAppendixFile = appendixFileService.findById(id); if(!optionalAppendixFile.isPresent()) diff --git a/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java b/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java index 03f4784..0025243 100644 --- a/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java +++ b/src/main/java/com/codesdream/ase/exception/notfound/AppendixFileNotFoundException.java @@ -2,7 +2,9 @@ package com.codesdream.ase.exception.notfound; import com.codesdream.ase.model.activity.AppendixFile; -public class AppendixFileNotFoundException extends NotFoundException { +import java.io.IOException; + +public class AppendixFileNotFoundException extends IOException { public int id; public int type; public static final int ID_NOT_FOUND = 1, FILE_NOT_fOUND = 2, STREAM_FAILURE = 3; diff --git a/src/test/java/com/codesdream/ase/test/FileSystemTest.java b/src/test/java/com/codesdream/ase/test/FileSystemTest.java index c80f8d9..01e66d6 100644 --- a/src/test/java/com/codesdream/ase/test/FileSystemTest.java +++ b/src/test/java/com/codesdream/ase/test/FileSystemTest.java @@ -1,6 +1,7 @@ package com.codesdream.ase.test; import com.codesdream.ase.component.activity.FileSystem; +import com.codesdream.ase.exception.notfound.AppendixFileNotFoundException; import com.codesdream.ase.model.activity.AppendixFile; import com.codesdream.ase.repository.activity.AppendixFileRespository; import com.codesdream.ase.service.AppendixFileService; @@ -47,6 +48,38 @@ public class FileSystemTest { } } + @Test + public void wrongIDTest() + { + int id = 1; + try { + InputStream inputStream = fileSystem.getFile(id); + } catch (AppendixFileNotFoundException e) { + System.out.println(e.getLocalizedMessage()); + } + } + + @Test + public void fileNotExistErrorTest() + { + int id = 268; + try { + InputStream inputStream = fileSystem.getFile(id); + } catch (AppendixFileNotFoundException e) { + System.out.println(e.getLocalizedMessage()); + } + } + + @Test + public void refreshDataBaseTest() + { + fileSystem.databaseRefresh(); + } + @Test + public void refreshDiskTest() + { + fileSystem.diskRefresh(); + } @Test public void createFile() { @@ -63,7 +96,12 @@ public class FileSystemTest { } - InputStream inputStream = fileSystem.getFile(id1); + InputStream inputStream = null; + try { + inputStream = fileSystem.getFile(id1); + } catch (AppendixFileNotFoundException e) { + e.printStackTrace(); + } Scanner scanner = new Scanner(inputStream, "UTF-8"); String text = scanner.useDelimiter("\\A").next(); System.out.println(text);