XPath Example

  private Document plist;
  
  protected void setUp() 
    throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true); // NEVER FORGET THIS!
    DocumentBuilder builder = factory.newDocumentBuilder();
    
    plist = builder.parse(new File("thunderbirdplist.xml"));
  }
  
  public void testNoTwoKeyElementsAreAdjacent() 
    throws TransformerException {
     
    assertXpathNotExists(
     "//key/following-sibling::*[1]/self::key", 
     plist);
    
  }

  public void testCreatorCodeIsMOZM() throws TransformerException {
     
    assertXpathEvaluatesTo("MOZM",
     "//key[. = 'CFBundleSignature']/following-sibling::string",
     plist);
    
  }  
  
  
  public void testThereIsAnIcon() throws TransformerException {
     
    assertXpathExists(
     "//key[. = 'CFBundleIconFile']", 
     plist);
    assertXpathExists(
     "//key[. = 'CFBundleIconFile']/following-sibling::string", 
     plist);
    
  }

Previous | Next | Top | Cafe con Leche

Copyright 2005, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 17, 2005