]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-3178: Added convenience methods to Authority, AuthorityItem in common-api...
authorAron Roberts <aron@socrates.berkeley.edu>
Tue, 4 Oct 2011 17:28:48 +0000 (17:28 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Tue, 4 Oct 2011 17:28:48 +0000 (17:28 +0000)
services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java
services/common-api/src/test/java/org/collectionspace/services/common/api/test/RefNameTest.java

index 93e09ca4e09bf3f8d6e69274400cf306ceadf52d..6a9370649194e0a7fc3150d8d7234121a307b2d5 100755 (executable)
@@ -36,27 +36,21 @@ import java.util.regex.Pattern;
  *\r
  * User: laramie\r
  */\r
\r
 public class RefName {\r
+\r
     public static final String HACK_VOCABULARIES = "Vocabularies"; //TODO: get rid of these.\r
     public static final String HACK_ORGANIZATIONS = "Organizations"; //TODO: get rid of these.\r
     public static final String HACK_ORGAUTHORITIES = "Orgauthorities";  //TODO: get rid of these.\r
     public static final String HACK_PERSONAUTHORITIES = "Personauthorities";  //TODO: get rid of these.\r
     public static final String HACK_LOCATIONAUTHORITIES = "Locationauthorities";  //TODO: get rid of these.\r
-\r
-    public static final String URN_PREFIX      = "urn:cspace:";\r
+    public static final String URN_PREFIX = "urn:cspace:";\r
     public static final String URN_NAME_PREFIX = "urn:cspace:name";\r
-\r
     public static final String REFNAME = "refName";\r
-\r
-    public static final String AUTHORITY_REGEX      = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?";\r
+    public static final String AUTHORITY_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?";\r
     public static final String AUTHORITY_ITEM_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\):item:name\\((.*)\\)\\'?([^\\']*)\\'?";\r
-\r
-    public static final String AUTHORITY_EXAMPLE  = "urn:cspace:collectionspace.org:Loansin:name(shortID)'displayName'";\r
+    public static final String AUTHORITY_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID)'displayName'";\r
     public static final String AUTHORITY_EXAMPLE2 = "urn:cspace:collectionspace.org:Loansin:name(shortID)";\r
-\r
-    public static final String AUTHORITY_ITEM_EXAMPLE ="urn:cspace:collectionspace.org:Loansin:name(shortID):item:name(itemShortID)'itemDisplayName'";\r
-\r
+    public static final String AUTHORITY_ITEM_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID):item:name(itemShortID)'itemDisplayName'";\r
     public static final String EX_tenantName = "collectionspace.org";\r
     public static final String EX_resource = "Loansin";\r
     public static final String EX_shortIdentifier = "shortID";\r
