KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > merge > MergeMainTest


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Grzegorz Lukasik
5  *
6  * Cobertura is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License,
9  * or (at your option) any later version.
10  *
11  * Cobertura is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Cobertura; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */

21 package net.sourceforge.cobertura.merge;
22
23 import java.util.List JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 import junit.framework.TestCase;
29 import net.sourceforge.cobertura.coveragedata.ClassData;
30 import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler;
31 import net.sourceforge.cobertura.coveragedata.ProjectData;
32
33 /**
34  * Tests merging feature by launching Main class.
35  */

36 public class MergeMainTest extends TestCase {
37     private ClassData firstClass = new ClassData("test.First");
38     private ClassData secondClass = new ClassData("test.Second");
39     private ClassData seventhClass = new ClassData("Seventh");
40
41     private ProjectData greenProject = new ProjectData();
42     private ProjectData redProject = new ProjectData();
43     private ProjectData blueProject = new ProjectData();
44     
45     private List JavaDoc filesToRemove = new ArrayList JavaDoc();
46     
47     private File JavaDoc createTempSerFile() throws IOException JavaDoc {
48         File JavaDoc result = File.createTempFile( "cobertura", ".ser");
49         result.delete();
50         filesToRemove.add(result);
51         return result;
52     }
53     
54     protected void tearDown() throws Exception JavaDoc {
55         for( int i=0; i<filesToRemove.size(); i++) {
56             ((File JavaDoc)filesToRemove.get(i)).delete();
57         }
58     }
59     
60     public void testNewDestinationFile() throws IOException JavaDoc {
61         // Create some coverage data
62
greenProject.addClassData( firstClass);
63         redProject.addClassData( secondClass);
64         redProject.addClassData( seventhClass);
65
66         // Generate filenames for serialized data
67
File JavaDoc greenFile = createTempSerFile();
68         File JavaDoc redFile = createTempSerFile();
69         File JavaDoc dataFile = createTempSerFile();
70         
71         // Save coverage data for created data
72
CoverageDataFileHandler.saveCoverageData( greenProject, greenFile);
73         CoverageDataFileHandler.saveCoverageData( redProject, redFile);
74         
75         // Run merge task
76
String JavaDoc[] args = {"--datafile", dataFile.getAbsolutePath(),
77                 greenFile.getAbsolutePath(), redFile.getAbsolutePath()};
78         
79         Main.main( args);
80         
81         // Read merged data
82
ProjectData merged = CoverageDataFileHandler.loadCoverageData( dataFile);
83         
84         // Check if everything is ok
85
assertEquals( 3, merged.getNumberOfClasses());
86         assertNotNull( merged.getClassData("test.First"));
87         assertNotNull( merged.getClassData("test.Second"));
88         assertNotNull( merged.getClassData("Seventh"));
89         assertNull( merged.getClassData("test.Third"));
90     }
91
92     public void testExistingDestinationFile() throws IOException JavaDoc {
93         // Create some coverage data
94
greenProject.addClassData( firstClass);
95         redProject.addClassData( secondClass);
96
97         // Generate filenames for serialized data
98
File JavaDoc greenFile = createTempSerFile();
99         File JavaDoc dataFile = createTempSerFile();
100         
101         // Save coverage data for created data
102
CoverageDataFileHandler.saveCoverageData( greenProject, greenFile);
103         CoverageDataFileHandler.saveCoverageData( redProject, dataFile);
104         
105         // Run merge task
106
String JavaDoc[] args = {"--datafile", dataFile.getAbsolutePath(),
107                 greenFile.getAbsolutePath()};
108         
109         Main.main( args);
110         
111         // Read merged data
112
ProjectData merged = CoverageDataFileHandler.loadCoverageData( dataFile);
113         
114         // Check if everything is ok
115
assertEquals( 2, merged.getNumberOfClasses());
116         assertNotNull( merged.getClassData("test.First"));
117         assertNotNull( merged.getClassData("test.Second"));
118     }
119
120     public void testBaseDir() throws IOException JavaDoc {
121         // Create some coverage data
122
greenProject.addClassData( firstClass);
123         redProject.addClassData( seventhClass);
124         blueProject.addClassData( secondClass);
125
126         // Generate filenames for serialized data
127
File JavaDoc greenFile = createTempSerFile();
128         File JavaDoc redFile = createTempSerFile();
129         File JavaDoc blueFile = createTempSerFile();
130         File JavaDoc dataFile = createTempSerFile();
131         
132         dataFile.delete();
133         
134         // Save coverage data for created data
135
CoverageDataFileHandler.saveCoverageData( greenProject, greenFile);
136         CoverageDataFileHandler.saveCoverageData( redProject, redFile);
137         CoverageDataFileHandler.saveCoverageData( blueProject, blueFile);
138         
139         // Run merge task
140
String JavaDoc[] args = {"--datafile", dataFile.getAbsolutePath(),
141                 greenFile.getAbsolutePath(),
142                 "--basedir", redFile.getParent(), redFile.getName(), blueFile.getName()};
143         
144         Main.main( args);
145         
146         // Read merged data
147
ProjectData merged = CoverageDataFileHandler.loadCoverageData( dataFile);
148         
149         // Check if everything is ok
150
assertEquals( 3, merged.getNumberOfClasses());
151         assertNotNull( merged.getClassData("test.First"));
152         assertNotNull( merged.getClassData("test.Second"));
153         assertNotNull( merged.getClassData("Seventh"));
154     }
155 }
156
Popular Tags