import org.junit.Before; 
import org.junit.Test; 
import org.xml.sax.SAXException; 

import javax.xml.XMLConstants; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 
import java.io.StringReader; 

public class JI9047928 {

	private StreamSource schemaSource; 
	private StreamSource xmlSource; 
	@Before 
	public void setup() throws Exception { 
		schemaSource = new StreamSource(new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
				"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:h=\"http://www.w3.org/1999/xhtml\"\n" + 
				" xmlns:sn=\"http://www.test.de/config.xml\"\n" + 
				" targetNamespace=\"http://www.test.de/config.xml\" elementFormDefault=\"qualified\">\n" + 
				" <xsd:element name=\"test\" type=\"sn:object\">\n" + 
				" <xsd:unique name=\"testunique\">\n" + 
				" <xsd:selector xpath=\"sn:innerObject\"/>\n" + 
				" <xsd:field xpath=\"sn:innerInnerObject/@test-unique-attribute\"/>\n" + 
				" </xsd:unique>\n" + 
				" </xsd:element>\n" + 
				"\n" + 
				" <xsd:complexType name=\"object\">\n" + 
				" <xsd:sequence>\n" + 
				" <xsd:element name=\"innerObject\" maxOccurs=\"unbounded\" type=\"sn:testType\" />\n" + 
				" </xsd:sequence>\n" + 
				" </xsd:complexType>\n" + 
				"\n" + 
				" <xsd:complexType name=\"testType\">\n" + 
				" <xsd:sequence>\n" + 
				" <xsd:element name=\"innerInnerObject\" maxOccurs=\"unbounded\" type=\"sn:testObjectType\"/>\n" + 
				" </xsd:sequence>\n" + 
				" </xsd:complexType>\n" + 
				"\n" + 
				" <xsd:complexType name=\"testObjectType\">\n" + 
				" <xsd:attribute use=\"optional\" name=\"test-unique-attribute\" type=\"xsd:int\" />\n" + 
				" </xsd:complexType>\n" + 
				"\n" + 
				"</xsd:schema>\n")); 
		xmlSource = new StreamSource(new StringReader("<test xmlns=\"http://www.test.de/config.xml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.test.de/config.xml\">\n" + 
				" <innerObject>\n" + 
				" <innerInnerObject test-unique-attribute=\"1\" />\n" + 
				" <innerInnerObject test-unique-attribute=\"1\" />\n" + 
				" </innerObject>\n" + 
				"</test>")); 

	} 

	@Test(expected = SAXException.class) 
	public void testUniqueness() throws Exception { 
		final Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaSource); 
		final Validator val = schema.newValidator(); 
		val.validate(xmlSource); 
	} 


}
