Skip to content

Commit

Permalink
fix apache#195 add context unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
github-ygy committed Feb 28, 2019
1 parent f573e64 commit 9be3210
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
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));
}

}
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();
}

}

0 comments on commit 9be3210

Please sign in to comment.