From: markt Date: Mon, 15 Jun 2009 16:21:22 +0000 (+0000) Subject: Partly address bug 47124: Name unit tests consistently. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=36a9cc5aeb8aefe003fa7cd38987cc5ca7c44dd0;p=tomcat7.0 Partly address bug 47124: Name unit tests consistently. All JUnit test cases are named Test*. There was at least one class named Test* that was not a JUnit test case. These were renamed *Test. Helper / stub implementations are named Tester* git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@784843 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/startup/Tomcat.java b/java/org/apache/catalina/startup/Tomcat.java index e3b067243..e86678dda 100644 --- a/java/org/apache/catalina/startup/Tomcat.java +++ b/java/org/apache/catalina/startup/Tomcat.java @@ -77,7 +77,7 @@ import org.apache.catalina.session.StandardManager; * see setters for doc. It can be used for simple tests and * demo. * - * @see TomcatStartupAPITest for examples on how to use this + * @see TestTomcat for examples on how to use this * @author Costin Manolache */ public class Tomcat { diff --git a/test/org/apache/TestAll.java b/test/org/apache/TestAll.java index 0a3894801..999b332c9 100644 --- a/test/org/apache/TestAll.java +++ b/test/org/apache/TestAll.java @@ -1,7 +1,7 @@ package org.apache; -import org.apache.catalina.ha.session.TestSerializablePrincipal; -import org.apache.catalina.startup.TomcatStartupAPITest; +import org.apache.catalina.ha.session.TesterSerializablePrincipal; +import org.apache.catalina.startup.TestTomcat; import org.apache.catalina.tribes.test.TribesTestSuite; import org.apache.el.lang.TestELSupport; import org.apache.el.parser.TestELParser; @@ -16,9 +16,9 @@ public class TestAll { public static Test suite() { TestSuite suite = new TestSuite("Test for org.apache"); // o.a.catalina.ha.session - suite.addTestSuite(TestSerializablePrincipal.class); // TODO rename + suite.addTestSuite(TesterSerializablePrincipal.class); // TODO rename // o.a.catalina.startup - suite.addTestSuite(TomcatStartupAPITest.class); + suite.addTestSuite(TestTomcat.class); // o.a.catalina.tomcat.util.http - TODO fix package suite.addTestSuite(TestCookies.class); // TODO rename // Tribes diff --git a/test/org/apache/catalina/ha/session/TestPrincipal.java b/test/org/apache/catalina/ha/session/TestPrincipal.java deleted file mode 100644 index 7a7fed1c8..000000000 --- a/test/org/apache/catalina/ha/session/TestPrincipal.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE 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.catalina.ha.session; - -import java.io.Serializable; -import java.security.Principal; - -public class TestPrincipal implements Principal, Serializable { - - private String name; - - public TestPrincipal(String theName) { - name = theName; - } - - public String getName() { - return name; - } - -} \ No newline at end of file diff --git a/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java b/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java deleted file mode 100644 index 4f572d88f..000000000 --- a/test/org/apache/catalina/ha/session/TestSerializablePrincipal.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE 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.catalina.ha.session; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.ArrayList; -import java.util.List; - -import org.apache.catalina.realm.GenericPrincipal; - -import junit.framework.TestCase; - -public class TestSerializablePrincipal extends TestCase { - - /** - * Simple serialization / de-serialization test for bug 43840. - */ - public void testWriteReadPrincipal() { - // Get a temporary file to use for the serialization test - File file = null; - try { - file = File.createTempFile("ser", null); - file.deleteOnExit(); - } catch (IOException e) { - e.printStackTrace(); - fail("ioe creating temporary file"); - } - - // Create the Principal to serialize - List roles = new ArrayList(); - roles.add("RoleA"); - roles.add("RoleB"); - TestPrincipal tpOriginal = new TestPrincipal("inner"); - GenericPrincipal gpOriginal = - new GenericPrincipal(null, "usr", "pwd", roles, tpOriginal); - - // Do the serialization - try { - FileOutputStream fos = new FileOutputStream(file); - ObjectOutputStream oos = new ObjectOutputStream(fos); - SerializablePrincipal.writePrincipal(gpOriginal, oos); - oos.close(); - fos.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - fail("fnfe creating object output stream"); - } catch (IOException e) { - e.printStackTrace(); - fail("ioe serializing principal"); - } - - // De-serialize the Principal - GenericPrincipal gpNew = null; - try { - FileInputStream fis = new FileInputStream(file); - ObjectInputStream ois = new ObjectInputStream(fis); - gpNew = SerializablePrincipal.readPrincipal(ois, null); - } catch (FileNotFoundException e) { - e.printStackTrace(); - fail("fnfe reading object output stream"); - } catch (IOException e) { - e.printStackTrace(); - fail("ioe de-serializing principal"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - fail("cnfe de-serializing principal"); - } - - // Now test how similar original and de-serialized versions are - assertEquals("User names different", gpOriginal.getName(), - gpNew.getName()); - assertEquals("Passwords different", gpOriginal.getPassword(), - gpNew.getPassword()); - assertEquals("Number of roles different", gpOriginal.getRoles().length, - gpNew.getRoles().length); - for (int i = 0; i < gpOriginal.getRoles().length; i++) { - assertEquals("Role name index " + i + "different", - gpOriginal.getRoles()[i], gpNew.getRoles()[i]); - } - // These are the key tests for bug 43840 - assertNotSame("Inner principal not present", gpNew, - gpNew.getUserPrincipal()); - assertEquals("Inner user names are different", tpOriginal.getName(), - gpNew.getUserPrincipal().getName()); - } - -} diff --git a/test/org/apache/catalina/ha/session/TesterPrincipal.java b/test/org/apache/catalina/ha/session/TesterPrincipal.java new file mode 100644 index 000000000..774f31076 --- /dev/null +++ b/test/org/apache/catalina/ha/session/TesterPrincipal.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE 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.catalina.ha.session; + +import java.io.Serializable; +import java.security.Principal; + +public class TesterPrincipal implements Principal, Serializable { + + private String name; + + public TesterPrincipal(String theName) { + name = theName; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/test/org/apache/catalina/ha/session/TesterSerializablePrincipal.java b/test/org/apache/catalina/ha/session/TesterSerializablePrincipal.java new file mode 100644 index 000000000..867943f08 --- /dev/null +++ b/test/org/apache/catalina/ha/session/TesterSerializablePrincipal.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE 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.catalina.ha.session; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.catalina.realm.GenericPrincipal; + +import junit.framework.TestCase; + +public class TesterSerializablePrincipal extends TestCase { + + /** + * Simple serialization / de-serialization test for bug 43840. + */ + public void testWriteReadPrincipal() { + // Get a temporary file to use for the serialization test + File file = null; + try { + file = File.createTempFile("ser", null); + file.deleteOnExit(); + } catch (IOException e) { + e.printStackTrace(); + fail("ioe creating temporary file"); + } + + // Create the Principal to serialize + List roles = new ArrayList(); + roles.add("RoleA"); + roles.add("RoleB"); + TesterPrincipal tpOriginal = new TesterPrincipal("inner"); + GenericPrincipal gpOriginal = + new GenericPrincipal(null, "usr", "pwd", roles, tpOriginal); + + // Do the serialization + try { + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream oos = new ObjectOutputStream(fos); + SerializablePrincipal.writePrincipal(gpOriginal, oos); + oos.close(); + fos.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail("fnfe creating object output stream"); + } catch (IOException e) { + e.printStackTrace(); + fail("ioe serializing principal"); + } + + // De-serialize the Principal + GenericPrincipal gpNew = null; + try { + FileInputStream fis = new FileInputStream(file); + ObjectInputStream ois = new ObjectInputStream(fis); + gpNew = SerializablePrincipal.readPrincipal(ois, null); + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail("fnfe reading object output stream"); + } catch (IOException e) { + e.printStackTrace(); + fail("ioe de-serializing principal"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + fail("cnfe de-serializing principal"); + } + + // Now test how similar original and de-serialized versions are + assertEquals("User names different", gpOriginal.getName(), + gpNew.getName()); + assertEquals("Passwords different", gpOriginal.getPassword(), + gpNew.getPassword()); + assertEquals("Number of roles different", gpOriginal.getRoles().length, + gpNew.getRoles().length); + for (int i = 0; i < gpOriginal.getRoles().length; i++) { + assertEquals("Role name index " + i + "different", + gpOriginal.getRoles()[i], gpNew.getRoles()[i]); + } + // These are the key tests for bug 43840 + assertNotSame("Inner principal not present", gpNew, + gpNew.getUserPrincipal()); + assertEquals("Inner user names are different", tpOriginal.getName(), + gpNew.getUserPrincipal().getName()); + } + +} diff --git a/test/org/apache/catalina/startup/TestTomcat.java b/test/org/apache/catalina/startup/TestTomcat.java new file mode 100644 index 000000000..62f2c491e --- /dev/null +++ b/test/org/apache/catalina/startup/TestTomcat.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE 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.catalina.startup; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import junit.framework.TestCase; + +import org.apache.catalina.core.StandardContext; +import org.apache.tomcat.util.buf.ByteChunk; + +public class TestTomcat extends TestCase { + Tomcat tomcat; + // if you run in eclipse - or tomcat src dir + String base = "./"; + File tempDir; + static int port = 8001; + long t0; + + /** + * Simple servlet to test in-line registration + */ + public static class HelloWorld extends HttpServlet { + public void doGet(HttpServletRequest req, HttpServletResponse res) + throws IOException { + res.getWriter().write("Hello world"); + } + } + + public void setUp() throws Exception { + t0 = System.currentTimeMillis(); + tempDir = new File("output/tmp"); + tempDir.mkdir(); + + tomcat = new Tomcat(); + tomcat.setBaseDir(tempDir.getAbsolutePath()); + + // If each test is running on same port - they + // may interfere with each other (on unix at least) + port++; + tomcat.setPort(port); + } + + public void tearDown() throws Exception { + tomcat.stop(); + System.err.println("Test time: " + + (System.currentTimeMillis() - t0)); + } + + /** + * Start tomcat with a single context and one + * servlet - all programmatic, no server.xml or + * web.xml used. + * + * @throws Exception + */ + public void testProgrammatic() throws Exception { + + StandardContext ctx = + tomcat.addContext("/", + tempDir.getAbsolutePath()); + // You can customize the context by calling + // its API + + tomcat.addServlet(ctx, "myServlet", + new HelloWorld()); + ctx.addServletMapping("/", "myServlet"); + + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + port + "/"); + assertEquals(res.toString(), "Hello world"); + } + + public void testSingleWebapp() throws Exception { + // Currently in sandbox/tomcat-lite + File appDir = + new File(base + "output/build/webapps/examples"); + // app dir is relative to server home + StandardContext ctx = + tomcat.addWebapp(null, "/examples", + appDir.getAbsolutePath()); + + ctx.start(); + tomcat.start(); + + ByteChunk res = getUrl("http://localhost:" + port + "/examples/servlets/servlet/HelloWorldExample"); + assertTrue(res.toString().indexOf("

Hello World!

") > 0); + } + + public void testLaunchTime() throws Exception { + tomcat.addContext(null, "/", base); + tomcat.start(); + } + + /** + * Wrapper for getting the response. + */ + public static ByteChunk getUrl(String path) throws IOException { + ByteChunk out = new ByteChunk(); + getUrl(path, out, null); + return out; + } + + public static int getUrl(String path, + ByteChunk out, + Map> resHead) throws IOException { + URL url = new URL(path); + HttpURLConnection connection = + (HttpURLConnection) url.openConnection(); + connection.setReadTimeout(100000); + connection.connect(); + int rc = connection.getResponseCode(); + if (resHead != null) { + Map> head = connection.getHeaderFields(); + resHead.putAll(head); + } + InputStream is = connection.getInputStream(); + BufferedInputStream bis = new BufferedInputStream(is); + byte[] buf = new byte[2048]; + int rd = 0; + while((rd = bis.read(buf)) > 0) { + out.append(buf, 0, rd); + } + return rc; + } +} diff --git a/test/org/apache/catalina/startup/TomcatStartupAPITest.java b/test/org/apache/catalina/startup/TomcatStartupAPITest.java deleted file mode 100644 index 25fdf077c..000000000 --- a/test/org/apache/catalina/startup/TomcatStartupAPITest.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE 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.catalina.startup; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.List; -import java.util.Map; - -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import junit.framework.TestCase; - -import org.apache.catalina.core.StandardContext; -import org.apache.tomcat.util.buf.ByteChunk; - -public class TomcatStartupAPITest extends TestCase { - Tomcat tomcat; - // if you run in eclipse - or tomcat src dir - String base = "./"; - File tempDir; - static int port = 8001; - long t0; - - /** - * Simple servlet to test in-line registration - */ - public static class HelloWorld extends HttpServlet { - public void doGet(HttpServletRequest req, HttpServletResponse res) - throws IOException { - res.getWriter().write("Hello world"); - } - } - - public void setUp() throws Exception { - t0 = System.currentTimeMillis(); - tempDir = new File("output/tmp"); - tempDir.mkdir(); - - tomcat = new Tomcat(); - tomcat.setBaseDir(tempDir.getAbsolutePath()); - - // If each test is running on same port - they - // may interfere with each other (on unix at least) - port++; - tomcat.setPort(port); - } - - public void tearDown() throws Exception { - tomcat.stop(); - System.err.println("Test time: " + - (System.currentTimeMillis() - t0)); - } - - /** - * Start tomcat with a single context and one - * servlet - all programmatic, no server.xml or - * web.xml used. - * - * @throws Exception - */ - public void testProgrammatic() throws Exception { - - StandardContext ctx = - tomcat.addContext("/", - tempDir.getAbsolutePath()); - // You can customize the context by calling - // its API - - tomcat.addServlet(ctx, "myServlet", - new HelloWorld()); - ctx.addServletMapping("/", "myServlet"); - - tomcat.start(); - - ByteChunk res = getUrl("http://localhost:" + port + "/"); - assertEquals(res.toString(), "Hello world"); - } - - public void testSingleWebapp() throws Exception { - // Currently in sandbox/tomcat-lite - File appDir = - new File(base + "output/build/webapps/examples"); - // app dir is relative to server home - StandardContext ctx = - tomcat.addWebapp(null, "/examples", - appDir.getAbsolutePath()); - - ctx.start(); - tomcat.start(); - - ByteChunk res = getUrl("http://localhost:" + port + "/examples/servlets/servlet/HelloWorldExample"); - assertTrue(res.toString().indexOf("

Hello World!

") > 0); - } - - public void testLaunchTime() throws Exception { - tomcat.addContext(null, "/", base); - tomcat.start(); - } - - /** - * Wrapper for getting the response. - */ - public static ByteChunk getUrl(String path) throws IOException { - ByteChunk out = new ByteChunk(); - getUrl(path, out, null); - return out; - } - - public static int getUrl(String path, - ByteChunk out, - Map> resHead) throws IOException { - URL url = new URL(path); - HttpURLConnection connection = - (HttpURLConnection) url.openConnection(); - connection.setReadTimeout(100000); - connection.connect(); - int rc = connection.getResponseCode(); - if (resHead != null) { - Map> head = connection.getHeaderFields(); - resHead.putAll(head); - } - InputStream is = connection.getInputStream(); - BufferedInputStream bis = new BufferedInputStream(is); - byte[] buf = new byte[2048]; - int rd = 0; - while((rd = bis.read(buf)) > 0) { - out.append(buf, 0, rd); - } - return rc; - } -} diff --git a/test/org/apache/catalina/tribes/test/NioSenderTest.java b/test/org/apache/catalina/tribes/test/NioSenderTest.java new file mode 100644 index 000000000..2ad672456 --- /dev/null +++ b/test/org/apache/catalina/tribes/test/NioSenderTest.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE 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.catalina.tribes.test; + +import java.nio.channels.SelectionKey; +import java.util.Iterator; +import java.nio.channels.Selector; +import org.apache.catalina.tribes.transport.nio.NioSender; +import org.apache.catalina.tribes.membership.MemberImpl; +import org.apache.catalina.tribes.io.ChannelData; +import org.apache.catalina.tribes.io.XByteBuffer; +import org.apache.catalina.tribes.Member; +import org.apache.catalina.tribes.Channel; + +/** + *

Title:

+ * + *

Description:

+ * + *

Company:

+ * + * @author not attributable + * @version 1.0 + */ +public class NioSenderTest { + private Selector selector = null; + private int counter = 0; + MemberImpl mbr; + private static int testOptions = Channel.SEND_OPTIONS_DEFAULT; + public NioSenderTest() { + // Default constructor + } + + public synchronized int inc() { + return ++counter; + } + + public synchronized ChannelData getMessage(Member mbr) { + String msg = new String("Thread-"+Thread.currentThread().getName()+" Message:"+inc()); + ChannelData data = new ChannelData(true); + data.setMessage(new XByteBuffer(msg.getBytes(),false)); + data.setAddress(mbr); + + return data; + } + + public void init() throws Exception { + selector = Selector.open(); + mbr = new MemberImpl("localhost",4444,0); + NioSender sender = new NioSender(); + sender.setDestination(mbr); + sender.setDirectBuffer(true); + sender.setSelector(selector); + sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr))); + sender.connect(); + } + + public void run() { + while (true) { + + int selectedKeys = 0; + try { + selectedKeys = selector.select(100); + // if ( selectedKeys == 0 ) { + // System.out.println("No registered interests. Sleeping for a second."); + // Thread.sleep(100); + } catch (Exception e) { + e.printStackTrace(); + continue; + } + + if (selectedKeys == 0) { + continue; + } + + Iterator it = selector.selectedKeys().iterator(); + while (it.hasNext()) { + SelectionKey sk = it.next(); + it.remove(); + try { + int readyOps = sk.readyOps(); + sk.interestOps(sk.interestOps() & ~readyOps); + NioSender sender = (NioSender) sk.attachment(); + if ( sender.process(sk, (testOptions&Channel.SEND_OPTIONS_USE_ACK)==Channel.SEND_OPTIONS_USE_ACK) ) { + System.out.println("Message completed for handler:"+sender); + Thread.sleep(2000); + sender.reset(); + sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr))); + } + + + } catch (Throwable t) { + t.printStackTrace(); + return; + } + } + } + } + + public static void main(String[] args) throws Exception { + NioSenderTest sender = new NioSenderTest(); + sender.init(); + sender.run(); + } +} diff --git a/test/org/apache/catalina/tribes/test/TestNioSender.java b/test/org/apache/catalina/tribes/test/TestNioSender.java deleted file mode 100644 index 277a9f20f..000000000 --- a/test/org/apache/catalina/tribes/test/TestNioSender.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE 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.catalina.tribes.test; - -import java.nio.channels.SelectionKey; -import java.util.Iterator; -import java.nio.channels.Selector; -import org.apache.catalina.tribes.transport.nio.NioSender; -import org.apache.catalina.tribes.membership.MemberImpl; -import org.apache.catalina.tribes.io.ChannelData; -import org.apache.catalina.tribes.io.XByteBuffer; -import org.apache.catalina.tribes.Member; -import org.apache.catalina.tribes.Channel; - -/** - *

Title:

- * - *

Description:

- * - *

Company:

- * - * @author not attributable - * @version 1.0 - */ -public class TestNioSender { - private Selector selector = null; - private int counter = 0; - MemberImpl mbr; - private static int testOptions = Channel.SEND_OPTIONS_DEFAULT; - public TestNioSender() { - // Default constructor - } - - public synchronized int inc() { - return ++counter; - } - - public synchronized ChannelData getMessage(Member mbr) { - String msg = new String("Thread-"+Thread.currentThread().getName()+" Message:"+inc()); - ChannelData data = new ChannelData(true); - data.setMessage(new XByteBuffer(msg.getBytes(),false)); - data.setAddress(mbr); - - return data; - } - - public void init() throws Exception { - selector = Selector.open(); - mbr = new MemberImpl("localhost",4444,0); - NioSender sender = new NioSender(); - sender.setDestination(mbr); - sender.setDirectBuffer(true); - sender.setSelector(selector); - sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr))); - sender.connect(); - } - - public void run() { - while (true) { - - int selectedKeys = 0; - try { - selectedKeys = selector.select(100); - // if ( selectedKeys == 0 ) { - // System.out.println("No registered interests. Sleeping for a second."); - // Thread.sleep(100); - } catch (Exception e) { - e.printStackTrace(); - continue; - } - - if (selectedKeys == 0) { - continue; - } - - Iterator it = selector.selectedKeys().iterator(); - while (it.hasNext()) { - SelectionKey sk = it.next(); - it.remove(); - try { - int readyOps = sk.readyOps(); - sk.interestOps(sk.interestOps() & ~readyOps); - NioSender sender = (NioSender) sk.attachment(); - if ( sender.process(sk, (testOptions&Channel.SEND_OPTIONS_USE_ACK)==Channel.SEND_OPTIONS_USE_ACK) ) { - System.out.println("Message completed for handler:"+sender); - Thread.sleep(2000); - sender.reset(); - sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr))); - } - - - } catch (Throwable t) { - t.printStackTrace(); - return; - } - } - } - } - - public static void main(String[] args) throws Exception { - TestNioSender sender = new TestNioSender(); - sender.init(); - sender.run(); - } -} diff --git a/test/org/apache/catalina/tribes/test/TribesTestSuite.java b/test/org/apache/catalina/tribes/test/TribesTestSuite.java index 8d8beec9b..e206aa8d4 100644 --- a/test/org/apache/catalina/tribes/test/TribesTestSuite.java +++ b/test/org/apache/catalina/tribes/test/TribesTestSuite.java @@ -16,7 +16,7 @@ */ package org.apache.catalina.tribes.test; -import org.apache.catalina.tribes.test.channel.ChannelStartStop; +import org.apache.catalina.tribes.test.channel.TestChannelStartStop; import org.apache.catalina.tribes.test.channel.TestChannelOptionFlag; import org.apache.catalina.tribes.test.channel.TestDataIntegrity; import org.apache.catalina.tribes.test.channel.TestMulticastPackages; @@ -27,7 +27,7 @@ import org.apache.catalina.tribes.test.interceptors.TestOrderInterceptor; import org.apache.catalina.tribes.test.interceptors.TestTwoPhaseCommit; import org.apache.catalina.tribes.test.io.TestSenderConnections; import org.apache.catalina.tribes.test.io.TestSerialization; -import org.apache.catalina.tribes.test.membership.MemberSerialization; +import org.apache.catalina.tribes.test.membership.TestMemberSerialization; import org.apache.catalina.tribes.test.membership.TestDomainFilter; import org.apache.catalina.tribes.test.membership.TestMemberArrival; import org.apache.catalina.tribes.test.membership.TestTcpFailureDetector; @@ -46,7 +46,7 @@ public class TribesTestSuite public static Test suite() { TestSuite suite = new TestSuite(); // o.a.catalina.tribes.test.channel - suite.addTestSuite(ChannelStartStop.class); + suite.addTestSuite(TestChannelStartStop.class); suite.addTestSuite(TestChannelOptionFlag.class); suite.addTestSuite(TestDataIntegrity.class); suite.addTestSuite(TestMulticastPackages.class); @@ -60,7 +60,7 @@ public class TribesTestSuite suite.addTestSuite(TestSenderConnections.class); suite.addTestSuite(TestSerialization.class); // o.a.catalina.tribes.test.membership - suite.addTestSuite(MemberSerialization.class); + suite.addTestSuite(TestMemberSerialization.class); suite.addTestSuite(TestDomainFilter.class); suite.addTestSuite(TestMemberArrival.class); suite.addTestSuite(TestTcpFailureDetector.class); diff --git a/test/org/apache/catalina/tribes/test/channel/ChannelStartStop.java b/test/org/apache/catalina/tribes/test/channel/ChannelStartStop.java deleted file mode 100644 index 7828e70c2..000000000 --- a/test/org/apache/catalina/tribes/test/channel/ChannelStartStop.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE 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 - */ -package org.apache.catalina.tribes.test.channel; - -import org.apache.catalina.tribes.Channel; -import org.apache.catalina.tribes.group.GroupChannel; -import junit.framework.TestCase; -import org.apache.catalina.tribes.transport.ReceiverBase; - -/** - * @author Filip Hanik - * @version 1.0 - */ -public class ChannelStartStop extends TestCase { - GroupChannel channel = null; - int udpPort = 45543; - protected void setUp() throws Exception { - super.setUp(); - channel = new GroupChannel(); - } - - protected void tearDown() throws Exception { - super.tearDown(); - try {channel.stop(Channel.DEFAULT);}catch (Exception ignore){ /* Ignore */ } - } - - public void testDoubleFullStart() throws Exception { - int count = 0; - try { - channel.start(Channel.DEFAULT); - count++; - } catch ( Exception x){x.printStackTrace();} - try { - channel.start(Channel.DEFAULT); - count++; - } catch ( Exception x){x.printStackTrace();} - assertEquals(count,2); - channel.stop(Channel.DEFAULT); - } - - public void testScrap() throws Exception { - System.out.println(channel.getChannelReceiver().getClass()); - ((ReceiverBase)channel.getChannelReceiver()).setMaxThreads(1); - } - - - public void testDoublePartialStart() throws Exception { - //try to double start the RX - int count = 0; - try { - channel.start(Channel.SND_RX_SEQ); - channel.start(Channel.MBR_RX_SEQ); - count++; - } catch ( Exception x){x.printStackTrace();} - try { - channel.start(Channel.MBR_RX_SEQ); - count++; - } catch ( Exception x){/*expected*/} - assertEquals(count,1); - channel.stop(Channel.DEFAULT); - //double the membership sender - count = 0; - try { - channel.start(Channel.SND_RX_SEQ); - channel.start(Channel.MBR_TX_SEQ); - count++; - } catch ( Exception x){x.printStackTrace();} - try { - channel.start(Channel.MBR_TX_SEQ); - count++; - } catch ( Exception x){/*expected*/} - assertEquals(count,1); - channel.stop(Channel.DEFAULT); - - count = 0; - try { - channel.start(Channel.SND_RX_SEQ); - count++; - } catch ( Exception x){x.printStackTrace();} - try { - channel.start(Channel.SND_RX_SEQ); - count++; - } catch ( Exception x){/*expected*/} - assertEquals(count,1); - channel.stop(Channel.DEFAULT); - - count = 0; - try { - channel.start(Channel.SND_TX_SEQ); - count++; - } catch ( Exception x){x.printStackTrace();} - try { - channel.start(Channel.SND_TX_SEQ); - count++; - } catch ( Exception x){/*expected*/} - assertEquals(count,1); - channel.stop(Channel.DEFAULT); - } - - public void testFalseOption() throws Exception { - int flag = 0xFFF0;//should get ignored by the underlying components - int count = 0; - try { - channel.start(flag); - count++; - } catch ( Exception x){x.printStackTrace();} - try { - channel.start(flag); - count++; - } catch ( Exception x){/*expected*/} - assertEquals(count,2); - channel.stop(Channel.DEFAULT); - } - - public void testUdpReceiverStart() throws Exception { - ReceiverBase rb = (ReceiverBase)channel.getChannelReceiver(); - rb.setUdpPort(udpPort); - channel.start(Channel.DEFAULT); - Thread.sleep(1000); - channel.stop(Channel.DEFAULT); - } - -} diff --git a/test/org/apache/catalina/tribes/test/channel/TestChannelStartStop.java b/test/org/apache/catalina/tribes/test/channel/TestChannelStartStop.java new file mode 100644 index 000000000..517c51685 --- /dev/null +++ b/test/org/apache/catalina/tribes/test/channel/TestChannelStartStop.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE 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 + */ +package org.apache.catalina.tribes.test.channel; + +import org.apache.catalina.tribes.Channel; +import org.apache.catalina.tribes.group.GroupChannel; +import junit.framework.TestCase; +import org.apache.catalina.tribes.transport.ReceiverBase; + +/** + * @author Filip Hanik + * @version 1.0 + */ +public class TestChannelStartStop extends TestCase { + GroupChannel channel = null; + int udpPort = 45543; + protected void setUp() throws Exception { + super.setUp(); + channel = new GroupChannel(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + try {channel.stop(Channel.DEFAULT);}catch (Exception ignore){ /* Ignore */ } + } + + public void testDoubleFullStart() throws Exception { + int count = 0; + try { + channel.start(Channel.DEFAULT); + count++; + } catch ( Exception x){x.printStackTrace();} + try { + channel.start(Channel.DEFAULT); + count++; + } catch ( Exception x){x.printStackTrace();} + assertEquals(count,2); + channel.stop(Channel.DEFAULT); + } + + public void testScrap() throws Exception { + System.out.println(channel.getChannelReceiver().getClass()); + ((ReceiverBase)channel.getChannelReceiver()).setMaxThreads(1); + } + + + public void testDoublePartialStart() throws Exception { + //try to double start the RX + int count = 0; + try { + channel.start(Channel.SND_RX_SEQ); + channel.start(Channel.MBR_RX_SEQ); + count++; + } catch ( Exception x){x.printStackTrace();} + try { + channel.start(Channel.MBR_RX_SEQ); + count++; + } catch ( Exception x){/*expected*/} + assertEquals(count,1); + channel.stop(Channel.DEFAULT); + //double the membership sender + count = 0; + try { + channel.start(Channel.SND_RX_SEQ); + channel.start(Channel.MBR_TX_SEQ); + count++; + } catch ( Exception x){x.printStackTrace();} + try { + channel.start(Channel.MBR_TX_SEQ); + count++; + } catch ( Exception x){/*expected*/} + assertEquals(count,1); + channel.stop(Channel.DEFAULT); + + count = 0; + try { + channel.start(Channel.SND_RX_SEQ); + count++; + } catch ( Exception x){x.printStackTrace();} + try { + channel.start(Channel.SND_RX_SEQ); + count++; + } catch ( Exception x){/*expected*/} + assertEquals(count,1); + channel.stop(Channel.DEFAULT); + + count = 0; + try { + channel.start(Channel.SND_TX_SEQ); + count++; + } catch ( Exception x){x.printStackTrace();} + try { + channel.start(Channel.SND_TX_SEQ); + count++; + } catch ( Exception x){/*expected*/} + assertEquals(count,1); + channel.stop(Channel.DEFAULT); + } + + public void testFalseOption() throws Exception { + int flag = 0xFFF0;//should get ignored by the underlying components + int count = 0; + try { + channel.start(flag); + count++; + } catch ( Exception x){x.printStackTrace();} + try { + channel.start(flag); + count++; + } catch ( Exception x){/*expected*/} + assertEquals(count,2); + channel.stop(Channel.DEFAULT); + } + + public void testUdpReceiverStart() throws Exception { + ReceiverBase rb = (ReceiverBase)channel.getChannelReceiver(); + rb.setUdpPort(udpPort); + channel.start(Channel.DEFAULT); + Thread.sleep(1000); + channel.stop(Channel.DEFAULT); + } + +} diff --git a/test/org/apache/catalina/tribes/test/membership/MemberSerialization.java b/test/org/apache/catalina/tribes/test/membership/MemberSerialization.java deleted file mode 100644 index 9fc7106c2..000000000 --- a/test/org/apache/catalina/tribes/test/membership/MemberSerialization.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE 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 - */ -package org.apache.catalina.tribes.test.membership; - -import junit.framework.TestCase; -import org.apache.catalina.tribes.membership.MemberImpl; -import java.util.Arrays; - -/** - *

Title:

- * - *

Description:

- * - *

Company:

- * - * @author not attributable - * @version 1.0 - */ -public class MemberSerialization extends TestCase { - MemberImpl m1, m2, p1,p2; - byte[] payload = null; - int udpPort = 3445; - protected void setUp() throws Exception { - super.setUp(); - payload = new byte[333]; - Arrays.fill(payload,(byte)1); - m1 = new MemberImpl("localhost",3333,1,payload); - m2 = new MemberImpl("localhost",3333,1); - payload = new byte[333]; - Arrays.fill(payload,(byte)2); - p1 = new MemberImpl("127.0.0.1",3333,1,payload); - p2 = new MemberImpl("localhost",3331,1,payload); - m1.setDomain(new byte[] {1,2,3,4,5,6,7,8,9}); - m2.setDomain(new byte[] {1,2,3,4,5,6,7,8,9}); - m1.setCommand(new byte[] {1,2,4,5,6,7,8,9}); - m2.setCommand(new byte[] {1,2,4,5,6,7,8,9}); - m1.setUdpPort(udpPort); - m2.setUdpPort(m1.getUdpPort()); - } - - public void testCompare() throws Exception { - assertTrue(m1.equals(m2)); - assertTrue(m2.equals(m1)); - assertTrue(p1.equals(m2)); - assertFalse(m1.equals(p2)); - assertFalse(m1.equals(p2)); - assertFalse(m2.equals(p2)); - assertFalse(p1.equals(p2)); - } - - public void testUdpPort() throws Exception { - byte[] md1 = m1.getData(); - byte[] md2 = m2.getData(); - - MemberImpl a1 = MemberImpl.getMember(md1); - MemberImpl a2 = MemberImpl.getMember(md2); - - assertEquals(true, a1.getUdpPort()==a2.getUdpPort()); - assertEquals(true,a1.getUdpPort()==udpPort); - - - } - - public void testSerializationOne() throws Exception { - MemberImpl m = m1; - byte[] md1 = m.getData(false,true); - byte[] mda1 = m.getData(false,false); - assertTrue(Arrays.equals(md1,mda1)); - assertTrue(md1==mda1); - mda1 = m.getData(true,true); - MemberImpl ma1 = MemberImpl.getMember(mda1); - assertTrue(compareMembers(m,ma1)); - mda1 = p1.getData(false); - assertFalse(Arrays.equals(md1,mda1)); - ma1 = MemberImpl.getMember(mda1); - assertTrue(compareMembers(p1,ma1)); - - md1 = m.getData(true,true); - Thread.sleep(50); - mda1 = m.getData(true,true); - MemberImpl a1 = MemberImpl.getMember(md1); - MemberImpl a2 = MemberImpl.getMember(mda1); - assertTrue(a1.equals(a2)); - assertFalse(Arrays.equals(md1,mda1)); - - - } - - public boolean compareMembers(MemberImpl impl1, MemberImpl impl2) { - boolean result = true; - result = result && Arrays.equals(impl1.getHost(),impl2.getHost()); - result = result && Arrays.equals(impl1.getPayload(),impl2.getPayload()); - result = result && Arrays.equals(impl1.getUniqueId(),impl2.getUniqueId()); - result = result && impl1.getPort() == impl2.getPort(); - return result; - } - - protected void tearDown() throws Exception { - super.tearDown(); - } - -} diff --git a/test/org/apache/catalina/tribes/test/membership/TestMemberSerialization.java b/test/org/apache/catalina/tribes/test/membership/TestMemberSerialization.java new file mode 100644 index 000000000..623f67446 --- /dev/null +++ b/test/org/apache/catalina/tribes/test/membership/TestMemberSerialization.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE 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 + */ +package org.apache.catalina.tribes.test.membership; + +import junit.framework.TestCase; +import org.apache.catalina.tribes.membership.MemberImpl; +import java.util.Arrays; + +/** + *

Title:

+ * + *

Description:

+ * + *

Company:

+ * + * @author not attributable + * @version 1.0 + */ +public class TestMemberSerialization extends TestCase { + MemberImpl m1, m2, p1,p2; + byte[] payload = null; + int udpPort = 3445; + protected void setUp() throws Exception { + super.setUp(); + payload = new byte[333]; + Arrays.fill(payload,(byte)1); + m1 = new MemberImpl("localhost",3333,1,payload); + m2 = new MemberImpl("localhost",3333,1); + payload = new byte[333]; + Arrays.fill(payload,(byte)2); + p1 = new MemberImpl("127.0.0.1",3333,1,payload); + p2 = new MemberImpl("localhost",3331,1,payload); + m1.setDomain(new byte[] {1,2,3,4,5,6,7,8,9}); + m2.setDomain(new byte[] {1,2,3,4,5,6,7,8,9}); + m1.setCommand(new byte[] {1,2,4,5,6,7,8,9}); + m2.setCommand(new byte[] {1,2,4,5,6,7,8,9}); + m1.setUdpPort(udpPort); + m2.setUdpPort(m1.getUdpPort()); + } + + public void testCompare() throws Exception { + assertTrue(m1.equals(m2)); + assertTrue(m2.equals(m1)); + assertTrue(p1.equals(m2)); + assertFalse(m1.equals(p2)); + assertFalse(m1.equals(p2)); + assertFalse(m2.equals(p2)); + assertFalse(p1.equals(p2)); + } + + public void testUdpPort() throws Exception { + byte[] md1 = m1.getData(); + byte[] md2 = m2.getData(); + + MemberImpl a1 = MemberImpl.getMember(md1); + MemberImpl a2 = MemberImpl.getMember(md2); + + assertEquals(true, a1.getUdpPort()==a2.getUdpPort()); + assertEquals(true,a1.getUdpPort()==udpPort); + + + } + + public void testSerializationOne() throws Exception { + MemberImpl m = m1; + byte[] md1 = m.getData(false,true); + byte[] mda1 = m.getData(false,false); + assertTrue(Arrays.equals(md1,mda1)); + assertTrue(md1==mda1); + mda1 = m.getData(true,true); + MemberImpl ma1 = MemberImpl.getMember(mda1); + assertTrue(compareMembers(m,ma1)); + mda1 = p1.getData(false); + assertFalse(Arrays.equals(md1,mda1)); + ma1 = MemberImpl.getMember(mda1); + assertTrue(compareMembers(p1,ma1)); + + md1 = m.getData(true,true); + Thread.sleep(50); + mda1 = m.getData(true,true); + MemberImpl a1 = MemberImpl.getMember(md1); + MemberImpl a2 = MemberImpl.getMember(mda1); + assertTrue(a1.equals(a2)); + assertFalse(Arrays.equals(md1,mda1)); + + + } + + public boolean compareMembers(MemberImpl impl1, MemberImpl impl2) { + boolean result = true; + result = result && Arrays.equals(impl1.getHost(),impl2.getHost()); + result = result && Arrays.equals(impl1.getPayload(),impl2.getPayload()); + result = result && Arrays.equals(impl1.getUniqueId(),impl2.getUniqueId()); + result = result && impl1.getPort() == impl2.getPort(); + return result; + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + +}