Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/ObjectContextTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/ObjectContextTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/ObjectContextTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/ObjectContextTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,203 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.object.jndi; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import javax.naming.Binding; +import javax.naming.NameClassPair; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import org.apache.oodt.commons.object.jndi.ObjectContext; +import junit.framework.TestCase; + +/** + * Unit test for {@link ObjectContext}. + * + * @author Kelly + * @version $Revision: 1.2 $ + */ +public class ObjectContextTest extends TestCase { + /** + * Creates a new <code>ObjectContextTest</code> instance. + * + * @param caseName a <code>String</code> value. + */ + public ObjectContextTest(String caseName) { + super(caseName); + } + + public void setUp() throws Exception { + super.setUp(); + + aliasFile = File.createTempFile("test", ".properties"); + aliasFile.deleteOnExit(); + Properties aliases = new Properties(); + aliases.setProperty("urn:alias:x", "urn:a:x"); + FileOutputStream out = new FileOutputStream(aliasFile); + aliases.save(out, "Temporary properties"); + out.close(); + + a1 = new TestContext("urn:a"); + a2 = new TestContext("urn:a"); + b = new TestContext("urn:b"); + + oldValue = System.getProperty("org.apache.oodt.commons.object.jndi.aliases"); + System.setProperty("org.apache.oodt.commons.object.jndi.aliases", aliasFile.toString()); + + List contexts = new ArrayList(); + contexts.add(a1); + contexts.add(a2); + contexts.add(b); + context = new ObjectContext(contexts); + } + + public void tearDown() throws Exception { + aliasFile.delete(); + if (oldValue == null) + System.getProperties().remove("org.apache.oodt.commons.object.jndi.aliases"); + else + System.setProperty("org.apache.oodt.commons.object.jndi.aliases", oldValue); + super.tearDown(); + } + + /** + * Test various aspects of the object context. + * + * @throws NamingException if an error occurs. + */ + public void testObjectContext() throws NamingException { + // Test retrieval of nonexistent bindings + try { + context.lookup("urn:a:x"); + fail("Found nonexistent object"); + } catch (NamingException ex) {} + + // Test binding names that don't match any delegate's namespace prefix. + try { + context.bind("urn:c:x", this); + fail("Bound nonconforming name"); + } catch (NamingException ex) {} + + // Test binding and retrieval + context.bind("urn:a:x", this); // Bind something + assertSame(this, context.lookup("urn:a:x")); // Look it up + assertSame(this, context.lookup("urn:alias:x")); // Try the alias + assertTrue(a1.bindings.values().contains(this)); // It should be in both a1... + assertTrue(a2.bindings.values().contains(this)); // ...and a2 + assertTrue(!b.bindings.values().contains(this)); // But not b. + + context.bind("urn:b:x", getClass()); // Now bind something for b + assertSame(getClass(), context.lookup("urn:b:x")); // Look it up + assertTrue(!a1.bindings.values().contains(getClass())); // It should not be in a1... + assertTrue(!a2.bindings.values().contains(getClass())); // ...nor a2 + assertTrue(b.bindings.values().contains(getClass())); // But should be in b. + + // Test binding a bound name + try { + context.bind("urn:a:x", ""); + fail("Able to bind an already-bound name"); + } catch (NamingException ex) {} + + // Test rebinding a bound name + context.rebind("urn:a:x", context); // Bind to a different object + assertSame(context, context.lookup("urn:a:x")); // Look it up + assertTrue(!a1.bindings.values().contains(this)); // The old object should be gone from a1... + assertTrue(!a2.bindings.values().contains(this)); // ...and from a2 + assertTrue(!b.bindings.values().contains(context)); // And the new object isn't in b + + // Test renaming + context.rename("urn:a:x", "urn:a:y"); // Change x to y + try { + context.lookup("urn:a:x"); // Look it up + fail("Found object under old name"); + } catch (NamingException ex) {} + assertSame(context, context.lookup("urn:a:y")); // Just the name has changed + assertTrue(a1.bindings.keySet().contains("urn:a:y")); // The new name is in a1 + assertTrue(!a1.bindings.keySet().contains("urn:a:x")); // But not the old + assertTrue(a2.bindings.keySet().contains("urn:a:y")); // The new name is in a2 + assertTrue(!a2.bindings.keySet().contains("urn:a:x")); // But not the old + assertTrue(!b.bindings.values().contains(context)); // It was never in b + + // Test listing + int count = 0; + boolean sawA = false; + boolean sawB = false; + NamingEnumeration e = context.list(""); + while (e.hasMore()) { + NameClassPair p = (NameClassPair) e.next(); + if ("urn:a:y".equals(p.getName()) && context.getClass().getName().equals(p.getClassName())) + sawA = true; + else if ("urn:b:x".equals(p.getName()) && "java.lang.Class".equals(p.getClassName())) + sawB = true; + else + fail("Unexpected binding \"" + p.getName() + "\" to " + p.getClassName()); + ++count; + } + assertEquals(3, count); + assertTrue(sawA); + assertTrue(sawB); + + // Test listing of bindings + count = 0; + sawA = false; + sawB = false; + e = context.listBindings(""); + while (e.hasMore()) { + Binding b = (Binding) e.next(); + if ("urn:a:y".equals(b.getName()) && context == b.getObject()) + sawA = true; + else if ("urn:b:x".equals(b.getName()) && getClass() == b.getObject()) + sawB = true; + else + fail("Unexpected binding \"" + b.getName() + "\" to " + b.getObject()); + ++count; + } + assertEquals(3, count); + assertTrue(sawA); + assertTrue(sawB); + + // Test unbinding + context.unbind("urn:a:y"); // Unbind it + try { + context.lookup("urn:a:y"); // Look it up + fail("Found unbound object"); + } catch (NamingException ex) {} + assertTrue(a1.bindings.isEmpty()); // It's not in a1... + assertTrue(a2.bindings.isEmpty()); // ...nor in a2 + } + + /** First delegate context for "urn:a" namespace. */ + private TestContext a1; + + /** Second delegate context for "urn:a" namespace. */ + private TestContext a2; + + /** Delegate context for "urn:b" namespace. */ + private TestContext b; + + /** Test subject: the object context. */ + private ObjectContext context; + + /** Test alias file. */ + private File aliasFile; + + /** Old value of sys prop org.apache.oodt.commons.object.jndi.aliases. */ + private String oldValue; +}
Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContext.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContext.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContext.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContext.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,203 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.object.jndi; + +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import javax.naming.Binding; +import javax.naming.CompositeName; +import javax.naming.Context; +import javax.naming.InvalidNameException; +import javax.naming.Name; +import javax.naming.NameAlreadyBoundException; +import javax.naming.NameClassPair; +import javax.naming.NameNotFoundException; +import javax.naming.NameParser; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.OperationNotSupportedException; + +/** + * A context for testing. This context uses a simple {@link Map} to track bindings. + * Names must start with a given prefix string or they cause {@link + * InvalidNameException}s. + * + * @author Kelly + * @version $Revision: 1.1 $ + */ +class TestContext implements Context { + /** + * Creates a new <code>TestContext</code> instance. + * + * @param prefix What every name must start with. + */ + public TestContext(String prefix) { + this.prefix = prefix; + } + + public Object lookup(Name name) throws NamingException { + return lookup(name.toString()); + } + public Object lookup(String name) throws NamingException { + Object rc = bindings.get(name); + if (rc == null) + throw new NameNotFoundException(name); + return rc; + } + public void bind(Name name, Object obj) throws NamingException { + bind(name.toString(), obj); + } + public void bind(String name, Object obj) throws NamingException { + if (!name.startsWith(prefix)) throw new InvalidNameException("Name doesn't start with " + prefix); + if (bindings.containsKey(name)) + throw new NameAlreadyBoundException(name); + bindings.put(name, obj); + } + public void rebind(Name name, Object obj) throws NamingException { + rebind(name.toString(), obj); + } + public void rebind(String name, Object obj) throws NamingException { + if (!name.startsWith(prefix)) throw new InvalidNameException("Name doesn't start with " + prefix); + bindings.put(name, obj); + } + public void unbind(Name name) throws NamingException { + unbind(name.toString()); + } + public void unbind(String name) throws NamingException { + if (bindings.remove(name) == null) + throw new NameNotFoundException(name); + } + public void rename(Name oldName, Name newName) throws NamingException { + rename(oldName.toString(), newName.toString()); + } + public void rename(String oldName, String newName) throws NamingException { + if (!bindings.containsKey(oldName)) + throw new NameNotFoundException(oldName); + if (bindings.containsKey(newName)) + throw new NameAlreadyBoundException(newName); + if (!newName.startsWith(prefix)) + throw new InvalidNameException("Name doesn't start with " + prefix); + bindings.put(newName, bindings.remove(oldName)); + } + public NamingEnumeration list(Name name) throws NamingException { + return list(name.toString()); + } + public NamingEnumeration list(String name) throws NamingException { + if (name.length() > 0) + throw new OperationNotSupportedException("subcontexts not supported"); + final Iterator i = bindings.entrySet().iterator(); + return new NamingEnumeration() { + public Object next() { + Map.Entry e = (Map.Entry) i.next(); + return new NameClassPair((String) e.getKey(), e.getValue().getClass().getName()); + } + public boolean hasMore() { + return i.hasNext(); + } + public void close() {} + public boolean hasMoreElements() { + return hasMore(); + } + public Object nextElement() { + return next(); + } + }; + } + public NamingEnumeration listBindings(Name name) throws NamingException { + return listBindings(name.toString()); + } + public NamingEnumeration listBindings(String name) throws NamingException { + if (name.length() > 0) + throw new OperationNotSupportedException("subcontexts not supported"); + final Iterator i = bindings.entrySet().iterator(); + return new NamingEnumeration() { + public Object next() { + Map.Entry e = (Map.Entry) i.next(); + return new Binding((String) e.getKey(), e.getValue()); + } + public boolean hasMore() { + return i.hasNext(); + } + public void close() {} + public boolean hasMoreElements() { + return hasMore(); + } + public Object nextElement() { + return next(); + } + + }; + } + public void destroySubcontext(Name name) throws NamingException { + destroySubcontext(name.toString()); + } + public void destroySubcontext(String name) throws NamingException { + throw new OperationNotSupportedException("subcontexts not supported"); + } + public Context createSubcontext(Name name) throws NamingException { + return createSubcontext(name.toString()); + } + public Context createSubcontext(String name) throws NamingException { + throw new OperationNotSupportedException("subcontexts not supported"); + } + public Object lookupLink(Name name) throws NamingException { + return lookupLink(name.toString()); + } + public Object lookupLink(String name) throws NamingException { + return lookup(name); + } + public NameParser getNameParser(Name name) { + return getNameParser(name.toString()); + } + public NameParser getNameParser(String name) { + return new ObjectNameParser(); + } + public String composeName(String name, String prefix) throws NamingException { + return composeName(new CompositeName(name), new CompositeName(prefix)).toString(); + } + public Name composeName(Name name, Name prefix) throws NamingException { + Name result = (Name) prefix.clone(); + result.addAll(name); + return result; + } + public Object addToEnvironment(String key, Object val) { + if (environment == null) environment = new Hashtable(); + return environment.put(key, val); + } + public Object removeFromEnvironment(String key) { + if (environment == null) environment = new Hashtable(); + return environment.remove(key); + } + public Hashtable getEnvironment() { + if (environment == null) environment = new Hashtable(); + return environment; + } + public void close() {} + public String getNameInNamespace() { + return ""; + } + + /** What holds the bindings. Keys are {@link String}s, values are {@link Object}s. */ + Map bindings = new HashMap(); + + /** What every key must start with. */ + private String prefix; + + /** Context's environment. */ + private Hashtable environment; +} Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContextTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContextTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContextTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/object/jndi/TestContextTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,96 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.object.jndi; + +import javax.naming.Binding; +import javax.naming.InvalidNameException; +import javax.naming.NameAlreadyBoundException; +import javax.naming.NameClassPair; +import javax.naming.NameNotFoundException; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import junit.framework.TestCase; + +/** + * Unit test for the test context. + * + * @author Kelly + * @version $Revision: 1.1 $ + */ +public class TestContextTest extends TestCase { + /** + * Creates a new <code>TestContextTest</code> instance. + * + * @param caseName a <code>String</code> value. + */ + public TestContextTest(String caseName) { + super(caseName); + } + + /** + * Test various aspects of the test context. + * + * @throws NamingException if an error occurs. + */ + public void testTestContext() throws NamingException { + TestContext ctx = new TestContext("urn:x:"); // Make it + try { + ctx.lookup("urn:x:y"); // Lookup a nonexistent binding + fail("Got a binding that doesn't exist"); // Got something? Yikes. + } catch (NameNotFoundException ex) {} + + try { + ctx.bind("urn:y:z", this); // Bind an invalid prefix + fail("Bound an invalid prefix"); // Worked? Dang. + } catch (InvalidNameException ex) {} + + ctx.bind("urn:x:a", this); // Bind something. + assertSame(this, ctx.lookup("urn:x:a")); // Look it up + + try { + ctx.bind("urn:x:a", getClass()); // Bind it again + fail("Able to re-bind"); // Worked? Crap. + } catch (NameAlreadyBoundException ex) {} + + ctx.rebind("urn:x:a", getClass()); // Rebind it again + assertSame(getClass(), ctx.lookup("urn:x:a")); // Look it up + + ctx.rename("urn:x:a", "urn:x:b"); // Rename the binding + assertSame(getClass(), ctx.lookup("urn:x:b")); // Look it up + + NamingEnumeration e = ctx.list(""); // List the context + assertTrue(e.hasMore()); // Got something? Good. + NameClassPair p = (NameClassPair) e.next(); // Get it + assertEquals("urn:x:b", p.getName()); // Right name? Good. + assertEquals("java.lang.Class", p.getClassName()); // Right class? Good. + assertTrue(!e.hasMore()); // Got no more? Good. + + e = ctx.listBindings(""); // List the bindings + assertTrue(e.hasMore()); // Got something? Good. + Binding b = (Binding) e.next(); // Get it + assertEquals("urn:x:b", p.getName()); // Right name? Good. + assertSame(getClass(), b.getObject()); // Right object? Good. + assertTrue(!e.hasMore()); // Got no more? Good. + + assertSame(getClass(), ctx.lookupLink("urn:x:b")); // Look up via the link + + ctx.unbind("urn:x:b"); // Unbind it + e = ctx.list(""); // List the context + assertTrue(!e.hasMore()); // Got no more? Good. + e = ctx.listBindings(""); // List the bindings + assertTrue(!e.hasMore()); // Got no more? Good. + } +} Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/Base64Test.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/Base64Test.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/Base64Test.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/Base64Test.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,40 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.util; + +import java.io.*; +import java.util.*; +import junit.framework.*; + +/** Unit test the {@link Base64} class. + * + * @author Kelly + */ +public class Base64Test extends TestCase { + /** Construct the test case for the {@link Base64} class. */ + public Base64Test(String name) { + super(name); + } + + /** Test encoding and decoding. + */ + public void testEncDec() { + byte[] a = Base64.encode("abcde".getBytes()); + assertTrue("Base-64 encoding failed", Arrays.equals("YWJjZGU=".getBytes(), a)); + byte[] b = Base64.decode("YWJjZGU=".getBytes()); + assertTrue("Base-64 decoding failed", Arrays.equals("abcde".getBytes(), b)); + } +} Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/CacheMapTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/CacheMapTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/CacheMapTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/CacheMapTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,112 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.util; + +import java.io.*; +import java.util.*; +import junit.framework.*; + +/** Unit test the {@link CacheMap} class. + * + * @author Kelly + */ +public class CacheMapTest extends TestCase { + /** Construct the test case for the {@link CacheMap} class. */ + public CacheMapTest(String name) { + super(name); + } + + /** Test the caching operation. */ + public void testCache() { + CacheMap c = new CacheMap(3); + c.put("alpha", "1"); + c.put("beta", "2"); + c.put("gamma", "3"); + // gamma beta alpha + assertEquals("2", c.get("beta")); + // beta gamma alpha + c.put("delta", "4"); + // delta beta gamma + assertNull(c.get("alpha")); + } + + /** Test the {@link CacheMap#size} and {@link CacheMap#isEmpty} methods. */ + public void testSize() { + CacheMap c = new CacheMap(3); + assertEquals(0, c.size()); + assertTrue(c.isEmpty()); + c.put("alpha", "1"); + // alpha + assertEquals(1, c.size()); + c.put("beta", "2"); + // beta alpha + assertEquals(2, c.size()); + c.put("gamma", "3"); + // gamma beta alpha + assertEquals(3, c.size()); + c.put("delta", "4"); + // delta gamma beta + assertEquals(3, c.size()); + c.clear(); + assertTrue(c.isEmpty()); + } + + /** Test the {@link CacheMap#containsKey} and {@link CacheMap#containsValue} methods. */ + public void testContains() { + CacheMap c = new CacheMap(3); + c.put("alpha", "1"); + c.put("beta", "2"); + c.put("gamma", "3"); + + assertTrue(c.containsKey("alpha")); + assertTrue(!c.containsKey("hungus")); + assertTrue(c.containsValue("2")); + assertTrue(!c.containsValue("x")); + } + + /** Test value replacement for the same key. */ + public void testRePut() { + CacheMap c = new CacheMap(3); + c.put("alpha", "1"); + c.put("beta", "2"); + c.put("gamma", "3"); + // (gamma, 3) (beta, 2) (alpha, 1) + c.put("alpha", "x"); + // (alpha, x), (gamma, 3) (beta, 2) + assertEquals("x", c.get("alpha")); + // (alpha, x), (gamma, 3) (beta, 2) + c.put("delta", "y"); + // (delta, y) (alpha, x), (gamma, 3) + assertEquals(3, c.size()); + assertNull(c.get("beta")); + } + + /** Test the {@link CacheMap#remove} method. */ + public void testRemove() { + CacheMap c = new CacheMap(3); + c.put("alpha", "1"); + c.put("beta", "2"); + c.put("gamma", "3"); + // gamma beta alpha + c.put("delta", "4"); + // delta gamma beta + assertEquals("3", c.remove("gamma")); + // delta beta + assertNull(c.remove("gamma")); + assertNull(c.remove("alpha")); + } +} + Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/EnterpriseEntityResolverTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/EnterpriseEntityResolverTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/EnterpriseEntityResolverTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/EnterpriseEntityResolverTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,121 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.util; + +import java.io.File; +import java.util.Collections; +import java.util.List; +import junit.framework.TestCase; + +/** + * Unit test the EnterpriseEntityResolver class. + * + * @author Kelly + */ +public class EnterpriseEntityResolverTest extends TestCase { + /** Construct the test case for the XML class. + * + * @param name Case name + */ + public EnterpriseEntityResolverTest(String name) { + super(name); + } + + /** + * Create a temporary directory with a temporary file as the test entity. + * + * @throws Exception if an error occurs. + */ + public void setUp() throws Exception { + super.setUp(); + testDir = File.createTempFile("eet", ".dir"); + testDir.delete(); + testDir.mkdir(); + testFile = new File(testDir, "test-entry-do-not-remove.dtd"); + if (!testFile.createNewFile()) + throw new Exception(testFile + " already exists, but shouldn't"); + } + + /** + * Delete the temporary file entity and test directory. + * + * @throws Exception if an error occurs. + */ + public void tearDown() throws Exception{ + testFile.delete(); + testDir.delete(); + super.tearDown(); + } + + /** + * Test if the entity parser works at initialization time. + */ + public void testEntityParsing() { + assertEquals("test-entry-do-not-remove.dtd", + EnterpriseEntityResolver.entities.get("-//JPL//DTD TEST ENTRY DO NOT REMOVE//EN")); + } + + /** + * Test if the resolver computes filenames based on public and system identifiers. + */ + public void testFilenameComputation() { + assertNull(EnterpriseEntityResolver.computeFilename(null, null)); + assertNull(EnterpriseEntityResolver.computeFilename(null, "unknown")); + assertNull(EnterpriseEntityResolver.computeFilename("unknown", null)); + assertNull(EnterpriseEntityResolver.computeFilename("unknown", "unknown")); + + assertEquals("test-entry-do-not-remove.dtd", + EnterpriseEntityResolver.computeFilename("-//JPL//DTD TEST ENTRY DO NOT REMOVE//EN", null)); + assertEquals("test-entry-do-not-remove.dtd", + EnterpriseEntityResolver.computeFilename(null, "http://oodt.jpl.nasa.gov/test-entry-do-not-remove.dtd")); + assertEquals("test-entry-do-not-remove.dtd", + EnterpriseEntityResolver.computeFilename("-//JPL//DTD TEST ENTRY DO NOT REMOVE//EN", + "illegal-url:test-entry-do-not-remove.dtd")); + assertEquals("test-entry-do-not-remove.dtd", + EnterpriseEntityResolver.computeFilename("illegal-FPI", + "http://oodt.jpl.nasa.gov/test-entry-do-not-remove.dtd")); + assertEquals("test-entry-do-not-remove.dtd", + EnterpriseEntityResolver.computeFilename("-//JPL//DTD TEST ENTRY DO NOT REMOVE//EN", + "http://oodt.jpl.nasa.gov/test-entry-do-not-remove.dtd")); + } + + /** + * Test if the resolver finds a file based on a list of directories and name. + */ + public void testFileFinding() { + List dirs = Collections.singletonList(testDir.toString()); + assertEquals(testFile, EnterpriseEntityResolver.findFile(dirs, testFile.getName())); + } + + /** + * Test if the resolver can convert a string specification of entity references + * into a {@link List}. + */ + public void testDirFinding() { + List dirs = EnterpriseEntityResolver.getEntityRefDirs(""); + assertTrue(dirs.isEmpty()); + dirs = EnterpriseEntityResolver.getEntityRefDirs("/tmp,/usr/local/xml"); + assertEquals(2, dirs.size()); + assertEquals("/tmp", dirs.get(0)); + assertEquals("/usr/local/xml", dirs.get(1)); + } + + /** Test directory to hold <var>testFile</var>, the test entity. */ + private File testDir; + + /** The test entity. */ + private File testFile; +} Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/LDAPTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/LDAPTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/LDAPTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/LDAPTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.util; + +import java.io.*; +import java.util.*; +import junit.framework.*; + +/** Unit test the {@link LDAP} class. + * + * @author Kelly + */ +public class LDAPTest extends TestCase { + /** Construct the test case for the {@link LDAP} class. */ + public LDAPTest(String name) { + super(name); + } + + /** Test the {@link LDAP#toLDAPString} method. */ + public void testToLDAPString() { + String str = "Hello (World), \\How are you*?"; + String result = LDAP.toLDAPString(str); + assertEquals("Hello \\28World\\29, \\5cHow are you\\2a?", result); + } +} + Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/UtilityTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/UtilityTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/UtilityTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/UtilityTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.util; + +import java.io.File; +import java.io.IOException; +import junit.framework.TestCase; + +public class UtilityTest extends TestCase { + public UtilityTest(String caseName) { + super(caseName); + } + + public void testDelete() throws IOException { + File top = File.createTempFile("topdir", ".dir"); + top.delete(); + top.mkdir(); + File f1 = File.createTempFile("nesteddir", ".file", top); + File f2 = File.createTempFile("nesteddir", ".file", top); + File d1 = File.createTempFile("nesteddir", ".dir", top); + d1.delete(); + d1.mkdir(); + File f3 = File.createTempFile("nesteddir", ".file", d1); + File d2 = File.createTempFile("nesteddir", ".dir", d1); + d2.delete(); + d2.mkdir(); + File f4 = File.createTempFile("nesteddir", ".file", d2); + + assertTrue(Utility.delete(top)); + assertTrue(!top.exists()); + } +} Added: oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/XMLTest.java URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/XMLTest.java?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/XMLTest.java (added) +++ oodt/trunk/commons/src/test/java/org/apache/oodt/commons/util/XMLTest.java Mon Dec 29 18:39:28 2014 @@ -0,0 +1,179 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE.txt file distributed with this work for +// additional information regarding copyright ownership. The ASF licenses this +// file to you under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +package org.apache.oodt.commons.util; + +import java.io.*; +import java.util.*; +import junit.framework.*; +import org.w3c.dom.*; +import org.xml.sax.*; + +/** Unit test the XML class. + * + * @author Kelly + */ +public class XMLTest extends TestCase { + /** Construct the test case for the XML class. */ + public XMLTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("test.xml"))); + inputSource = new InputSource(reader); + } + + protected void tearDown() throws Exception { + reader.close(); + } + + /** Test the {@link XML#createDocument} method. + */ + public void testDocumentCreation() { + Document doc = XML.createDocument(); + assertTrue(doc instanceof Document); + } + + /** Test the {@link XML#createDOMParser} and {@link XML#serialize} methods. + */ + public void testDOM() throws Exception { + DOMParser p = XML.createDOMParser(); + p.parse(inputSource); + Document doc = p.getDocument(); + doc.normalize(); + assertEquals("log", doc.getDocumentElement().getTagName()); + java.util.zip.CRC32 crc = new java.util.zip.CRC32(); + String result = XML.serialize(doc); + crc.update(result.getBytes()); + long value = crc.getValue(); + assertTrue("Stringified DOM document CRC mismatch, got value = " + value, 3880488030L == value || 2435419114L == value || /* added by Chris Mattmann: pretty print fix */3688328384L == value || /* other newline treatment */ 750262163L == value || 3738296466L == value /* Apache incubator warmed up the file, so it suffered thermal expansion */); + } + + /** Test the {@link XML#createSAXParser} method. + */ + public void testSAXParser() throws Exception { + SAXParser p = XML.createSAXParser(); + MyHandler handler = new MyHandler(); + p.setContentHandler(handler); + p.parse(inputSource); + // 25 refers to the 25 elements in the test.xml document. + assertEquals(25, handler.getElementCount()); + } + + /** Test the {@link XML#dump(PrintWriter,Node)} method. */ + public void testDump() throws Exception { + DOMParser p = XML.createDOMParser(); + p.parse(inputSource); + Document doc = p.getDocument(); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + XML.dump(printWriter, doc); + printWriter.close(); + java.util.zip.CRC32 crc = new java.util.zip.CRC32(); + crc.update(stringWriter.getBuffer().toString().getBytes()); + long value = crc.getValue(); + assertTrue("Dumped DOM tree CRC mismatch; got " + value, value == 828793L || value == 2241317601L); + } + + /** Test the {@link XML#unwrappedText} method. */ + public void testUnwrappedText() throws Exception { + DOMParser p = XML.createDOMParser(); + p.parse(inputSource); + Document doc = p.getDocument(); + doc.normalize(); + + Node node = doc.getDocumentElement().getFirstChild().getFirstChild().getNextSibling() + .getNextSibling().getFirstChild().getNextSibling().getNextSibling().getNextSibling().getNextSibling(); + assertEquals("Geeba, geeba. Geeba geeba geeba! Geeba, geeba, blooor? Bloorien bloreinda!", + XML.unwrappedText(node)); + assertNull(XML.unwrappedText(null)); + } + + /** Test the {@link XML#add(Node,String,String)} method. */ + public void testAddString() throws Exception { + Document doc = XML.createDocument(); + Element root = doc.createElement("root"); + doc.appendChild(root); + XML.add(root, "child", "child text"); + assertEquals("child", root.getFirstChild().getNodeName()); + assertEquals("child text", root.getFirstChild().getFirstChild().getNodeValue()); + NodeList children = root.getChildNodes(); + assertEquals(1, children.getLength()); + try { + XML.add(null, "child", "child text"); + } catch (IllegalArgumentException ex) { + return; + } + fail("Adding to a null node should fail by throwing IllegalArgumentException"); + } + + /** Test the {@link XML#add(Node,String,Object)} method. */ + public void testAddObject() throws Exception { + Object obj = new Object() { + public String toString() { + return "child text"; + } + }; + Document doc = XML.createDocument(); + Element root = doc.createElement("root"); + doc.appendChild(root); + XML.add(root, "child", obj); + assertEquals("child", root.getFirstChild().getNodeName()); + assertEquals("child text", root.getFirstChild().getFirstChild().getNodeValue()); + NodeList children = root.getChildNodes(); + assertEquals(1, children.getLength()); + try { + XML.add(null, "child", obj); + } catch (IllegalArgumentException ex) { + return; + } + fail("Adding to a null node should fail by throwing IllegalArgumentException"); + } + + /** Test the {@link XML#escape} method. */ + public void testEscape() { + assertEquals("", XML.escape("")); + assertEquals("So I said, "She said, 'Both 3 & 2 are < 5 but > 0' but he said ꯍ", + XML.escape("So I said, \"She said, 'Both 3 & 2 are < 5 but > 0' but he said \uabcd")); + } + + + /** Test the {@link XML#getDOMImplementation} method. */ + public void testGetDOMImplementation() { + DOMImplementation impl = XML.getDOMImplementation(); + assertNotNull(impl); + DOMImplementation impl2 = XML.getDOMImplementation(); + assertSame(impl, impl2); + } + + /** Used by {@link #testSAXParser} */ + private static class MyHandler extends org.xml.sax.helpers.DefaultHandler { + private int elementCount = 0; + public void endElement(String uri, String localName, String rawName) { + ++elementCount; + } + public int getElementCount() { + return elementCount; + } + } + + /** Reader for the test data file. */ + private BufferedReader reader; + + /** Input source for the {@link #reader}. */ + private InputSource inputSource; +} + Added: oodt/trunk/commons/src/test/resources/test-multiserver.xml URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/resources/test-multiserver.xml?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/resources/test-multiserver.xml (added) +++ oodt/trunk/commons/src/test/resources/test-multiserver.xml Mon Dec 29 18:39:28 2014 @@ -0,0 +1,27 @@ +<?xml version='1.0' encoding='UTF-8'?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more contributor +license agreements. See the NOTICE.txt file distributed with this work for +additional information regarding copyright ownership. The ASF licenses this +file to you under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. +--> +<multiserver xmlns="http://oodt.jpl.nasa.gov/edm-commons/xml/multiserver" id="test.app"> + <server class="org.apache.oodt.commons.MultiServerTest$Svr1" id="urn:eda:rmi:Test1" bind="true" /> + <server class="org.apache.oodt.commons.MultiServerTest$Svr2" id="urn:eda:rmi:Test2" bind="false" /> + <server class="org.apache.oodt.commons.MultiServerTest$Svr3" id="urn:eda:rmi:Test3" bind="rebind" /> + <server class="org.apache.oodt.commons.MultiServerTest$Svr4" id="urn:eda:rmi:Test4" bind="360000" /> + <properties> + <property name="my.setting">My Value</property> + <property name="my.other.setting">My Other Value</property> + </properties> +</multiserver> Added: oodt/trunk/commons/src/test/resources/test.xml URL: http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/resources/test.xml?rev=1648405&view=auto ============================================================================== --- oodt/trunk/commons/src/test/resources/test.xml (added) +++ oodt/trunk/commons/src/test/resources/test.xml Mon Dec 29 18:39:28 2014 @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE log [ +<!ELEMENT log (stream*)> +<!ELEMENT stream (streamID, analyst, message*)> +<!ELEMENT streamID (#PCDATA)> +<!ELEMENT analyst (#PCDATA)> +<!ELEMENT message (server, host, time, code, text)> +<!ELEMENT server (#PCDATA)> +<!ELEMENT host (#PCDATA)> +<!ELEMENT time (#PCDATA)> +<!ELEMENT code (#PCDATA)> +<!ELEMENT text (#PCDATA)> +]> +<log> + <stream> + <streamID>3</streamID> + <analyst>hyon</analyst> + + <message> + <server>TDS</server> + <host>latte</host> + <time>1352</time> + <code>121</code> + <text>Geeba, geeba. Geeba geeba geeba! Geeba, geeba, blooor? + Bloorien bloreinda! + </text> + </message> + + <message> + <server>Instrument</server> + <host>sorrento</host> + <time>1353</time> + <code>211</code> + <text>Geeba, bloor.</text> + </message> + + </stream> + + + <stream> + <streamID>4</streamID> + <analyst>kelly</analyst> + + <message> + <server>Analyst</server> + <host>killamanjaro</host> + <time>1354</time> + <code>212</code> + <text>Wahoo</text> + </message> + </stream> +</log> Propchange: oodt/trunk/commons/src/test/resources/test.xml ------------------------------------------------------------------------------ svn:executable = *
