ソースを参照

update test case

reghao 4 ヶ月 前
コミット
0092f3f39c
2 ファイル変更72 行追加33 行削除
  1. 0 33
      web/src/test/java/BlogTest.java
  2. 72 0
      web/src/test/java/DateTest.java

+ 0 - 33
web/src/test/java/BlogTest.java

@@ -1,33 +0,0 @@
-import cn.reghao.jutil.jdk.security.RandomString;
-import cn.reghao.bnt.web.WebApplication;
-import cn.reghao.bnt.web.admin.db.repository.UserRepository;
-import cn.reghao.bnt.web.admin.model.po.User;
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-import lombok.extern.slf4j.Slf4j;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.security.crypto.password.PasswordEncoder;
-import org.springframework.test.context.ActiveProfiles;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.context.WebApplicationContext;
-import org.springframework.web.method.HandlerMethod;
-import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
-import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
-import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
-import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
-
-import java.lang.annotation.Annotation;
-import java.util.*;
-import java.util.stream.Collectors;
-
-/**
- * @author reghao
- * @date 2022-08-11 10:14:51
- */
-@Slf4j
-@ActiveProfiles("dev")
-@SpringBootTest(classes = WebApplication.class)
-public class BlogTest {
-}

+ 72 - 0
web/src/test/java/DateTest.java

@@ -1,5 +1,13 @@
+import cn.reghao.bnt.web.util.JarFileResources;
 import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.core.io.ClassPathResource;
 
+import java.io.*;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
 import java.time.DayOfWeek;
 import java.time.Duration;
 import java.time.LocalDateTime;
@@ -20,6 +28,8 @@ public class DateTest {
     String[] shengXiao = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
     String[] jieQi = {"立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑",
             "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至", "小寒", "大寒"};
+
+    @Test
     public void dateTest() {
         LocalDateTime localDateTime = LocalDateTime.now();
         DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
@@ -38,4 +48,66 @@ public class DateTest {
         Duration duration = Duration.between(localDateTime, localDateTime1);
         System.out.println();
     }
+
+
+    // print input stream
+    private static void printInputStream(InputStream is) {
+        try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
+             BufferedReader reader = new BufferedReader(streamReader)) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                System.out.println(line);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Test
+    public void getFileFromJar() throws IOException, URISyntaxException {
+        ClassLoader classLoader = this.getClass().getClassLoader();
+        ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
+        ClassLoader classLoader2 = DateTest.class.getClassLoader();
+
+        String resourcePath = "templates/index.html";
+        URL resource = classLoader.getResource(resourcePath);
+        File file = new File(resource.toURI());
+        InputStream inputStream = classLoader.getResourceAsStream(resourcePath);
+
+        URL resource1 = classLoader1.getResource(resourcePath);
+        URL resource2 = classLoader2.getResource(resourcePath);
+
+        InputStream resourceAsStream = new ClassPathResource(resourcePath).getInputStream();
+
+        
+        /*URL resource = Thread.currentThread().getContextClassLoader().getResource(avatarResource);
+        String avatarPath = resource.getPath();
+        System.out.println(avatarPath);
+        File file = new File(avatarPath);
+        byte[] imageBytes = Files.readAllBytes(file.toPath());*/
+        
+        JarFileResources app = new JarFileResources();
+        // Sample 3 - read all files from a resources folder (jar version)
+        try {
+            // get paths from src/main/resources/json
+            List<Path> result = app.getPathsFromResourceJar("json");
+            for (Path path : result) {
+                System.out.println("Path : " + path);
+
+                String filePathInJar = path.toString();
+                // Windows will returns /json/file1.json, cut the first /
+                // the correct path should be json/file1.json
+                if (filePathInJar.startsWith("/")) {
+                    filePathInJar = filePathInJar.substring(1, filePathInJar.length());
+                }
+
+                System.out.println("filePathInJar : " + filePathInJar);
+                // read a file from resource folder
+                InputStream is = app.getFileFromResourceAsStream(filePathInJar);
+                printInputStream(is);
+            }
+        } catch (URISyntaxException | IOException e) {
+            e.printStackTrace();
+        }
+    }
 }