Skip to content

Commit

Permalink
Merge pull request #11 from Javinator9889/database
Browse files Browse the repository at this point in the history
Database merge - completed tests and everything working OK
  • Loading branch information
Javinator9889 committed May 27, 2018
2 parents f77ef50 + 52de736 commit c896b13
Show file tree
Hide file tree
Showing 18 changed files with 638 additions and 401 deletions.
@@ -0,0 +1,88 @@
package javinator9889.securepass;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.Map;

import javinator9889.securepass.io.database.DatabaseManager;
import javinator9889.securepass.io.database.DatabaseOperations;

/**
* Created by Javinator9889 on 20/05/2018.
*/
@RunWith(AndroidJUnit4.class)
public class DatabaseTest {
private Context context = InstrumentationRegistry.getTargetContext();

private DatabaseOperations op;

@Before
public void initParams() {
DatabaseManager manager = DatabaseManager.newInstance(context, "1234");
this.op = DatabaseOperations.newInstance(manager);
}

@Test
public void insertIntoDB() {
long defaultCategoryId = op.registerDefaultCategory();
long newAccountId = op.registerNewAccount("cuenta", "password",
"icono", "descripción", defaultCategoryId);
long newCategoryId = op.registerNewCategory("cat2");
long newQRCodeId = op.registerQRCode(newAccountId, "qr1", "desc",
"1234");
long newSecurityCodeId = op.registerNewSecurityCodeSource("sec1");
long newFieldForSecCodeId = op.registerNewFieldForSecurityCodeSource("12345",
false,
newSecurityCodeId);
op.updateInformationForCategory("cat3", newCategoryId);
op.updateInformationForEntry("cuent", "pass", "icon",
"desc", newCategoryId, newAccountId);
op.updateInformationForQRCode(newAccountId, "qr1,2", "descq", "data",
newQRCodeId);
op.updateInformationForSecurityCode("secCode", newSecurityCodeId);
op.updateInformationForField("12321", true, newFieldForSecCodeId,
newSecurityCodeId);
printer();
op.registerNewAccount("cuenta", "password", "icono",
"descripción", defaultCategoryId);
op.registerNewCategory("cat2");
op.registerQRCode(newAccountId, "qr1", "desc",
"1234");
op.registerNewSecurityCodeSource("sec1");
op.registerNewFieldForSecurityCodeSource("12345",false,
newSecurityCodeId);
printer();
op.deleteCategory(newCategoryId);
op.deleteAccount(newAccountId);
op.deleteQRCode(newQRCodeId);
op.deleteSecurityCode(newSecurityCodeId);
op.deleteField(newFieldForSecCodeId);
printer();
}

private void printer() {
for (Map<String, Object> objectMap : op.getAllCategories())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllEntries())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllQRCodes())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllSecurityCodes())
System.out.println(objectMap);
for (Map<String, Object> objectMap : op.getAllFields())
System.out.println(objectMap);
}

@After
public void closeConnection() {
op.finishConnection();
}
}

This file was deleted.

Expand Up @@ -81,17 +81,18 @@ public void storeDataInDB() {
entry.getAccountPassword(),
entry.getIcon(),
entry.getDescription(),
entry.getCategory());
entry.getCategory().getId());
for (QRCode qrCode : qrCodes)
operations.registerQRCode(
qrCode.getEntry(),
qrCode.getEntry().getId(),
qrCode.getName(),
qrCode.getDescription(),
qrCode.getQrData());
for (SecurityCode code : securityCodes)
operations.registerNewSecurityCodeSource(code);
operations.registerNewSecurityCodeSource(code.getAccountName());
for (Field field : fields)
operations.registerNewFieldForSecurityCodeSource(field);
operations.registerNewFieldForSecurityCodeSource(field.getCode(), field.isCodeUsed(),
field.getSecurityCodeID());
operations.finishConnection();
}

Expand Down
39 changes: 23 additions & 16 deletions APP/app/src/main/java/javinator9889/securepass/io/IOManager.java
Expand Up @@ -16,6 +16,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javinator9889.securepass.R;
import javinator9889.securepass.util.cipher.PasswordCipher;
Expand All @@ -28,29 +30,34 @@

