Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 057834765ffb49032d599e83b6faf38415b5a29b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 *******************************************************************************/
 
package org.eclipse.cdt.core.parser.tests;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Set;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;

/**
 * @author aniefer
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public abstract class AutomatedFramework extends TestCase {

	public AutomatedFramework() {
		super();
	}

	public AutomatedFramework(String name) {
		super(name);
	}

	protected abstract AutomatedFramework newTest( String name );
	protected abstract void loadProperties() throws Exception;
	public	  abstract void doFile() throws Throwable;

	private void fillSuite( TestSuite suite, File path ){
		File files[] = null;
		if( path.isFile() ){
			files = new File[ 1 ];
			files[0] = path;
		}
		else
			files = path.listFiles();
	
		File file = null;
		String filePath = null;
		int i = 0;
		try{
			file = files[ i++ ];
			while( file != null )
			{
				if( file.isDirectory() )
					fillSuite( suite, file );
				else if( file.isFile() && nameFilter.accept( file.getParentFile(), file.getName() ) ){
					try{
						filePath = file.getCanonicalPath();
					} catch ( Exception e ){
						continue;
					}
					
					if(	filePath.endsWith(".cpp") || filePath.endsWith(".hpp") ||  //$NON-NLS-1$ //$NON-NLS-2$
						filePath.endsWith(".cc") || filePath.endsWith(".CC") || //$NON-NLS-1$ //$NON-NLS-2$
						filePath.endsWith(".C") || //$NON-NLS-1$
						filePath.endsWith(".hxx") || filePath.endsWith(".hh") ) //$NON-NLS-1$ //$NON-NLS-2$
					{
						AutomatedTest.natures.put( filePath, "cpp" ); //$NON-NLS-1$
					} else if( filePath.endsWith(".c") ){  //$NON-NLS-1$
						AutomatedTest.natures.put( filePath, "c" ); //$NON-NLS-1$
					} else {
						AutomatedTest.natures.put( filePath, AutomatedTest.defaultNature );
					}
					
					AutomatedTest.fileList.add( file );
					suite.addTest( newTest( file.getName().replace(',', '_') ) );
				}				
				file = files[ i++ ];
			}
		} catch( ArrayIndexOutOfBoundsException e ){
			//done
		}
	}

	public void reportFailed() {
		fail( "Unable to open " + outputFile + "for output of results." ); //$NON-NLS-1$ //$NON-NLS-2$
	}

	public void propertiesFailed() {
		fail( "Unable to load properties file." ); //$NON-NLS-1$
	}
	
	protected void runTest() throws Throwable {
		String name = getName();
		
		if( name.equals("propertiesFailed") ) //$NON-NLS-1$
			propertiesFailed();
		else if ( name.equals("reportFailed") ) //$NON-NLS-1$
			reportFailed();
		else
			doFile();
	}

	public Test createSuite() {
		TestSuite suite = new TestSuite();
		
		try{
			loadProperties();
		} catch( Exception e ){
			suite.addTest( newTest( "propertiesFailed") ); //$NON-NLS-1$
		}
		
		if( outputFile != null && !outputFile.equals("") ){ //$NON-NLS-1$
			try{
				
				File output = new File( outputFile );
				
				if( output.exists() ){
					output.delete();
				}
				
				output.createNewFile();
			
				report = new FileOutputStream( output );
			
			} catch( Exception e ) {
				suite.addTest( newTest( "reportFailed" ) ); //$NON-NLS-1$
			}
		}
		
		Set keys = testSources.keySet();
		Iterator iter = keys.iterator();
		int size = keys.size();
		String item = null;
		for( int i = size; i > 0; i-- )
		{
			item = (String) iter.next();
			File file = new File( item );
			if( file.exists() ){
				defaultNature = (String) testSources.get( item );
				fillSuite( suite, file );		
			}
		}
	
		return suite;
	}

	protected static ISourceElementRequestor nullCallback = new NullSourceElementRequestor();
	protected static Properties properties = new Properties();
	protected static String defaultNature;
	protected static String outputFile = null;
	protected static HashMap testSources = new HashMap();
	protected static HashMap natures = new HashMap();
	protected static LinkedList fileList = new LinkedList();
	private static FilenameFilter nameFilter = new Filter();
	protected static FileOutputStream report = null;
	
	static private class Filter implements FilenameFilter
	{
		public boolean accept(File dir, String name) {
			if( name.endsWith(".cpp") 	||  //$NON-NLS-1$
				name.endsWith(".c") 	||  //$NON-NLS-1$
				name.endsWith(".cc") 	|| //$NON-NLS-1$
				name.endsWith(".CC") 	|| //$NON-NLS-1$
				name.endsWith(".C") 	||  //$NON-NLS-1$
				name.endsWith(".h") 	|| //$NON-NLS-1$
				name.endsWith(".hh")	|| //$NON-NLS-1$
				name.endsWith(".hpp")	|| //$NON-NLS-1$
				name.endsWith(".hxx")) //$NON-NLS-1$
			{
				return true;
			}
			else
				return false;
		}
	}

}

Back to the top