From 8799068b593b9dd40b569a520c32b7e7dd541783 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 17 Feb 2018 23:53:48 +0300 Subject: run tests on h2 database --- juick-server-jdbc/build.gradle | 2 +- .../java/com/juick/service/BaseJdbcService.java | 5 +- .../com/juick/service/MessagesServiceImpl.java | 31 ++-- .../com/juick/service/PMQueriesServiceImpl.java | 2 +- .../com/juick/service/PushQueriesServiceImpl.java | 9 +- .../com/juick/service/SubscriptionServiceImpl.java | 17 +- .../configuration/RepositoryConfiguration.java | 182 +++++++++++++++++++-- 7 files changed, 204 insertions(+), 44 deletions(-) (limited to 'juick-server-jdbc') diff --git a/juick-server-jdbc/build.gradle b/juick-server-jdbc/build.gradle index 4e7efe0e..7c1b2b1f 100644 --- a/juick-server-jdbc/build.gradle +++ b/juick-server-jdbc/build.gradle @@ -39,7 +39,7 @@ dependencies { providedRuntime "commons-fileupload:commons-fileupload:1.3.3" testCompile project(path: ':juick-core', configuration: 'testArtifacts') - testCompile "ch.vorburger.mariaDB4j:mariaDB4j:2.2.3" + testCompile 'com.h2database:h2:1.4.196' testCompile "junit:junit:${rootProject.junitVersion}" testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" testCompile "org.mockito:mockito-core:${rootProject.mockitoVersion}" diff --git a/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java b/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java index 67ddf713..d0bf0195 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java @@ -17,6 +17,7 @@ package com.juick.service; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport; import javax.annotation.PostConstruct; @@ -28,9 +29,9 @@ import javax.sql.DataSource; */ public class BaseJdbcService extends NamedParameterJdbcDaoSupport { @Inject - DataSource dataSource; + JdbcTemplate jdbcTemplate; @PostConstruct public void init() { - setDataSource(dataSource); + setJdbcTemplate(jdbcTemplate); } } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index 0ebd2838..bcce56ca 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -43,11 +43,10 @@ import org.springframework.util.Assert; import javax.inject.Inject; import java.sql.*; +import java.time.Instant; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.Date; import java.util.stream.Collectors; /** @@ -178,16 +177,15 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Override public int createReply(final int mid, final int rid, final int uid, final String txt, final String attachment) { int ridnew = getReplyIDIncrement(mid); - getJdbcTemplate().execute("SET @ts=CURRENT_TIMESTAMP()"); + Date ts = Date.from(Instant.now()); getJdbcTemplate().update("INSERT INTO replies(message_id, reply_id, user_id, replyto, attach, txt, ts) " + - "VALUES (?, ?, ?, ?, ?, ?, @ts)", - new Object[]{mid, ridnew, uid, rid, attachment, txt}, - new int[]{Types.INTEGER, Types.SMALLINT, Types.INTEGER, Types.SMALLINT, Types.CHAR, Types.VARCHAR}); + "VALUES (?, ?, ?, ?, ?, ?, ?)", + mid, ridnew, uid, rid, attachment, txt, ts); if (ridnew > 0) { getJdbcTemplate().update( - "UPDATE messages SET replies = replies + 1, updated=@ts WHERE message_id = ?", - mid); + "UPDATE messages SET replies = replies + 1, updated=? WHERE message_id = ?", + ts, mid); } return ridnew; } @@ -197,9 +195,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return getJdbcTemplate().execute((ConnectionCallback) conn -> { conn.setAutoCommit(false); final int replyNo; - try (PreparedStatement ps = conn.prepareStatement("START TRANSACTION")) { - ps.executeUpdate(); - } try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) { ps.setInt(1, mid); try (ResultSet resultSet = ps.executeQuery()) { @@ -317,7 +312,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "LEFT JOIN favorites " + "ON messages.message_id = favorites.message_id " + "WHERE messages.message_id = ? " - + "GROUP BY mid, rid, replyto, uid, nick, banned, ago, ts, readonly, " + + "GROUP BY mid, rid, replyto, uid, nick, banned, ago, messages.ts, readonly, " + "privacy, replies, attach, place_id, lat, lon, tags, repliesby, q", new MessageMapper(), mid); @@ -906,12 +901,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return getJdbcTemplate().queryForList("SELECT replies.message_id FROM replies " + "INNER JOIN messages ON replies.message_id = messages.message_id " + "LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id " + - "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND replies.ts > DATE_ADD(now(), INTERVAL -2 HOUR) " + - "AND messages.popular=0 GROUP BY message_id having COUNT(DISTINCT(replies.user_id)) > 5 " + + "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND replies.ts > TIMESTAMPADD(HOUR, -2, CURRENT_TIMESTAMP) " + + "AND messages.popular=0 GROUP BY messages.message_id having COUNT(DISTINCT(replies.user_id)) > 5 " + "UNION ALL SELECT favorites.message_id FROM favorites " + "INNER JOIN messages ON messages.message_id = favorites.message_id " + "LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id " + - "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND favorites.ts > DATE_ADD(NOW(), INTERVAL -2 HOUR) " + - "AND messages.popular=0 GROUP BY message_id HAVING COUNT(DISTINCT favorites.user_id) > 1;", Integer.class); + "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND favorites.ts > TIMESTAMPADD(HOUR, -2, CURRENT_TIMESTAMP) " + + "AND messages.popular=0 GROUP BY messages.message_id HAVING COUNT(DISTINCT favorites.user_id) > 1;", Integer.class); } } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java index fcb8a6f3..af3fa404 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -43,7 +43,7 @@ public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesSe if (ret) { getJdbcTemplate().update( "INSERT INTO pm_streams(user_id, user_id_to, lastmessage, unread) VALUES (?, ?, NOW(), 1) " - + "ON DUPLICATE KEY UPDATE lastmessage = NOW(), unread = unread + 1", + + "ON DUPLICATE KEY UPDATE lastmessage = CURRENT_TIMESTAMP(), unread = unread + 1", uidFrom, uid_to); } return ret; diff --git a/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java index 878d10d5..7f97956c 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java @@ -18,6 +18,7 @@ package com.juick.service; import org.apache.commons.collections4.CollectionUtils; +import org.springframework.dao.DuplicateKeyException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; @@ -132,7 +133,11 @@ public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueri @Transactional @Override public boolean addAPNSToken(Integer uid, String token) { - return getJdbcTemplate().update("INSERT IGNORE INTO ios(user_id,token) VALUES (?, ?)", - uid, token) > 0; + try { + return getJdbcTemplate().update("INSERT INTO ios(user_id,token) VALUES (?, ?)", + uid, token) > 0; + } catch (DuplicateKeyException e) { + return true; + } } } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java index 9dd41721..66b62833 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -21,6 +21,7 @@ import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.NotifyOpts; import org.apache.commons.lang3.StringUtils; +import org.springframework.dao.DuplicateKeyException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; @@ -147,8 +148,12 @@ public class SubscriptionServiceImpl extends BaseJdbcService implements Subscrip @Transactional @Override public boolean subscribeMessage(final int mid, final int vuid) { - return getJdbcTemplate().update( - "INSERT IGNORE INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", vuid, mid) == 1; + try { + return getJdbcTemplate().update( + "INSERT INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", vuid, mid) == 1; + } catch (DuplicateKeyException e) { + return true; + } } @Transactional @@ -161,8 +166,12 @@ public class SubscriptionServiceImpl extends BaseJdbcService implements Subscrip @Transactional @Override public boolean subscribeUser(final User user, final User toUser) { - return getJdbcTemplate().update( - "INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(), user.getUid()) == 1; + try { + return getJdbcTemplate().update( + "INSERT INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(), user.getUid()) == 1; + } catch (DuplicateKeyException e) { + return true; + } } @Transactional diff --git a/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java b/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java index 72968e8e..08b5b787 100644 --- a/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java +++ b/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java @@ -17,8 +17,6 @@ package com.juick.configuration; -import ch.vorburger.exec.ManagedProcessException; -import ch.vorburger.mariadb4j.DB; import com.juick.service.ImagesService; import com.juick.service.MockImagesService; import com.juick.service.search.SearchService; @@ -40,29 +38,181 @@ import java.util.List; @ComponentScan(basePackages = "com.juick.service") public class RepositoryConfiguration { - @Bean(destroyMethod = "stop") - DB db() throws ManagedProcessException { - DB db = DB.newEmbeddedDB(33306); - - db.start(); - db.createDB("juick"); - db.source("schema.sql"); - - return db; - } - @DependsOn("db") @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName("net.sf.log4jdbc.DriverSpy"); - dataSource.setUrl("jdbc:log4jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root"); + dataSource.setDriverClassName("org.h2.Driver"); + dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;mode=MYSQL"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() { - return new JdbcTemplate(dataSource()); + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_users (user_id int(10) unsigned NOT NULL, " + + "bl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_tags (user_id int(10) unsigned NOT NULL, " + + "tag_id int(10) unsigned NOT NULL)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS wl_users (user_id int(10) unsigned NOT NULL, " + + "wl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages (" + + "message_id int(10) unsigned NOT NULL AUTO_INCREMENT," + + "user_id int(10) unsigned NOT NULL," + + "place_id int(10) unsigned DEFAULT NULL," + + "lat decimal(10,7) DEFAULT NULL," + + "lon decimal(10,7) DEFAULT NULL," + + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," + + "replies smallint(5) unsigned NOT NULL DEFAULT 0," + + "maxreplyid smallint(5) unsigned NOT NULL DEFAULT 0," + + "privacy tinyint(4) NOT NULL DEFAULT '1'," + + "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + + "readonly tinyint(1) NOT NULL DEFAULT 0," + + "likes smallint(6) NOT NULL DEFAULT 0," + + "`popular` tinyint(4) NOT NULL DEFAULT '0'," + + "hidden tinyint(3) unsigned NOT NULL DEFAULT 0," + + "updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + + ")"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_tags (" + + "message_id int(10) unsigned NOT NULL," + + "tag_id int(10) unsigned NOT NULL" + + ")"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (" + + "id int(10) unsigned NOT NULL AUTO_INCREMENT," + + "nick char(64) NOT NULL," + + "`lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__'," + + "passw char(32) NOT NULL," + + "banned tinyint(3) unsigned NOT NULL DEFAULT 0, " + + "PRIMARY KEY(id), UNIQUE KEY(nick))"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS useroptions (" + + "user_id int(10) unsigned NOT NULL," + + "jnotify tinyint(1) NOT NULL DEFAULT 1," + + "`repliesview` tinyint(1) NOT NULL DEFAULT '0'," + + "subscr_active tinyint(1) NOT NULL DEFAULT 1)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_users (" + + "user_id int(10) unsigned NOT NULL," + + "suser_id int(10) unsigned NOT NULL," + + "jid char(64) DEFAULT NULL," + + "active tinyint(1) NOT NULL DEFAULT 1," + + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " + + "PRIMARY KEY (suser_id, user_id))"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_messages (" + + "message_id int(10) unsigned NOT NULL," + + "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, message_id))"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_tags (" + + "tag_id int(10) unsigned NOT NULL," + + "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, tag_id))"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_txt (" + + "message_id int(10) unsigned NOT NULL," + + "tags varchar(255)," + + "repliesby varchar(96)," + + "txt TEXT)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS tags(" + + "tag_id int(10) unsigned NOT NULL AUTO_INCREMENT," + + "synonym_id int(10) unsigned DEFAULT NULL," + + "name char(48) NOT NULL," + + "top tinyint(1) unsigned NOT NULL DEFAULT 0," + + "stat_messages int(10) unsigned NOT NULL DEFAULT 0," + + "stat_users smallint(5) unsigned NOT NULL DEFAULT 0)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS jids(" + + "user_id int(10) unsigned NOT NULL AUTO_INCREMENT," + + "jid char(64) NOT NULL," + + "active tinyint(1) unsigned NOT NULL DEFAULT '1'," + + "loginhash char(36) unsigned DEFAULT NULL," + + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS replies (" + + "message_id int(10) unsigned NOT NULL," + + "reply_id smallint(5) unsigned NOT NULL," + + "user_id int(10) unsigned NOT NULL," + + "replyto smallint(5) unsigned NOT NULL DEFAULT 0," + + "ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + + "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + + "txt text)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS twitter(" + + "user_id int(10) unsigned NOT NULL," + + "access_token char(64) NOT NULL," + + "access_token_secret char(64) NOT NULL," + + "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + + "uname char(64) NOT NULL," + + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS facebook(" + + "user_id int(10) unsigned NOT NULL," + + "fb_id int(20) unsigned NOT NULL," + + "access_token char(255) NOT NULL," + + "loginhash char(36) NOT NULL," + + "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + + "fb_name char(64) NOT NULL," + + "fb_link char(64) NOT NULL," + + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS vk(" + + "user_id int(10) unsigned NOT NULL," + + "vk_id int(20) unsigned NOT NULL," + + "access_token char(128) NOT NULL," + + "loginhash char(36) NOT NULL," + + "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + + "vk_name char(64) NOT NULL," + + "vk_link char(64) NOT NULL," + + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); + jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS emails (" + + " user_id int(10) unsigned NOT NULL," + + " email char(64) NOT NULL," + + " subscr_hour tinyint(4) DEFAULT NULL," + + " PRIMARY KEY (email)" + + ")"); + jdbcTemplate.execute("CREATE TABLE favorites (" + + " user_id int(10) unsigned NOT NULL," + + " message_id int(10) unsigned NOT NULL," + + " ts datetime NOT NULL" + + ")"); + jdbcTemplate.execute("CREATE TABLE logins (" + + " user_id int(10) unsigned NOT NULL," + + " hash char(16) NOT NULL," + + " PRIMARY KEY (user_id)" + + ")"); + jdbcTemplate.execute("CREATE TABLE `telegram` (" + + " `user_id` int(10) unsigned DEFAULT NULL," + + " `tg_id` bigint(20) NOT NULL," + + " `tg_name` char(64) NOT NULL," + + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + + " `loginhash` char(36) DEFAULT NULL" + + ")"); + jdbcTemplate.execute("CREATE TABLE `pm` (" + + " `user_id` int(10) unsigned NOT NULL," + + " `user_id_to` int(10) unsigned NOT NULL," + + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + + " `txt` text NOT NULL" + + ")"); + jdbcTemplate.execute("CREATE TABLE `pm_streams` (" + + " `user_id` int(10) unsigned NOT NULL," + + " `user_id_to` int(10) unsigned NOT NULL," + + " `lastmessage` datetime NOT NULL," + + " `lastview` datetime DEFAULT NULL," + + " `unread` smallint(5) unsigned NOT NULL DEFAULT '0'" + + ")"); + jdbcTemplate.execute("CREATE TABLE `android` (" + + " `user_id` int(10) unsigned NOT NULL," + + " `regid` char(255) NOT NULL," + + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + + ")"); + jdbcTemplate.execute("CREATE TABLE `ios` (" + + " `user_id` int(10) unsigned NOT NULL," + + " `token` char(64) NOT NULL," + + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + + ")"); + jdbcTemplate.execute("CREATE TABLE `winphone` (" + + " `user_id` int(10) unsigned NOT NULL," + + " `url` char(255) NOT NULL," + + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + + ")"); + jdbcTemplate.execute("CREATE TABLE `tags_ignore` (" + + " `tag_id` int(10) unsigned NOT NULL" + + ")"); + jdbcTemplate.execute("CREATE TABLE `users_subscr` (" + + " `user_id` int(10) unsigned NOT NULL," + + " `cnt` smallint(5) unsigned NOT NULL DEFAULT '0'," + + " PRIMARY KEY (`user_id`)" + + ")"); + return jdbcTemplate; } @Bean -- cgit v1.2.3