View Javadoc

1   package org.apache.continuum.web.action;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.opensymphony.xwork2.Action;
23  import org.apache.commons.io.IOUtils;
24  import org.apache.continuum.web.action.stub.ViewBuildsReportActionStub;
25  import org.apache.maven.continuum.Continuum;
26  import org.apache.maven.continuum.model.project.BuildResult;
27  import org.apache.maven.continuum.model.project.Project;
28  import org.apache.maven.continuum.model.project.ProjectGroup;
29  import org.jmock.Mock;
30  
31  import java.util.ArrayList;
32  import java.util.Calendar;
33  import java.util.List;
34  
35  public class ViewBuildsReportActionTest
36      extends AbstractActionTest
37  {
38      private ViewBuildsReportActionStub action;
39  
40      private Mock continuum;
41  
42      private List<BuildResult> buildResults = new ArrayList<BuildResult>();
43  
44      @Override
45      protected void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          action = new ViewBuildsReportActionStub();
51          continuum = mock( Continuum.class );
52          action.setContinuum( (Continuum) continuum.proxy() );
53      }
54  
55      public void testInvalidRowCount()
56      {
57          action.setRowCount( -1 );
58          String result = action.execute();
59  
60          assertEquals( Action.INPUT, result );
61          assertTrue( action.hasFieldErrors() );
62          assertFalse( action.hasActionErrors() );
63          continuum.verify();
64      }
65  
66      public void testEndDateBeforeStartDate()
67      {
68          action.setStartDate( "04/25/2010" );
69          action.setEndDate( "04/24/2010" );
70          String result = action.execute();
71  
72          assertEquals( Action.INPUT, result );
73          assertTrue( action.hasFieldErrors() );
74          assertFalse( action.hasActionErrors() );
75          continuum.verify();
76      }
77  
78      public void testMalformedStartDate()
79      {
80          action.setStartDate( "not a date" );
81          String result = action.execute();
82  
83          assertEquals( Action.ERROR, result );
84          assertTrue( action.hasActionErrors() );
85          assertFalse( action.hasFieldErrors() );
86          continuum.verify();
87      }
88  
89      public void testMalformedEndDate()
90      {
91          action.setEndDate( "not a date" );
92          String result = action.execute();
93  
94          assertEquals( Action.ERROR, result );
95          assertTrue( action.hasActionErrors() );
96          assertFalse( action.hasFieldErrors() );
97          continuum.verify();
98      }
99  
100     public void testStartDateSameWithEndDate()
101     {
102         continuum.expects( once() ).method( "getBuildResultsInRange" ).will( returnValue( buildResults ) );
103 
104         action.setStartDate( "04/25/2010" );
105         action.setEndDate( "04/25/2010" );
106         String result = action.execute();
107 
108         assertSuccessResult( result );
109         continuum.verify();
110     }
111 
112     public void testEndDateWithNoStartDate()
113     {
114         continuum.expects( once() ).method( "getBuildResultsInRange" ).will( returnValue( buildResults ) );
115         action.setEndDate( "04/25/2010" );
116         String result = action.execute();
117 
118         assertSuccessResult( result );
119         continuum.verify();
120     }
121 
122     public void testExportToCsv()
123         throws Exception
124     {
125         Calendar cal = Calendar.getInstance();
126         cal.set( 2010, 1, 1, 1, 1, 1 );
127 
128         List<BuildResult> results = createBuildResult( cal.getTimeInMillis() );
129 
130         continuum.expects( once() ).method( "getBuildResultsInRange" ).will( returnValue( results ) );
131         action.setProjectGroupId( 0 );
132         action.setBuildStatus( 0 );
133         action.setStartDate( "" );
134         action.setEndDate( "" );
135         action.setTriggeredBy( "" );
136 
137         String result = action.downloadBuildsReport();
138 
139         assertEquals( "send-file", result );
140         assertFileContentsEqual( IOUtils.toString( action.getInputStream() ), cal.getTime().toString() );
141         continuum.verify();
142     }
143 
144     private void assertSuccessResult( String result )
145     {
146         assertEquals( Action.SUCCESS, result );
147         assertFalse( action.hasFieldErrors() );
148         assertFalse( action.hasActionErrors() );
149     }
150 
151     private List<BuildResult> createBuildResult( long timeInMillis )
152     {
153         List<BuildResult> results = new ArrayList<BuildResult>();
154 
155         BuildResult result = new BuildResult();
156 
157         ProjectGroup group = new ProjectGroup();
158         group.setName( "Test Group" );
159 
160         Project project = new Project();
161         project.setName( "Test Project" );
162         project.setProjectGroup( group );
163 
164         result.setProject( project );
165         result.setState( 2 );
166         result.setStartTime( timeInMillis );
167         result.setUsername( "test-admin" );
168 
169         results.add( result );
170 
171         return results;
172     }
173 
174     private void assertFileContentsEqual( String report, String buildDate )
175     {
176         String result = "Project Group,Project Name,Build Date,Triggered By,Build Status\n" +
177             "Test Group,Test Project," + buildDate + ",test-admin,Ok\n";
178 
179         assertEquals( report, result );
180     }
181 }