001/* 002 * Copyright (c) 2004-2012, Willem Cazander 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 006 * that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the 009 * following disclaimer. 010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 011 * the following disclaimer in the documentation and/or other materials provided with the distribution. 012 * 013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 022 */ 023 024package org.x4o.xml.core; 025 026import java.io.FileNotFoundException; 027 028import org.x4o.xml.core.config.X4OLanguagePropertyKeys; 029import org.x4o.xml.test.TestParser; 030import org.xml.sax.SAXException; 031import org.xml.sax.SAXParseException; 032import junit.framework.TestCase; 033 034/** 035 * Tests a simple x4o xml language. 036 * 037 * @author Willem Cazander 038 * @version 1.0 Jul 24, 2006 039 */ 040public class EmptyXmlTest extends TestCase { 041 042 public void setUp() throws Exception { 043 } 044 045 public void testFileNotFound() throws Exception { 046 TestParser parser = new TestParser(); 047 try { 048 parser.parseFile("tests/empty-xml/non-excisting-file.xml"); 049 } catch (FileNotFoundException e) { 050 assertEquals(true, e.getMessage().contains("non-excisting-file.xml")); 051 return; 052 } 053 assertEquals(true,false); 054 } 055 056 public void testResourceNotFound() throws Exception { 057 TestParser parser = new TestParser(); 058 try { 059 parser.parseResource("tests/empty-xml/non-excisting-resource.xml"); 060 } catch (NullPointerException e) { 061 assertEquals(true,e.getMessage().contains("Could not find resource")); 062 return; 063 } 064 assertEquals(true,false); 065 } 066 067 public void testResourceParsing() throws Exception { 068 TestParser parser = new TestParser(); 069 try { 070 parser.parseResource("tests/empty-xml/empty-test.xml"); 071 } catch (SAXParseException e) { 072 assertEquals("No ElementNamespaceContext found for empty namespace.", e.getMessage()); 073 return; 074 } 075 assertEquals(true,false); 076 } 077 078 public void testResourceEmptyReal() throws Exception { 079 TestParser parser = new TestParser(); 080 try { 081 parser.parseResource("tests/empty-xml/empty-real.xml"); 082 } catch (SAXException e) { 083 assertEquals(true,e.getMessage().contains("Premature end of file.")); 084 return; 085 } 086 assertEquals(true,false); 087 } 088 089 public void testResourceEmptyXml() throws Exception { 090 TestParser parser = new TestParser(); 091 try { 092 parser.parseResource("tests/empty-xml/empty-xml.xml"); 093 } catch (SAXException e) { 094 boolean hasError = e.getMessage().contains("Premature end of file."); // java6+ sax message 095 if (hasError==false) { 096 hasError = e.getMessage().contains("A well-formed document requires a root element."); // xercesImpl sax message 097 } 098 assertEquals(true,hasError); 099 return; 100 } 101 assertEquals(true,false); 102 } 103 104 public void testEmptyX40() throws Exception { 105 TestParser parser = new TestParser(); 106 parser.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); 107 try { 108 parser.parseResource("tests/empty-xml/empty-x4o.xml"); 109 assertEquals(true,parser.getElementLanguage().getRootElement().getChilderen().isEmpty()); 110 } finally { 111 parser.doReleasePhaseManual(); 112 } 113 } 114}