Repository: alibaba/fescar
Pull request #237 (2019-01-23 06:22:22)

add  unit test for session package
<!-- Please make sure you have read and understood the contributing guidelines -->

### Ⅰ. Describe what this PR did
add  unit test for session package

### Ⅱ. Does this pull request fix one issue?
<!-- If that, add "fixes #xxx" below in the next line, for example, fixes #97. -->


### Ⅲ. Why don't you add test cases (unit test/integration test)? 


### Ⅳ. Describe how to verify it


### Ⅴ. Special notes for reviews




Patch:
diff --git a/pom.xml b/pom.xml
index 24a401967..612e33510 100644
--- a/pom.xml
+++ b/pom.xml
@@ -172,6 +172,11 @@
             <artifactId>junit</artifactId>
             <version>${junit.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <version>6.4</version>
+        </dependency>
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>dubbo</artifactId>
diff --git a/server/pom.xml b/server/pom.xml
index 2be7ee220..ea8953b30 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -39,6 +39,11 @@
             <groupId>commons-lang</groupId>
             <artifactId>commons-lang</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>


Test patch:
diff --git a/server/src/test/java/com/alibaba/fescar/server/session/BranchSessionTest.java b/server/src/test/java/com/alibaba/fescar/server/session/BranchSessionTest.java
new file mode 100644
index 000000000..396057ae5
--- /dev/null
+++ b/server/src/test/java/com/alibaba/fescar/server/session/BranchSessionTest.java
@@ -0,0 +1,62 @@
+/*
+ *  Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package com.alibaba.fescar.server.session;
+
+import com.alibaba.fescar.core.model.BranchType;
+import com.alibaba.fescar.server.UUIDGenerator;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ * @author tianming.xm@gmail.com
+ * @since 2019/1/23
+ */
+public class BranchSessionTest {
+
+    @Test(dataProvider = "branchSessionProvider")
+    public void codecTest(BranchSession branchSession){
+        byte[] result = branchSession.encode();
+        Assert.assertNotNull(result);
+        BranchSession expected = new BranchSession();
+        expected.decode(result);
+        Assert.assertEquals(branchSession.getTransactionId(),expected.getTransactionId());
+        Assert.assertEquals(branchSession.getBranchId(),expected.getBranchId());
+        Assert.assertEquals(branchSession.getResourceId(),expected.getResourceId());
+        Assert.assertEquals(branchSession.getLockKey(),expected.getLockKey());
+        Assert.assertEquals(branchSession.getApplicationId(),expected.getApplicationId());
+        Assert.assertEquals(branchSession.getTxServiceGroup(),expected.getTxServiceGroup());
+        Assert.assertEquals(branchSession.getClientId(),expected.getClientId());
+        Assert.assertEquals(branchSession.getApplicationData(),expected.getApplicationData());
+
+    }
+    @DataProvider
+    public static Object[][] branchSessionProvider() {
+        BranchSession branchSession = new BranchSession();
+        branchSession.setTransactionId(UUIDGenerator.generateUUID());
+        branchSession.setBranchId(1L);
+        branchSession.setClientId("c1");
+        branchSession.setResourceGroupId("my_test_tx_group");
+        branchSession.setResourceId("tb_1");
+        branchSession.setLockKey("t_1");
+        branchSession.setBranchType(BranchType.AT);
+        branchSession.setApplicationId("demo-child-app");
+        branchSession.setTxServiceGroup("my_test_tx_group");
+        branchSession.setApplicationData("{\"data\":\"test\"}");
+        branchSession.setBranchType(BranchType.AT);
+        return new Object[][] {{ branchSession}};
+    }
+}
diff --git a/server/src/test/java/com/alibaba/fescar/server/session/DefaultSessionManagerTest.java b/server/src/test/java/com/alibaba/fescar/server/session/DefaultSessionManagerTest.java
index ec349cd6e..6c26b7f82 100644
--- a/server/src/test/java/com/alibaba/fescar/server/session/DefaultSessionManagerTest.java
+++ b/server/src/test/java/com/alibaba/fescar/server/session/DefaultSessionManagerTest.java
@@ -1,11 +1,28 @@
+/*
+ *  Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
 package com.alibaba.fescar.server.session;
 
 import com.alibaba.fescar.core.model.BranchStatus;
 import com.alibaba.fescar.core.model.BranchType;
 import com.alibaba.fescar.core.model.GlobalStatus;
-import org.junit.Assert;
-import org.junit.Test;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
@@ -13,19 +30,19 @@ import java.util.List;
  * @author tianming.xm@gmail.com
  * @since 2019/1/22
  */
+
 public class DefaultSessionManagerTest {
 
     private SessionManager sessionManager = new DefaultSessionManager("test");
 
-    @Test
-    public void addGlobalSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void addGlobalSessionTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void findGlobalSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void findGlobalSessionTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
         GlobalSession expected =  sessionManager.findGlobalSession(globalSession.getTransactionId());
         Assert.assertNotNull(expected);
@@ -35,22 +52,22 @@ public class DefaultSessionManagerTest {
         Assert.assertEquals(expected.getTransactionName(),globalSession.getTransactionName());
         Assert.assertEquals(expected.getTransactionId(),globalSession.getTransactionId());
         Assert.assertEquals(expected.getStatus(),globalSession.getStatus());
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void updateGlobalSessionStatusTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void updateGlobalSessionStatusTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
         globalSession.setStatus(GlobalStatus.Finished);
         sessionManager.updateGlobalSessionStatus(globalSession,GlobalStatus.Finished);
         GlobalSession expected =  sessionManager.findGlobalSession(globalSession.getTransactionId());
         Assert.assertNotNull(expected);
         Assert.assertEquals(GlobalStatus.Finished,expected.getStatus());
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void removeGlobalSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void removeGlobalSessionTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
         sessionManager.removeGlobalSession(globalSession);
         GlobalSession expected =  sessionManager.findGlobalSession(globalSession.getTransactionId());
@@ -58,159 +75,129 @@ public class DefaultSessionManagerTest {
 
     }
 
-    @Test
-    public void addBranchSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void addBranchSessionTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.addBranchSession(globalSession,branchSession);
+        sessionManager.removeBranchSession(globalSession,branchSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void updateBranchSessionStatusTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void updateBranchSessionStatusTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.addBranchSession(globalSession,branchSession);
         sessionManager.updateBranchSessionStatus(branchSession, BranchStatus.PhaseTwo_Committed);
+        sessionManager.removeBranchSession(globalSession,branchSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void removeBranchSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void removeBranchSessionTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.addBranchSession(globalSession,branchSession);
+        sessionManager.removeBranchSession(globalSession,branchSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void allSessionsTest() throws Exception{
-        GlobalSession globalSession1 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        GlobalSession globalSession2 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        sessionManager.addGlobalSession(globalSession1);
-        sessionManager.addGlobalSession(globalSession2);
-        Collection<GlobalSession> globalSessions =  sessionManager.allSessions();
-        Assert.assertNotNull(globalSessions);
-        Assert.assertEquals(2,globalSessions.size());
-    }
-
-    @Test
-    public void findGlobalSessionsTest() throws Exception{
-        GlobalSession globalSession1 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        GlobalSession globalSession2 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        sessionManager.addGlobalSession(globalSession1);
-        sessionManager.addGlobalSession(globalSession2);
-
+    @Test(dataProvider = "globalSessionsProvider")
+    public void allSessionsTest(List<GlobalSession> globalSessions) throws Exception{
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.addGlobalSession(globalSession);
+        }
+        Collection<GlobalSession> expectedGlobalSessions =  sessionManager.allSessions();
+        Assert.assertNotNull(expectedGlobalSessions);
+        Assert.assertEquals(2,expectedGlobalSessions.size());
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.removeGlobalSession(globalSession);
+        }
+    }
+
+    @Test(dataProvider = "globalSessionsProvider")
+    public void findGlobalSessionsTest(List<GlobalSession> globalSessions) throws Exception{
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.addGlobalSession(globalSession);
+        }
         SessionCondition sessionCondition = new SessionCondition(GlobalStatus.Begin,30*24*3600);
-        List<GlobalSession> globalSessions = sessionManager.findGlobalSessions(sessionCondition);
-        Assert.assertNotNull(globalSessions);
-        Assert.assertEquals(2,globalSessions.size());
+        Collection<GlobalSession> expectedGlobalSessions =  sessionManager.findGlobalSessions(sessionCondition);
+        Assert.assertNotNull(expectedGlobalSessions);
+        Assert.assertEquals(2,expectedGlobalSessions.size());
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.removeGlobalSession(globalSession);
+        }
     }
 
-    @Test
-    public void onBeginTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onBeginTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
+        sessionManager.onEnd(globalSession);
     }
 
-    @Test
-    public void onStatusChangeTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onStatusChangeTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
         sessionManager.onStatusChange(globalSession,GlobalStatus.Finished);
+        sessionManager.onEnd(globalSession);
     }
 
-    @Test
-    public void onBranchStatusChangeTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void onBranchStatusChangeTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.onBegin(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.onAddBranch(globalSession,branchSession);
         sessionManager.onBranchStatusChange(globalSession,branchSession,BranchStatus.PhaseTwo_Committed);
     }
 
-    @Test
-    public void onAddBranchTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void onAddBranchTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.onBegin(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.onAddBranch(globalSession,branchSession);
     }
 
-    @Test
-    public void onRemoveBranchTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void onRemoveBranchTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.onBegin(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.onAddBranch(globalSession,branchSession);
         sessionManager.onRemoveBranch(globalSession,branchSession);
     }
 
-    @Test
-    public void onCloseTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onCloseTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
         sessionManager.onClose(globalSession);
     }
 
