More implementation of db handling and tests

master
Justyna Ilczuk 2013-04-26 16:04:21 +02:00
parent 6c8437ffae
commit 41608e3dc3
7 changed files with 295 additions and 42 deletions

BIN
MobiReader/hello.db Normal file

Binary file not shown.

View File

@ -40,6 +40,7 @@ file.reference.commons-io-1.2.jar=/home/att/studia/semestr4/Java/resources/preon
file.reference.commons-io-1.3.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/commons-io-1.3.1.jar file.reference.commons-io-1.3.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/commons-io-1.3.1.jar
file.reference.commons-logging-1.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/commons-logging-1.1.jar file.reference.commons-logging-1.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/commons-logging-1.1.jar
file.reference.guava-14.0.1.jar=/home/att/studia/semestr4/Java/compression/guava-14.0.1.jar file.reference.guava-14.0.1.jar=/home/att/studia/semestr4/Java/compression/guava-14.0.1.jar
file.reference.joda-time-2.2.jar=/home/att/studia/semestr4/Java/resources/joda-time-2.2/joda-time-2.2.jar
file.reference.limbo-1.1-20081112.085229-2.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/limbo-1.1-20081112.085229-2.jar file.reference.limbo-1.1-20081112.085229-2.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/limbo-1.1-20081112.085229-2.jar
file.reference.log4j-1.2.12.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/log4j-1.2.12.jar file.reference.log4j-1.2.12.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/log4j-1.2.12.jar
file.reference.logkit-1.0.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/logkit-1.0.1.jar file.reference.logkit-1.0.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/logkit-1.0.1.jar
@ -48,6 +49,7 @@ file.reference.preon-binding-1.0-SNAPSHOT.jar=/home/att/studia/semestr4/Java/res
file.reference.preon-bitbuffer-1.0-SNAPSHOT.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/preon-bitbuffer-1.0-SNAPSHOT.jar file.reference.preon-bitbuffer-1.0-SNAPSHOT.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/preon-bitbuffer-1.0-SNAPSHOT.jar
file.reference.preon-bitbuffer-1.0-SNAPSHOT.jar-1=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/preon-bitbuffer-1.0-SNAPSHOT.jar file.reference.preon-bitbuffer-1.0-SNAPSHOT.jar-1=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/preon-bitbuffer-1.0-SNAPSHOT.jar
file.reference.servlet-api-2.3.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/servlet-api-2.3.jar file.reference.servlet-api-2.3.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/servlet-api-2.3.jar
file.reference.sqlite-jdbc-3.7.2.jar=/home/att/studia/semestr4/Java/resources/sqlite-jdbc-3.7.2.jar
file.reference.stax-api-1.0.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/stax-api-1.0.1.jar file.reference.stax-api-1.0.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/stax-api-1.0.1.jar
file.reference.stringtemplate-3.0.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/stringtemplate-3.0.jar file.reference.stringtemplate-3.0.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/stringtemplate-3.0.jar
file.reference.wstx-asl-3.2.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/wstx-asl-3.2.1.jar file.reference.wstx-asl-3.2.1.jar=/home/att/studia/semestr4/Java/resources/preon-1.0-SNAPSHOT/lib/ext/wstx-asl-3.2.1.jar
@ -80,7 +82,9 @@ javac.classpath=\
${file.reference.servlet-api-2.3.jar}:\ ${file.reference.servlet-api-2.3.jar}:\
${file.reference.stax-api-1.0.1.jar}:\ ${file.reference.stax-api-1.0.1.jar}:\
${file.reference.stringtemplate-3.0.jar}:\ ${file.reference.stringtemplate-3.0.jar}:\
${file.reference.wstx-asl-3.2.1.jar} ${file.reference.wstx-asl-3.2.1.jar}:\
${file.reference.joda-time-2.2.jar}:\
${file.reference.sqlite-jdbc-3.7.2.jar}
# Space-separated list of extra javac options # Space-separated list of extra javac options
javac.compilerargs= javac.compilerargs=
javac.deprecation=false javac.deprecation=false

Binary file not shown.

