View Javadoc

1   /*
2    *  jDTAUS Core Client Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.client.versioning.test;
22  
23  import java.io.StringReader;
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
26  import org.jdtaus.core.container.ri.client.versioning.ParseException;
27  import org.jdtaus.core.container.ri.client.versioning.Token;
28  import org.jdtaus.core.container.ri.client.versioning.VersionParser;
29  
30  /**
31   * Tests the {@code VersionParser} implementation.
32   *
33   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
34   * @version $JDTAUS: VersionParserTest.java 8641 2012-09-27 06:45:17Z schulte $
35   */
36  public class VersionParserTest extends TestCase
37  {
38  
39      public void testParseable() throws Exception
40      {
41          this.assertValidVersion( "1" );
42          this.assertValidVersion( "1.1" );
43          this.assertValidVersion( "SNAPSHOT" );
44          this.assertValidVersion( "RELEASE" );
45          this.assertValidVersion( "LATEST" );
46          this.assertValidVersion( "4aug2007r7-dev" );
47          this.assertValidVersion( "1.0-alpha-10-stable-1" );
48      }
49  
50      public void testUnparseable() throws Exception
51      {
52          this.assertInvalidVersion( "." );
53      }
54  
55      protected void assertValidVersion( final String version ) throws Exception
56      {
57          final VersionParser parser =
58              new VersionParser( new StringReader( version ) );
59  
60          final Token[] tokens = parser.parse();
61          final StringBuffer buf = new StringBuffer().append( "Parses '" +
62                                                              version + "' to" );
63  
64          for ( int i = 0; i < tokens.length; i++ )
65          {
66              buf.append( "\n\t" ).append( "token[" ).append( i ).append( "]=" ).
67                  append( tokens[i].image );
68  
69          }
70  
71          System.out.println( buf.toString() );
72      }
73  
74      protected void assertInvalidVersion( final String version )
75      {
76          try
77          {
78              final VersionParser parser =
79                  new VersionParser( new StringReader( version ) );
80  
81              parser.parse();
82              fail( "Could parse '" + version + "'." );
83          }
84          catch ( ParseException e )
85          {
86              Assert.assertNotNull( e.getMessage() );
87              System.out.println( e.toString() );
88          }
89      }
90  
91  }