-    @Test
-    public void onEndTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onEndTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
         sessionManager.onEnd(globalSession);
     }
+
+    @DataProvider
+    public static Object[][] globalSessionProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        return new Object[][] {{ globalSession}};
+    }
+
+    @DataProvider
+    public static Object[][] globalSessionsProvider() {
+        GlobalSession globalSession1 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        GlobalSession globalSession2 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        return new Object[][] {{Arrays.asList(globalSession1,globalSession2)}};
+    }
+
+    @DataProvider
+    public static Object[][] branchSessionProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        BranchSession branchSession = new BranchSession();
+        branchSession.setTransactionId(globalSession.getTransactionId());
+        branchSession.setBranchId(1L);
+        branchSession.setResourceGroupId("my_test_tx_group");
+        branchSession.setResourceId("tb_1");
+        branchSession.setLockKey("t_1");
+        branchSession.setBranchType(BranchType.AT);
+        branchSession.setApplicationId("demo-child-app");
+        branchSession.setTxServiceGroup("my_test_tx_group");
+        branchSession.setApplicationData("{\"data\":\"test\"}");
+        return new Object[][] {{ globalSession,branchSession}};
+    }
 }
diff --git a/server/src/test/java/com/alibaba/fescar/server/session/FileBasedSessionManagerTest.java b/server/src/test/java/com/alibaba/fescar/server/session/FileBasedSessionManagerTest.java
index b5b5c212d..b42d0f943 100644
--- a/server/src/test/java/com/alibaba/fescar/server/session/FileBasedSessionManagerTest.java
+++ b/server/src/test/java/com/alibaba/fescar/server/session/FileBasedSessionManagerTest.java
@@ -1,37 +1,55 @@
+/*
+ *  Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
 package com.alibaba.fescar.server.session;
 
+
+
 import com.alibaba.fescar.core.model.BranchStatus;
 import com.alibaba.fescar.core.model.BranchType;
 import com.alibaba.fescar.core.model.GlobalStatus;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
 /**
- * @author pingchun@meili-inc.com
+ * @author tianming.xm@gmail.com
  * @since 2019/1/22
  */
 public class FileBasedSessionManagerTest {
 
     private SessionManager sessionManager = null;
 
-    @Before
+    @BeforeClass
     public void setUp() throws Exception{
         sessionManager = new FileBasedSessionManager("root.data",".");
     }
 
-    @Test
-    public void addGlobalSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void addGlobalSessionTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void findGlobalSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void findGlobalSessionTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
         GlobalSession expected =  sessionManager.findGlobalSession(globalSession.getTransactionId());
         Assert.assertNotNull(expected);
@@ -41,22 +59,22 @@ public class FileBasedSessionManagerTest {
         Assert.assertEquals(expected.getTransactionName(),globalSession.getTransactionName());
         Assert.assertEquals(expected.getTransactionId(),globalSession.getTransactionId());
         Assert.assertEquals(expected.getStatus(),globalSession.getStatus());
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void updateGlobalSessionStatusTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void updateGlobalSessionStatusTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
         globalSession.setStatus(GlobalStatus.Finished);
         sessionManager.updateGlobalSessionStatus(globalSession,GlobalStatus.Finished);
         GlobalSession expected =  sessionManager.findGlobalSession(globalSession.getTransactionId());
         Assert.assertNotNull(expected);
         Assert.assertEquals(GlobalStatus.Finished,expected.getStatus());
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void removeGlobalSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void removeGlobalSessionTest(GlobalSession globalSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
         sessionManager.removeGlobalSession(globalSession);
         GlobalSession expected =  sessionManager.findGlobalSession(globalSession.getTransactionId());
@@ -64,159 +82,129 @@ public class FileBasedSessionManagerTest {
 
     }
 
-    @Test
-    public void addBranchSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void addBranchSessionTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.addBranchSession(globalSession,branchSession);
+        sessionManager.removeBranchSession(globalSession,branchSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void updateBranchSessionStatusTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void updateBranchSessionStatusTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.addBranchSession(globalSession,branchSession);
         sessionManager.updateBranchSessionStatus(branchSession, BranchStatus.PhaseTwo_Committed);
+        sessionManager.removeBranchSession(globalSession,branchSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void removeBranchSessionTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void removeBranchSessionTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.addGlobalSession(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.addBranchSession(globalSession,branchSession);
+        sessionManager.removeBranchSession(globalSession,branchSession);
+        sessionManager.removeGlobalSession(globalSession);
     }
 
-    @Test
-    public void allSessionsTest() throws Exception{
-        GlobalSession globalSession1 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        GlobalSession globalSession2 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        sessionManager.addGlobalSession(globalSession1);
-        sessionManager.addGlobalSession(globalSession2);
-        Collection<GlobalSession> globalSessions =  sessionManager.allSessions();
-        Assert.assertNotNull(globalSessions);
-        Assert.assertEquals(2,globalSessions.size());
-    }
-
-    @Test
-    public void findGlobalSessionsTest() throws Exception{
-        GlobalSession globalSession1 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        GlobalSession globalSession2 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
-        sessionManager.addGlobalSession(globalSession1);
-        sessionManager.addGlobalSession(globalSession2);
-
+    @Test(dataProvider = "globalSessionsProvider")
+    public void allSessionsTest(List<GlobalSession> globalSessions) throws Exception{
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.addGlobalSession(globalSession);
+        }
+        Collection<GlobalSession> expectedGlobalSessions =  sessionManager.allSessions();
+        Assert.assertNotNull(expectedGlobalSessions);
+        Assert.assertEquals(2,expectedGlobalSessions.size());
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.removeGlobalSession(globalSession);
+        }
+    }
+
+    @Test(dataProvider = "globalSessionsProvider")
+    public void findGlobalSessionsTest(List<GlobalSession> globalSessions) throws Exception{
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.addGlobalSession(globalSession);
+        }
         SessionCondition sessionCondition = new SessionCondition(GlobalStatus.Begin,30*24*3600);
-        List<GlobalSession> globalSessions = sessionManager.findGlobalSessions(sessionCondition);
-        Assert.assertNotNull(globalSessions);
-        Assert.assertEquals(2,globalSessions.size());
+        Collection<GlobalSession> expectedGlobalSessions =  sessionManager.findGlobalSessions(sessionCondition);
+        Assert.assertNotNull(expectedGlobalSessions);
+        Assert.assertEquals(2,expectedGlobalSessions.size());
+        for(GlobalSession globalSession:globalSessions){
+            sessionManager.removeGlobalSession(globalSession);
+        }
     }
 
-    @Test
-    public void onBeginTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onBeginTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
+        sessionManager.onEnd(globalSession);
     }
 
-    @Test
-    public void onStatusChangeTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onStatusChangeTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
         sessionManager.onStatusChange(globalSession,GlobalStatus.Finished);
+        sessionManager.onEnd(globalSession);
     }
 
-    @Test
-    public void onBranchStatusChangeTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void onBranchStatusChangeTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.onBegin(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.onAddBranch(globalSession,branchSession);
         sessionManager.onBranchStatusChange(globalSession,branchSession,BranchStatus.PhaseTwo_Committed);
     }
 
-    @Test
-    public void onAddBranchTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void onAddBranchTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.onBegin(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.onAddBranch(globalSession,branchSession);
     }
 
-    @Test
-    public void onRemoveBranchTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "branchSessionProvider")
+    public void onRemoveBranchTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
         sessionManager.onBegin(globalSession);
