diff --git a/test1/Test1Database.mwb b/test1/Test1Database.mwb new file mode 100644 index 0000000..1f207b9 Binary files /dev/null and b/test1/Test1Database.mwb differ diff --git a/test1/bin/test1/FileSystemController.class b/test1/bin/test1/FileSystemController.class new file mode 100644 index 0000000..4efaf1e Binary files /dev/null and b/test1/bin/test1/FileSystemController.class differ diff --git a/test1/bin/test1/FileSystemDatabase.class b/test1/bin/test1/FileSystemDatabase.class new file mode 100644 index 0000000..60a5521 Binary files /dev/null and b/test1/bin/test1/FileSystemDatabase.class differ diff --git a/test1/bin/test1/FileSystemGUI$1.class b/test1/bin/test1/FileSystemGUI$1.class new file mode 100644 index 0000000..173444f Binary files /dev/null and b/test1/bin/test1/FileSystemGUI$1.class differ diff --git a/test1/bin/test1/FileSystemGUI$2.class b/test1/bin/test1/FileSystemGUI$2.class new file mode 100644 index 0000000..10f261d Binary files /dev/null and b/test1/bin/test1/FileSystemGUI$2.class differ diff --git a/test1/bin/test1/FileSystemGUI$3.class b/test1/bin/test1/FileSystemGUI$3.class new file mode 100644 index 0000000..4b4963a Binary files /dev/null and b/test1/bin/test1/FileSystemGUI$3.class differ diff --git a/test1/bin/test1/FileSystemGUI.class b/test1/bin/test1/FileSystemGUI.class new file mode 100644 index 0000000..c4c0b6f Binary files /dev/null and b/test1/bin/test1/FileSystemGUI.class differ diff --git a/test1/bin/test1/JUnitTesting/DeleteFileSystemTest.class b/test1/bin/test1/JUnitTesting/DeleteFileSystemTest.class new file mode 100644 index 0000000..269f5eb Binary files /dev/null and b/test1/bin/test1/JUnitTesting/DeleteFileSystemTest.class differ diff --git a/test1/bin/test1/JUnitTesting/IsFileTest.class b/test1/bin/test1/JUnitTesting/IsFileTest.class new file mode 100644 index 0000000..7db0199 Binary files /dev/null and b/test1/bin/test1/JUnitTesting/IsFileTest.class differ diff --git a/test1/bin/test1/JUnitTesting/LoadFileSystemTest.class b/test1/bin/test1/JUnitTesting/LoadFileSystemTest.class new file mode 100644 index 0000000..6f32e7b Binary files /dev/null and b/test1/bin/test1/JUnitTesting/LoadFileSystemTest.class differ diff --git a/test1/bin/test1/JUnitTesting/SaveFileSystemTest.class b/test1/bin/test1/JUnitTesting/SaveFileSystemTest.class new file mode 100644 index 0000000..6b294ab Binary files /dev/null and b/test1/bin/test1/JUnitTesting/SaveFileSystemTest.class differ diff --git a/test1/bin/test1/JUnitTesting/SearchFileSystemTest.class b/test1/bin/test1/JUnitTesting/SearchFileSystemTest.class new file mode 100644 index 0000000..5f2b8ab Binary files /dev/null and b/test1/bin/test1/JUnitTesting/SearchFileSystemTest.class differ diff --git a/test1/bin/test1/JUnitTesting/SpaceFile.txt b/test1/bin/test1/JUnitTesting/SpaceFile.txt new file mode 100644 index 0000000..86b936c --- /dev/null +++ b/test1/bin/test1/JUnitTesting/SpaceFile.txt @@ -0,0 +1,16 @@ + + + +H:\ + + Programs + + + + + Images + + + + Games + diff --git a/test1/bin/test1/JUnitTesting/Test1File.txt b/test1/bin/test1/JUnitTesting/Test1File.txt new file mode 100644 index 0000000..70d6a42 --- /dev/null +++ b/test1/bin/test1/JUnitTesting/Test1File.txt @@ -0,0 +1,20 @@ +C:\ + Documents + Images + Image1.jpg + Image2.jpg + Image3.png + + Works + Letter.doc + Accountant + Accounting.xls + AnnualReport.xls + Program Files + Skype + Skype.exe + Readme.txt + Mysql + Mysql.exe + Mysql.com + diff --git a/test1/bin/test1/mysql-connector-java-8.0.20.jar b/test1/bin/test1/mysql-connector-java-8.0.20.jar new file mode 100644 index 0000000..f4bd739 Binary files /dev/null and b/test1/bin/test1/mysql-connector-java-8.0.20.jar differ diff --git a/test1/src/test1/FileSystemController.java b/test1/src/test1/FileSystemController.java new file mode 100644 index 0000000..4478a4d --- /dev/null +++ b/test1/src/test1/FileSystemController.java @@ -0,0 +1,173 @@ +package test1; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class FileSystemController { + + private ArrayList ListOfPaths = new ArrayList(); + private FileSystemDatabase FileSystemDatabase; + + public FileSystemController() + { + FileSystemDatabase = new FileSystemDatabase(); + //empties the FileSystem table on start up, only for testing purposes + FileSystemDatabase.DeleteFileSystem(); + } + + public static int NumberOfTabs(String line) + { + int tabs = (int)line.chars().filter(c -> c == (int)'\t').count(); + return tabs; + } + + public static String RemoveTabs(String line) + { + String lineContents = line.replace("\t", ""); + return lineContents; + } + + /* Checks if the string supplied contains a "." + * Not a perfect solution as directories can contain "." + * A better method would be to compare the end of the files with known file extensions + * */ + public static boolean IsFile(String line) + { + if (line.contains(".")) + { + return true; + } + return false; + } + + /* Takes a file as input and calls the recursive function ExtractFileSystemStructure on that file*/ + public void LoadFileSystem(File file) throws IOException + { + try + { + FileReader fr = new FileReader(file); + BufferedReader buffer = new BufferedReader(fr); + String path = ""; + ExtractFileSystemStructure(buffer, path, path, -1); + fr.close(); + System.out.println("ListOfPaths: " + ListOfPaths); + } + catch(FileNotFoundException e) + { + e.printStackTrace(); + return; + } + } + + public ArrayList SearchFileSystem(String filter) + { + return FileSystemDatabase.SearchFileSystem(filter); + } + + public void SaveCurrentFileSystem() + { + FileSystemDatabase.SaveFileSystem(ListOfPaths); + } + + /* Recursive function that adds all paths found in a file system to the ListOfPaths list */ + private boolean ExtractFileSystemStructure(BufferedReader buffer, String parentDirectoryPath, String currentPath, int previousLineDepth) throws IOException + { + // Marks the current position of the buffered reader + buffer.mark(1); + String line = buffer.readLine(); + + // BASE CASE // + //If we reach the end of the file, stop + if (line == null) + { + return false; + } + + // EDGE CASE // + //If we read a blank line, skip this line + if (RemoveTabs(line).length() == 0) + { + return ExtractFileSystemStructure(buffer, parentDirectoryPath, currentPath, previousLineDepth); + } + + + //If we read a line whose number of tabs is less than the previous line, we stop and go back one line + if (NumberOfTabs(line) < previousLineDepth) + { + buffer.reset(); + return true; + } + + //If we read a line whose number of tabs is greater than the previous line, then we need to include the current file/directory to the current path + if (NumberOfTabs(line) > previousLineDepth) + { + if (IsFile(line)) + { + ListOfPaths.add(CreatePath(line, currentPath)); + return ExtractFileSystemStructure(buffer, currentPath, parentDirectoryPath, NumberOfTabs(line)); + } + else + { + String newPath = CreatePath(line, currentPath); + ListOfPaths.add(newPath); + //function will return true, if there is a directory/file outside of the current directory + //only returns false when the end of the file is reached + if(ExtractFileSystemStructure(buffer, currentPath, newPath, NumberOfTabs(line))) + { + return ExtractFileSystemStructure(buffer, currentPath, newPath, NumberOfTabs(line)); + } + return false; + } + } + + //If we read a line whose number of tabs is equal to the previous line, then we need don't need to include the entire current path, only the parent directories path + if (NumberOfTabs(line) == previousLineDepth) + { + if (IsFile(line)) + { + ListOfPaths.add(CreatePath(line, parentDirectoryPath)); + return ExtractFileSystemStructure(buffer, parentDirectoryPath, currentPath, NumberOfTabs(line)); + } + else + { + String newPath = CreatePath(line, parentDirectoryPath); + ListOfPaths.add(newPath); + //function will return true, if there is a directory/file outside of the current directory + + if(ExtractFileSystemStructure(buffer, parentDirectoryPath, newPath, NumberOfTabs(line))) + { + return ExtractFileSystemStructure(buffer, parentDirectoryPath, newPath, NumberOfTabs(line)); + } + return false; + } + } + + return false; + } + + /* Creates a path based on the current line and path supplied */ + private String CreatePath(String line, String path) + { + String newPath; + //this stops an extra \ from appearing if we are at the base of the file system + if (ListOfPaths.size() == 0 || ListOfPaths.size() == 1 || ListOfPaths.get(0) == path) + { + newPath = path + RemoveTabs(line); + } + else + { + newPath = path + "\\" + RemoveTabs(line); + } + return newPath; + } + + public ArrayList GetListOfPaths() + { + return ListOfPaths; + } + +} \ No newline at end of file diff --git a/test1/src/test1/FileSystemDatabase.java b/test1/src/test1/FileSystemDatabase.java new file mode 100644 index 0000000..0e149a6 --- /dev/null +++ b/test1/src/test1/FileSystemDatabase.java @@ -0,0 +1,107 @@ +package test1; +import java.sql.*; +import java.util.ArrayList; + +public class FileSystemDatabase { + + private Connection conn; + + public FileSystemDatabase() + { + try + { + System.out.println("Attempting to connect to database..."); + //Class.forName("com.mysql.jdbc.Driver"); + conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test1Database", "root", "root"); + + if (conn !=null) + { + System.out.println("Success"); + } + else + { + System.out.println("Failure"); + } + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + } + + public ArrayList SelectAll() + { + try { + Statement st = conn.createStatement(); + String sql; + sql = "SELECT * FROM FileSystem"; + ResultSet rs = st.executeQuery(sql); + ArrayList results = new ArrayList(); + while(rs.next()) + { + results.add(rs.getString("path")); + } + return results; + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public void SaveFileSystem(ArrayList ListOfPaths) + { + try { + String sql; + PreparedStatement preparedStmt; + for (String path : ListOfPaths) + { + sql = "INSERT INTO FileSystem (path) VALUES (?)"; + preparedStmt = conn.prepareStatement(sql); + preparedStmt.setString (1, path); + preparedStmt.execute(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* Empties the FileSystem table */ + public void DeleteFileSystem() + { + Statement st; + try { + st = conn.createStatement(); + String sql = "TRUNCATE FileSystem"; + st.executeUpdate(sql); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + /* Returns the paths of any rows that contain the filter supplied */ + public ArrayList SearchFileSystem(String filter) + { + Statement st; + try { + st = conn.createStatement(); + String sql = "SELECT path FROM FileSystem WHERE path LIKE '%" + filter+ "%'"; + ResultSet rs = st.executeQuery(sql); + ArrayList results = new ArrayList(); + while(rs.next()) + { + results.add(rs.getString("path")); + } + return results; + + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + +} diff --git a/test1/src/test1/FileSystemGUI.java b/test1/src/test1/FileSystemGUI.java new file mode 100644 index 0000000..7c415bd --- /dev/null +++ b/test1/src/test1/FileSystemGUI.java @@ -0,0 +1,135 @@ +package test1; + +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.border.EmptyBorder; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JFileChooser; + +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.ArrayList; +import java.awt.event.ActionEvent; +import javax.swing.JTextField; +import javax.swing.JList; +import javax.swing.JLabel; +import java.awt.Font; + +public class FileSystemGUI extends JFrame { + + private FileSystemController fileSystemController; + + private JPanel contentPane; + private JTextField TF_SearchFilter; + private DefaultListModel resultsModel; + private JButton BSearchFileSystem; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + FileSystemGUI frame = new FileSystemGUI(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /* Gets the search results based on the filter supplied and display the results in the resultsModel */ + private void BSearchFileSystemActionPerformed() + { + resultsModel.clear(); + ArrayList results = fileSystemController.SearchFileSystem(TF_SearchFilter.getText()); + for(String result : results) + { + resultsModel.addElement(result); + } + } + + /* Once a file is selected, it is saved to the DB and the search button is enabled */ + private void BLoadFileSystemActionPerformed() throws IOException + { + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + int response = chooser.showOpenDialog(null); + if (response == JFileChooser.APPROVE_OPTION) + { + fileSystemController.LoadFileSystem(chooser.getSelectedFile()); + fileSystemController.SaveCurrentFileSystem(); + BSearchFileSystem.setEnabled(true); + } + } + + + /** + * Create the frame. + */ + public FileSystemGUI() { + // + fileSystemController = new FileSystemController(); + // + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 714, 558); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + + JButton BLoadFileSystem = new JButton("Load New File System"); + BLoadFileSystem.setBounds(139, 114, 175, 45); + BLoadFileSystem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + try { + BLoadFileSystemActionPerformed(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); + contentPane.setLayout(null); + contentPane.add(BLoadFileSystem); + + TF_SearchFilter = new JTextField(); + TF_SearchFilter.setBounds(139, 58, 360, 45); + contentPane.add(TF_SearchFilter); + TF_SearchFilter.setColumns(10); + + BSearchFileSystem = new JButton("Search"); + BSearchFileSystem.setEnabled(false); + BSearchFileSystem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + BSearchFileSystemActionPerformed(); + } + }); + BSearchFileSystem.setBounds(324, 114, 175, 45); + contentPane.add(BSearchFileSystem); + + resultsModel = new DefaultListModel<>(); + + JScrollPane scrollPane = new JScrollPane(); + scrollPane.setBounds(139, 201, 360, 216); + contentPane.add(scrollPane); + JList mylist_1 = new JList<>( resultsModel ); + scrollPane.setViewportView(mylist_1); + + JLabel LBLSearchBox = new JLabel("Search Box:"); + LBLSearchBox.setFont(new Font("Tahoma", Font.BOLD, 12)); + LBLSearchBox.setBounds(139, 33, 85, 14); + contentPane.add(LBLSearchBox); + + JLabel LBLSearchResults = new JLabel("Search Results:"); + LBLSearchResults.setFont(new Font("Tahoma", Font.BOLD, 12)); + LBLSearchResults.setBounds(139, 176, 104, 14); + contentPane.add(LBLSearchResults); + } +} diff --git a/test1/src/test1/JUnitTesting/DeleteFileSystemTest.java b/test1/src/test1/JUnitTesting/DeleteFileSystemTest.java new file mode 100644 index 0000000..1a3fc2a --- /dev/null +++ b/test1/src/test1/JUnitTesting/DeleteFileSystemTest.java @@ -0,0 +1,30 @@ +package test1.JUnitTesting; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import test1.FileSystemDatabase; + +class DeleteFileSystemTest { + + @Test + void TestFileSystemIsDeleted() { + //ARRANGE + FileSystemDatabase fileSystemDatabase = new FileSystemDatabase(); + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Program Files"); + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + fileSystemDatabase.DeleteFileSystem(); + ArrayList results = fileSystemDatabase.SelectAll(); + //ASSERT + assertEquals(0, results.size()); + } + +} diff --git a/test1/src/test1/JUnitTesting/IsFileTest.java b/test1/src/test1/JUnitTesting/IsFileTest.java new file mode 100644 index 0000000..ff2babd --- /dev/null +++ b/test1/src/test1/JUnitTesting/IsFileTest.java @@ -0,0 +1,51 @@ +package test1.JUnitTesting; + +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.Test; + +import test1.FileSystemController; + +class IsFileTest { + + @Test + public void EmptyString() { + //ARRANGE + String testString = ""; + //ACT + boolean result = FileSystemController.IsFile(testString); + //ASSERT + assertEquals(false, result); + } + + @Test + public void NonFileString() { + //ARRANGE + String testString = "NotAFile"; + //ACT + boolean result = FileSystemController.IsFile(testString); + //ASSERT + assertEquals(false, result); + } + + @Test + public void FileString() { + //ARRANGE + String testString = "image.jpg"; + //ACT + boolean result = FileSystemController.IsFile(testString); + //ASSERT + assertEquals(true, result); + } + + //this is a limitation of the current implementation, explained in the IsFile method + @Test + public void DirectoryStringThatContainsAPeriod() { + //ARRANGE + String testString = "Directory 5.2"; + //ACT + boolean result = FileSystemController.IsFile(testString); + //ASSERT + assertEquals(false, result); + } +} diff --git a/test1/src/test1/JUnitTesting/LoadFileSystemTest.java b/test1/src/test1/JUnitTesting/LoadFileSystemTest.java new file mode 100644 index 0000000..56d396d --- /dev/null +++ b/test1/src/test1/JUnitTesting/LoadFileSystemTest.java @@ -0,0 +1,109 @@ +package test1.JUnitTesting; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import org.junit.Rule; +import org.junit.jupiter.api.Test; +import org.junit.rules.TemporaryFolder; + +import test1.FileSystemController; + +class LoadFileSystemTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + void EmptyFile() throws IOException { + try { + //ARRANGE + FileSystemController fileSystemController = new FileSystemController(); + File file = new File(getClass().getClassLoader().getResource("test1/JUnitTesting/EmptyFile.txt").toURI()); + + //ACT + fileSystemController.LoadFileSystem(file); + //ASSERT + assertEquals(0, fileSystemController.GetListOfPaths().size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + void TestCorrectPathsExtractedFromFileWithMultipleSpaces() throws IOException { + try { + //ARRANGE + FileSystemController fileSystemController = new FileSystemController(); + File file = new File(getClass().getClassLoader().getResource("test1/JUnitTesting/SpaceFile.txt").toURI()); + //ACT + fileSystemController.LoadFileSystem(file); + ArrayList ListOfPaths = fileSystemController.GetListOfPaths(); + //ASSERT + assertEquals("H:\\", ListOfPaths.get(0)); + assertEquals("H:\\Programs", ListOfPaths.get(1)); + assertEquals("H:\\Images", ListOfPaths.get(2)); + assertEquals("H:\\Games", ListOfPaths.get(3)); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + void TestCorrectNumberOfPathsExtractedFromFile() throws IOException { + try { + //ARRANGE + FileSystemController fileSystemController = new FileSystemController(); + File file = new File(getClass().getClassLoader().getResource("test1/JUnitTesting/Test1File.txt").toURI()); + + //ACT + fileSystemController.LoadFileSystem(file); + //ASSERT + assertEquals(18, fileSystemController.GetListOfPaths().size()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + void TestCorrectPathsExtractedFromFile() throws IOException { + try { + //ARRANGE + FileSystemController fileSystemController = new FileSystemController(); + File file = new File(getClass().getClassLoader().getResource("test1/JUnitTesting/Test1File.txt").toURI()); + + //ACT + fileSystemController.LoadFileSystem(file); + ArrayList ListOfPaths = fileSystemController.GetListOfPaths(); + //ASSERT + assertEquals("C:\\", ListOfPaths.get(0)); + assertEquals("C:\\Documents", ListOfPaths.get(1)); + assertEquals("C:\\Documents\\Images", ListOfPaths.get(2)); + assertEquals("C:\\Documents\\Images\\Image1.jpg", ListOfPaths.get(3)); + assertEquals("C:\\Documents\\Images\\Image2.jpg", ListOfPaths.get(4)); + assertEquals("C:\\Documents\\Images\\Image3.png", ListOfPaths.get(5)); + assertEquals("C:\\Documents\\Works", ListOfPaths.get(6)); + assertEquals("C:\\Documents\\Works\\Letter.doc", ListOfPaths.get(7)); + assertEquals("C:\\Documents\\Works\\Accountant", ListOfPaths.get(8)); + assertEquals("C:\\Documents\\Works\\Accountant\\Accounting.xls", ListOfPaths.get(9)); + assertEquals("C:\\Documents\\Works\\Accountant\\AnnualReport.xls", ListOfPaths.get(10)); + assertEquals("C:\\Program Files", ListOfPaths.get(11)); + assertEquals("C:\\Program Files\\Skype", ListOfPaths.get(12)); + assertEquals("C:\\Program Files\\Skype\\Skype.exe", ListOfPaths.get(13)); + assertEquals("C:\\Program Files\\Skype\\Readme.txt", ListOfPaths.get(14)); + assertEquals("C:\\Program Files\\Mysql", ListOfPaths.get(15)); + assertEquals("C:\\Program Files\\Mysql\\Mysql.exe", ListOfPaths.get(16)); + assertEquals("C:\\Program Files\\Mysql\\Mysql.com", ListOfPaths.get(17)); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/test1/src/test1/JUnitTesting/SaveFileSystemTest.java b/test1/src/test1/JUnitTesting/SaveFileSystemTest.java new file mode 100644 index 0000000..a58b12b --- /dev/null +++ b/test1/src/test1/JUnitTesting/SaveFileSystemTest.java @@ -0,0 +1,61 @@ +package test1.JUnitTesting; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import test1.FileSystemDatabase; + +class SaveFileSystemTest { + + private FileSystemDatabase fileSystemDatabase; + + @BeforeEach + void Start() { + fileSystemDatabase = new FileSystemDatabase(); + fileSystemDatabase.DeleteFileSystem(); + } + + @AfterEach + void Finish() { + fileSystemDatabase.DeleteFileSystem(); + } + + @Test + void TestCorrectPathsAreStoredInDB() { + //ARRANGE + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Program Files"); + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + ArrayList results = fileSystemDatabase.SelectAll(); + //ASSERT + assertEquals("C:\\", results.get(0)); + assertEquals("C:\\Documents", results.get(1)); + assertEquals("C:\\Documents\\Images", results.get(2)); + assertEquals("C:\\Program Files", results.get(3)); + } + + @Test + void TestCorrectNumberOfPathsAreStoredInDB() { + //ARRANGE + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Program Files"); + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + ArrayList results = fileSystemDatabase.SelectAll(); + //ASSERT + assertEquals(4, results.size()); + } + +} diff --git a/test1/src/test1/JUnitTesting/SearchFileSystemTest.java b/test1/src/test1/JUnitTesting/SearchFileSystemTest.java new file mode 100644 index 0000000..ae1c14a --- /dev/null +++ b/test1/src/test1/JUnitTesting/SearchFileSystemTest.java @@ -0,0 +1,99 @@ +package test1.JUnitTesting; + +import static org.junit.Assert.assertEquals; +import java.util.ArrayList; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import test1.FileSystemDatabase; + +class SearchFileSystemTest { + + private FileSystemDatabase fileSystemDatabase; + + @BeforeEach + void Start() { + fileSystemDatabase = new FileSystemDatabase(); + fileSystemDatabase.DeleteFileSystem(); + } + + @AfterEach + void Finish() { + fileSystemDatabase.DeleteFileSystem(); + } + + @Test + void TestCorrectPathsAreReturnedFromSearch() { + //ARRANGE + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Documents\\Images\\Image1.jpg"); + + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + ArrayList results = fileSystemDatabase.SearchFileSystem("Image"); + + //ASSERT + assertEquals("C:\\Documents\\Images", results.get(0)); + assertEquals("C:\\Documents\\Images\\Image1.jpg", results.get(1)); + } + + @Test + void TestCorrectNumberOfPathsAreReturnedFromSearch() { + //ARRANGE + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Documents\\Images\\Image1.jpg"); + + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + ArrayList results = fileSystemDatabase.SearchFileSystem("Image"); + + //ASSERT + assertEquals(2, results.size()); + } + + @Test + void TestCorrectPathsAreReturnedFromSearchWithEmptyFilter() { + //ARRANGE + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Documents\\Images\\Image1.jpg"); + + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + ArrayList results = fileSystemDatabase.SearchFileSystem(""); + + //ASSERT + assertEquals("C:\\", results.get(0)); + assertEquals("C:\\Documents", results.get(1)); + assertEquals("C:\\Documents\\Images", results.get(2)); + assertEquals("C:\\Documents\\Images\\Image1.jpg", results.get(3)); + } + + @Test + void TestCorrectNumberOfPathsAreReturnedFromSearchWithEmptyFilter() { + //ARRANGE + ArrayList ListOfPaths = new ArrayList(); + ListOfPaths.add("C:\\"); + ListOfPaths.add("C:\\Documents"); + ListOfPaths.add("C:\\Documents\\Images"); + ListOfPaths.add("C:\\Documents\\Images\\Image1.jpg"); + + //ACT + fileSystemDatabase.SaveFileSystem(ListOfPaths); + ArrayList results = fileSystemDatabase.SearchFileSystem(""); + + //ASSERT + assertEquals(4, results.size()); + } + +} diff --git a/test1/src/test1/mysql-connector-java-8.0.20.jar b/test1/src/test1/mysql-connector-java-8.0.20.jar new file mode 100644 index 0000000..f4bd739 Binary files /dev/null and b/test1/src/test1/mysql-connector-java-8.0.20.jar differ