Skip to content

Commit

Permalink
LPS-107396 Avoid powermock
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyangzhou committed Feb 25, 2020
1 parent 4180b47 commit 7f376d8
Showing 1 changed file with 10 additions and 44 deletions.
Expand Up @@ -15,27 +15,15 @@
package com.liferay.portal.dao.db;

import com.liferay.portal.kernel.dao.db.DB;
import com.liferay.portal.kernel.dao.db.DBInspector;
import com.liferay.portal.kernel.dao.db.test.BaseDBTestCase;
import com.liferay.portal.kernel.dao.jdbc.DataAccess;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.mockito.stubbing.OngoingStubbing;

import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.mockito.expectation.ConstructorExpectationSetup;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
* @author Shinn Lok
* @author Alberto Chaparro
*/
@PrepareForTest({DataAccess.class, OracleDB.class})
@RunWith(PowerMockRunner.class)
public class OracleDBTest extends BaseDBTestCase {

@Test
Expand All @@ -61,8 +49,6 @@ public void testRewordAlterColumnTypeNoSemicolon() throws Exception {

@Test
public void testRewordAlterColumnTypeNotNullWhenNotNull() throws Exception {
_mockIsNullable(false);

Assert.assertEquals(
"alter table DLFolder modify userName VARCHAR2(75 CHAR);\n",
buildSQL(
Expand All @@ -71,7 +57,7 @@ public void testRewordAlterColumnTypeNotNullWhenNotNull() throws Exception {

@Test
public void testRewordAlterColumnTypeNotNullWhenNull() throws Exception {
_mockIsNullable(true);
_isNullable = true;

Assert.assertEquals(
"alter table DLFolder modify userName VARCHAR2(75 CHAR) not " +
Expand All @@ -82,16 +68,14 @@ public void testRewordAlterColumnTypeNotNullWhenNull() throws Exception {

@Test
public void testRewordAlterColumnTypeNullWhenNotNull() throws Exception {
_mockIsNullable(false);

Assert.assertEquals(
"alter table DLFolder modify userName VARCHAR2(75 CHAR) null;\n",
buildSQL("alter_column_type DLFolder userName VARCHAR(75) null;"));
}

@Test
public void testRewordAlterColumnTypeNullWhenNull() throws Exception {
_mockIsNullable(true);
_isNullable = true;

Assert.assertEquals(
"alter table DLFolder modify userName VARCHAR2(75 CHAR);\n",
Expand All @@ -107,34 +91,16 @@ public void testRewordAlterColumnTypeString() throws Exception {

@Override
protected DB getDB() {
return new OracleDB(0, 0);
}

private void _mockIsNullable(boolean nullable) throws Exception {
PowerMockito.mockStatic(DataAccess.class);
return new OracleDB(0, 0) {

PowerMockito.when(
DataAccess.getConnection()
).thenReturn(
null
);
@Override
protected boolean isNullable(String tableName, String columnName) {
return _isNullable;
}

DBInspector dbInspector = PowerMockito.mock(DBInspector.class);

ConstructorExpectationSetup<DBInspector>
dbInspectorConstructorExpectationSetup = PowerMockito.whenNew(
DBInspector.class);

OngoingStubbing dbInspectorOngoingStubbing =
dbInspectorConstructorExpectationSetup.withAnyArguments();

dbInspectorOngoingStubbing.thenReturn(dbInspector);

PowerMockito.when(
dbInspector.isNullable("DLFolder", "userName")
).thenReturn(
nullable
);
};
}

private boolean _isNullable;

}

0 comments on commit 7f376d8

Please sign in to comment.