--- /dev/null
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.apache.catalina.tribes.test.interceptors;\r
+\r
+import java.io.Serializable;\r
+\r
+import org.apache.catalina.tribes.Channel;\r
+import org.apache.catalina.tribes.ChannelListener;\r
+import org.apache.catalina.tribes.Member;\r
+import org.apache.catalina.tribes.group.GroupChannel;\r
+import org.apache.catalina.tribes.group.interceptors.DomainFilterInterceptor;\r
+import junit.framework.TestCase;\r
+import junit.framework.TestResult;\r
+import junit.framework.TestSuite;\r
+\r
+public class TestDomainInterceptor extends TestCase {\r
+\r
+ GroupChannel[] channels = null;\r
+ DomainFilterInterceptor[] domainitcs = null;\r
+ TestListener[] test = null;\r
+ int channelCount = 4;\r
+ Thread[] threads = null;\r
+ byte[] commonDomain = new byte[] {1,1,1,1};\r
+ byte[] oddDomain = new byte[] {2,1,1,1};\r
+ protected void setUp() throws Exception {\r
+ System.out.println("Setup");\r
+ super.setUp();\r
+ channels = new GroupChannel[channelCount];\r
+ domainitcs = new DomainFilterInterceptor[channelCount];\r
+ test = new TestListener[channelCount];\r
+ threads = new Thread[channelCount];\r
+ for ( int i=0; i<channelCount; i++ ) {\r
+ channels[i] = new GroupChannel();\r
+ channels[i].getMembershipService().setDomain(i>0?commonDomain:oddDomain);\r
+ domainitcs[i] = new DomainFilterInterceptor();\r
+ domainitcs[i].setDomain(i>0?commonDomain:oddDomain);\r
+ channels[i].addInterceptor(domainitcs[i]);\r
+ test[i] = new TestListener(i);\r
+ channels[i].addChannelListener(test[i]);\r
+ final int j = i;\r
+ threads[i] = new Thread() {\r
+ public void run() {\r
+ try {\r
+ channels[j].start(Channel.DEFAULT);\r
+ Thread.sleep(50);\r
+ } catch (Exception x) {\r
+ x.printStackTrace();\r
+ }\r
+ }\r
+ };\r
+ }\r
+ for ( int i=0; i<channelCount; i++ ) threads[i].start();\r
+ for ( int i=0; i<channelCount; i++ ) threads[i].join();\r
+ Thread.sleep(1000);\r
+ }\r
+\r
+ public void testDomainMembership() throws Exception {\r
+ assertEquals("Testing odd channel - no members.",0,channels[0].getMembers().length);\r
+ for (int i=1; i<channelCount; i++ ) {\r
+ assertEquals("["+i+"] Testing common channels - should have " + \r
+ (channelCount - 1) + " members.", (channelCount - 2),\r
+ channels[i].getMembers().length);\r
+ }\r
+ }\r
+\r
+ public void testDomainMessages() throws Exception {\r
+ }\r
+\r
+\r
+ protected void tearDown() throws Exception {\r
+ System.out.println("tearDown");\r
+ super.tearDown();\r
+ for ( int i=0; i<channelCount; i++ ) {\r
+ channels[i].stop(Channel.DEFAULT);\r
+ }\r
+ }\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ TestSuite suite = new TestSuite();\r
+ suite.addTestSuite(TestDomainInterceptor.class);\r
+ suite.run(new TestResult());\r
+ }\r
+\r
+ public static class TestListener implements ChannelListener {\r
+ int id = -1;\r
+ public TestListener(int id) {\r
+ this.id = id;\r
+ }\r
+ int cnt = 0;\r
+ int total = 0;\r
+ boolean fail = false;\r
+ public synchronized void messageReceived(Serializable msg, Member sender) {\r
+ total++;\r
+ Integer i = (Integer)msg;\r
+ if ( i.intValue() != cnt ) fail = true;\r
+ else cnt++;\r
+ System.out.println("Listener["+id+"] Message received:"+i+" Count:"+total+" Fail:"+fail);\r
+\r
+ }\r
+\r
+ public boolean accept(Serializable msg, Member sender) {\r
+ return (msg instanceof Integer);\r
+ }\r
+ }\r
+\r
+\r
+\r
+\r
+\r
+\r
+}\r