[bvalid-codewatch] SF.net SVN: bvalid: [35] trunk/src/test
Status: Beta
Brought to you by:
cwilper
|
From: <cw...@us...> - 2006-05-10 05:14:00
|
Revision: 35 Author: cwilper Date: 2006-05-09 13:10:49 -0700 (Tue, 09 May 2006) ViewCVS: http://svn.sourceforge.net/bvalid/?rev=35&view=rev Log Message: ----------- Added locator unit tests Modified Paths: -------------- trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java trunk/src/test/net/sf/bvalid/catalog/SchemaCatalogTest.java Added Paths: ----------- trunk/src/test/data/ trunk/src/test/data/existing-schema.xsd trunk/src/test/net/sf/bvalid/TestConfig.java trunk/src/test/net/sf/bvalid/locator/ trunk/src/test/net/sf/bvalid/locator/CachingSchemaLocatorTest.java trunk/src/test/net/sf/bvalid/locator/CatalogSchemaLocatorTest.java trunk/src/test/net/sf/bvalid/locator/ChainingSchemaLocatorTest.java trunk/src/test/net/sf/bvalid/locator/LocatorPackageTestSuite.java trunk/src/test/net/sf/bvalid/locator/SchemaLocatorTest.java trunk/src/test/net/sf/bvalid/locator/URLSchemaLocatorTest.java Added: trunk/src/test/data/existing-schema.xsd =================================================================== --- trunk/src/test/data/existing-schema.xsd (rev 0) +++ trunk/src/test/data/existing-schema.xsd 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,14 @@ +<schema targetNamespace="http://example.org/sample/" + xmlns:this="http://example.org/sample/" + xmlns="http://www.w3.org/2001/XMLSchema" + elementFormDefault="qualified" + attributeFormDefault="unqualified"> + + <element name="sample" type="this:rootType"/> + + <complexType name="rootType"> + <sequence> + <element name="sample-element" type="string" maxOccurs="unbounded"/> + </sequence> + </complexType> +</schema> Modified: trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java =================================================================== --- trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java 2006-05-09 20:08:57 UTC (rev 34) +++ trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java 2006-05-09 20:10:49 UTC (rev 35) @@ -6,6 +6,7 @@ import junit.swingui.TestRunner; import net.sf.bvalid.catalog.CatalogPackageTestSuite; +import net.sf.bvalid.locator.LocatorPackageTestSuite; import net.sf.bvalid.util.JettyTestSetup; public class BValidPackageTestSuite extends TestCase { @@ -19,15 +20,15 @@ suite.addTestSuite(ValidatorFactoryTest.class); suite.addTestSuite(ValidatorOptionTest.class); - // sub-packages + // sub-package suites suite.addTest(CatalogPackageTestSuite.suite()); + suite.addTest(LocatorPackageTestSuite.suite()); - boolean fork = true; - String forkValue = System.getProperty("jetty.fork"); - if (forkValue != null) { - fork = !(forkValue.equalsIgnoreCase("false") || forkValue.equalsIgnoreCase("no")); - } - return new JettyTestSetup(suite, 7357, "/", ".", fork); + return new JettyTestSetup(suite, + TestConfig.TEST_PORT, + "/", + TestConfig.TEST_DATADIR, + TestConfig.JETTY_FORK); } public static void main(String[] args) throws Exception { Added: trunk/src/test/net/sf/bvalid/TestConfig.java =================================================================== --- trunk/src/test/net/sf/bvalid/TestConfig.java (rev 0) +++ trunk/src/test/net/sf/bvalid/TestConfig.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,83 @@ +package net.sf.bvalid; + +import java.util.*; + +import org.apache.log4j.Logger; + +public abstract class TestConfig { + + public final static String EXISTING_SCHEMA = "existing-schema.xsd"; + public final static String MISSING_SCHEMA = "missing-schema.xsd"; + + // jetty.fork + public static boolean JETTY_FORK = true; + + // test.datadir + public static String TEST_DATADIR = "src/test/data/"; + + // test.port + public static int TEST_PORT = 7375; + + public static String BASE_URL; + public static String EXISTING_SCHEMA_PATH; + public static String EXISTING_SCHEMA_URL; + public static String MISSING_SCHEMA_PATH; + public static String MISSING_SCHEMA_URL; + + private static final Logger _LOG = Logger.getLogger(TestConfig.class.getName()); + + static { + + String jettyFork = System.getProperty("jetty.fork"); + if (jettyFork != null) { + boolean wasInvalid = false; + if (jettyFork.equalsIgnoreCase("true") + || jettyFork.equalsIgnoreCase("yes")) { + JETTY_FORK = true; + } else if (jettyFork.equalsIgnoreCase("false") + || jettyFork.equalsIgnoreCase("no")) { + JETTY_FORK = false; + } else { + _LOG.warn("Specified jetty.fork was invalid, using default: " + JETTY_FORK); + wasInvalid = true; + } + if (!wasInvalid) { + _LOG.info("Using specified jetty.fork: " + JETTY_FORK); + } + } else { + _LOG.info("Using default jetty.fork: " + JETTY_FORK); + } + + String testDatadir = System.getProperty("test.datadir"); + if (testDatadir != null) { + TEST_DATADIR = testDatadir; + if (!TEST_DATADIR.endsWith("/")) { + TEST_DATADIR += "/"; + } + _LOG.info("Using specified test.datadir: " + TEST_DATADIR); + } else { + _LOG.info("Using default test.datadir: " + TEST_DATADIR); + } + + String testPort = System.getProperty("test.port"); + if (testPort != null) { + try { + TEST_PORT = Integer.parseInt(testPort); + _LOG.info("Using specified test.port: " + TEST_PORT); + } catch (Exception e) { + _LOG.warn("Specified test.port was invalid, using default: " + TEST_PORT); + } + } else { + _LOG.info("Using default test.port: " + TEST_PORT); + } + + BASE_URL = "http://localhost:" + TEST_PORT + "/"; + + EXISTING_SCHEMA_PATH = TEST_DATADIR + EXISTING_SCHEMA; + EXISTING_SCHEMA_URL = BASE_URL + EXISTING_SCHEMA; + MISSING_SCHEMA_PATH = TEST_DATADIR + MISSING_SCHEMA; + MISSING_SCHEMA_URL = BASE_URL + MISSING_SCHEMA; + + } + +} Modified: trunk/src/test/net/sf/bvalid/catalog/SchemaCatalogTest.java =================================================================== --- trunk/src/test/net/sf/bvalid/catalog/SchemaCatalogTest.java 2006-05-09 20:08:57 UTC (rev 34) +++ trunk/src/test/net/sf/bvalid/catalog/SchemaCatalogTest.java 2006-05-09 20:10:49 UTC (rev 35) @@ -18,24 +18,27 @@ InputStream in = new ByteArrayInputStream(_CONTENT.getBytes()); - getCatalog().put(_URI, in); + SchemaCatalog catalog = getCatalog(); + String testClass = catalog.getClass().getName(); + catalog.put(_URI, in); + int i = 0; String uri = "undefined"; - Iterator iter = getCatalog().listURIs(); + Iterator iter = catalog.listURIs(); while (iter.hasNext()) { - assertEquals("Catalog should only contain one URI at this point", + assertEquals(testClass + " should only contain one URI at this point", i, 0); uri = (String) iter.next(); i++; } - assertEquals("Catalog contains wrong URI", + assertEquals(testClass + " contains wrong URI", uri, _URI); - assertEquals("Catalog.contains should have returned true after put", - getCatalog().contains(_URI), + assertEquals(testClass + ".contains should have returned true after put", + catalog.contains(_URI), true); } @@ -43,10 +46,14 @@ public void testGet() throws Exception { InputStream in = new ByteArrayInputStream(_CONTENT.getBytes()); - getCatalog().put(_URI, in); - InputStream gotIn = getCatalog().get(_URI); + SchemaCatalog catalog = getCatalog(); + String testClass = catalog.getClass().getName(); + catalog.put(_URI, in); + + InputStream gotIn = catalog.get(_URI); + try { BufferedReader reader = new BufferedReader(new InputStreamReader(gotIn)); StringBuffer buf = new StringBuffer(); @@ -60,7 +67,7 @@ if (_CONTENT.endsWith("\n")) { buf.append(_CONTENT); } - assertEquals("Got different schema content than what was put", + assertEquals(testClass + " gave different schema content than what was put", buf.toString(), _CONTENT); } finally { @@ -72,17 +79,21 @@ public void testRemove() throws Exception { InputStream in = new ByteArrayInputStream(_CONTENT.getBytes()); - getCatalog().put(_URI, in); - getCatalog().remove(_URI); + SchemaCatalog catalog = getCatalog(); + String testClass = catalog.getClass().getName(); - Iterator iter = getCatalog().listURIs(); - assertEquals("Catalog should be empty after remove", + catalog.put(_URI, in); + + catalog.remove(_URI); + + Iterator iter = catalog.listURIs(); + assertEquals(testClass + " should be empty after remove", iter.hasNext(), false); - in = getCatalog().get(_URI); - assertNull("Catalog.get should have returned null after removal", + in = catalog.get(_URI); + assertNull(testClass + ".get should have returned null after removal", in); } Added: trunk/src/test/net/sf/bvalid/locator/CachingSchemaLocatorTest.java =================================================================== --- trunk/src/test/net/sf/bvalid/locator/CachingSchemaLocatorTest.java (rev 0) +++ trunk/src/test/net/sf/bvalid/locator/CachingSchemaLocatorTest.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,39 @@ +package net.sf.bvalid.locator; + +import java.io.*; + +import junit.framework.TestCase; +import junit.swingui.TestRunner; + +import net.sf.bvalid.TestConfig; +import net.sf.bvalid.catalog.MemorySchemaCatalog; +import net.sf.bvalid.catalog.SchemaCatalog; + +public class CachingSchemaLocatorTest extends SchemaLocatorTest { + + private SchemaLocator _locator; + private SchemaCatalog _catalog; + + public CachingSchemaLocatorTest(String name) { super (name); } + + public void setUp() throws Exception { + _catalog = new MemorySchemaCatalog(); + _catalog.put(TestConfig.EXISTING_SCHEMA_URL, + new FileInputStream(new File(TestConfig.EXISTING_SCHEMA_PATH))); + _locator = new CachingSchemaLocator(new MemorySchemaCatalog(), + new MemorySchemaCatalog(), + new CatalogSchemaLocator(_catalog)); + } + + public void tearDown() { + } + + protected SchemaLocator getTestLocator() { + return _locator; + } + + public static void main(String[] args) { + TestRunner.run(CachingSchemaLocatorTest.class); + } + +} Added: trunk/src/test/net/sf/bvalid/locator/CatalogSchemaLocatorTest.java =================================================================== --- trunk/src/test/net/sf/bvalid/locator/CatalogSchemaLocatorTest.java (rev 0) +++ trunk/src/test/net/sf/bvalid/locator/CatalogSchemaLocatorTest.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,37 @@ +package net.sf.bvalid.locator; + +import java.io.*; + +import junit.framework.TestCase; +import junit.swingui.TestRunner; + +import net.sf.bvalid.TestConfig; +import net.sf.bvalid.catalog.MemorySchemaCatalog; +import net.sf.bvalid.catalog.SchemaCatalog; + +public class CatalogSchemaLocatorTest extends SchemaLocatorTest { + + private SchemaLocator _locator; + private SchemaCatalog _catalog; + + public CatalogSchemaLocatorTest(String name) { super (name); } + + public void setUp() throws Exception { + _catalog = new MemorySchemaCatalog(); + _catalog.put(TestConfig.EXISTING_SCHEMA_URL, + new FileInputStream(new File(TestConfig.EXISTING_SCHEMA_PATH))); + _locator = new CatalogSchemaLocator(_catalog); + } + + public void tearDown() { + } + + protected SchemaLocator getTestLocator() { + return _locator; + } + + public static void main(String[] args) { + TestRunner.run(CatalogSchemaLocatorTest.class); + } + +} Added: trunk/src/test/net/sf/bvalid/locator/ChainingSchemaLocatorTest.java =================================================================== --- trunk/src/test/net/sf/bvalid/locator/ChainingSchemaLocatorTest.java (rev 0) +++ trunk/src/test/net/sf/bvalid/locator/ChainingSchemaLocatorTest.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,46 @@ +package net.sf.bvalid.locator; + +import java.io.*; +import java.util.*; + +import junit.framework.TestCase; +import junit.swingui.TestRunner; + +import net.sf.bvalid.TestConfig; +import net.sf.bvalid.catalog.MemorySchemaCatalog; +import net.sf.bvalid.catalog.SchemaCatalog; + +public class ChainingSchemaLocatorTest extends SchemaLocatorTest { + + private SchemaLocator _locator; + private SchemaCatalog _catalog1; + private SchemaCatalog _catalog2; + + public ChainingSchemaLocatorTest(String name) { super (name); } + + public void setUp() throws Exception { + + _catalog1 = new MemorySchemaCatalog(); + _catalog2 = new MemorySchemaCatalog(); + _catalog2.put(TestConfig.EXISTING_SCHEMA_URL, + new FileInputStream(new File(TestConfig.EXISTING_SCHEMA_PATH))); + + List locators = new ArrayList(); + locators.add(new CatalogSchemaLocator(_catalog1)); + locators.add(new CatalogSchemaLocator(_catalog2)); + + _locator = new ChainingSchemaLocator(locators); + } + + public void tearDown() { + } + + protected SchemaLocator getTestLocator() { + return _locator; + } + + public static void main(String[] args) { + TestRunner.run(ChainingSchemaLocatorTest.class); + } + +} Added: trunk/src/test/net/sf/bvalid/locator/LocatorPackageTestSuite.java =================================================================== --- trunk/src/test/net/sf/bvalid/locator/LocatorPackageTestSuite.java (rev 0) +++ trunk/src/test/net/sf/bvalid/locator/LocatorPackageTestSuite.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,24 @@ +package net.sf.bvalid.locator; + +import junit.framework.Test; +import junit.framework.TestSuite; +import junit.swingui.TestRunner; + +public class LocatorPackageTestSuite { + + public static Test suite() { + + TestSuite suite = new TestSuite(LocatorPackageTestSuite.class.getName()); + + suite.addTestSuite(CachingSchemaLocatorTest.class); + suite.addTestSuite(CatalogSchemaLocatorTest.class); + suite.addTestSuite(ChainingSchemaLocatorTest.class); + suite.addTestSuite(URLSchemaLocatorTest.class); + + return suite; + } + + public static void main(String[] args) { + TestRunner.run(LocatorPackageTestSuite.class); + } +} Added: trunk/src/test/net/sf/bvalid/locator/SchemaLocatorTest.java =================================================================== --- trunk/src/test/net/sf/bvalid/locator/SchemaLocatorTest.java (rev 0) +++ trunk/src/test/net/sf/bvalid/locator/SchemaLocatorTest.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,73 @@ +package net.sf.bvalid.locator; + +import java.io.*; + +import junit.framework.TestCase; + +import net.sf.bvalid.TestConfig; +import net.sf.bvalid.ValidatorException; + +public abstract class SchemaLocatorTest extends TestCase { + + public SchemaLocatorTest(String name) { super(name); } + + /** + * Get a locator that can resolve EXISTING_SCHEMA_LOCATION but + * but cannot resolve MISSING_SCHEMA_LOCATION + */ + protected abstract SchemaLocator getTestLocator(); + + public void testGetExisting() throws Exception { + + SchemaLocator locator = getTestLocator(); + String testMethod = locator.getClass().getName() + ".get"; + + InputStream in = null; + try { + in = locator.get(TestConfig.EXISTING_SCHEMA_URL, true); + assertNotNull(testMethod + "(..) should never return null if required is true", in); + } catch (ValidatorException e) { + fail(testMethod + "(..) for existing schema threw ValidatorException"); + } finally { + try { in.close(); } catch (Exception e) { } + } + } + + public void testGetMissingRequired() throws Exception { + + SchemaLocator locator = getTestLocator(); + String testMethod = locator.getClass().getName() + ".get"; + + boolean threwException = false; + InputStream in = null; + try { + in = locator.get(TestConfig.MISSING_SCHEMA_URL, true); + } catch (ValidatorException e) { + threwException = true; + } finally { + try { in.close(); } catch (Exception e) { } + } + assertEquals(testMethod + "(..) should have thrown exception for missing required schema", + threwException, + true); + } + + + public void testGetMissingOptional() throws Exception { + + SchemaLocator locator = getTestLocator(); + String testMethod = locator.getClass().getName() + ".get"; + + InputStream in = null; + try { + in = locator.get(TestConfig.MISSING_SCHEMA_URL, false); + assertNull(testMethod + "(..) returned non-null value for missing optional schema", + in); + } catch (ValidatorException e) { + fail(testMethod + "(..) threw exception for missing optional schema"); + } finally { + try { in.close(); } catch (Exception e) { } + } + } + +} Added: trunk/src/test/net/sf/bvalid/locator/URLSchemaLocatorTest.java =================================================================== --- trunk/src/test/net/sf/bvalid/locator/URLSchemaLocatorTest.java (rev 0) +++ trunk/src/test/net/sf/bvalid/locator/URLSchemaLocatorTest.java 2006-05-09 20:10:49 UTC (rev 35) @@ -0,0 +1,31 @@ +package net.sf.bvalid.locator; + +import java.io.*; + +import junit.framework.TestCase; +import junit.swingui.TestRunner; + +import net.sf.bvalid.TestConfig; + +public class URLSchemaLocatorTest extends SchemaLocatorTest { + + private SchemaLocator _locator; + + public URLSchemaLocatorTest(String name) { super (name); } + + public void setUp() throws Exception { + _locator = new URLSchemaLocator(); + } + + public void tearDown() { + } + + protected SchemaLocator getTestLocator() { + return _locator; + } + + public static void main(String[] args) { + TestRunner.run(URLSchemaLocatorTest.class); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |