Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added libs/test/byte-buddy-1.12.19.jar
Binary file not shown.
Binary file added libs/test/byte-buddy-agent-1.12.19.jar
Binary file not shown.
Binary file added libs/test/greenmail-standalone-1.6.15.jar
Binary file not shown.
Binary file added libs/test/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added libs/test/junit-4.13.2.jar
Binary file not shown.
Binary file added libs/test/mockito-core-4.11.0.jar
Binary file not shown.
Binary file added libs/test/objenesis-3.3.jar
Binary file not shown.
5 changes: 4 additions & 1 deletion nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ javac.source=1.8
javac.target=1.8
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
${build.classes.dir}:\
libs/test/junit-4.13.2.jar:\
libs/test/hamcrest-core-1.3.jar:\
libs/test/greenmail-standalone-1.6.15.jar
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
Expand Down
13 changes: 13 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
mkdir -p build/test/stubs
mkdir -p build/test/classes

javac -d build/test/stubs test/stubs/library/assistant/database/DatabaseHandler.java test/stubs/library/assistant/ui/listmember/MemberListController.java

javac -sourcepath "" -cp "libs/*:libs/test/*:build/test/stubs" -d build/test/classes \
src/library/assistant/data/model/Book.java \
src/library/assistant/data/model/MailServerInfo.java \
src/library/assistant/database/DataHelper.java \
test/library/assistant/database/DataHelperTest.java

java -cp "build/test/classes:build/test/stubs:libs/*:libs/test/*" org.junit.runner.JUnitCore library.assistant.database.DataHelperTest
32 changes: 2 additions & 30 deletions src/library/assistant/alert/AlertMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,7 @@ public static void showErrorMessage(String title, String content) {
}

public static void showErrorMessage(Exception ex) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error occured");
alert.setHeaderText("Error Occured");
alert.setContentText(ex.getLocalizedMessage());

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();

Label label = new Label("The exception stacktrace was:");

TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);

textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);

GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);

alert.getDialogPane().setExpandableContent(expContent);

styleAlert(alert);
alert.showAndWait();
showErrorMessage(ex, "Error Occured", ex.getLocalizedMessage());
}

public static void showErrorMessage(Exception ex, String title, String content) {
Expand Down Expand Up @@ -107,6 +78,7 @@ public static void showErrorMessage(Exception ex, String title, String content)
expContent.add(textArea, 0, 1);

alert.getDialogPane().setExpandableContent(expContent);
styleAlert(alert);
alert.showAndWait();
}

Expand Down
7 changes: 6 additions & 1 deletion src/library/assistant/database/DataHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package library.assistant.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -20,8 +21,12 @@ public class DataHelper {
private final static Logger LOGGER = LogManager.getLogger(DatabaseHandler.class.getName());

public static boolean insertNewBook(Book book) {
return insertNewBook(book, DatabaseHandler.getInstance().getConnection());
}

public static boolean insertNewBook(Book book, Connection conn) {
try {
PreparedStatement statement = DatabaseHandler.getInstance().getConnection().prepareStatement(
PreparedStatement statement = conn.prepareStatement(
"INSERT INTO BOOK(id,title,author,publisher,isAvail) VALUES(?,?,?,?,?)");
statement.setString(1, book.getId());
statement.setString(2, book.getTitle());
Expand Down
14 changes: 5 additions & 9 deletions src/library/assistant/encryption/EncryptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Random;
import java.security.SecureRandom;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.crypto.Cipher;
Expand Down Expand Up @@ -117,11 +114,10 @@ private static byte[] generateSecureKey() throws NoSuchAlgorithmException {
return data;
}

private static byte[] prepareIV() throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
String randomVal = String.valueOf(new Random(System.currentTimeMillis()).nextLong());
byte[] hash = digest.digest(randomVal.getBytes(StandardCharsets.UTF_8));
return Arrays.copyOfRange(hash, 0, 16);
private static byte[] prepareIV() {
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
return iv;
}

private static void writeKey(CipherSpec spec) throws Exception {
Expand Down
5 changes: 0 additions & 5 deletions src/library/assistant/ui/addmember/MemberAddLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package library.assistant.ui.addmember;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -25,8 +24,4 @@ public void start(Stage stage) throws Exception {
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}
63 changes: 39 additions & 24 deletions src/library/assistant/ui/issuedlist/IssuedListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import javafx.concurrent.Task;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
Expand Down Expand Up @@ -81,31 +82,45 @@ public void setBookReturnCallback(BookReturnCallback callback) {

private void loadData() {
list.clear();
DatabaseHandler handler = DatabaseHandler.getInstance();
String qu = "SELECT ISSUE.bookID, ISSUE.memberID, ISSUE.issueTime, MEMBER.name, BOOK.title FROM ISSUE\n"
+ "LEFT OUTER JOIN MEMBER\n"
+ "ON MEMBER.id = ISSUE.memberID\n"
+ "LEFT OUTER JOIN BOOK\n"
+ "ON BOOK.id = ISSUE.bookID";
ResultSet rs = handler.execQuery(qu);
Preferences pref = Preferences.getPreferences();
try {
int counter = 0;
while (rs.next()) {
counter += 1;
String memberName = rs.getString("name");
String bookID = rs.getString("bookID");
String bookTitle = rs.getString("title");
Timestamp issueTime = rs.getTimestamp("issueTime");
System.out.println("Issued on " + issueTime);
Integer days = Math.toIntExact(TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - issueTime.getTime())) + 1;
Float fine = LibraryAssistantUtil.getFineAmount(days);
IssueInfo issueInfo = new IssueInfo(counter, bookID, bookTitle, memberName, LibraryAssistantUtil.formatDateTimeString(new Date(issueTime.getTime())), days, fine);
list.add(issueInfo);

Task<List<IssueInfo>> task = new Task<List<IssueInfo>>() {
@Override
protected List<IssueInfo> call() throws Exception {
List<IssueInfo> tempList = new ArrayList<>();
DatabaseHandler handler = DatabaseHandler.getInstance();
String qu = "SELECT ISSUE.bookID, ISSUE.memberID, ISSUE.issueTime, MEMBER.name, BOOK.title FROM ISSUE\n"
+ "LEFT OUTER JOIN MEMBER\n"
+ "ON MEMBER.id = ISSUE.memberID\n"
+ "LEFT OUTER JOIN BOOK\n"
+ "ON BOOK.id = ISSUE.bookID";
ResultSet rs = handler.execQuery(qu);
Preferences pref = Preferences.getPreferences();
try {
int counter = 0;
while (rs.next()) {
counter += 1;
String memberName = rs.getString("name");
String bookID = rs.getString("bookID");
String bookTitle = rs.getString("title");
Timestamp issueTime = rs.getTimestamp("issueTime");
System.out.println("Issued on " + issueTime);
Integer days = Math.toIntExact(TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - issueTime.getTime())) + 1;
Float fine = LibraryAssistantUtil.getFineAmount(days, pref);
IssueInfo issueInfo = new IssueInfo(counter, bookID, bookTitle, memberName, LibraryAssistantUtil.formatDateTimeString(new Date(issueTime.getTime())), days, fine);
tempList.add(issueInfo);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return tempList;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
};

task.setOnSucceeded(event -> {
list.setAll(task.getValue());
});

new Thread(task).start();
}

@FXML
Expand Down
5 changes: 0 additions & 5 deletions src/library/assistant/ui/issuedlist/IssuedListLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -21,8 +20,4 @@ public void start(Stage stage) throws Exception {
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}
5 changes: 0 additions & 5 deletions src/library/assistant/ui/listbook/BookListLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -21,8 +20,4 @@ public void start(Stage stage) throws Exception {
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}
5 changes: 0 additions & 5 deletions src/library/assistant/ui/listmember/MemberListLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package library.assistant.ui.listmember;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -21,8 +20,4 @@ public void start(Stage stage) throws Exception {
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package library.assistant.ui.notifoverdue;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -19,8 +18,4 @@ public void start(Stage stage) throws Exception {
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}
5 changes: 0 additions & 5 deletions src/library/assistant/ui/settings/SettingsLoader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package library.assistant.ui.settings;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -25,8 +24,4 @@ public void start(Stage stage) throws Exception {
}).start();
}

public static void main(String[] args) {
launch(args);
}

}
4 changes: 4 additions & 0 deletions src/library/assistant/util/LibraryAssistantUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public static Object loadWindow(URL loc, String title, Stage parentStage) {

public static Float getFineAmount(int totalDays) {
Preferences pref = Preferences.getPreferences();
return getFineAmount(totalDays, pref);
}

public static Float getFineAmount(int totalDays, Preferences pref) {
Integer fineDays = totalDays - pref.getnDaysWithoutFine();
Float fine = 0f;
if (fineDays > 0) {
Expand Down
67 changes: 67 additions & 0 deletions test/library/assistant/database/DataHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package library.assistant.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import library.assistant.data.model.Book;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class DataHelperTest {

@Test
public void testInsertNewBook() throws SQLException {
// Arrange
Connection mockConn = mock(Connection.class);
PreparedStatement mockStmt = mock(PreparedStatement.class);
Book book = new Book("B100", "Test Title", "Test Author", "Test Publisher", true);

when(mockConn.prepareStatement(anyString())).thenReturn(mockStmt);
when(mockStmt.executeUpdate()).thenReturn(1);

// Act
boolean result = DataHelper.insertNewBook(book, mockConn);

// Assert
assertTrue(result);
verify(mockStmt).setString(1, "B100");
verify(mockStmt).setString(2, "Test Title");
verify(mockStmt).setString(3, "Test Author");
verify(mockStmt).setString(4, "Test Publisher");
verify(mockStmt).setBoolean(5, true);
verify(mockStmt).executeUpdate();
}

@Test
public void testInsertNewBookFailure() throws SQLException {
// Arrange
Connection mockConn = mock(Connection.class);
PreparedStatement mockStmt = mock(PreparedStatement.class);
Book book = new Book("B100", "Test Title", "Test Author", "Test Publisher", true);

when(mockConn.prepareStatement(anyString())).thenReturn(mockStmt);
when(mockStmt.executeUpdate()).thenReturn(0);

// Act
boolean result = DataHelper.insertNewBook(book, mockConn);

// Assert
assertFalse(result);
}

@Test
public void testInsertNewBookException() throws SQLException {
// Arrange
Connection mockConn = mock(Connection.class);
Book book = new Book("B100", "Test Title", "Test Author", "Test Publisher", true);

when(mockConn.prepareStatement(anyString())).thenThrow(new SQLException("DB Error"));

// Act
boolean result = DataHelper.insertNewBook(book, mockConn);

// Assert
assertFalse(result);
}
}
Loading