-        BranchSession branchSession = new BranchSession();
-        branchSession.setTransactionId(globalSession.getTransactionId());
-        branchSession.setBranchId(1L);
-        branchSession.setResourceGroupId("my_test_tx_group");
-        branchSession.setResourceId("tb_1");
-        branchSession.setLockKey("t_1");
-        branchSession.setBranchType(BranchType.AT);
-        branchSession.setApplicationId("demo-child-app");
-        branchSession.setTxServiceGroup("my_test_tx_group");
-        branchSession.setApplicationData("{\"data\":\"test\"}");
         sessionManager.onAddBranch(globalSession,branchSession);
         sessionManager.onRemoveBranch(globalSession,branchSession);
     }
 
-    @Test
-    public void onCloseTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onCloseTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
         sessionManager.onClose(globalSession);
     }
 
-    @Test
-    public void onEndTest() throws Exception{
-        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+    @Test(dataProvider = "globalSessionProvider")
+    public void onEndTest(GlobalSession globalSession) throws Exception{
         sessionManager.onBegin(globalSession);
         sessionManager.onEnd(globalSession);
     }
+
+    @DataProvider
+    public static Object[][] globalSessionProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        return new Object[][] {{ globalSession}};
+    }
+
+    @DataProvider
+    public static Object[][] globalSessionsProvider() {
+        GlobalSession globalSession1 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        GlobalSession globalSession2 = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        return new Object[][] {{Arrays.asList(globalSession1,globalSession2)}};
+    }
+
+    @DataProvider
+    public static Object[][] branchSessionProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        BranchSession branchSession = new BranchSession();
+        branchSession.setTransactionId(globalSession.getTransactionId());
+        branchSession.setBranchId(1L);
+        branchSession.setResourceGroupId("my_test_tx_group");
+        branchSession.setResourceId("tb_1");
+        branchSession.setLockKey("t_1");
+        branchSession.setBranchType(BranchType.AT);
+        branchSession.setApplicationId("demo-child-app");
+        branchSession.setTxServiceGroup("my_test_tx_group");
+        branchSession.setApplicationData("{\"data\":\"test\"}");
+        return new Object[][] {{ globalSession,branchSession}};
+    }
 }
