From 71fb63443ba2e00af1985cfc54ed77a0b640f898 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 2 Sep 2016 06:50:49 +0200 Subject: [PATCH] Added a test feature reachable by using `--test 1` to test entries in a local cache4.db file for compatibility. --- .gitignore | 1 + .../CommandLineController.java | 9 ++++ .../telegram_backup/CommandLineOptions.java | 7 +++ .../telegram_backup/TestFeatures.java | 54 +++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 src/main/java/de/fabianonline/telegram_backup/TestFeatures.java diff --git a/.gitignore b/.gitignore index f0bec91..6689a05 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ data/ .DS_Store .idea src/main/main.iml +cache4.* diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index 4224050..1fa63f9 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -128,6 +128,15 @@ public class CommandLineController { } } + if (CommandLineOptions.val_test != null) { + if (CommandLineOptions.val_test == 1) { + TestFeatures.test1(); + } else { + System.out.println("Unknown test " + CommandLineOptions.val_test); + } + System.exit(1); + } + logger.debug("CommandLineOptions.val_export: {}", CommandLineOptions.val_export); if (CommandLineOptions.val_export != null) { if (CommandLineOptions.val_export.toLowerCase().equals("html")) { diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java index 281dabd..a1f43c4 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineOptions.java @@ -33,6 +33,7 @@ class CommandLineOptions { public static Integer val_limit_messages = null; public static String val_target = null; public static String val_export = null; + public static Integer val_test = null; public static void parseOptions(String[] args) { String last_cmd = null; @@ -51,6 +52,9 @@ class CommandLineOptions { case "--export": val_export = arg; break; + + case "--test": + val_test = Integer.parseInt(arg); break; } last_cmd = null; continue; @@ -101,6 +105,9 @@ class CommandLineOptions { case "--no-media": cmd_no_media = true; break; + + case "--test": + last_cmd = "--test"; continue; default: throw new RuntimeException("Unknown command " + arg); diff --git a/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java b/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java new file mode 100644 index 0000000..c71ab8e --- /dev/null +++ b/src/main/java/de/fabianonline/telegram_backup/TestFeatures.java @@ -0,0 +1,54 @@ +package de.fabianonline.telegram_backup; + +import com.github.badoualy.telegram.tl.api.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; +import java.sql.SQLException; +import java.sql.ResultSet; +import java.io.IOException; + +class TestFeatures { + public static void test1() { + // Tests entries in a cache4.db in the current working directory for compatibility + try { + Class.forName("org.sqlite.JDBC"); + } catch(ClassNotFoundException e) { + CommandLineController.show_error("Could not load jdbc-sqlite class."); + } + + String path = "jdbc:sqlite:cache4.db"; + + Connection conn = null; + Statement stmt = null; + + try { + conn = DriverManager.getConnection(path); + stmt = conn.createStatement(); + } catch (SQLException e) { + CommandLineController.show_error("Could not connect to SQLITE database."); + } + + int unsupported_constructor = 0; + int success = 0; + + try { + ResultSet rs = stmt.executeQuery("SELECT data FROM messages"); + while (rs.next()) { + try { + TLApiContext.getInstance().deserializeMessage(rs.getBytes(1)); + } catch (com.github.badoualy.telegram.tl.exception.UnsupportedConstructorException e) { + unsupported_constructor++; + } catch (IOException e) { + System.out.println("IOException: " + e); + } + success++; + } + } catch (SQLException e) { + System.out.println("SQL exception: " + e); + } + + System.out.println("Success: " + success); + System.out.println("Unsupported constructor: " + unsupported_constructor); + } +}