1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with 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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 using System;
25 using System.Reflection;
26 using System.IO;
27 
28 // __________  implementation  ____________________________________
29 
30 /** Create and modify a spreadsheet document.
31  */
32 namespace cliversion
33 {
34 public class RunTests
35 {
36 
Main(String[] args)37     public static int Main(String[] args)
38     {
39 //        System.Diagnostics.Debugger.Launch();
40         //get the path to the directory
41         string sLocation = Assembly.GetExecutingAssembly().Location;
42         sLocation = sLocation.Substring(0, sLocation.LastIndexOf('\\'));
43         // Create a reference to the current directory.
44         DirectoryInfo di = new DirectoryInfo(sLocation);
45         // Create an array representing the files in the current directory.
46         FileInfo[] fi = di.GetFiles();
47 
48         //For every found dll try to figure out if it contains a
49         //cliversion.Version class
50 		foreach (FileInfo fiTemp in fi)
51 		{
52 			if (fiTemp.Extension != ".dll"
53                 || ! fiTemp.Name.StartsWith("version"))
54 				continue;
55 
56             Assembly ass = null;
57             Object objVersion = null;
58 			try
59 			{
60                 string sName = fiTemp.Name.Substring(0, fiTemp.Name.LastIndexOf(".dll"));
61 				ass = Assembly.Load(sName);
62 			}
63 			catch (BadImageFormatException)
64 			{
65 				continue;
66 			}
67 			catch (Exception e)
68 			{
69 				Console.WriteLine("#Unexpected Exception");
70 				Console.WriteLine(e.Message);
71                 return -1;
72 			}
73 
74             //Assembly is loaded, instantiate cliversion.Version
75 			try
76 			{
77 				//This runs the test
78 				objVersion = ass.CreateInstance("cliversion.Version");
79 				if (objVersion == null)
80 					continue;
81 				Console.WriteLine("#Tested successfully " + fiTemp.Name);
82 				//Starting the office the second time may fail without this pause
83 				System.Threading.Thread.Sleep(2000);
84 			}
85 			catch (Exception e)
86 			{
87 				TargetInvocationException te = e as TargetInvocationException;
88 				if (te != null)
89 				{
90 					FileNotFoundException fe = e.InnerException as FileNotFoundException;
91 					if (fe != null)
92 					{
93 						Console.WriteLine(fiTemp.Name + " did not find " + fe.FileName +
94 							". Maybe the " + fe.FileName + " is not installed or does not match the referenced version."  +
95 							"Original message: " + fe.Message + "\n\n FusionLog: \n" + fe.FusionLog );
96 						return -1;
97 					}
98 					FileLoadException fl = e.InnerException as FileLoadException;
99 					if (fl != null)
100 					{
101 						Console.WriteLine(fiTemp.Name + " could not load " + fl.FileName +
102 							". Maybe the version of " + fl.FileName + " does not match the referenced version. " +
103 							"Original message: " + fl.Message + "\n\n FusionLog: \n" + fl.FusionLog );
104 						return -1;
105 					}
106 
107                     if (e.InnerException != null)
108                     {
109                         Console.WriteLine(e.InnerException);
110                     }
111 				}
112 				Console.WriteLine("#Unexpected Exception");
113 				Console.WriteLine(e.Message);
114 				return -1;
115 			}
116 		}
117         //For some unknown reason this program hangs sometimes when started from java. This is
118         //a workaround that makes the problem disappear.
119         System.Threading.Thread.Sleep(1000);
120         return 0;
121     }
122 }
123 }
124