1   package org.apache.maven.cvslib;
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 java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.StringTokenizer;
26  
27  import org.apache.maven.changelog.ChangeLog;
28  import org.apache.maven.util.EnhancedStringTokenizer;
29  import org.apache.maven.util.RepositoryUtils;
30  import org.apache.tools.ant.types.Commandline;
31  
32  import junit.framework.TestCase;
33  
34  /**
35   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
36   * @version $Id: CvsChangeLogGeneratorTest.java 532339 2007-04-25 12:28:56Z ltheussl $
37   */
38  class ExposeGenerator extends CvsChangeLogGenerator
39  {
40  
41      protected Commandline getScmLogCommand()
42      {
43          return super.getScmLogCommand();
44      }
45  }
46  
47  /**
48   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
49   * @version $Id: CvsChangeLogGeneratorTest.java 532339 2007-04-25 12:28:56Z ltheussl $
50   */
51  public class CvsChangeLogGeneratorTest extends TestCase
52  {
53      class Test
54      {
55          String conn;
56          String args;
57          Class throwable;
58          Map map;
59    
60          public Test(String params, String conn, String args, Class throwable)
61          {
62              this.conn = conn;
63              this.args = args;
64              this.throwable = throwable;
65              this.map = null;
66              if (params != null)
67              {
68                  map = new HashMap();
69                  StringTokenizer tokens = new StringTokenizer(params, "|");
70                  while (tokens.hasMoreTokens())
71                  {
72                      String name = tokens.nextToken();
73                      assertTrue("params must have an even number of values.", tokens.hasMoreTokens());
74                      String value = tokens.nextToken();
75                      map.put(name, value);
76                  }
77              }
78          }
79  
80      }
81  
82      static SimpleDateFormat standardFormat = new SimpleDateFormat("yyyy-MM-dd");
83      static String now = standardFormat.format(new Date(System.currentTimeMillis() + (long) 1 * 24 * 60 * 60 * 1000));
84      static String range30 = standardFormat.format(new Date(System.currentTimeMillis() - (long) 30 * 24 * 60 * 60 * 1000));
85      static String range10 = standardFormat.format(new Date(System.currentTimeMillis() - (long) 10 * 24 * 60 * 60 * 1000));
86      
87      Test[] tests =
88          {
89              new Test(null, null, "", NullPointerException.class),
90              new Test(null, "asd:asd", "", IllegalArgumentException.class),
91              new Test(null, null, "", NullPointerException.class),
92              new Test(null, "asd:asd", "", IllegalArgumentException.class),
93              new Test(
94                  null,
95                  "scm:csvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
96                  "",
97                  IllegalArgumentException.class),
98              new Test(
99                  null,
100                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
101                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log",
102                 null),
103             new Test(
104                 null,
105                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven:anoncvs",
106                 "",
107                 IllegalArgumentException.class),
108             new Test(
109                 null,
110                 "scm:cvs|pserver|anoncvs@cvs.apache.org|D:\\home\\cvspublic|maven",
111                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:D:\\home\\cvspublic|log",
112                 null),
113             new Test(
114                 null,
115                 "scm:cvs|pserver|anoncvs@cvs.apache.org|D:/home/cvspublic|maven",
116                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:D:/home/cvspublic|log",
117                 null),
118             new Test(
119                 null,
120                 "scm:cvs:lserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
121                 "cvs|-d|anoncvs@cvs.apache.org:/home/cvspublic|log",
122                 null) ,
123             new Test(
124                 null,
125                 "scm:cvs|local|local|D:/home/cvspublic|maven",
126                 "cvs|-d|D:/home/cvspublic|log",
127                 null),
128             new Test(
129                 null,
130                 "scm:cvs:extssh:someuser@cvs.apache.org:/home/cvs:maven",
131                 "cvs|-d|:extssh:someuser@cvs.apache.org:/home/cvs|log",
132                 null),
133             new Test(
134                 "type|range|range|30",
135                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
136                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-d " + range30 + "<" + now,
137                 null),
138             new Test(
139                 "type|range|range|10",
140                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
141                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-d " + range10 + "<" + now,
142                 null),
143             new Test(
144                 "type|date|start|2004-04-01",
145                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
146                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-d 2004-04-01<" + now,
147                 null),
148             new Test(
149                 "type|date|start|1996-06-12",
150                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
151                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-d 1996-06-12<" + now,
152                 null),
153             new Test(
154                 "type|date|start|1996-06-12|end|1998-05-13",
155                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
156                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-d 1996-06-12<1998-05-13",
157                 null),
158             new Test(
159                 "type|tag|start|my_tag_name",
160                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
161                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-rmy_tag_name::",
162                 null),
163             new Test(
164                 "type|tag|start|my_tag_name|end|end_tag_name",
165                 "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:maven",
166                 "cvs|-d|:pserver:anoncvs@cvs.apache.org:/home/cvspublic|log|-rmy_tag_name::end_tag_name",
167                 null),
168             };
169 
170     public void testParse() throws Throwable
171     {
172         for (int i = 0; i < tests.length; i++)
173         {
174 
175             Test t = tests[i];
176             testParse(t, i);
177         }
178     }
179 
180     public void testParse(Test test, int index) throws Throwable
181     {
182         String[] expected = RepositoryUtils.tokenizerToArray(new EnhancedStringTokenizer(test.args, "|"));
183 
184         ExposeGenerator eg = new ExposeGenerator();
185         try
186         {
187             ChangeLog changelog = new ChangeLog();
188             if (test.map != null)
189             {
190                 changelog.setType((String)test.map.get("type"));
191                 changelog.setRange((String)test.map.get("range"));
192                 changelog.setMarkerStart((String)test.map.get("start"));
193                 changelog.setMarkerEnd((String)test.map.get("end"));
194                 changelog.setDateFormat((String)test.map.get("dateformat"));
195             }
196             else
197             {
198                 changelog.setType("range");
199             }
200             changelog.setRepositoryConnection(test.conn);
201             eg.init(changelog);
202             Commandline cl = eg.getScmLogCommand();
203             String[] clArgs = cl.getCommandline();
204             if (test.throwable == null)
205             {
206                 assertEquals("index " + index + ": clArgs.length", expected.length, clArgs.length);
207                 for (int i = 0; i < expected.length; i++)
208                 {
209                     if ( clArgs[i].startsWith( "-d \"" ) )
210                     {
211                         clArgs[i] = "-d " + clArgs[i].substring( 4, clArgs[i].length() - 1 );
212                     }
213                     assertEquals("index " + index + ": clArgs[" + i + "]", expected[i], clArgs[i]);
214                 }
215             }
216             else
217             {
218                 fail("index " + index + ": Failed to throw :" + test.throwable.getName());
219             }
220 
221         }
222         catch (Exception t)
223         {
224             if (test.throwable != null && test.throwable.isAssignableFrom(t.getClass()))
225             {
226                 //Success
227             }
228             else
229             {
230                 throw new RuntimeException("Caught unexpected exception \"" + t.getLocalizedMessage() + "\" testing " + test.conn + " (index " + index + ")", t);
231             }
232         }
233 
234     }
235 
236     
237 
238     
239 }