From b5a7b00729921930701b24204f95439a685730d4 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 9 Aug 2019 17:27:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E7=8E=AF=E5=A2=83API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fanruan/api/env/EnvKit.java | 15 ++++++++ .../java/com/fanruan/api/env/EnvKitTest.java | 35 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/test/java/com/fanruan/api/env/EnvKitTest.java diff --git a/src/main/java/com/fanruan/api/env/EnvKit.java b/src/main/java/com/fanruan/api/env/EnvKit.java index d0d9119..c745880 100644 --- a/src/main/java/com/fanruan/api/env/EnvKit.java +++ b/src/main/java/com/fanruan/api/env/EnvKit.java @@ -1,8 +1,11 @@ package com.fanruan.api.env; +import com.fr.stable.project.ProjectConstants; import com.fr.workspace.WorkContext; import com.fr.workspace.Workspace; import com.fr.workspace.resource.WorkResource; +import com.fr.workspace.simple.SimpleWork; +import org.jetbrains.annotations.NotNull; /** * @author richie @@ -26,4 +29,16 @@ public class EnvKit { public static WorkResource getWorkResource() { return WorkContext.getWorkResource(); } + + /** + * 设置一个目录为工作目录 + * @param path 工作目录位置 + */ + public static void setCurrent(@NotNull String path) { + if (path.endsWith(ProjectConstants.WEBINF_NAME)) { + SimpleWork.checkIn(path); + } else { + throw new IllegalArgumentException("Cannot set " + path + " as work directory."); + } + } } diff --git a/src/test/java/com/fanruan/api/env/EnvKitTest.java b/src/test/java/com/fanruan/api/env/EnvKitTest.java new file mode 100644 index 0000000..a8ee617 --- /dev/null +++ b/src/test/java/com/fanruan/api/env/EnvKitTest.java @@ -0,0 +1,35 @@ +package com.fanruan.api.env; + +import com.fanruan.api.Prepare; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-09 + */ +public class EnvKitTest extends Prepare { + + @Test + public void getWorkResource() { + String path = "usr/shared/webroot/WEB-INF"; + EnvKit.setCurrent(path); + boolean r = EnvKit.getWorkResource().exist("a/b.txt"); + Assert.assertFalse(r); + } + + @Test + public void getAndSetCurrent() { + String path = "/usr/shared/webroot"; + try { + EnvKit.setCurrent(path); + Assert.fail(); + } catch (IllegalArgumentException e) { + Assert.assertTrue(true); + } + path = "usr/shared/webroot/WEB-INF"; + EnvKit.setCurrent(path); + Assert.assertEquals(EnvKit.getCurrent().getPath(), path); + } +} \ No newline at end of file