Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to Junit4 #682

Merged
merged 1 commit into from Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 49 additions & 41 deletions pgjdbc/src/test/java/org/postgresql/core/ParserTest.java
Expand Up @@ -5,124 +5,132 @@

package org.postgresql.core;

import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Assert;
import org.junit.Test;

/**
* Test cases for the Parser.
* @author Jeremy Whiting jwhiting@redhat.com
*/
public class ParserTest extends TestCase {
public class ParserTest {

/**
* Test to make sure delete command is detected by parser and detected via
* api. Mix up the case of the command to check detection continues to work.
*/
@Test
public void testDeleteCommandParsing() {
char[] command = new char[6];
"DELETE".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse upper case command.", Parser.parseDeleteKeyword(command, 0));
assertTrue("Failed to correctly parse upper case command.", Parser.parseDeleteKeyword(command, 0));
"DelEtE".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseDeleteKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseDeleteKeyword(command, 0));
"deleteE".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseDeleteKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseDeleteKeyword(command, 0));
"delete".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse lower case command.", Parser.parseDeleteKeyword(command, 0));
assertTrue("Failed to correctly parse lower case command.", Parser.parseDeleteKeyword(command, 0));
"Delete".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseDeleteKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseDeleteKeyword(command, 0));
}

/**
* Test UPDATE command parsing.
*/
@Test
public void testUpdateCommandParsing() {
char[] command = new char[6];
"UPDATE".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse upper case command.", Parser.parseUpdateKeyword(command, 0));
assertTrue("Failed to correctly parse upper case command.", Parser.parseUpdateKeyword(command, 0));
"UpDateE".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseUpdateKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseUpdateKeyword(command, 0));
"updatE".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseUpdateKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseUpdateKeyword(command, 0));
"Update".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseUpdateKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseUpdateKeyword(command, 0));
"update".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse lower case command.", Parser.parseUpdateKeyword(command, 0));
assertTrue("Failed to correctly parse lower case command.", Parser.parseUpdateKeyword(command, 0));
}

/**
* Test MOVE command parsing.
*/
@Test
public void testMoveCommandParsing() {
char[] command = new char[4];
"MOVE".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse upper case command.", Parser.parseMoveKeyword(command, 0));
assertTrue("Failed to correctly parse upper case command.", Parser.parseMoveKeyword(command, 0));
"mOVe".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseMoveKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseMoveKeyword(command, 0));
"movE".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseMoveKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseMoveKeyword(command, 0));
"Move".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseMoveKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseMoveKeyword(command, 0));
"move".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse lower case command.", Parser.parseMoveKeyword(command, 0));
assertTrue("Failed to correctly parse lower case command.", Parser.parseMoveKeyword(command, 0));
}

/**
* Test WITH command parsing.
*/
@Test
public void testWithCommandParsing() {
char[] command = new char[4];
"WITH".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse upper case command.", Parser.parseWithKeyword(command, 0));
assertTrue("Failed to correctly parse upper case command.", Parser.parseWithKeyword(command, 0));
"wITh".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseWithKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseWithKeyword(command, 0));
"witH".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseWithKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseWithKeyword(command, 0));
"With".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseWithKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseWithKeyword(command, 0));
"with".getChars(0, 4, command, 0);
Assert.assertTrue("Failed to correctly parse lower case command.", Parser.parseWithKeyword(command, 0));
assertTrue("Failed to correctly parse lower case command.", Parser.parseWithKeyword(command, 0));
}

/**
* Test SELECT command parsing.
*/
@Test
public void testSelectCommandParsing() {
char[] command = new char[6];
"SELECT".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse upper case command.", Parser.parseSelectKeyword(command, 0));
assertTrue("Failed to correctly parse upper case command.", Parser.parseSelectKeyword(command, 0));
"sELect".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseSelectKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseSelectKeyword(command, 0));
"selecT".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseSelectKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseSelectKeyword(command, 0));
"Select".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse mixed case command.", Parser.parseSelectKeyword(command, 0));
assertTrue("Failed to correctly parse mixed case command.", Parser.parseSelectKeyword(command, 0));
"select".getChars(0, 6, command, 0);
Assert.assertTrue("Failed to correctly parse lower case command.", Parser.parseSelectKeyword(command, 0));
assertTrue("Failed to correctly parse lower case command.", Parser.parseSelectKeyword(command, 0));
}

