forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix apache#195 add context unit test
- Loading branch information
1 parent
f573e64
commit 9be3210
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
core/src/test/java/com/alibaba/fescar/core/context/ContextCoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.core.context; | ||
|
||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by guoyao on 2019/2/28. | ||
*/ | ||
public class ContextCoreTest { | ||
|
||
private final String FIRST_KEY="first_key"; | ||
private final String FIRST_VALUE="first_value"; | ||
private final String SECOND_KEY="second_key"; | ||
private final String SECOND_VALUE="second_value"; | ||
private final String NOT_EXIST_KEY="not_exist_key"; | ||
|
||
@Test | ||
public void testPut() { | ||
ContextCore load=ContextCoreLoader.load(); | ||
Assert.assertNull(load.put(FIRST_KEY, FIRST_VALUE)); | ||
Assert.assertNull(load.put(SECOND_KEY, SECOND_VALUE)); | ||
Assert.assertEquals(FIRST_VALUE,load.put(FIRST_KEY, SECOND_VALUE)); | ||
Assert.assertEquals(SECOND_VALUE,load.put(SECOND_KEY, FIRST_VALUE)); | ||
//clear keys | ||
load.remove(FIRST_KEY); | ||
load.remove(SECOND_KEY); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
ContextCore load=ContextCoreLoader.load(); | ||
load.put(FIRST_KEY, FIRST_VALUE); | ||
load.put(SECOND_KEY, FIRST_VALUE); | ||
Assert.assertEquals(FIRST_VALUE, load.get(FIRST_KEY)); | ||
Assert.assertEquals(FIRST_VALUE, load.get(SECOND_KEY)); | ||
load.put(FIRST_KEY, SECOND_VALUE); | ||
load.put(SECOND_KEY, SECOND_VALUE); | ||
Assert.assertEquals(SECOND_VALUE, load.get(FIRST_KEY)); | ||
Assert.assertEquals(SECOND_VALUE, load.get(SECOND_KEY)); | ||
Assert.assertNull(load.get(NOT_EXIST_KEY)); | ||
//clear keys | ||
load.remove(FIRST_KEY); | ||
load.remove(SECOND_KEY); | ||
load.remove(NOT_EXIST_KEY); | ||
} | ||
|
||
@Test | ||
public void testRemove() { | ||
ContextCore load=ContextCoreLoader.load(); | ||
load.put(FIRST_KEY, FIRST_VALUE); | ||
load.put(SECOND_KEY, SECOND_VALUE); | ||
Assert.assertEquals(FIRST_VALUE,load.remove(FIRST_KEY)); | ||
Assert.assertEquals(SECOND_VALUE,load.remove(SECOND_KEY)); | ||
Assert.assertNull(load.remove(NOT_EXIST_KEY)); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
core/src/test/java/com/alibaba/fescar/core/context/RootContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.core.context; | ||
|
||
import com.alibaba.fescar.common.exception.ShouldNeverHappenException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by guoyao on 2019/2/28. | ||
*/ | ||
public class RootContextTest { | ||
|
||
private final String DEFAULT_XID="default_xid"; | ||
|
||
@Test | ||
public void testBind_And_Unbind() { | ||
Assert.assertNull(RootContext.unbind()); | ||
RootContext.bind(DEFAULT_XID); | ||
Assert.assertEquals(DEFAULT_XID, RootContext.unbind()); | ||
} | ||
|
||
@Test | ||
public void testGetXID() { | ||
RootContext.bind(DEFAULT_XID); | ||
Assert.assertEquals(DEFAULT_XID, RootContext.getXID()); | ||
Assert.assertEquals(DEFAULT_XID, RootContext.unbind()); | ||
Assert.assertNull(RootContext.getXID()); | ||
} | ||
|
||
@Test | ||
public void testInGlobalTransaction() { | ||
Assert.assertTrue(!RootContext.inGlobalTransaction()); | ||
RootContext.bind(DEFAULT_XID); | ||
Assert.assertTrue(RootContext.inGlobalTransaction()); | ||
RootContext.unbind(); | ||
Assert.assertTrue(!RootContext.inGlobalTransaction()); | ||
} | ||
|
||
@Test(expected =ShouldNeverHappenException.class) | ||
public void testAssertNotInGlobalTransactionWithException() { | ||
RootContext.assertNotInGlobalTransaction(); | ||
RootContext.bind(DEFAULT_XID); | ||
RootContext.assertNotInGlobalTransaction(); | ||
} | ||
|
||
@Test | ||
public void testAssertNotInGlobalTransaction() { | ||
RootContext.assertNotInGlobalTransaction(); | ||
} | ||
|
||
} |