@@ -64,18 +58,19 @@ public class RefName {
     public static final String EX_itemShortIdentifier = "itemShortID";\r
     public static final String EX_itemDisplayName = "itemDisplayName";\r
 \r
-\r
     public static class Authority {\r
+\r
         public String tenantName = "";\r
         public String resource = "";\r
         public String shortIdentifier = "";\r
         public String displayName = "";\r
+\r
         public static Authority parse(String urn) {\r
             Authority info = new Authority();\r
             Pattern p = Pattern.compile(AUTHORITY_REGEX);\r
             Matcher m = p.matcher(urn);\r
-            if (m.find()){\r
-                if (m.groupCount()<4){\r
+            if (m.find()) {\r
+                if (m.groupCount() < 4) {\r
                     return null;\r
                 }\r
                 info.tenantName = m.group(1);\r
@@ -86,43 +81,50 @@ public class RefName {
             }\r
             return null;\r
         }\r
-        public boolean equals(Object other){\r
-            if (other == null){\r
+        \r
+        public String getShortIdentifier() {\r
+            return this.shortIdentifier;\r
+        }\r
+\r
+        public boolean equals(Object other) {\r
+            if (other == null) {\r
                 return false;\r
             }\r
-            if (other instanceof Authority){\r
-                Authority ao = (Authority)other;\r
-                return (   this.tenantName.equals(ao.tenantName)\r
+            if (other instanceof Authority) {\r
+                Authority ao = (Authority) other;\r
+                return (this.tenantName.equals(ao.tenantName)\r
                         && this.resource.equals(ao.resource)\r
-                        && this.shortIdentifier.equals(ao.shortIdentifier)\r
-                       );\r
+                        && this.shortIdentifier.equals(ao.shortIdentifier));\r
             } else {\r
                 return false;\r
             }\r
         }\r
+\r
         public String getRelativeUri() {\r
-               return "/"+resource+"/"+URN_NAME_PREFIX+"("+shortIdentifier+")";\r
+            return "/" + resource + "/" + URN_NAME_PREFIX + "(" + shortIdentifier + ")";\r
         }\r
+\r
         public String toString() {\r
             String displaySuffix = (displayName != null && (!displayName.isEmpty())) ? '\'' + displayName + '\'' : "";\r
             return URN_PREFIX + tenantName + ':' + resource + ":" + "name" + "(" + shortIdentifier + ")" + displaySuffix;\r
         }\r
-\r
     }\r
 \r
     public static class AuthorityItem {\r
+\r
         public Authority inAuthority;\r
         public String shortIdentifier = "";\r
         public String displayName = "";\r
+\r
         public static AuthorityItem parse(String urn) {\r
             Authority info = new Authority();\r
             AuthorityItem termInfo = new AuthorityItem();\r
             termInfo.inAuthority = info;\r
             Pattern p = Pattern.compile(AUTHORITY_ITEM_REGEX);\r
             Matcher m = p.matcher(urn);\r
-            if (m.find()){\r
-                if (m.groupCount()<5){\r
-                    return  null;\r
+            if (m.find()) {\r
+                if (m.groupCount() < 5) {\r
+                    return null;\r
                 }\r
                 termInfo.inAuthority.tenantName = m.group(1);\r
                 termInfo.inAuthority.resource = m.group(2);\r
@@ -133,12 +135,21 @@ public class RefName {
             }\r
             return null;\r
         }\r
-        public boolean equals(Object other){\r
-            if (other == null){\r
+        \r
+        public String getParentShortIdentifier() {\r
+            return this.inAuthority.shortIdentifier;\r
+        }\r
+        \r
+        public String getShortIdentifier() {\r
+            return this.shortIdentifier;\r
+        }\r
+    \r
+        public boolean equals(Object other) {\r
+            if (other == null) {\r
                 return false;\r
             }\r
-            if (other instanceof AuthorityItem){\r
-                AuthorityItem aio = (AuthorityItem)other;\r
+            if (other instanceof AuthorityItem) {\r
+                AuthorityItem aio = (AuthorityItem) other;\r
                 boolean ok = true;\r
                 ok = ok && aio.inAuthority != null;\r
                 ok = ok && aio.inAuthority.equals(this.inAuthority);\r
@@ -149,21 +160,22 @@ public class RefName {
                 return false;\r
             }\r
         }\r
+\r
         public String getRelativeUri() {\r
-               return inAuthority.getRelativeUri()+"/items/"+URN_NAME_PREFIX+"("+shortIdentifier+")";\r
+            return inAuthority.getRelativeUri() + "/items/" + URN_NAME_PREFIX + "(" + shortIdentifier + ")";\r
         }\r
+\r
         public String toString() {\r
             String displaySuffix = (displayName != null && (!displayName.isEmpty())) ? '\'' + displayName + '\'' : "";\r
             Authority ai = inAuthority;\r
-            if (ai==null){\r
-               return URN_PREFIX+"ERROR:inAuthorityNotSet: (" + shortIdentifier + ")" + displaySuffix;\r
+            if (ai == null) {\r
+                return URN_PREFIX + "ERROR:inAuthorityNotSet: (" + shortIdentifier + ")" + displaySuffix;\r
             } else {\r
-               String base = URN_PREFIX + ai.tenantName + ':' + ai.resource + ":" + "name" + "(" + ai.shortIdentifier + ")" ;\r
-               String refname = base+":item:name("+shortIdentifier+")"+displaySuffix;\r
-               return refname;\r
+                String base = URN_PREFIX + ai.tenantName + ':' + ai.resource + ":" + "name" + "(" + ai.shortIdentifier + ")";\r
+                String refname = base + ":item:name(" + shortIdentifier + ")" + displaySuffix;\r
+                return refname;\r
             }\r
         }\r
-\r
     }\r
 \r
     public static Authority buildAuthority(String tenantName, String serviceName, String authorityShortIdentifier, String authorityDisplayName) {\r
@@ -179,7 +191,7 @@ public class RefName {
     }\r
 \r
     public static AuthorityItem buildAuthorityItem(String tenantName, String serviceName, String authorityShortIdentifier,\r
-                                                   String itemShortIdentifier, String itemDisplayName) {\r
+            String itemShortIdentifier, String itemDisplayName) {\r
         Authority authority = buildAuthority(tenantName, serviceName, authorityShortIdentifier, "");\r
         return buildAuthorityItem(authority, itemShortIdentifier, itemDisplayName);\r
     }\r
@@ -204,7 +216,7 @@ public class RefName {
      * so that calling shortIdToPath("TestAuthority") returns "urn:cspace:name(TestAuthority)", and\r
      * then this value may be put into a path, such as "/personauthorities/urn:cspace:name(TestAuthority)/items".\r
      */\r
-    public static String shortIdToPath(String shortId){\r
-        return URN_NAME_PREFIX+'('+shortId+')';\r
+    public static String shortIdToPath(String shortId) {\r
+        return URN_NAME_PREFIX + '(' + shortId + ')';\r
     }\r
 }\r
index 2bb2b198fbd306cf1ee81193a99271a75316bc51..f6a91344bdf88d33cb6b88c2e2ba22764dad5fed 100755 (executable)
@@ -85,5 +85,31 @@ import org.testng.annotations.Test;
         check(item.toString(), item3.toString(), "buildAuthorityItem(Authority,str,str) from AUTHORITY_EXAMPLE2 vs. AUTHORITY_ITEM_EXAMPLE");\r
         Assert.assertEquals(item, item3);\r
     }\r
+    \r
+    /**\r
+     * Test convenience getters that return short identifiers for authorities and authority items.\r
+     */\r
+    @Test\r
+    public void testShortIDGetters(){\r
+        testBanner("testShortIDGetters");\r
+        RefName.Authority authority = RefName.Authority.parse(RefName.AUTHORITY_EXAMPLE);\r
+        Assert.assertEquals(authority.getShortIdentifier(), RefName.EX_shortIdentifier,\r
+                "Short identifier from parsing parent authority refName does not match value of Authority.getShortIdentifier().");\r
+        \r
+        RefName.AuthorityItem item = RefName.buildAuthorityItem(RefName.AUTHORITY_EXAMPLE,\r
+                                                                RefName.EX_itemShortIdentifier,\r
+                                                                RefName.EX_itemDisplayName);\r
+        Assert.assertEquals(item.getParentShortIdentifier(), RefName.EX_shortIdentifier,\r
+              "Parent short identifier from parsing authority refName does not match value of AuthorityItem.getParentShortIdentifier().");\r
+        Assert.assertEquals(item.getShortIdentifier(), RefName.EX_itemShortIdentifier,\r
+              "Short identifier from item does not match value of AuthorityItem.getShortIdentifier().");\r
+        \r
+        RefName.AuthorityItem parsedItem = RefName.AuthorityItem.parse(RefName.AUTHORITY_ITEM_EXAMPLE);\r
+        Assert.assertEquals(parsedItem.getParentShortIdentifier(), RefName.EX_shortIdentifier,\r
+              "Parent short identifier from parsing item refName does not match value of AuthorityItem.getParentShortIdentifier().");\r
+        Assert.assertEquals(parsedItem.getShortIdentifier(), RefName.EX_itemShortIdentifier,\r
+              "Short identifier from parsing item refName does not match value of AuthorityItem.getShortIdentifier().");\r
+\r
+    }\r
 \r
 }\r