1 /* 2 * GeoAPI - Java interfaces for OGC/ISO standards 3 * http://www.geoapi.org 4 * 5 * Copyright (C) 2008-2011 Open Geospatial Consortium, Inc. 6 * All Rights Reserved. http://www.opengeospatial.org/ogc/legal 7 * 8 * Permission to use, copy, and modify this software and its documentation, with 9 * or without modification, for any purpose and without fee or royalty is hereby 10 * granted, provided that you include the following on ALL copies of the software 11 * and documentation or portions thereof, including modifications, that you make: 12 * 13 * 1. The full text of this NOTICE in a location viewable to users of the 14 * redistributed or derivative work. 15 * 2. Notice of any changes or modifications to the OGC files, including the 16 * date changes were made. 17 * 18 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE 19 * NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 20 * TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT 21 * THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY 22 * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. 23 * 24 * COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR 25 * CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. 26 * 27 * The name and trademarks of copyright holders may NOT be used in advertising or 28 * publicity pertaining to the software without specific, written prior permission. 29 * Title to copyright in this software and any associated documentation will at all 30 * times remain with copyright holders. 31 */ 32 package org.opengis.test.util; 33 34 import java.util.Map; 35 import java.util.Locale; 36 import java.util.HashMap; 37 import java.util.regex.Pattern; 38 39 import org.opengis.util.*; 40 41 import org.junit.*; 42 import org.opengis.test.TestCase; 43 44 import static org.junit.Assume.*; 45 import static org.opengis.test.Assert.*; 46 import static org.opengis.test.Validators.*; 47 48 49 /** 50 * Tests {@linkplain GenericName generic name} and related objects from the {@code org.opengis.util} 51 * package. Name instances are created using a {@link NameFactory} given at construction time. 52 * 53 * @author Martin Desruisseaux (Geomatys) 54 * @version 3.0 55 * @since 2.2 56 */ 57 public class NameTest extends TestCase { 58 /** 59 * The factory to be used for testing {@linkplain GenericName generic name} instances, 60 * or {@code null} if none. 61 */ 62 protected final NameFactory factory; 63 64 /** 65 * Creates a new test using the given factory. If the given factory is {@code null}, 66 * then the tests will be skipped. 67 * 68 * @param factory The factory to be used for creation of instances to be tested. 69 */ NameTest(final NameFactory factory)70 protected NameTest(final NameFactory factory) { 71 this.factory = factory; 72 } 73 74 /** 75 * Creates a namespace having the given name and separator. 76 * 77 * @param name 78 * The name of the namespace to be returned. This argument can be created using 79 * <code>{@linkplain #createGenericName createGenericName}(null, parsedNames)</code>. 80 * @param headSeparator 81 * The separator to insert between the namespace and the {@linkplain AbstractName#head 82 * head}. For HTTP namespace, it is {@code "://"}. For URN namespace, it is typically 83 * {@code ":"}. 84 * @param separator 85 * The separator to insert between {@linkplain AbstractName#getParsedNames parsed names} 86 * in that namespace. For HTTP namespace, it is {@code "."}. For URN namespace, it is 87 * typically {@code ":"}. 88 * @return A namespace having the given name and separator. 89 */ createNameSpace(final GenericName name, final String headSeparator, final String separator)90 private NameSpace createNameSpace(final GenericName name, 91 final String headSeparator, final String separator) 92 { 93 final Map<String,String> properties = new HashMap<String,String>(4); 94 properties.put("separator", separator); 95 properties.put("separator.head", headSeparator); 96 return factory.createNameSpace(name, properties); 97 } 98 99 /** 100 * Tests the creation of "My documents" folder name. 101 * This test uses the following factory methods: 102 * <p> 103 * <ul> 104 * <li>{@link NameFactory#createInternationalString}</li> 105 * </ul> 106 */ 107 @Test testInternationalString()108 public void testInternationalString() { 109 assumeNotNull(factory); 110 final Map<Locale,String> names = new HashMap<Locale,String>(); 111 names.put(Locale.ENGLISH, "My documents"); 112 names.put(Locale.FRENCH, "Mes documents"); 113 InternationalString localized = factory.createInternationalString(names); 114 validate(localized); 115 for (final Map.Entry<Locale,String> entry : names.entrySet()) { 116 assertEquals("toString(Locale) should returns the value given to the factory method.", 117 entry.getValue(), localized.toString(entry.getKey())); 118 } 119 assertContains("toString() should returns one of the values given to the factory method.", 120 names.values(), localized.toString()); 121 } 122 123 /** 124 * Tests the creation of {@code "EPSG:4326"} as local names. First the {@code "EPSG"} 125 * namespace is created. Then a {@code "4326"} local name is created in that namespace. 126 * This test uses the following factory methods: 127 * <p> 128 * <ul> 129 * <li>{@link NameFactory#createLocalName}</li> 130 * <li>{@link NameFactory#createNameSpace}</li> 131 * </ul> 132 */ 133 @Test testLocalName()134 public void testLocalName() { 135 assumeNotNull(factory); 136 final String EPSG = "EPSG"; 137 final LocalName authority = factory.createLocalName(null, EPSG); 138 validate(authority); 139 assertTrue(authority.scope().isGlobal()); 140 assertEquals(EPSG, authority.toString()); 141 assertEquals(EPSG, authority.toInternationalString().toString()); 142 143 final NameSpace ns = createNameSpace(authority, ":", ":"); 144 validate(ns); 145 assertEquals(authority, ns.name()); 146 147 final String WGS84 = "4326"; 148 final LocalName code = factory.createLocalName(ns, WGS84); 149 validate(code); 150 assertEquals(ns, code.scope()); 151 assertEquals(WGS84, code.toString()); 152 assertEquals(EPSG + ':' + WGS84, code.toFullyQualifiedName().toString()); 153 } 154 155 /** 156 * Tests the creation of {@code "urn:ogc:def:crs:epsg:4326"} as a scoped name. 157 * This test uses the following factory methods: 158 * <p> 159 * <ul> 160 * <li>{@link NameFactory#createGenericName}</li> 161 * </ul> 162 */ 163 @Test testScopedName()164 public void testScopedName() { 165 assumeNotNull(factory); 166 final String[] parsed = new String[] { 167 "urn","ogc","def","crs","epsg","4326" 168 }; 169 GenericName name = factory.createGenericName(null, parsed); 170 validate(name); 171 172 assertEquals("Name should be already fully qualified.", 173 name, name.toFullyQualifiedName()); 174 175 assertTrue("Fully qualified name should be \"urn:ogc:def:crs:epsg:4326\" (separator may vary).", 176 Pattern.matches("urn\\p{Punct}ogc\\p{Punct}def\\p{Punct}crs\\p{Punct}epsg\\p{Punct}4326", 177 name.toString())); 178 179 assertEquals("Depth shall be counted from the global namespace.", 6, name.depth()); 180 181 for (int i=parsed.length; --i>=0;) { 182 name = name.tip(); 183 validate(name); 184 assertEquals(parsed[i], name.toString()); 185 name = name.scope().name(); 186 } 187 } 188 189 /** 190 * Tests the parsing of {@code "urn:ogc:def:crs:epsg:4326"} as a scoped name. 191 * This test uses the following factory methods: 192 * <p> 193 * <ul> 194 * <li>{@link NameFactory#createLocalName}</li> 195 * <li>{@link NameFactory#createNameSpace}</li> 196 * <li>{@link NameFactory#parseGenericName}</li> 197 * </ul> 198 */ 199 @Test testParsedURN()200 public void testParsedURN() { 201 assumeNotNull(factory); 202 final LocalName urn = factory.createLocalName(null, "urn"); 203 validate(urn); 204 final NameSpace ns = createNameSpace(urn, ":", ":"); 205 validate(ns); 206 final GenericName name = factory.parseGenericName(ns, "ogc:def:crs:epsg:4326"); 207 validate(name); 208 209 assertEquals("Depth shall be counted from the \"urn\" namespace.", 5, name.depth()); 210 assertEquals("ogc:def:crs:epsg:4326", name.toString()); 211 assertEquals("urn:ogc:def:crs:epsg:4326", name.toFullyQualifiedName().toString()); 212 } 213 214 /** 215 * Tests the parsing of {@code "http://www.opengis.net/gml/srs/epsg.xml#4326"} as a local name. 216 * This test uses the following factory methods: 217 * <p> 218 * <ul> 219 * <li>{@link NameFactory#createLocalName}</li> 220 * <li>{@link NameFactory#createNameSpace}</li> 221 * <li>{@link NameFactory#parseGenericName}</li> 222 * </ul> 223 */ 224 @Test testParsedHTTP()225 public void testParsedHTTP() { 226 assumeNotNull(factory); 227 GenericName name = factory.createLocalName(null, "http"); 228 assertEquals(1, name.depth()); 229 assertEquals("http", name.head().toString()); 230 assertEquals("http", name.tip().toString()); 231 NameSpace ns = createNameSpace(name, "://", "."); 232 validate(ns); 233 234 name = factory.parseGenericName(ns, "www.opengis.net"); 235 assertEquals(3, name.depth()); 236 assertEquals("www", name.head().toString()); 237 assertEquals("net", name.tip().toString()); 238 ns = createNameSpace(name, "/", "/"); 239 validate(ns); 240 241 name = factory.parseGenericName(ns, "gml/srs/epsg.xml"); 242 assertEquals(3, name.depth()); 243 assertEquals("gml", name.head().toString()); 244 assertEquals("epsg.xml", name.tip().toString()); 245 ns = createNameSpace(name, "#", ":"); 246 validate(ns); 247 248 name = factory.createLocalName(ns, "4326"); 249 assertEquals(1, name.depth()); 250 assertEquals("4326", name.head().toString()); 251 assertEquals("4326", name.tip().toString()); 252 validate(name); 253 254 assertEquals("4326", name.toString()); 255 name = name.toFullyQualifiedName(); 256 assertEquals("http://www.opengis.net/gml/srs/epsg.xml#4326", name.toString()); 257 assertEquals(8, name.depth()); 258 assertEquals("http", name.head().toString()); 259 assertEquals("4326", name.tip().toString()); 260 } 261 } 262