### Question:
NameExtractor { public QName getName(String name, String namespace) { String[] parts = name.split(":"); if (parts.length < 1 || parts.length > 2) { throw new RuntimeException("Variable name '" + name + "' is invalid"); } String namespacePrefix = null; String localPart = parts[parts.length - 1]; if (parts.length == 2) { namespacePrefix = parts[0]; } if (namespace != null && namespace.length() > 0) { if (namespacePrefix != null) { return new QName(namespace, localPart, namespacePrefix); } else { return new QName(namespace, localPart); } } if (namespacePrefix != null) { return new QName(DEFAULT_NS_PREFIX, localPart, namespacePrefix); } else { return new QName(localPart); } }  QName getName(String name, String namespace);  }### Answer:
@Test(expected = RuntimeException.class) public void shouldThrowExceptionForInvalidNameWithNotEnoughDetails() { extractor.getName(SEPARATOR, ""); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionForInvalidNameWithTooManyColons() { extractor.getName("a:b:c", ""); }
@Test public void shouldReturnQNameWithLocalPartOnly() { QName result = extractor.getName(LOCAL_PART, null); assertThat(result.getLocalPart(), is(LOCAL_PART)); assertThat(result.getNamespaceURI(), is(DEFAULT_NS_PREFIX)); assertThat(result.getPrefix(), is(DEFAULT_NS_PREFIX)); }
@Test public void shouldReturnQNameWithLocalPartAndPrefix() { QName result = extractor.getName(PREFIX + SEPARATOR + LOCAL_PART, null); assertThat(result.getLocalPart(), is(LOCAL_PART)); assertThat(result.getNamespaceURI(), is(DEFAULT_NS_PREFIX)); assertThat(result.getPrefix(), is(PREFIX)); }
@Test public void shouldReturnQNameWithLocalPartAndEmptyNamespace() { QName result = extractor.getName(LOCAL_PART, DEFAULT_NS_PREFIX); assertThat(result.getLocalPart(), is(LOCAL_PART)); assertThat(result.getNamespaceURI(), is(DEFAULT_NS_PREFIX)); assertThat(result.getPrefix(), is(DEFAULT_NS_PREFIX)); }
@Test public void shouldReturnQNameWithLocalPartAndPrefixAndEmptyNamespace() { QName result = extractor.getName(PREFIX + SEPARATOR + LOCAL_PART, DEFAULT_NS_PREFIX); assertThat(result.getLocalPart(), is(LOCAL_PART)); assertThat(result.getNamespaceURI(), is(DEFAULT_NS_PREFIX)); assertThat(result.getPrefix(), is(PREFIX)); }
@Test public void shouldReturnQNameWithLocalPartAndNamespace() { QName result = extractor.getName(LOCAL_PART, NAMESPACE); assertThat(result.getLocalPart(), is(LOCAL_PART)); assertThat(result.getNamespaceURI(), is(NAMESPACE)); assertThat(result.getPrefix(), is(DEFAULT_NS_PREFIX)); }
@Test public void shouldReturnQNameWithLocalPartAndPrefixAndNamespace() { QName result = extractor.getName(PREFIX + SEPARATOR + LOCAL_PART, NAMESPACE); assertThat(result.getLocalPart(), is(LOCAL_PART)); assertThat(result.getNamespaceURI(), is(NAMESPACE)); assertThat(result.getPrefix(), is(PREFIX)); }