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
88 changes: 88 additions & 0 deletions BlobTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

import org.junit.jupiter.api.*;

public class BlobTest {

private Blob blob;

@BeforeEach
public void setUp() {
// Create a Blob instance for testing
blob = new Blob("testFile.txt");
}

// Test the 'getFileName' method
@Test
public void testGetFileName() {
// Verify that the 'getFileName' method returns the correct file name
assertEquals("testFile.txt", blob.getFileName(), "File name should match");
}

// Test the 'fileContents' method
@Test
public void testFileContents() throws IOException {
// Create a temporary test file with known content
String testContent = "This is a test file content.";
String fileName = "testFile.txt";
PrintWriter writer = new PrintWriter(fileName);
writer.print(testContent);
writer.close();

// Set the Blob instance to use the temporary test file
blob = new Blob(fileName);

// Verify that 'fileContents' returns the expected content
String content = blob.fileContents();
assertEquals(testContent, content, "File content should match");

// Clean up: delete the temporary test file
File fileToDelete = new File(fileName);
assertTrue(fileToDelete.delete(), "Test file should be deleted");
}

// Test the 'makeFile' method
@Test
public void testMakeFile() throws NoSuchAlgorithmException, IOException {
// Create a temporary test file with known content
String testContent = "This is a test file content.";
String fileName = "testFile.txt";
PrintWriter writer = new PrintWriter(fileName);
writer.print(testContent);
writer.close();

// Set the Blob instance to use the temporary test file
blob = new Blob(fileName);

// Call the 'makeFile' method to create a blob file
blob.makeFile();

// Verify that the blob file was created and contains the expected content
String blobFileName = blob.getSha1(testContent);
File blobFile = new File(blobFileName);
assertTrue(blobFile.exists(), "Blob file should exist");
assertEquals(testContent, blob.fileContents(), "Blob file content should match");

// Clean up: delete the temporary test file and the created blob file
File fileToDelete = new File(fileName);
assertTrue(fileToDelete.delete(), "Test file should be deleted");
assertTrue(blobFile.delete(), "Blob file should be deleted");
}

// Test the 'getSha1' method
@Test
public void testGetSha1() throws NoSuchAlgorithmException {
// Compute SHA-1 hash for a known input
String input = "Test Input";
String expectedSha1 = "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83";

// Verify that 'getSha1' returns the expected hash
String sha1 = blob.getSha1(input);
assertEquals(expectedSha1, sha1, "SHA-1 hash should match");
}
}
22 changes: 11 additions & 11 deletions Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ public Index() {

public void init() throws FileNotFoundException // credit from stackoverflow.com
{
// <<<<<<< master
// File objects = new File("C:\\Users\\danie\\OneDrive\\Desktop\\Topics Repos\\Programming-Git-Bari\\objects");
// =======
// File objects = new File("./Objects");
// >>>>>>> master
// <<<<<<< master
File objects = new File("C:\\Users\\danie\\OneDrive\\Desktop\\Topics Repos\\Programming-Git-Bari\\objects");
// =======
File objects = new File("./Objects");
// >>>>>>> master
if (!objects.exists()) {
objects.mkdirs();
}

// <<<<<<< master
// PrintWriter pw = new PrintWriter(
// "C:\\Users\\danie\\OneDrive\\Desktop\\Topics Repos\\Programming-Git-Bari\\index.txt");
// =======
// PrintWriter pw = new PrintWriter("./Index");
// >>>>>>> master
// <<<<<<< master
PrintWriter pw = new PrintWriter(
"C:\\Users\\danie\\OneDrive\\Desktop\\Topics Repos\\Programming-Git-Bari\\index.txt");
// =======
// PrintWriter pw = new PrintWriter("./Index");
// >>>>>>> master

String words = "";
pw.print(words);
Expand Down
48 changes: 48 additions & 0 deletions IndexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import static org.junit.Assert.*;

import java.io.File;

import org.junit.*;

public class IndexTest {

private Index index;

@Before
public void setUp() {
index = new Index();
}

@Test
public void testInit() throws Exception {
index.init();
File objectsDirectory = new File(
"C:\\Users\\danie\\OneDrive\\Desktop\\Topics Repos\\Programming-Git-Bari\\objects");
assertTrue(objectsDirectory.exists());

File indexFile = new File("C:\\Users\\danie\\OneDrive\\Desktop\\Topics Repos\\Programming-Git-Bari\\index.txt");
assertTrue(indexFile.exists());
}

@Test
public void testAdd() throws Exception {
index.add("testFile");
assertTrue(index.blobs.containsKey("testFile"));
assertNotNull(index.blobs.get("testFile"));
}

@Test
public void testRemove() {
index.blobs.put("testFile", "testSha1");
index.remove("testFile");
assertNull(index.blobs.get("testFile"));
}

@Test
public void testPrintBlobs() {
index.blobs.put("testFile1", "testSha1");
index.blobs.put("testFile2", "testSha2");
index.printBlobs();

}
}