@Test
public void testEscapeProcessing() throws Exception {
Assert.assertEquals("DATE '1999-01-09'", Parser.replaceProcessing("{d '1999-01-09'}", true, false));
Assert.assertEquals("DATE '1999-01-09'", Parser.replaceProcessing("{D '1999-01-09'}", true, false));
Assert.assertEquals("TIME '20:00:03'", Parser.replaceProcessing("{t '20:00:03'}", true, false));
Assert.assertEquals("TIME '20:00:03'", Parser.replaceProcessing("{T '20:00:03'}", true, false));
Assert.assertEquals("TIMESTAMP '1999-01-09 20:11:11.123455'", Parser.replaceProcessing("{ts '1999-01-09 20:11:11.123455'}", true, false));
Assert.assertEquals("TIMESTAMP '1999-01-09 20:11:11.123455'", Parser.replaceProcessing("{Ts '1999-01-09 20:11:11.123455'}", true, false));
assertEquals("DATE '1999-01-09'", Parser.replaceProcessing("{d '1999-01-09'}", true, false));
assertEquals("DATE '1999-01-09'", Parser.replaceProcessing("{D '1999-01-09'}", true, false));
assertEquals("TIME '20:00:03'", Parser.replaceProcessing("{t '20:00:03'}", true, false));
assertEquals("TIME '20:00:03'", Parser.replaceProcessing("{T '20:00:03'}", true, false));
assertEquals("TIMESTAMP '1999-01-09 20:11:11.123455'", Parser.replaceProcessing("{ts '1999-01-09 20:11:11.123455'}", true, false));
assertEquals("TIMESTAMP '1999-01-09 20:11:11.123455'", Parser.replaceProcessing("{Ts '1999-01-09 20:11:11.123455'}", true, false));

Assert.assertEquals("user", Parser.replaceProcessing("{fn user()}", true, false));
Assert.assertEquals("cos(1)", Parser.replaceProcessing("{fn cos(1)}", true, false));
Assert.assertEquals("extract(week from DATE '2005-01-24')", Parser.replaceProcessing("{fn week({d '2005-01-24'})}", true, false));
assertEquals("user", Parser.replaceProcessing("{fn user()}", true, false));
assertEquals("cos(1)", Parser.replaceProcessing("{fn cos(1)}", true, false));
assertEquals("extract(week from DATE '2005-01-24')", Parser.replaceProcessing("{fn week({d '2005-01-24'})}", true, false));

Assert.assertEquals("\"T1\" LEFT OUTER JOIN t2 ON \"T1\".id = t2.id",
assertEquals("\"T1\" LEFT OUTER JOIN t2 ON \"T1\".id = t2.id",
Parser.replaceProcessing("{oj \"T1\" LEFT OUTER JOIN t2 ON \"T1\".id = t2.id}", true, false));

Assert.assertEquals("ESCAPE '_'", Parser.replaceProcessing("{escape '_'}", true, false));
assertEquals("ESCAPE '_'", Parser.replaceProcessing("{escape '_'}", true, false));

// nothing should be changed in that case, no valid escape code
Assert.assertEquals("{obj : 1}", Parser.replaceProcessing("{obj : 1}", true, false));
assertEquals("{obj : 1}", Parser.replaceProcessing("{obj : 1}", true, false));
}

@Test
public void testUnterminatedEscape() throws Exception {
Assert.assertEquals("{oj ", Parser.replaceProcessing("{oj ", true, false));
assertEquals("{oj ", Parser.replaceProcessing("{oj ", true, false));
}
}
15 changes: 8 additions & 7 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/ANTTest.java
Expand Up @@ -5,16 +5,17 @@

package org.postgresql.test.jdbc2;

import junit.framework.TestCase;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

