Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to use the LOC writers; unit test the BagInfo subclass
- Loading branch information
Showing
5 changed files
with
186 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/fcrepo/federation/bagit/BagInfoTxtWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.fcrepo.federation.bagit; | ||
|
||
import java.io.Closeable; | ||
import java.io.OutputStream; | ||
|
||
import gov.loc.repository.bagit.impl.BagInfoTxtWriterImpl; | ||
/** | ||
* Just a proxy to implement Closeable | ||
* @author ba2213 | ||
* | ||
*/ | ||
public class BagInfoTxtWriter extends BagInfoTxtWriterImpl implements Closeable { | ||
|
||
public BagInfoTxtWriter(OutputStream out, String encoding) { | ||
super(out, encoding); | ||
} | ||
|
||
public BagInfoTxtWriter(OutputStream out, String encoding, int lineLength, int indent) { | ||
super(out, encoding, lineLength, indent); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/fcrepo/federation/bagit/functions/GetBagInfoTxtWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.fcrepo.federation.bagit.functions; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
|
||
import org.fcrepo.federation.bagit.BagInfoTxtWriter; | ||
|
||
import com.google.common.base.Function; | ||
|
||
public class GetBagInfoTxtWriter implements Function<String, gov.loc.repository.bagit.BagInfoTxtWriter> { | ||
|
||
@Override | ||
public BagInfoTxtWriter apply(String input) { | ||
try { | ||
return new BagInfoTxtWriter(new FileOutputStream(new File(input)), "UTF-8", 79, 0); // 79 char length, 0 indent | ||
} catch (FileNotFoundException e) { | ||
throw new IllegalStateException("Could not open BagInfo writer at " + input, e); | ||
} | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
src/test/java/org/fcrepo/federation/bagit/BagInfoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.fcrepo.federation.bagit; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import org.fcrepo.federation.bagit.functions.GetBagInfoTxtWriter; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.modeshape.jcr.value.Name; | ||
import org.modeshape.jcr.value.NameFactory; | ||
import org.modeshape.jcr.value.Property; | ||
import org.modeshape.jcr.value.PropertyFactory; | ||
import org.modeshape.jcr.value.StringFactory; | ||
|
||
import gov.loc.repository.bagit.Bag.BagConstants; | ||
import gov.loc.repository.bagit.BagFile; | ||
|
||
public class BagInfoTest { | ||
|
||
private BagFile mockBF; | ||
private PropertyFactory mockPF; | ||
private NameFactory mockNF; | ||
private BagConstants mockBC; | ||
private GetBagInfoTxtWriter mockWriterFunc; | ||
private BagInfo testObj; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
mockBF = mock(BagFile.class); | ||
ByteArrayInputStream bytes = new ByteArrayInputStream( | ||
"Bag-Count: 1 of 1".getBytes("UTF-8")); | ||
when(mockBF.newInputStream()).thenReturn(bytes); | ||
mockPF = mock(PropertyFactory.class); | ||
mockNF = mock(NameFactory.class); | ||
mockBC = mock(BagConstants.class); | ||
when(mockBC.getBagInfoTxt()).thenReturn("bag-info.txt"); | ||
when(mockBC.getBagEncoding()).thenReturn("UTF-8"); | ||
mockWriterFunc = mock(GetBagInfoTxtWriter.class); | ||
BagInfo.getBagInfoTxtWriter = mockWriterFunc; | ||
testObj = new BagInfo("/foo", mockBF, mockPF, mockNF, mockBC); | ||
} | ||
|
||
// Public API test | ||
|
||
@Test | ||
public void testSave() throws IOException { | ||
BagInfoTxtWriter mockWriter = mock(BagInfoTxtWriter.class); | ||
when(mockWriterFunc.apply(any(String.class))).thenReturn(mockWriter); | ||
Property mockProp = mockProperty("Bag.Count", "2 of 5"); | ||
when(mockPF.create(any(Name.class), eq("1 of 1"))).thenReturn(mockProp); | ||
testObj.save(); | ||
verify(mockWriter).write("Bag-Count", "2 of 5"); | ||
} | ||
|
||
@Test | ||
public void testDelete() throws IOException { | ||
//nb: this does not save the changes to the file, that's done in save() | ||
Property mockProp = mockProperty("Bag.Count", "2 of 5"); | ||
when(mockPF.create(any(Name.class), eq("1 of 1"))).thenReturn(mockProp); | ||
assertEquals(1, testObj.getProperties().size()); | ||
testObj.delete(); | ||
assertEquals(0, testObj.getProperties().size()); | ||
} | ||
|
||
@Test | ||
public void testGetProperties() { | ||
Property mockProp = mockProperty("Bag.Count", "2 of 5"); | ||
when(mockPF.create(any(Name.class), eq("1 of 1"))).thenReturn(mockProp); | ||
Map<Name, Property> props = testObj.getProperties(); | ||
assertEquals(1, props.size()); | ||
Property prop = props.values().iterator().next(); | ||
assertEquals("Bag.Count", prop.getName().getLocalName()); | ||
assertEquals("2 of 5", prop.getString()); | ||
} | ||
|
||
@Test | ||
public void testSetProperties() { | ||
Property mockProp = mockProperty("Bag.Count", "1 of 5"); | ||
Property[] props = new Property[]{mockProp}; | ||
Collection<Property> values = Arrays.asList(props); | ||
Map<Name, Property> mockProps = mock(Map.class); | ||
when(mockProps.values()).thenReturn(values); | ||
testObj.setProperties(mockProps); | ||
assertEquals("1 of 5", testObj.get("Bag-Count")); | ||
mockProp = mockProperty("Bag.Count", "2 of 5"); | ||
props = new Property[]{mockProp}; | ||
values = Arrays.asList(props); | ||
when(mockProps.values()).thenReturn(values); | ||
testObj.setProperties(mockProps); | ||
assertEquals("2 of 5", testObj.get("Bag-Count")); | ||
} | ||
|
||
private static Property mockProperty(String name, String value) { | ||
Property mockProp = mock(Property.class); | ||
Name mockName = mock(Name.class); | ||
when(mockName.getLocalName()).thenReturn(name); | ||
when(mockName.getNamespaceUri()).thenReturn("info:fedora/bagit/"); | ||
when(mockProp.getName()).thenReturn(mockName); | ||
when(mockProp.getString()).thenReturn(value); | ||
return mockProp; | ||
} | ||
|
||
//TODO Conversion methods that should be tested | ||
} |