Author: proyal
Date: Fri Apr 27 11:33:48 2007
New Revision: 533187

URL: http://svn.apache.org/viewvc?view=rev&rev=533187
Log:
new example to be shown at apachecon eu 2007. not yet complete, but should be 
in SVN prior to flying :)

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

Modified: mina/branches/1.1/example/pom.xml
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/pom.xml?view=diff&rev=533187&r1=533186&r2=533187
==============================================================================
--- mina/branches/1.1/example/pom.xml (original)
+++ mina/branches/1.1/example/pom.xml Fri Apr 27 11:33:48 2007
@@ -8,7 +8,7 @@
   </parent>
   <artifactId>mina-example</artifactId>
   <name>Apache MINA Examples</name>
-  <packaging>jar</packaging>  
+  <packaging>jar</packaging>
 
   <dependencies>
     <dependency>
@@ -24,7 +24,7 @@
       <version>${pom.version}</version>
       <scope>compile</scope>
     </dependency>
-    
+
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
@@ -45,21 +45,28 @@
       <version>2.0.4</version>
       <scope>compile</scope>
     </dependency>
-    
+
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-support</artifactId>
       <version>2.0.4</version>
       <scope>compile</scope>
     </dependency>
-    
+
     <dependency>
       <groupId>commons-net</groupId>
       <artifactId>commons-net</artifactId>
       <version>1.4.1</version>
       <scope>test</scope>
     </dependency>
-    
+
+    <dependency>
+      <groupId>jmock</groupId>
+      <artifactId>jmock</artifactId>
+      <version>1.2.0</version>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
 </project>

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/Haiku.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,60 @@
+/*
+ * 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 ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/Haiku.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidationServer.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,52 @@
+/*
+ * 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.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @version $Rev: $, $Date:  $
+ */
+ 
+  public class HaikuValidationServer {
+      public static void main( String... args ) throws Exception {
+          ExecutorService executor = Executors.newCachedThreadPool();
+          SocketAcceptor acceptor = 
+            new SocketAcceptor( Runtime.getRuntime().availableProcessors(), 
executor );
+
+          SocketAcceptorConfig config = new SocketAcceptorConfig();
+
+          config.getFilterChain().addLast( "executor", new ExecutorFilter( 
executor ) );
+          config.getFilterChain().addLast( "to-string", 
+            new ProtocolCodecFilter( new TextLineCodecFactory( 
Charset.forName( "US-ASCII" ) ) ) );
+          config.getFilterChain().addLast( "to-haiki", new ToHaikuIoFilter() );
+
+          acceptor.bind( new InetSocketAddress( 42458 ), new 
HaikuValidatorIoHandler(), config );
+      }
+  }

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

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
 Fri Apr 27 11:33:48 2007
@@ -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 ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,44 @@
+/*
+ * 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.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/HaikuValidatorIoHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,56 @@
+/*
+ * 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 ([EMAIL PROTECTED])
+ * @version $Rev: $, $Date:  $
+ */
+public class InvalidHaikuException extends Exception {
+    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/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/InvalidHaikuException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,60 @@
+package org.apache.mina.example.haiku;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/PhraseUtilities.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
 (added)
+++ 
mina/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,55 @@
+/*
+ * 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.common.IoFilterAdapter;
+import org.apache.mina.common.IoSession;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/main/java/org/apache/mina/example/haiku/ToHaikuIoFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
 (added)
+++ 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,58 @@
+/*
+ * 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.common.IoHandler;
+import org.apache.mina.common.IoSession;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorIoHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
 (added)
+++ 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,94 @@
+/*
+ * 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 ([EMAIL PROTECTED])
+ * @version $Rev: $, $Date:  $
+ */
+public class HaikuValidatorTest extends TestCase {
+    // from http://allrileyedup.blogspot.com/2006/10/dont-hassle-haiku.html -- 
good friend of [EMAIL PROTECTED]
+    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/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/HaikuValidatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
 (added)
+++ 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,68 @@
+/*
+ * 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 ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/PhraseUtilitiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java?view=auto&rev=533187
==============================================================================
--- 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
 (added)
+++ 
mina/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
 Fri Apr 27 11:33:48 2007
@@ -0,0 +1,73 @@
+/*
+ * 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.common.IoFilter;
+import org.apache.mina.common.IoSession;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+/**
+ * @author Apache Mina Project ([EMAIL PROTECTED])
+ * @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/branches/1.1/example/src/test/java/org/apache/mina/example/haiku/ToHaikuIoFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to