Exemple #1
0
// Doing a unified test for Fields and SetFields
func TestFields(t *testing.T) {

	fl := bagins.NewTagFieldList()
	test_len := func(l int) { // DRY!
		if len(fl.Fields()) != l {
			t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!")
		}
	}

	test_len(0)

	newFields := []bagins.TagField{
		*bagins.NewTagField("label1", "value1"),
		*bagins.NewTagField("label2", "value2"),
		*bagins.NewTagField("label3", "value3"),
	}
	fl.SetFields(newFields)
	test_len(3)

	for i := 0; i < 3; i++ {
		exp := fmt.Sprintf("label%d", i+1)
		act := fl.Fields()[i].Label()
		if exp != act {
			t.Error("Expected", exp, "but returned", act)
		}
	}
}
Exemple #2
0
func TestRemoveField(t *testing.T) {
	fl := bagins.NewTagFieldList()
	test_len := func(l int) { // DRY again!
		if len(fl.Fields()) != l {
			t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!")
		}
	}

	for i := 0; i < 100; i++ {
		tmp := strconv.Itoa(i)
		fl.AddField(*bagins.NewTagField(tmp, tmp))
	}
	test_len(100)

	// Should error if removing out of range.
	if err := fl.RemoveField(-6); err == nil {
		t.Error("Trying to remove negative index does not produce expected error!")
	}
	if err := fl.RemoveField(100); err == nil {
		t.Error("Trying to remove out of bound index does not produce expected error!")
	}
	test_len(100)

	// Remove every other one of the first 25 and test
	for i := 0; i < 50; i++ {
		if i%2 == 0 {
			fl.RemoveField(i)
		}
	}
	test_len(75)

}
Exemple #3
0
// Setups up a bag with some custom tag files.
func setupCustomBag(bagName string) (*bagins.Bag, error) {
	bag, err := bagins.NewBag(os.TempDir(), bagName, []string{"md5", "sha256"}, true)
	if err != nil {
		return nil, err
	}
	bag.AddTagfile("bag-info.txt")
	bagInfo, _ := bag.TagFile("bag-info.txt")
	bagInfo.Data.SetFields([]bagins.TagField{
		*bagins.NewTagField("Source-Organization", "APTrust"),
		*bagins.NewTagField("Bagging-Date", "2016-06-01"),
		*bagins.NewTagField("Bag-Count", "1"),
		*bagins.NewTagField("Internal-Sender-Description", "This is a test bag with no content."),
		*bagins.NewTagField("Internal-Sender-Identification", "Bag XYZ"),
	})
	bag.AddTagfile("aptrust-info.txt")
	aptrustInfo, _ := bag.TagFile("aptrust-info.txt")
	aptrustInfo.Data.SetFields([]bagins.TagField{
		*bagins.NewTagField("Title", "APTrust Generic Test Bag"),
		*bagins.NewTagField("Rights", "Consortia"),
	})
	errors := bag.Save()
	if errors != nil && len(errors) > 0 {
		return nil, errors[0]
	}
	return bag, nil
}
Exemple #4
0
func TestTagFileCreate(t *testing.T) {
	testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt")
	tagFile, _ := bagins.NewTagFile(testPath)
	tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "A metadata element MUST consist of a label, a colon, and a value, each separated by optional whitespace.  It is RECOMMENDED that lines not exceed 79 characters in length.  Long values may be continued onto the next line by inserting a newline (LF), a carriage return (CR), or carriage return plus newline (CRLF) and indenting the next line with linear white space (spaces or tabs)."))
	tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encodeing", "UTF-8"))

	err := tagFile.Create()
	if err != nil {
		t.Error(err)
	}
	fileInfo, err := os.Stat(testPath)
	if err != nil {
		t.Error("File and path", testPath, "not created!")
	}
	if fileInfo.Size() == 0 {
		t.Error("Tag file was created but is empty.")
	}
	os.RemoveAll(filepath.Dir(testPath))
}
Exemple #5
0
// Using single test for set and get values since they rely on one another.
func TestValue(t *testing.T) {
	f := bagins.NewTagField("label", "value")
	if f.Value() != "value" {
		t.Error("Tag Field value not created properly!")
	}

	f.SetValue("new value")
	if f.Value() != "new value" {
		t.Error("Tag Field value not set or read properly!")
	}

	if f.Label() != "label" {
		t.Error("Tag Field label value not retained when value set!")
	}
}
Exemple #6
0
// Using a single test for set and get labels since they rely on one another.
func TestLabel(t *testing.T) {
	f := bagins.NewTagField("label", "value")
	if f.Label() != "label" {
		t.Error("Tag Field label not created properly!")
	}

	f.SetLabel("new-label")
	if f.Label() != "new-label" {
		t.Error("Tag Field label not reset properly!")
	}

	if f.Value() != "value" {
		t.Error("Tag Field value not set or retained properly when label changed!")
	}
}
Exemple #7
0
func TestTagFileToString(t *testing.T) {
	testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt")
	tagFile, _ := bagins.NewTagFile(testPath)
	tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "0.97"))
	tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encoding", "UTF-8"))
	tagFile.Data.AddField(*bagins.NewTagField("Long-Line", "A metadata element MUST consist of a label, a colon, and a value, each separated by optional whitespace.  It is RECOMMENDED that lines not exceed 79 characters in length.  Long values may be continued onto the next line by inserting a newline (LF), a carriage return (CR), or carriage return plus newline (CRLF) and indenting the next line with linear white space (spaces or tabs)."))

	str, err := tagFile.ToString()
	if err != nil {
		t.Error(err)
	}
	expected := `BagIt-Version:  0.97
Tag-File-Character-Encoding:  UTF-8
Long-Line:  A metadata element MUST consist of a label, a colon, and a value,
    each separated by optional whitespace.  It is RECOMMENDED that lines not
    exceed 79 characters in length.  Long values may be continued onto the next
    line by inserting a newline (LF), a carriage return (CR), or carriage
    return plus newline (CRLF) and indenting the next line with linear white
    space (spaces or tabs).
`
	if str != expected {
		t.Errorf("ToString() returned\n\n%s  \nExpected\n\n%s", str, expected)
	}
}
Exemple #8
0
func TestNewField(t *testing.T) {
	exp_label := "test-field"
	exp_value := "this is my test"
	f := bagins.NewTagField(exp_label, exp_value)

	if f == nil { // This should test type but for the life of me I can't figure out how.
		t.Error("Tag Field object not returned")
	}
	if f.Label() != exp_label {
		t.Error("Tag Field label not created properly!")
	}
	if f.Value() != exp_value {
		t.Error("Tag Field value not created properly!")
	}
}
Exemple #9
0
func TestSave(t *testing.T) {
	// Setup test bag
	bag, _ := setupTestBag("_GOTEST_BAG_CLOSE_")
	defer os.RemoveAll(bag.Path())

	// Add some data to the manifest and make sure it writes it on close.
	bag.Manifests[0].Data["data/fakefile.txt"] = "da909ba395016f2a64b04d706520db6afa74fc95"

	// It should not throw an error.
	if errs := bag.Save(); len(errs) != 0 {
		for idx := range errs {
			t.Error(errs[idx])
		}
	}

	// The manifest file should contain data.
	content, err := ioutil.ReadFile(bag.Manifests[0].Name())
	if err != nil {
		t.Error(err)
	}
	exp := 59 // Length of values entered above and newline.
	if len(content) != 59 {
		t.Error("Expected ", exp, "but found", len(content), "characters written")
	}

	// Add some tagfile data to make sure it writes it on close.
	tfName := "extratagfile.txt"
	bag.AddTagfile("extratagfile.txt")
	tf, _ := bag.TagFile(tfName)
	tf.Data.AddField(*bagins.NewTagField("MyNewField", "This is testdata."))

	// it should not throw an error.
	if errs := bag.Save(); len(errs) != 0 {
		for idx := range errs {
			t.Error(errs[idx])
		}
	}

	// The TagFile should contain data.
	content, err = ioutil.ReadFile(tf.Name())
	if err != nil {
		t.Error(err)
	}
	exp = 10 // Some length the string needs to be abovel
	if len(content) < exp {
		t.Error("Didn't find data in tagfile", tfName, "as expected!")
	}
}
Exemple #10
0
// Setups up a bag with custom tag files, a custom tag directory,
// and a tag manifest.
func setupTagfileBag(bagName string) (*bagins.Bag, error) {
	bag, err := setupCustomBag(bagName)
	if err != nil {
		return nil, err
	}

	// Tag file in top-level directory
	bag.AddTagfile("laser-tag.txt")
	customTagFile1, _ := bag.TagFile("laser-tag.txt")
	customTagFile1.Data.SetFields([]bagins.TagField{
		*bagins.NewTagField("tic", "tac"),
		*bagins.NewTagField("tick", "tock"),
	})

	// Tag files in custom directory
	bag.AddTagfile("custom-tags/player-stats.txt")
	customTagFile2, _ := bag.TagFile("custom-tags/player-stats.txt")
	customTagFile2.Data.SetFields([]bagins.TagField{
		*bagins.NewTagField("Batting-Average", ".340"),
		*bagins.NewTagField("What-Time-Is-It", "2016-06-01T12:00:00Z"),
		*bagins.NewTagField("ERA", "1.63"),
		*bagins.NewTagField("Bats", "Left"),
		*bagins.NewTagField("Throws", "Right"),
	})
	bag.AddTagfile("custom-tags/tv-schedule.txt")
	customTagFile3, _ := bag.TagFile("custom-tags/tv-schedule.txt")
	customTagFile3.Data.SetFields([]bagins.TagField{
		*bagins.NewTagField("3:00PM", "House Party"),
		*bagins.NewTagField("4:00PM", "Dexter"),
	})

	errors := bag.Save()
	if errors != nil && len(errors) > 0 {
		return nil, errors[0]
	}
	return bag, nil
}
Exemple #11
0
func TestAddField(t *testing.T) {
	fl := bagins.NewTagFieldList()
	exp_len := 100
	for i := 0; i < exp_len; i++ {
		tmp := strconv.Itoa(i)
		fl.AddField(*bagins.NewTagField(tmp, tmp))
	}

	if len(fl.Fields()) != exp_len {
		t.Error("Expected", exp_len, "fields but returned", len(fl.Fields()), "!")
	}

	for i, f := range fl.Fields() {
		if f.Value() != strconv.Itoa(i) {
			t.Error("Expected field value of", strconv.Itoa(i), "but returned", f.Value(), "!")
		}
	}
}
Exemple #12
0
func TestReadTagFile(t *testing.T) {
	// Expected Data
	exp_list := [][]string{
		[]string{"description", strings.Repeat("test ", 40)},
		[]string{"title", "This is my title"},
		[]string{"description", strings.Repeat("more ", 80)},
	}

	// Prep the test file
	testPath := filepath.Join(os.TempDir(), "_GOTEST_READTAGFILE_bagit.txt")
	tagFile, _ := bagins.NewTagFile(testPath)
	for _, exp := range exp_list {
		tagFile.Data.AddField(*bagins.NewTagField(exp[0], exp[1]))
	}
	tagFile.Create()
	defer os.Remove(testPath)

	// Open and test parsing the file.
	tf, errs := bagins.ReadTagFile(testPath)
	for _, err := range errs {
		t.Error(err)
	}
	if len(tf.Data.Fields()) != 3 {
		t.Error("Expected 3 but returned", len(tf.Data.Fields()), "fields!")
	}

	fields := tagFile.Data.Fields()
	for idx, exp := range exp_list {
		if fields[idx].Label() != exp[0] {
			t.Error("Tag field", idx, "label", fields[idx].Label(), "is not expected value of", exp[0])
		}
		if fields[idx].Value() != exp[1] {
			t.Error("Tag field", idx, "value", fields[idx].Value(), "is not expected value of", exp[1])
		}
	}
}