001/*
002 *  jDTAUS Core Client Container
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.container.ri.client.versioning.test;
022
023import java.io.StringReader;
024import junit.framework.Assert;
025import junit.framework.TestCase;
026import org.jdtaus.core.container.ri.client.versioning.ParseException;
027import org.jdtaus.core.container.ri.client.versioning.Token;
028import org.jdtaus.core.container.ri.client.versioning.VersionParser;
029
030/**
031 * Tests the {@code VersionParser} implementation.
032 *
033 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
034 * @version $JDTAUS: VersionParserTest.java 8641 2012-09-27 06:45:17Z schulte $
035 */
036public class VersionParserTest extends TestCase
037{
038
039    public void testParseable() throws Exception
040    {
041        this.assertValidVersion( "1" );
042        this.assertValidVersion( "1.1" );
043        this.assertValidVersion( "SNAPSHOT" );
044        this.assertValidVersion( "RELEASE" );
045        this.assertValidVersion( "LATEST" );
046        this.assertValidVersion( "4aug2007r7-dev" );
047        this.assertValidVersion( "1.0-alpha-10-stable-1" );
048    }
049
050    public void testUnparseable() throws Exception
051    {
052        this.assertInvalidVersion( "." );
053    }
054
055    protected void assertValidVersion( final String version ) throws Exception
056    {
057        final VersionParser parser =
058            new VersionParser( new StringReader( version ) );
059
060        final Token[] tokens = parser.parse();
061        final StringBuffer buf = new StringBuffer().append( "Parses '" +
062                                                            version + "' to" );
063
064        for ( int i = 0; i < tokens.length; i++ )
065        {
066            buf.append( "\n\t" ).append( "token[" ).append( i ).append( "]=" ).
067                append( tokens[i].image );
068
069        }
070
071        System.out.println( buf.toString() );
072    }
073
074    protected void assertInvalidVersion( final String version )
075    {
076        try
077        {
078            final VersionParser parser =
079                new VersionParser( new StringReader( version ) );
080
081            parser.parse();
082            fail( "Could parse '" + version + "'." );
083        }
084        catch ( ParseException e )
085        {
086            Assert.assertNotNull( e.getMessage() );
087            System.out.println( e.toString() );
088        }
089    }
090
091}