1   package org.apache.maven.perforcelib;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import org.apache.maven.util.EnhancedStringTokenizer;
22  import org.apache.maven.util.RepositoryUtils;
23  import org.apache.tools.ant.types.Commandline;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * @author <a href="jim@crossleys.org">Jim Crossley</a>
29   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
30   * @version $Id: 
31   */
32  public class PerforceChangeLogGeneratorTest extends TestCase
33  {
34      class Test
35      {
36          String conn;
37          String args;
38          Class throwable;
39  
40          public Test(String conn, String args, Class throwable)
41          {
42              this.conn = conn;
43              this.args = args;
44              this.throwable = throwable;
45          }
46  
47      }
48  
49      Test[] tests =
50          {
51              new Test(null, "", NullPointerException.class),
52              new Test("asd:asd", "", IllegalArgumentException.class),
53              new Test("scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:jakarta-turbine-maven",
54                       "",
55                       IllegalArgumentException.class),
56              new Test("scm:perforce:host:port://depot/projects/name/...",
57                       "p4|-p|host:port|filelog|-tl|//depot/projects/name/...",
58                       null),
59              new Test("scm:perforce:port://depot/projects/name/...",
60                       "p4|-p|port|filelog|-tl|//depot/projects/name/...",
61                       null),
62              new Test("scm:perforce://depot/projects/name/...",
63                       "p4|filelog|-tl|//depot/projects/name/...",
64                       null),
65          };
66  
67      public void testParse() throws Throwable
68      {
69          for (int i = 0; i < tests.length; i++)
70          {
71              Test t = tests[i];
72              testParse(t);
73          }
74      }
75  
76      public void testParse(Test test) throws Throwable
77      {
78          String[] expected = RepositoryUtils.tokenizerToArray(new EnhancedStringTokenizer(test.args, "|"));
79  
80          PerforceChangeLogGenerator eg = new PerforceChangeLogGenerator();
81          try {
82              eg.setConnection(test.conn);
83              Commandline cl = eg.getScmLogCommand();
84              String[] clArgs = cl.getCommandline();
85              if (test.throwable == null)
86              {
87                  assertEquals("clArgs.length", expected.length, clArgs.length);
88                  for (int i = 0; i < expected.length; i++)
89                  {
90                      assertEquals("clArgs[" + i + "]", expected[i], clArgs[i]);
91                  }
92              }
93              else
94              {
95                  fail("Failed to throw :" + test.throwable.getName());
96              }
97  
98          }
99          catch (Throwable t)
100         {
101             if (test.throwable != null && test.throwable.isAssignableFrom(t.getClass()))
102             {
103                 //Success
104             }
105             else
106             {
107                 throw new RuntimeException("Caught unexpected exception \"" + t.getLocalizedMessage() + "\" testing " + test.conn);
108             }
109         }
110     }
111 }