public class ANTTest extends TestCase {
public ANTTest(String name) {
super(name);
}
import org.junit.Test;

public class ANTTest {

/*
* This tests the acceptsURL() method with a couple of good and badly formed jdbc urls
*/
@Test
public void testANT() {
String url = System.getProperty("database");
String usr = System.getProperty("username");
Expand All @@ -24,7 +25,7 @@ public void testANT() {
assertNotNull(usr);
assertNotNull(psw);

assertTrue(!url.equals(""));
assertTrue(!usr.equals(""));
assertFalse(url.isEmpty());
assertFalse(usr.isEmpty());
}
}
Expand Up @@ -5,10 +5,15 @@

package org.postgresql.test.jdbc2;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.postgresql.core.BaseConnection;
import org.postgresql.test.TestUtil;

import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -20,14 +25,11 @@
* This test suite will check the behaviour of the findColumnIndex method. This is testing the
* behaviour when sanitiser is disabled.
*/
public class ColumnSanitiserDisabledTest extends TestCase {
public class ColumnSanitiserDisabledTest {
private Connection conn;

public ColumnSanitiserDisabledTest(String name) {
super(name);
}

protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
Properties props = new Properties();
props.setProperty("disableColumnSanitiser", Boolean.TRUE.toString());
conn = TestUtil.openDB(props);
Expand All @@ -46,7 +48,8 @@ protected void setUp() throws Exception {
data.close();
}

protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
TestUtil.dropTable(conn, "allmixedup");
TestUtil.closeDB(conn);
System.setProperty("disableColumnSanitiser", "false");
Expand All @@ -57,45 +60,54 @@ protected void tearDown() throws Exception {
* application supplied column names.
*/

@Test
public void testTableColumnLowerNowFindFindLowerCaseColumn() throws SQLException {
findColumn("id", true);
}

@Test
public void testTableColumnLowerNowFindFindUpperCaseColumn() throws SQLException {
findColumn("ID", true);
}

@Test
public void testTableColumnLowerNowFindFindMixedCaseColumn() throws SQLException {
findColumn("Id", false);
}

@Test
public void testTableColumnUpperNowFindFindLowerCaseColumn() throws SQLException {
findColumn("description", true);
}

@Test
public void testTableColumnUpperNowFindFindUpperCaseColumn() throws SQLException {
findColumn("DESCRIPTION", true);
}

@Test
public void testTableColumnUpperNowFindFindMixedCaseColumn() throws SQLException {
findColumn("Description", false);
}

@Test
public void testTableColumnMixedNowFindLowerCaseColumn() throws SQLException {
findColumn("foo", false);
}

@Test
public void testTableColumnMixedNowFindFindUpperCaseColumn() throws SQLException {
findColumn("FOO", false);
}

@Test
public void testTableColumnMixedNowFindFindMixedCaseColumn() throws SQLException {
findColumn("fOo", true);
}

private void findColumn(String label, boolean failOnNotFound) throws SQLException {
PreparedStatement query = conn.prepareStatement("select * from allmixedup");
if (0 == TestUtil.findColumn(query, label) && failOnNotFound) {
if ((TestUtil.findColumn(query, label) == 0) && failOnNotFound) {
fail(String.format("Expected to find the column with the label [%1$s].", label));
}
query.close();
Expand Down
Expand Up @@ -5,10 +5,15 @@

package org.postgresql.test.jdbc2;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.postgresql.core.BaseConnection;
import org.postgresql.test.TestUtil;

import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -20,14 +25,11 @@
* This test suite will check the behaviour of the findColumnIndex method. The tests will check the
* behaviour of the method when the sanitiser is enabled. Default behaviour of the driver.
*/
public class ColumnSanitiserEnabledTest extends TestCase {
public class ColumnSanitiserEnabledTest {
private Connection conn;

public ColumnSanitiserEnabledTest(String name) {
super(name);
}

protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
Properties props = new Properties();
props.setProperty("disableColumnSanitiser", Boolean.FALSE.toString());
conn = TestUtil.openDB(props);
Expand All @@ -52,45 +54,54 @@ protected void tearDown() throws Exception {
* application supplied column names.
*/

@Test
public void testTableColumnLowerNowFindFindLowerCaseColumn() throws SQLException {
findColumn("id", true);
}

@Test
public void testTableColumnLowerNowFindFindUpperCaseColumn() throws SQLException {
findColumn("ID", true);
}

@Test
public void testTableColumnLowerNowFindFindMixedCaseColumn() throws SQLException {
findColumn("Id", true);
}

@Test
public void testTableColumnUpperNowFindFindLowerCaseColumn() throws SQLException {
findColumn("description", true);
}

@Test
public void testTableColumnUpperNowFindFindUpperCaseColumn() throws SQLException {
findColumn("DESCRIPTION", true);
}

@Test
public void testTableColumnUpperNowFindFindMixedCaseColumn() throws SQLException {
findColumn("Description", true);
}

@Test
public void testTableColumnMixedNowFindLowerCaseColumn() throws SQLException {
findColumn("foo", true);
}

@Test
public void testTableColumnMixedNowFindFindUpperCaseColumn() throws SQLException {
findColumn("FOO", true);
}

@Test
public void testTableColumnMixedNowFindFindMixedCaseColumn() throws SQLException {
findColumn("fOo", true);
}

private void findColumn(String label, boolean failOnNotFound) throws SQLException {
PreparedStatement query = conn.prepareStatement("select * from allmixedup");
if (0 == TestUtil.findColumn(query, label) && failOnNotFound) {
if ((TestUtil.findColumn(query, label) == 0) && failOnNotFound) {
fail(String.format("Expected to find the column with the label [%1$s].", label));
}
query.close();
Expand Down