Skip to content

Commit 46770a4

Browse files
committed
add support for mandatory ResourceSyncDescription type
1 parent 8782221 commit 46770a4

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

src/main/java/org/openarchives/resourcesync/ResourceSync.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ResourceSync
2929
public static String REL_VIA = "via";
3030

3131
// capabilities
32+
public static String CAPABILITY_RESOURCESYNC = "resourcesync";
3233
public static String CAPABILITY_RESOURCELIST = "resourcelist";
3334
public static String CAPABILITY_RESOURCELIST_ARCHIVE = "resourcelist-archive";
3435
public static String CAPABILITY_CHANGELIST = "changelist";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.openarchives.resourcesync;
2+
3+
import java.util.List;
4+
5+
public class ResourceSyncDescription extends UrlSet
6+
{
7+
public ResourceSyncDescription()
8+
{
9+
super(ResourceSync.CAPABILITY_RESOURCESYNC);
10+
}
11+
12+
public ResourceSyncDescription(String describedby, String describedByContentType)
13+
{
14+
this();
15+
ResourceSyncLn ln = this.addLn(ResourceSync.REL_DESCRIBED_BY, describedby);
16+
ln.setType(describedByContentType);
17+
}
18+
19+
public void addCapabilityList(URL caplist)
20+
{
21+
if (!ResourceSync.CAPABILITY_CAPABILITYLIST.equals(caplist.getCapability()))
22+
{
23+
throw new SpecComplianceException("URL added to ResourceSyncDescription is not a Capability List");
24+
}
25+
this.addUrl(caplist);
26+
}
27+
28+
public URL addCapabilityList(String loc)
29+
{
30+
return this.addCapabilityList(loc, null);
31+
}
32+
33+
public URL addCapabilityList(String loc, String describedby)
34+
{
35+
URL caplist = new URL();
36+
caplist.setLoc(loc);
37+
caplist.addLn(ResourceSync.REL_DESCRIBED_BY, describedby);
38+
caplist.setCapability(ResourceSync.CAPABILITY_CAPABILITYLIST);
39+
this.addCapabilityList(caplist);
40+
return caplist;
41+
}
42+
43+
public List<ResourceSyncEntry> getCapabilityLists()
44+
{
45+
return this.getUrls();
46+
}
47+
}

src/main/java/org/openarchives/resourcesync/SpecComplianceException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.openarchives.resourcesync;
22

3-
public class SpecComplianceException extends Exception
3+
public class SpecComplianceException extends RuntimeException
44
{
55
public SpecComplianceException()
66
{
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.openarchives.resourcesync.test;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.JUnit4;
6+
import org.openarchives.resourcesync.ResourceSync;
7+
import org.openarchives.resourcesync.ResourceSyncDescription;
8+
import org.openarchives.resourcesync.ResourceSyncEntry;
9+
import org.openarchives.resourcesync.ResourceSyncLn;
10+
import org.openarchives.resourcesync.SpecComplianceException;
11+
import org.openarchives.resourcesync.URL;
12+
13+
import java.util.List;
14+
15+
@RunWith(JUnit4.class)
16+
public class TestDescription
17+
{
18+
@Test
19+
public void blankConstructor()
20+
{
21+
ResourceSyncDescription desc = new ResourceSyncDescription();
22+
assert ResourceSync.CAPABILITY_RESOURCESYNC.equals(desc.getCapability());
23+
}
24+
25+
@Test
26+
public void fullConstructor()
27+
{
28+
ResourceSyncDescription desc = new ResourceSyncDescription("http://desc", "text/html");
29+
assert ResourceSync.CAPABILITY_RESOURCESYNC.equals(desc.getCapability());
30+
31+
List<ResourceSyncLn> lns = desc.getLns();
32+
assert lns.size() == 1;
33+
34+
assert lns.get(0).getHref().equals("http://desc");
35+
assert lns.get(0).getType().equals("text/html");
36+
}
37+
38+
@Test
39+
public void addingCapabilityLists()
40+
{
41+
ResourceSyncDescription desc = new ResourceSyncDescription();
42+
43+
desc.addCapabilityList("http://cap1");
44+
45+
List<ResourceSyncEntry> entries = desc.getCapabilityLists();
46+
assert entries.size() == 1;
47+
assert entries.get(0).getLoc().equals("http://cap1");
48+
assert ResourceSync.CAPABILITY_CAPABILITYLIST.equals(entries.get(0).getCapability());
49+
50+
desc.addCapabilityList("http://cap2", "http://desc2");
51+
entries = desc.getCapabilityLists();
52+
assert entries.size() == 2;
53+
boolean trip = false;
54+
for (ResourceSyncEntry entry : entries)
55+
{
56+
if ("http://cap2".equals(entry.getLoc()))
57+
{
58+
List<ResourceSyncLn> lns = entry.getLns();
59+
assert lns.size() == 1;
60+
assert "http://desc2".equals(lns.get(0).getHref());
61+
trip = true;
62+
}
63+
}
64+
assert trip;
65+
66+
URL caplist = new URL();
67+
caplist.setCapability(ResourceSync.CAPABILITY_CAPABILITYLIST);
68+
caplist.setLoc("http://cap3");
69+
desc.addCapabilityList(caplist);
70+
71+
entries = desc.getCapabilityLists();
72+
assert entries.size() == 3;
73+
}
74+
75+
@Test
76+
public void error()
77+
{
78+
ResourceSyncDescription desc = new ResourceSyncDescription();
79+
80+
URL url = new URL();
81+
url.setLoc("http://cap1");
82+
url.setCapability(ResourceSync.CAPABILITY_CHANGEDUMP);
83+
84+
boolean exception = false;
85+
try
86+
{
87+
desc.addCapabilityList(url);
88+
}
89+
catch (SpecComplianceException e)
90+
{
91+
exception = true;
92+
}
93+
assert exception;
94+
}
95+
}

0 commit comments

Comments
 (0)