You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ed...@apache.org on 2008/07/23 22:19:01 UTC

svn commit: r679186 - in /mina/trunk/example/src: main/java/org/apache/mina/example/haiku/ test/java/org/apache/mina/example/haiku/

Author: edeoliveira
Date: Wed Jul 23 13:19:01 2008
New Revision: 679186

URL: http://svn.apache.org/viewvc?rev=679186&view=rev
Log:
DIRMINA-394 Port of haiku example to trunk

Added:
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java   (with props)
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java   (with props)
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java   (with props)
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java   (with props)
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java   (with props)
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java   (with props)
    mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java   (with props)
    mina/trunk/example/src/test/java/org/apache/mina/example/haiku/
    mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java   (with props)
    mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java   (with props)
    mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java   (with props)
    mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java   (with props)

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,62 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.util.Arrays;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class Haiku {
+    private final String[] phrases;
+
+    public Haiku(String... lines) {
+        this.phrases = lines;
+        if (null == lines || lines.length != 3) {
+            throw new IllegalArgumentException("Must pass in 3 phrases of text");
+        }
+    }
+
+    public String[] getPhrases() {
+        return phrases;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || getClass() != o.getClass())
+            return false;
+
+        Haiku haiku = (Haiku) o;
+
+        return Arrays.equals(phrases, haiku.phrases);
+    }
+
+    @Override
+    public int hashCode() {
+        return Arrays.hashCode(phrases);
+    }
+
+    @Override
+    public String toString() {
+        return Arrays.toString(phrases);
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.net.InetSocketAddress;
+import java.nio.charset.Charset;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
+import org.apache.mina.filter.executor.ExecutorFilter;
+import org.apache.mina.transport.socket.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+
+public class HaikuValidationServer {
+    public static void main(String... args) throws Exception {
+        ExecutorService executor = Executors.newCachedThreadPool();
+        SocketAcceptor acceptor = new NioSocketAcceptor(Runtime.getRuntime()
+                .availableProcessors());
+
+        acceptor.getFilterChain().addLast("executor",
+                new ExecutorFilter(executor));
+        acceptor.getFilterChain().addLast(
+                "to-string",
+                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
+                        .forName("US-ASCII"))));
+        acceptor.getFilterChain().addLast("to-haiki", new ToHaikuIoFilter());
+
+        acceptor.setHandler(new HaikuValidatorIoHandler());
+        acceptor.bind(new InetSocketAddress(42458));
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,41 @@
+/*
+ * 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.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class HaikuValidator {
+    private static final int[] SYLLABLE_COUNTS = { 5, 7, 5 };
+
+    public void validate(Haiku haiku) throws InvalidHaikuException {
+        String[] phrases = haiku.getPhrases();
+
+        for (int i = 0; i < phrases.length; i++) {
+            String phrase = phrases[i];
+            int count = PhraseUtilities.countSyllablesInPhrase(phrase);
+
+            if (count != SYLLABLE_COUNTS[i]) {
+                throw new InvalidHaikuException(i + 1, phrase, count,
+                        SYLLABLE_COUNTS[i]);
+            }
+        }
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,45 @@
+/*
+ * 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.mina.example.haiku;
+
+import org.apache.mina.core.service.IoHandlerAdapter;
+import org.apache.mina.core.session.IoSession;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+
+public class HaikuValidatorIoHandler extends IoHandlerAdapter {
+
+    private final HaikuValidator validator = new HaikuValidator();
+
+    @Override
+    public void messageReceived(IoSession session, Object message)
+            throws Exception {
+        Haiku haiku = (Haiku) message;
+
+        try {
+            validator.validate(haiku);
+            session.write("HAIKU!");
+        } catch (InvalidHaikuException e) {
+            session.write("NOT A HAIKU: " + e.getMessage());
+        }
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,62 @@
+/*
+ * 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.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class InvalidHaikuException extends Exception {
+    private static final long serialVersionUID = 34877739006797894L;
+
+    private final int position;
+
+    private final String phrase;
+
+    private final int syllableCount;
+
+    private final int expectedSyllableCount;
+
+    public InvalidHaikuException(int position, String phrase,
+            int syllableCount, int expectedSyllableCount) {
+        super("phrase " + position + ", '" + phrase + "' had " + syllableCount
+                + " syllables, not " + expectedSyllableCount);
+
+        this.position = position;
+        this.phrase = phrase;
+        this.syllableCount = syllableCount;
+        this.expectedSyllableCount = expectedSyllableCount;
+    }
+
+    public int getExpectedSyllableCount() {
+        return expectedSyllableCount;
+    }
+
+    public String getPhrase() {
+        return phrase;
+    }
+
+    public int getSyllableCount() {
+        return syllableCount;
+    }
+
+    public int getPhrasePosition() {
+        return position;
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,64 @@
+package org.apache.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class PhraseUtilities {
+    static int countSyllablesInPhrase(String phrase) {
+        int syllables = 0;
+
+        for (String word : phrase.split("[^\\w-]+")) {
+            if (word.length() > 0) {
+                syllables += countSyllablesInWord(word.toLowerCase());
+            }
+        }
+
+        return syllables;
+    }
+
+    static int countSyllablesInWord(String word) {
+        char[] chars = word.toCharArray();
+        int syllables = 0;
+        boolean lastWasVowel = false;
+
+        for (int i = 0; i < chars.length; i++) {
+            char c = chars[i];
+            if (isVowel(c)) {
+                if (!lastWasVowel
+                        || (i > 0 && isE(chars, i - 1) && isO(chars, i))) {
+                    ++syllables;
+                    lastWasVowel = true;
+                }
+            } else {
+                lastWasVowel = false;
+            }
+        }
+
+        if (word.endsWith("oned") || word.endsWith("ne")
+                || word.endsWith("ide") || word.endsWith("ve")
+                || word.endsWith("fe") || word.endsWith("nes")
+                || word.endsWith("mes")) {
+            --syllables;
+        }
+
+        return syllables;
+    }
+
+    static boolean isE(char[] chars, int position) {
+        return isCharacter(chars, position, 'e');
+    }
+
+    static boolean isCharacter(char[] chars, int position, char c) {
+        return chars[position] == c;
+    }
+
+    static boolean isO(char[] chars, int position) {
+        return isCharacter(chars, position, 'o');
+    }
+
+    static boolean isVowel(char c) {
+        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
+                || c == 'y';
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java (added)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,53 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.mina.core.filterchain.IoFilterAdapter;
+import org.apache.mina.core.session.IoSession;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class ToHaikuIoFilter extends IoFilterAdapter {
+
+    @SuppressWarnings( { "unchecked" })
+    @Override
+    public void messageReceived(NextFilter nextFilter, IoSession session,
+            Object message) throws Exception {
+        List<String> phrases = (List<String>) session.getAttribute("phrases");
+
+        if (null == phrases) {
+            phrases = new ArrayList<String>();
+            session.setAttribute("phrases", phrases);
+        }
+
+        phrases.add((String) message);
+
+        if (phrases.size() == 3) {
+            session.removeAttribute("phrases");
+
+            super.messageReceived(nextFilter, session, new Haiku(phrases
+                    .toArray(new String[3])));
+        }
+    }
+}

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java (added)
+++ mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,59 @@
+/*
+ * 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.mina.example.haiku;
+
+import org.apache.mina.core.service.IoHandler;
+import org.apache.mina.core.session.IoSession;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class HaikuValidatorIoHandlerTest extends MockObjectTestCase {
+    private IoHandler handler;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        handler = new HaikuValidatorIoHandler();
+    }
+
+    public void testValidHaiku() throws Exception {
+        Mock session = mock(IoSession.class);
+        session.expects(once()).method("write").with(eq("HAIKU!"));
+        IoSession sessionProxy = (IoSession) session.proxy();
+
+        handler.messageReceived(sessionProxy, new Haiku(
+                "Oh, I drank too much.", "Why, oh why did I sign up",
+                "For an eight thirty?"));
+    }
+
+    public void testInvalidHaiku() throws Exception {
+        Mock session = mock(IoSession.class);
+        session.expects(once()).method("write").with(
+                eq("NOT A HAIKU: phrase 1, 'foo' had 1 syllables, not 5"));
+        IoSession sessionProxy = (IoSession) session.proxy();
+
+        handler.messageReceived(sessionProxy,
+                new Haiku("foo", "a haiku", "poo"));
+    }
+}

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java (added)
+++ mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,81 @@
+/*
+ * 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.mina.example.haiku;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class HaikuValidatorTest extends TestCase {
+    // from http://allrileyedup.blogspot.com/2006/10/dont-hassle-haiku.html -- good friend of proyal@apache.org
+    private static final String[] HAIKUS = {
+            "This class is boring.\n" + "Will David ever shut up?\n"
+                    + "What is Steph wearing?",
+
+            "Oh, I drank too much.\n" + "Why, oh why did I sign up\n"
+                    + "For an eight thirty?",
+
+            "Which one should I do?\n" + "Wax my chest or perm my hair?\n"
+                    + "Can’t wait to decide.",
+
+            "Watch my video.\n" + "I can't stop this fee-ee-ling!\n"
+                    + "What is wrong with me?",
+
+            "The car chases me.\n" + "I must get away from it.\n"
+                    + "Turbo Boost! Oh, yeah.",
+
+            "My new slogan is\n" + "Don't hassle me... I'm oiling.\n"
+                    + "You know it’s so true.",
+
+            "Michael, I love you.\n" + "I long for you to tell me\n"
+                    + "\"KITT, need you buddy.\"",
+
+            "In Knight Rider, I’m\n" + "A Man Who Does Not Exist.\n"
+                    + "(Except in your dreams).",
+
+            "Yes, I’m Michael Knight\n" + "Check out my unbuttoned shirt.\n"
+                    + "And sexy tight pants.",
+
+            "My bitch ex-wife sucks.\n" + "And so do all the airlines.\n"
+                    + "I miss Knight Rider.",
+
+            "I am Michael Knight.\n" + "I am David Hasselhoff.\n"
+                    + "I’m not Rick James, bitch." };
+
+    private HaikuValidator validator;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        validator = new HaikuValidator();
+    }
+
+    public void testValidateHaikus() throws Exception {
+        for (String s : HAIKUS) {
+            String[] lines = s.split("\n");
+
+            Haiku haiku = new Haiku(lines);
+
+            validator.validate(haiku);
+        }
+    }
+}

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java (added)
+++ mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,69 @@
+/*
+ * 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.mina.example.haiku;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class PhraseUtilitiesTest extends TestCase {
+    public void testCountSyllablesInWord() throws Exception {
+        assertSyllableCount(1, "one");
+        assertSyllableCount(1, "I");
+        assertSyllableCount(1, "too");
+        assertSyllableCount(1, "why");
+        assertSyllableCount(1, "oh");
+        assertSyllableCount(1, "did");
+        assertSyllableCount(1, "sign");
+        assertSyllableCount(1, "up");
+        assertSyllableCount(1, "watch");
+        assertSyllableCount(1, "my");
+        assertSyllableCount(1, "what");
+        assertSyllableCount(1, "is");
+        assertSyllableCount(1, "wrong");
+        assertSyllableCount(1, "with");
+        assertSyllableCount(1, "me");
+        assertSyllableCount(1, "don't");
+        assertSyllableCount(1, "you");
+        assertSyllableCount(1, "love");
+        assertSyllableCount(2, "hassle");
+        assertSyllableCount(2, "oiling");
+        assertSyllableCount(2, "decide");
+        assertSyllableCount(2, "Michael");
+        assertSyllableCount(1, "I'm");
+        assertSyllableCount(1, "check");
+        assertSyllableCount(1, "out");
+        assertSyllableCount(1, "shirt");
+        assertSyllableCount(1, "bitch");
+        assertSyllableCount(1, "sucks");
+        assertSyllableCount(1, "James");
+        assertSyllableCount(2, "ex-wife");
+        assertSyllableCount(2, "airlines");
+        assertSyllableCount(3, "video");
+        assertSyllableCount(3, "fee-ee-ling");
+        assertSyllableCount(3, "unbuttoned");
+    }
+
+    private static void assertSyllableCount(int count, String word) {
+        assertEquals("syllables in " + word, count, PhraseUtilities
+                .countSyllablesInWord(word.toLowerCase()));
+    }
+}

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java?rev=679186&view=auto
==============================================================================
--- mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java (added)
+++ mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java Wed Jul 23 13:19:01 2008
@@ -0,0 +1,79 @@
+/*
+ * 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.mina.example.haiku;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.mina.core.filterchain.IoFilter;
+import org.apache.mina.core.session.IoSession;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+/**
+ * @author Apache Mina Project (dev@mina.apache.org)
+ * @version $Rev: $, $Date:  $
+ */
+public class ToHaikuIoFilterTest extends MockObjectTestCase {
+    private IoFilter filter;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        filter = new ToHaikuIoFilter();
+    }
+
+    public void testThreeStringsMakesAHaiku() throws Exception {
+        Mock list = mock(List.class);
+        list.expects(once()).method("add").with(eq("two")).will(
+                returnValue(true));
+        list.expects(once()).method("add").with(eq("three")).will(
+                returnValue(true));
+        list.expects(once()).method("toArray").with(isA(String[].class)).will(
+                returnValue(new String[] { "one", "two", "three" }));
+        list.expects(exactly(2)).method("size").will(
+                onConsecutiveCalls(returnValue(2), returnValue(3)));
+
+        Mock session = mock(IoSession.class);
+        session.expects(exactly(3)).method("getAttribute").with(eq("phrases"))
+                .will(
+                        onConsecutiveCalls(returnValue(null), returnValue(list
+                                .proxy()), returnValue(list.proxy()),
+                                returnValue(list.proxy())));
+        session.expects(exactly(1)).method("setAttribute").with(eq("phrases"),
+                eq(Collections.emptyList()));
+        session.expects(exactly(1)).method("removeAttribute").with(
+                eq("phrases"));
+
+        IoSession sessionProxy = (IoSession) session.proxy();
+
+        Mock nextFilter = mock(IoFilter.NextFilter.class);
+        nextFilter.expects(once()).method("messageReceived").with(
+                eq(sessionProxy), eq(new Haiku("one", "two", "three")));
+
+        IoFilter.NextFilter nextFilterProxy = (IoFilter.NextFilter) nextFilter
+                .proxy();
+
+        filter.messageReceived(nextFilterProxy, sessionProxy, "one");
+        filter.messageReceived(nextFilterProxy, sessionProxy, "two");
+        filter.messageReceived(nextFilterProxy, sessionProxy, "three");
+    }
+
+}

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain