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