public class IOManager {
private Context activityContext;
private InputStream sqlScriptInputFile;
private File filesDir;
private File filesCache;

private IOManager(@NonNull Context activityContext) {
this.activityContext = activityContext;
this.filesDir = activityContext.getFilesDir();
int sqlScript = R.raw.database_script;
this.sqlScriptInputFile = activityContext.getResources().openRawResource(sqlScript);
this.filesCache = activityContext.getCacheDir();
}

@NonNull
public static IOManager newInstance(Context activityContext) {
return new IOManager(activityContext);
}

public String loadSQLScript() throws IOException {
BufferedReader sqlStringsInFile = new BufferedReader(
new InputStreamReader(sqlScriptInputFile));
StringBuilder completeFileRead = new StringBuilder();
String currentLine;
while ((currentLine = sqlStringsInFile.readLine()) != null)
completeFileRead.append(currentLine).append("\n");
return completeFileRead.toString();
public List<String> loadSQLScript() throws IOException {
List<String> result = new ArrayList<>(5);
int[] sqlScripts = new int[]{R.raw.create_category, R.raw.create_entry, R.raw.create_qrcode,
R.raw.create_security_code, R.raw.create_field};
for (int sqlScript : sqlScripts) {
InputStream sqlScriptInputFile = this.activityContext.getResources()
.openRawResource(sqlScript);
StringBuilder builder = new StringBuilder();
BufferedReader sqlStringsInFile = new BufferedReader(
new InputStreamReader(sqlScriptInputFile));
String currentLine;
while ((currentLine = sqlStringsInFile.readLine()) != null)
builder.append(currentLine).append("\n");
result.add(builder.toString());
}
return result;
}

public void storePassword(@NonNull String userPassword) {
Expand All @@ -65,7 +72,7 @@ public String readPassword() {
}

public void writeDownloadedClass(@NonNull InputStream from) {
String filename = filesDir.getAbsolutePath() + "/class.bck";
String filename = filesCache.getAbsolutePath() + "/class.bck";
try {
OutputStream to = new FileOutputStream(filename);
IOUtils.copyStream(from, to);
Expand All @@ -76,7 +83,7 @@ public void writeDownloadedClass(@NonNull InputStream from) {
}

public InputStream readDownloadedClass() {
String filename = filesDir.getAbsolutePath() + "/class.bck";
String filename = filesCache.getAbsolutePath() + "/class.bck";
try {
return new FileInputStream(filename);
} catch (FileNotFoundException e) {
Expand All @@ -86,7 +93,7 @@ public InputStream readDownloadedClass() {
}

public void deleteDownloadedClass() {
String filename = filesDir.getAbsolutePath() + "/class.bck";
String filename = filesCache.getAbsolutePath() + "/class.bck";

File fileToDelete = new File(filename);
if (!fileToDelete.delete())
Expand Down
Expand Up @@ -8,6 +8,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import javinator9889.securepass.io.IOManager;
import javinator9889.securepass.util.resources.ISharedPreferencesManager;
Expand Down Expand Up @@ -45,31 +46,32 @@ private void initDB() {
@Override
public void run() {
SQLiteDatabase.loadLibs(databaseContext);
File tempDatabaseFile = databaseContext.getDatabasePath(Constants.SQL.DB_FILENAME);
databaseFile = databaseContext
.getDatabasePath(Constants.SQL.DB_FILENAME);
try {
tempDatabaseFile.createNewFile();
databaseFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//tempDatabaseFile.mkdirs();
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
tempDatabaseFile,
databaseFile,
databasePassword,
null);
String databaseScript;
List<String> databaseScripts;
ISharedPreferencesManager preferencesManager = SharedPreferencesManager
.newInstance();
try {
if (!preferencesManager.isDatabaseInitialized()) {
databaseScript = IOManager.newInstance(databaseContext).loadSQLScript();
database.execSQL(databaseScript);
database.execSQL(Constants.SQL.DB_DEFAULT_CATEGORY, new Object[]{"Global"});
databaseScripts = IOManager.newInstance(databaseContext).loadSQLScript();
for (String script : databaseScripts) {
database.execSQL(script);
}
DatabaseManager.this.createDefaultCategory();
}
} catch (IOException e) {
throw new RuntimeException(e.getCause());
} finally {
database.close();
DatabaseManager.this.databaseFile = tempDatabaseFile;
}
}
});
Expand All @@ -88,12 +90,18 @@ public Thread getDatabaseInitializer() {
}

private class ThreadExceptionHandler implements Thread.UncaughtExceptionHandler {

@Override
public void uncaughtException(Thread t, Throwable e) {
Log.e(t.getName(), "Exception in thread \"" + t.getName() + "\" with " +
"exception thrown -> " + e.getMessage() + "\nFull trace: ");
e.printStackTrace();
}
}

private void createDefaultCategory() {
DatabaseOperations operations = DatabaseOperations.newInstance(this);
long id = operations.registerDefaultCategory();
Log.d("DB", "Category ID: " + id);
operations.finishConnection();
}
}

0 comments on commit c896b13

Please sign in to comment.