diff --git a/server/src/test/java/com/alibaba/fescar/server/session/GlobalSessionTest.java b/server/src/test/java/com/alibaba/fescar/server/session/GlobalSessionTest.java
new file mode 100644
index 000000000..062de0d35
--- /dev/null
+++ b/server/src/test/java/com/alibaba/fescar/server/session/GlobalSessionTest.java
@@ -0,0 +1,128 @@
+/*
+ *  Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package com.alibaba.fescar.server.session;
+
+import com.alibaba.fescar.core.model.BranchStatus;
+import com.alibaba.fescar.core.model.BranchType;
+import com.alibaba.fescar.core.model.GlobalStatus;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ * @author tianming.xm@gmail.com
+ * @since 2019/1/23
+ */
+public class GlobalSessionTest {
+
+    @Test(dataProvider = "branchSessionMTProvider")
+    public void canBeCommittedAsyncTest(GlobalSession globalSession){
+        Assert.assertFalse(globalSession.canBeCommittedAsync());
+    }
+
+    @Test(dataProvider = "globalSessionProvider")
+    public void beginTest(GlobalSession globalSession) throws Exception{
+        globalSession.begin();
+    }
+
+    @Test(dataProvider = "globalSessionProvider")
+    public void changeStatusTest(GlobalSession globalSession) throws Exception{
+        globalSession.changeStatus(GlobalStatus.Committed);
+    }
+
+    @Test(dataProvider = "branchSessionProvider")
+    public void changeBranchStatusTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
+        globalSession.changeBranchStatus(branchSession, BranchStatus.PhaseTwo_Committed);
+    }
+
+    @Test(dataProvider = "globalSessionProvider")
+    public void closeTest(GlobalSession globalSession) throws Exception{
+        globalSession.close();
+    }
+
+    @Test(dataProvider = "globalSessionProvider")
+    public void endTest(GlobalSession globalSession) throws Exception{
+        globalSession.end();
+    }
+
+    @Test(dataProvider = "branchSessionProvider")
+    public void addBranchTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
+        globalSession.addBranch(branchSession);
+    }
+
+    @Test(dataProvider = "branchSessionProvider")
+    public void removeBranchTest(GlobalSession globalSession,BranchSession branchSession) throws Exception{
+        globalSession.addBranch(branchSession);
+        globalSession.removeBranch(branchSession);
+    }
+
+    @Test(dataProvider = "globalSessionProvider")
+    public void codecTest(GlobalSession globalSession){
+        byte[] result = globalSession.encode();
+        Assert.assertNotNull(result);
+        GlobalSession expected = new GlobalSession();
+        expected.decode(result);
+        Assert.assertEquals(expected.getTransactionId(),globalSession.getTransactionId());
+        Assert.assertEquals(expected.getTimeout(),globalSession.getTimeout());
+        Assert.assertEquals(expected.getApplicationId(),globalSession.getApplicationId());
+        Assert.assertEquals(expected.getTransactionServiceGroup(),globalSession.getTransactionServiceGroup());
+        Assert.assertEquals(expected.getTransactionName(),globalSession.getTransactionName());
+    }
+
+    @DataProvider
+    public static Object[][] globalSessionProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        globalSession.setActive(true);
+        globalSession.addSessionLifecycleListener(new DefaultSessionManager("default"));
+        return new Object[][] {{ globalSession}};
+    }
+
+    @DataProvider
+    public static Object[][] branchSessionProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        BranchSession branchSession = new BranchSession();
+        branchSession.setTransactionId(globalSession.getTransactionId());
+        branchSession.setBranchId(1L);
+        branchSession.setResourceGroupId("my_test_tx_group");
+        branchSession.setResourceId("tb_1");
+        branchSession.setLockKey("t_1");
+        branchSession.setBranchType(BranchType.AT);
+        branchSession.setApplicationId("demo-child-app");
+        branchSession.setTxServiceGroup("my_test_tx_group");
+        branchSession.setApplicationData("{\"data\":\"test\"}");
+        branchSession.setBranchType(BranchType.AT);
+        globalSession.add(branchSession);
+        return new Object[][] {{ globalSession,branchSession}};
+    }
+
+    @DataProvider
+    public static Object[][] branchSessionMTProvider() {
+        GlobalSession globalSession = new GlobalSession("demo-app","my_test_tx_group","test",6000);
+        BranchSession branchSession = new BranchSession();
+        branchSession.setTransactionId(globalSession.getTransactionId());
+        branchSession.setBranchId(1L);
+        branchSession.setResourceGroupId("my_test_tx_group");
+        branchSession.setResourceId("tb_1");
+        branchSession.setLockKey("t_1");
+        branchSession.setBranchType(BranchType.AT);
+        branchSession.setApplicationId("demo-child-app");
+        branchSession.setTxServiceGroup("my_test_tx_group");
+        branchSession.setApplicationData("{\"data\":\"test\"}");
+        branchSession.setBranchType(BranchType.MT);
+        globalSession.add(branchSession);
+        return new Object[][] {{ globalSession}};
+    }
+}