### Question:
BasicName implements Name { public String getString() { return getString(Path.DEFAULT_ENCODER); } BasicName( String namespaceUri,
                      String localName ); String getLocalName(); String getNamespaceUri(); String getString(); String getString( TextEncoder encoder ); String getString( NamespaceRegistry namespaceRegistry ); String getString( NamespaceRegistry namespaceRegistry,
                             TextEncoder encoder ); String getString( NamespaceRegistry namespaceRegistry,
                             TextEncoder encoder,
                             TextEncoder delimiterEncoder ); int compareTo( Name that ); @Override int hashCode(); @Override boolean equals( Object obj ); @Override String toString();  }### Answer:
@Test public void shouldUseFullNamespaceUriInResultFromGetStringWithoutNamespaceRegistry() { String encodedNamespaceUri = Path.DEFAULT_ENCODER.encode(validNamespaceUri); String encodedLocalName = Path.DEFAULT_ENCODER.encode(validLocalName); String result = name.getString(); assertThat(result, containsString(encodedNamespaceUri)); assertThat(result, containsString(encodedLocalName)); assertThat(result, is("{" + encodedNamespaceUri + "}" + encodedLocalName)); }
@Test public void shouldEncodeColonInLocalNameAndNamespaceUriInResultFromGetStringWithoutNamespaceRegistry() { validLocalName = "some:name:with:colons"; name = new BasicName(validNamespaceUri, validLocalName); String encodedNamespaceUri = encoder.encode(validNamespaceUri); String encodedLocalName = encoder.encode(validLocalName); String result = name.getString(encoder); assertThat(result, is("{" + encodedNamespaceUri + "}" + encodedLocalName)); assertThat(encodedNamespaceUri, is("http%3a%2f%2fwww.modeshape.org%2f1.0")); assertThat(encodedLocalName, is("some%3aname%3awith%3acolons")); }
@Test public void shouldUseNamespacePrefixInResultFromGetStringWithNamespaceRegistry() { String result = name.getString(namespaceRegistry, encoder); assertThat(result, is("mode:" + validLocalName)); validLocalName = "some:name:with:colons"; name = new BasicName(validNamespaceUri, validLocalName); result = name.getString(namespaceRegistry, encoder); assertThat(result, is("mode:some%3aname%3awith%3acolons")); }
@Test public void shouldNotIncludeNamespacePrefixOrColonInResultFromGetStringWithNamespaceRegistry() { validNamespaceUri = namespaceRegistry.getDefaultNamespaceUri(); name = new BasicName(validNamespaceUri, validLocalName); String result = name.getString(namespaceRegistry, encoder); assertThat(result, is(validLocalName)); result = name.getString(namespaceRegistry); assertThat(result, is(validLocalName)); validLocalName = "some:name:with:colons"; name = new BasicName(validNamespaceUri, validLocalName); result = name.getString(namespaceRegistry, encoder); assertThat(result, is(encoder.encode(validLocalName))); }
@Test public void shouldUseDelimiterEncoderToEncodeDelimiterBetweenPrefixAndLocalPart() { encoder = new Jsr283Encoder(); name = new BasicName(ModeShapeLexicon.Namespace.URI, "some:name:with:colons"); assertThat(name.getString(namespaceRegistry, encoder, delimiterEncoder), is("mode\\:some\uf03aname\uf03awith\uf03acolons")); assertThat(name.getString(null, encoder, delimiterEncoder), is("\\{" + encoder.encode(ModeShapeLexicon.Namespace.URI) + "\\}some\uf03aname\uf03awith\uf03acolons")); }
@Test public void shouldEncodeWhenNoNamespace() { String nameForEncoding = "test name"; String encodedNameForEncoding = encoder.encode(nameForEncoding); assertThat(encodedNameForEncoding, not(nameForEncoding)); name = new BasicName(null, nameForEncoding); String result = name.getString(namespaceRegistry, encoder); assertThat(result, is(encodedNameForEncoding)); result = name.getString(encoder); assertThat(result, is(encodedNameForEncoding)); }