View File

@ -22,12 +22,30 @@ public class Author {
return this.name; return this.name;
} }
public Author(String name, String titleOfBook) public Author(String name, String additionalInfo)
{ {
writtenTitles.add(titleOfBook); this.additionalInfo = additionalInfo;
this.name = name; this.name = name;
} }
public Author(String name)
{
this.name = name;
}
public void addTitle(String title)
{
writtenTitles.add(title);
}
public void addTitles(ArrayList<String> titles)
{
for(String title : titles)
{
writtenTitles.add(title);
}
}
public Author(String name, ArrayList<String> titles) public Author(String name, ArrayList<String> titles)
{ {
writtenTitles = titles; writtenTitles = titles;
@ -39,6 +57,11 @@ public class Author {
this.additionalInfo = info; this.additionalInfo = info;
} }
public String getAdditionalInfo()
{
return this.additionalInfo;
}
public String provideAllInfo() public String provideAllInfo()
{ {
String separator = "\n"; String separator = "\n";

View File

@ -17,11 +17,60 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
class StatementBuilder
{
public PreparedStatement prepareFindingAuthorByNameStatement(Connection connection) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(
"select * from authors where name=?");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement prepareAddingAuthorStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"insert or replace into authors(name, additional_info) values( ?, ?)");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement prepareAddingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"insert or replace into books(title, author_name, path_to_content) values( ?, ?, ?)");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement prepareFindingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"select * from books where title=?");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement prepareFindingBookByAuthorStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"select * from books where author_name=?");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
}
public class SqliteDatabaseHandler public class SqliteDatabaseHandler
{ {
static String DATABASE_FILE = "sample.db"; static String DATABASE_FILE = "sample.db";
String driver; String driver;
StatementBuilder statBld = new StatementBuilder();
public SqliteDatabaseHandler(String driver) public SqliteDatabaseHandler(String driver)
{ {
this.driver = driver; this.driver = driver;
@ -38,23 +87,9 @@ public class SqliteDatabaseHandler
return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE); return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_FILE);
} }
public PreparedStatement prepareAddingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"insert into books values($next_id, ?, ?, ?)");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public PreparedStatement prepareFindingBookStatement(Connection connection)
throws SQLException, ClassNotFoundException
{
PreparedStatement statement = connection.prepareStatement(
"select * from books where title=?");
statement.setQueryTimeout(30); // set timeout to 30 sec.
return statement;
}
public void addBooks(ArrayList<Book> books) throws ClassNotFoundException public void addBooks(ArrayList<Book> books) throws ClassNotFoundException
{ {
@ -71,14 +106,16 @@ public class SqliteDatabaseHandler
Connection connection = prepareConnection(); Connection connection = prepareConnection();
try try
{ {
//TODO Author author = this.findAuthor(book.getAuthor().getName());
//Check if author is in db, if he is, check his id if(author == null)
int authorId = 1; {
this.addAuthor(book.getAuthor());
}
PreparedStatement stat; PreparedStatement stat;
stat= this.prepareAddingBookStatement(connection); stat= statBld.prepareAddingBookStatement(connection);
stat.setString(2, book.getTitle()); stat.setString(1, book.getTitle());
stat.setInt(3, authorId); stat.setString(2, book.getAuthor().getName());
stat.setString(4, book.getPathToContent()); stat.setString(3, book.getPathToContent());
stat.executeUpdate(); stat.executeUpdate();
} }
catch(SQLException e) { System.err.println(e.getMessage()); } catch(SQLException e) { System.err.println(e.getMessage()); }
@ -107,14 +144,10 @@ public class SqliteDatabaseHandler
try try
{ {
PreparedStatement stat; PreparedStatement stat;
stat= this.prepareFindingBookStatement(connection); stat= statBld.prepareFindingBookStatement(connection);
stat.setString(1, title); stat.setString(1, title);
stat.executeQuery(); stat.executeQuery();
ResultSet rs = stat.executeQuery(); ResultSet rs = stat.executeQuery();
int authorId = rs.getInt("author_id");
String path = rs.getString("path_to_content"); String path = rs.getString("path_to_content");
Author author = new Author("Gepetto", title); Author author = new Author("Gepetto", title);
return new Book(title, author, path); return new Book(title, author, path);
@ -137,13 +170,148 @@ public class SqliteDatabaseHandler
} }
return null; return null;
} }
public Book getBook(Integer id) public ArrayList<Book> findBooksByAuthor(Author author) throws ClassNotFoundException
{ {
return Book.exemplaryBook(); try
{
Connection connection = prepareConnection();
try
{
PreparedStatement stat;
stat = statBld.prepareFindingBookByAuthorStatement(connection);
stat.setString(1, author.getName());
ResultSet rs = stat.executeQuery();
ArrayList<Book> books = new ArrayList<>();
while(rs.next())
{
String path = rs.getString("path_to_content");
String title = rs.getString("title");
books.add(new Book(title, author, path));
}
return books;
}
catch(SQLException e) { System.err.println(e.getMessage()); }
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
}
}
catch (SQLException e)
{
System.err.println("Problem occured during making connection to db");
System.err.println(e.getMessage());
}
return null;
} }
public void addAuthor(Author author) throws ClassNotFoundException
{
try
{
Connection connection = prepareConnection();
try
{
PreparedStatement stat;
stat= statBld.prepareAddingAuthorStatement(connection);
stat.setString(1, author.getName());
stat.setString(2, author.getAdditionalInfo());
stat.executeUpdate();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
}
}
catch (SQLException e)
{
System.err.println("Problem occured during making connection to db");
System.err.println(e.getMessage());
}
}
public Author findAuthor(String name) throws ClassNotFoundException
{
try
{
Connection connection = prepareConnection();
try
{
PreparedStatement stat;
stat= statBld.prepareFindingAuthorByNameStatement(connection);
stat.setString(1, name);
stat.executeQuery();
ResultSet rs = stat.executeQuery();
String additional_info = rs.getString("additional_info");
return new Author(name, additional_info);
}
catch(SQLException e) { System.err.println(e.getMessage()); }
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
}
}
catch (SQLException e)
{
System.err.println("Problem occured during making connection to db");
System.err.println(e.getMessage());
}
return null;
}
public void createTablesInDB() throws SQLException, ClassNotFoundException
{
try
{
Connection connection = prepareConnection();
try
{
Statement stat = connection.createStatement();
stat.executeUpdate("drop table if exists books");
stat.executeUpdate("drop table if exists authors");
stat.executeUpdate("create table books (title string, " +
"author_name string, path_to_content string)");
stat.executeUpdate("create table authors (name string unique, " +
"additional_info string)");
}
catch(SQLException e) { System.err.println(e.getMessage()); }
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e) { System.err.println(e.getMessage()); }
}
}
catch (SQLException e)
{
System.err.println("Problem occured during making connection to db");
System.err.println(e.getMessage());
}
}
public static void main () throws Exception public static void main () throws Exception
{// load the sqlite-JDBC driver using the current class loader {// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
@ -155,10 +323,14 @@ public class SqliteDatabaseHandler
connection = DriverManager.getConnection("jdbc:sqlite:sample.db"); connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec. statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists books"); statement.executeUpdate("drop table if exists books");
statement.executeUpdate("drop table if exists authors");
statement.executeUpdate("create table books (id integer, title string, " + statement.executeUpdate("create table books (id integer, title string, " +
"author_id integer, path_to_content string)"); "author_id integer, path_to_content string)");
statement.executeUpdate("create table authors (id integer, name string, " +
"additional_info string)");
statement.executeUpdate("insert into books values(1, 'leo', 1, '/somepath/leo.txt')"); statement.executeUpdate("insert into books values(1, 'leo', 1, '/somepath/leo.txt')");
statement.executeUpdate("insert into books values(2, 'yui', 1, '/somepath/yui.txt')"); statement.executeUpdate("insert into books values(2, 'yui', 1, '/somepath/yui.txt')");
ResultSet rs = statement.executeQuery("select * from books"); ResultSet rs = statement.executeQuery("select * from books");

View File

@ -4,6 +4,7 @@
*/ */
package tests; package tests;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import mobireader.Author; import mobireader.Author;
@ -21,6 +22,7 @@ import static org.junit.Assert.*;
* @author att * @author att
*/ */
public class SqlConnectionJUnitTest { public class SqlConnectionJUnitTest {
SqliteDatabaseHandler handler = new SqliteDatabaseHandler();
public SqlConnectionJUnitTest() { public SqlConnectionJUnitTest() {
} }
@ -34,7 +36,8 @@ public class SqlConnectionJUnitTest {
} }
@Before @Before
public void setUp() { public void setUp() throws SQLException, ClassNotFoundException {
handler.createTablesInDB();
} }
@After @After
@ -52,8 +55,6 @@ public class SqlConnectionJUnitTest {
@Test @Test
public void testAddingBookToDb() throws Exception public void testAddingBookToDb() throws Exception
{ {
SqliteDatabaseHandler handler = new SqliteDatabaseHandler();
String title = "Title"; String title = "Title";
String authorName = "Gepetto"; String authorName = "Gepetto";
Author gepetto = new Author(authorName, title); Author gepetto = new Author(authorName, title);
@ -66,9 +67,62 @@ public class SqlConnectionJUnitTest {
assertEquals(path, bookFromDB.getPathToContent()); assertEquals(path, bookFromDB.getPathToContent());
} }
@Test
public void testAddingFewBooksOfSameAuthorToDb() throws ClassNotFoundException
{
String title1 = "Title1";
String title2 = "Title2";
String title3 = "Title3";
String authorName = "Gepetto";
Author gepetto = new Author(authorName);
String path = "/somepath/book.txt";
ArrayList<Book> books = new ArrayList<>();
books.add(new Book(title1, gepetto, path));
books.add(new Book(title2, gepetto, path));
books.add(new Book(title3, gepetto, path));
handler.addBooks(books);
ArrayList<Book> booksFromDB = handler.findBooksByAuthor(gepetto);
assertEquals(books.size(), booksFromDB.size());
for(int i = 0; i < books.size(); i++ )
{
assertEquals(books.get(i).getTitle(), booksFromDB.get(i).getTitle());
assertEquals(books.get(i).getAuthor().getName(), booksFromDB.get(i).getAuthor().getName());
}
}
@Test
public void testAddingAuthorToDb() throws Exception
{
String authorName = "Geronimo";
String additionalInfo = "Likes wood";
Author authorToBeAdded = new Author(authorName, additionalInfo);
handler.addAuthor(authorToBeAdded);
Author authorFromDB = handler.findAuthor(authorName);
assertEquals(authorName, authorFromDB.getName());
assertEquals(additionalInfo, authorFromDB.getAdditionalInfo());
}
@Test
public void testAddingTheSameAuthorToDbFewTimes() throws Exception
{
String authorName = "Geronimo";
String additionalInfo = "Likes wood";
Author authorToBeAdded = new Author(authorName, additionalInfo);
handler.addAuthor(authorToBeAdded);
String newerInfo = authorToBeAdded.getAdditionalInfo() +
", and trees, and art.";
authorToBeAdded.addAdditionalInfo(newerInfo);
handler.addAuthor(authorToBeAdded);
Author anotherAuthor = new Author("Alfredo", "Enjoy waterfalls");
handler.addAuthor(anotherAuthor);
Author authorFromDB = handler.findAuthor(authorName);
assertEquals(authorName, authorFromDB.getName());
assertEquals(newerInfo, authorFromDB.getAdditionalInfo());
}
public void testAddingFewBooksToDB() throws Exception public void testAddingFewBooksToDB() throws Exception
{ {
SqliteDatabaseHandler handler = new SqliteDatabaseHandler();
ArrayList<Book> books = Book.createSomeExamples(); ArrayList<Book> books = Book.createSomeExamples();
handler.addBooks(books); handler.addBooks(books);
for(Book book : books) for(Book book : books)

BIN
Notes/planowanie1.ora Normal file

Binary file not shown.