Révision 357

tmp/org.txm.core.tests/META-INF/MANIFEST.MF (revision 357)
4 4
Bundle-SymbolicName: org.txm.core.tests;singleton:=true
5 5
Bundle-Version: 1.0.0.qualifier
6 6
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
7
Require-Bundle: org.txm.lexicon.core,
7
Require-Bundle: org.txm.utils,
8
 org.txm.lexicon.core,
8 9
 org.eclipse.core.runtime;bundle-version="3.10.0",
9 10
 org.txm.core;bundle-version="0.7.0",
10 11
 org.txm.statsengine.r.core,
tmp/org.txm.core.tests/src/org/txm/core/tests/junit/PluginXMLIDs.java (revision 357)
1
package org.txm.core.tests.junit;
2

  
3
import java.io.File;
4
import java.io.IOException;
5
import java.util.LinkedHashSet;
6

  
7
import org.txm.utils.io.IOUtils;
8

  
9
public class PluginXMLIDs extends TXMPluginTest {
10

  
11
	@Override
12
	public void TBXPluginTest(File projectDirectory, String name, String packagePath) throws IOException {
13

  
14
	}
15

  
16
	@Override
17
	public void RCPPluginTest(File projectDirectory, String name, String packagePath) throws IOException {
18
		File pluginXML = new File(projectDirectory, "plugin.xml");
19
		if (!pluginXML.exists()) {
20
			error("missing file "+pluginXML);
21
		}
22
		File javaSrcDirectory = getJavaSrcDirectory(projectDirectory);
23

  
24
		LinkedHashSet<String> ids = new LinkedHashSet<String>();
25
		ids.addAll(IOUtils.findWithGroup(pluginXML, "id=\"([^\"]+)\""));
26
		for (String id : ids) {
27
			if (id.startsWith(name)) {
28
				//System.out.println("id="+id);
29
				String path = id.substring(0, id.lastIndexOf(".")).replace(".", "/");
30
				String className = id.substring(id.lastIndexOf(".")+1);
31
				if (className.matches("[A-Z].+")) {
32
					File classFile = new File(javaSrcDirectory, path+"/"+className+".java");
33
					if (!classFile.exists()) {
34
						error("unbound id	"+id+"	"+classFile);
35
					}
36
				}
37
			}
38
		}
39

  
40
		testClassExistance(pluginXML, javaSrcDirectory, name, "id=\"([^\"]+)\"", "id");
41
		testClassExistance(pluginXML, javaSrcDirectory, name, "adaptableType=\"([^\"]+)\"", "adaptableType");
42
		testClassExistance(pluginXML, javaSrcDirectory, name, "class=\"([^\"]+)\"", "class");
43
		testClassExistance(pluginXML, javaSrcDirectory, name, "class=\"([^\"]+)\"", "commandId");
44
		testClassExistance(pluginXML, javaSrcDirectory, name, "class=\"([^\"]+)\"", "defaultHandler");
45
		testClassExistance(pluginXML, javaSrcDirectory, name, "instanceof value=\"([^\"]+)\"", "instanceof");
46
	}
47

  
48
	/**
49
	 * 
50
	 * @param pluginXML plugin.xml file
51
	 * @param javaSrcDirectory the Java source directory (contains the class to find)
52
	 * @param name the package to test
53
	 * @param pattern the pattern to match. must contains ONE group
54
	 * @param type the attribute to test
55
	 * @throws IOException
56
	 */
57
	private void testClassExistance(File pluginXML, File javaSrcDirectory, String name, String pattern, String type) throws IOException {
58
		LinkedHashSet<String> classes = new LinkedHashSet<String>();
59
		classes.addAll(IOUtils.findWithGroup(pluginXML, pattern, true));
60
		for (String id : classes) {
61
			if (id.startsWith(name)) {
62
				//System.out.println("id="+id);
63
				String path = id.substring(0, id.lastIndexOf(".")).replace(".", "/");
64
				String className = id.substring(id.lastIndexOf(".")+1);
65
				if (className.matches("[A-Z].+")) {
66
					File classFile = new File(javaSrcDirectory, path+"/"+className+".java");
67
					if (!classFile.exists()) {
68
						error("unbound "+type+"	"+id+"	"+classFile);
69
					}
70
				}
71
			}
72
		}
73
	}
74
}
0 75

  
tmp/org.txm.core.tests/src/org/txm/core/tests/junit/PluginPreferences.java (revision 357)
1
package org.txm.core.tests.junit;
2

  
3
import static org.junit.Assert.fail;
4

  
5
import java.io.File;
6

  
7
public class PluginPreferences extends TXMPluginTest {
8

  
9
	public void RCPPluginTest(File projectDirectory, String name, String packagePath) {
10
		
11
	}
12
	
13
	@Override
14
	public void TBXPluginTest(File projectDirectory, String name, String packagePath) {
15
		File javaSrcDirectory = getJavaSrcDirectory(projectDirectory);
16
		File preferencePackageDirectory = new File(javaSrcDirectory, packagePath+"/preferences");
17
		if (!preferencePackageDirectory.exists()) {
18
			error(preferencePackageDirectory.getAbsolutePath()+" is missing.");
19
			return;
20
		}
21
		
22
		// test content
23
		File[] files = preferencePackageDirectory.listFiles();
24
		if (files == null) {
25
			error("No preference class file.");
26
			return;
27
		}
28
		int c = 0;
29
		for (File prefFile : files) {
30
			if (prefFile.getName().endsWith("Preferences.java")) c++;
31
		}
32
		if (c == 0) error("No preference class file in "+preferencePackageDirectory);
33
	}
34
}
0 35

  
tmp/org.txm.core.tests/src/org/txm/core/tests/junit/AllTXMPluginTests.java (revision 357)
1
package org.txm.core.tests.junit;
2

  
3
import org.junit.runner.RunWith;
4
import org.junit.runners.Suite;
5
import org.junit.runners.Suite.SuiteClasses;
6

  
7
@RunWith(Suite.class)
8
@SuiteClasses({ PluginPreferences.class, PluginMessages.class, PluginXMLIDs.class })
9
public class AllTXMPluginTests {
10

  
11
}
0 12

  
tmp/org.txm.core.tests/src/org/txm/core/tests/junit/PluginMessages.java (revision 357)
1
package org.txm.core.tests.junit;
2

  
3
import static org.junit.Assert.fail;
4

  
5
import java.io.File;
6
import java.io.FileNotFoundException;
7
import java.io.IOException;
8
import java.io.UnsupportedEncodingException;
9
import java.util.ArrayList;
10
import java.util.LinkedHashSet;
11
import java.util.Properties;
12

  
13
import org.txm.utils.io.IOUtils;
14

  
15
public class PluginMessages extends TXMPluginTest {
16

  
17
	String langs[] = {"", "_fr", "_ru"};
18

  
19
	@Override
20
	public void TBXPluginTest(File projectDirectory, String name, String packagePath) throws UnsupportedEncodingException, FileNotFoundException, IOException {
21
		commonTest(projectDirectory, name, packagePath);
22
	}
23

  
24
	@Override
25
	public void RCPPluginTest(File projectDirectory, String name, String packagePath) throws UnsupportedEncodingException, FileNotFoundException, IOException {
26
		commonTest(projectDirectory, name, packagePath);
27

  
28
		File pluginXML = new File(projectDirectory, "plugin.xml");
29
		try {
30
			// OSGI-INF test
31
			File OSGI = new File(projectDirectory, "OSGI-INF/l10n");
32
			for (String lang : langs) {
33
				File propertiesFile = new File(OSGI, "bundle"+lang+".properties");
34
				if (!propertiesFile.exists()) {
35
					error("No "+propertiesFile.getAbsolutePath());
36
					return;
37
				} else {
38
					testKeys(propertiesFile, pluginXML, "=\"%([^\"]+)\"");
39
				}
40
			}
41

  
42
		} catch (IOException e) {
43
			error(e.getLocalizedMessage());
44
			e.printStackTrace();
45
		}
46
	}
47

  
48
	public void commonTest(File projectDirectory, String name, String packagePath) throws UnsupportedEncodingException, FileNotFoundException, IOException {
49
		File javaSrcDirectory = getJavaSrcDirectory(projectDirectory);
50

  
51
		File messagesPackageDirectory = new File(javaSrcDirectory, packagePath+"/messages");
52
		if (!messagesPackageDirectory.exists()) {
53
			error("missing file "+messagesPackageDirectory.getAbsolutePath());
54
			return;
55
		}
56

  
57
		// test content
58
		File[] files = messagesPackageDirectory.listFiles();
59
		if (files == null) {
60
			error("No messages class file.");
61
			return;
62
		}
63
		int c = 0;
64

  
65
		File classFile = null;
66
		for (File file : files) {
67
			if (file.getName().endsWith("Messages.java")) classFile = file;
68
		}
69

  
70
		if (classFile == null) {
71
			error("missing Java file "+messagesPackageDirectory);
72
			return;
73
		}
74

  
75
		for (String lang : langs) {
76
			
77
			File propertiesFile = new File(messagesPackageDirectory, "messages"+lang+".properties");
78
			if (!propertiesFile.exists()) {
79
				error("missing properties	"+propertiesFile.getAbsolutePath());
80
				return;
81
			} else {
82
				testKeys(propertiesFile, classFile, "public static String ([^;]+);");
83
			}
84
		}
85
	}
86

  
87
	/**
88
	 * Compare keys of @param propertiesFile with keys found in @param classFile with @param pattern
89
	 * @param propertiesFile
90
	 * @param classFile
91
	 * @param pattern
92
	 * @throws UnsupportedEncodingException
93
	 * @throws FileNotFoundException
94
	 * @throws IOException
95
	 */
96
	protected void testKeys(File propertiesFile, File classFile, String pattern) throws UnsupportedEncodingException, FileNotFoundException, IOException {
97
		Properties props = new Properties();
98
		props.load(IOUtils.getReader(propertiesFile, "UTF-8"));
99
		
100
		LinkedHashSet<String> keys = new LinkedHashSet<String>();
101
		keys.addAll(IOUtils.findWithGroup(classFile, pattern));
102
		
103
		// find missing keys in prop file
104
		for (String key : keys) {
105
			if (props.getProperty(key) == null) {
106
				error("missing key	'"+key+"'	"+propertiesFile.getName());
107
			}
108
		}
109
		
110
		// find unused keys
111
		for (Object key : props.keySet()) {
112
			String s = key.toString();
113
			if (!keys.contains(s)) {
114
				error("unused key	'"+key+"'	"+propertiesFile.getName());
115
			}
116
		}
117
	}
118
}
0 119

  
tmp/org.txm.core.tests/src/org/txm/core/tests/junit/TXMPluginTest.java (revision 357)
1
package org.txm.core.tests.junit;
2

  
3
import static org.junit.Assert.fail;
4

  
5
import java.io.File;
6
import java.io.FileFilter;
7
import java.io.FileNotFoundException;
8
import java.io.IOException;
9
import java.io.UnsupportedEncodingException;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12

  
13
import org.junit.Test;
14

  
15
public abstract class TXMPluginTest {
16
	public static int LEVEL = 1; // 1 errors, 2 warnings, 3 infos messages
17
	int error = 0;
18
	
19
	protected ArrayList<File> projects;
20
	protected ArrayList<String> messages = new ArrayList<String>();
21
	
22
	
23
	public static File workspace = new File(System.getProperty("user.home"), "workspace079");
24

  
25
	protected static ArrayList<File> getProjects() {
26
		ArrayList<File> ret = new ArrayList<File>();
27
		if (!workspace.exists()) {
28
			System.out.println("ERROR: "+workspace+" does not exists. Set it in TXMPluginTest.java.");
29
			return ret;
30
		} 
31

  
32
		ret.addAll(Arrays.asList(workspace.listFiles(new FileFilter() {
33
			@Override
34
			public boolean accept(File pathname) {
35
				return pathname.isDirectory() && (pathname.getName().endsWith(".rcp") || pathname.getName().endsWith(".core"));
36
			}
37
		})));
38

  
39
		return ret;
40
	}
41
	
42

  
43
	protected void error(String string) {
44
		if (LEVEL >= 1) messages.add("	ERROR	"+string);
45
		error++;
46
	}
47

  
48
	protected void warning(String string) {
49
		if (LEVEL >= 2) messages.add("	WARNING	"+string);
50
	}
51

  
52
	protected void info(String string) {
53
		if (LEVEL >= 3) messages.add("	INFO	"+string);
54
	}
55
	
56
	/**
57
	 * All Test must starts with "super.test();"
58
	 */
59
	@Test
60
	public void test() {
61
		
62
		System.out.print("Testing "+this.getClass().getSimpleName()+"...");
63
		try {
64
		projects = getProjects();
65
		
66
		if (projects.size() == 0) fail("No project found in "+workspace);
67
		
68
		int nProjectMessage = 0;
69
		for (File projectDirectory : projects) {
70
			messages.clear();
71
			String plugin_name = projectDirectory.getName();
72
			String plugin_package = plugin_name.replace(".", "/");
73
			
74
			if (projectDirectory.getName().endsWith(".core")) {
75
				//plugin_package = plugin_package.substring(0, plugin_package.length()-5);
76
				TBXPluginTest(projectDirectory, plugin_name, plugin_package);
77
			} else if (projectDirectory.getName().endsWith(".rcp")) {
78
				//plugin_package = plugin_package.substring(0, plugin_package.length()-4);
79
				RCPPluginTest(projectDirectory, plugin_name, plugin_package);
80
			} else {
81
				// ignore project
82
			}
83
			
84
			if (messages.size() > 0) {
85
				if (nProjectMessage == 0) {
86
					System.out.println();
87
					nProjectMessage++;
88
				}
89
				System.out.println(" P "+projectDirectory.getName());
90
				for (String s : messages) System.out.println(s);
91
			}
92
			
93
		}
94
		} catch(Exception e) {
95
			e.printStackTrace();
96
			fail("Internal test error: "+e.getLocalizedMessage());
97
		}
98
		if (error > 0) {
99
			fail("Errors during test "+this.getClass().getName());
100
		} else System.out.println("OK!");
101
	}
102
	
103
/**
104
 * 
105
 * @param projectDirectory
106
 * @return the Java src directory src/main or src
107
 */
108
	protected static File getJavaSrcDirectory(File projectDirectory) {
109
		File javaSrcDirectory = new File(projectDirectory, "src/main/java/");
110
		if (!javaSrcDirectory.exists()) javaSrcDirectory = new File(projectDirectory, "src/java");
111
		if (!javaSrcDirectory.exists()) javaSrcDirectory = new File(projectDirectory, "src/");
112
		return javaSrcDirectory;
113
	}
114
	
115
	public abstract void TBXPluginTest(File projectDirectory, String name, String packagePath) throws Exception;
116
	public abstract void RCPPluginTest(File projectDirectory, String name, String packagePath) throws Exception;
117
}
0 118

  

Formats disponibles : Unified diff