summaryrefslogtreecommitdiff
path: root/qadevOOo/tests/java/complex/unoapi/CheckModuleAPI.java
blob: d8d64f11fb99d979a3eca35c47eeba13561a8bdc (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

/** complex tests to check the UNO-API
 **/
package complex.unoapi;

// imports
import base.java_complex;
import complexlib.ComplexTestCase;
import helper.OfficeProvider;
import helper.ParameterNotFoundException;
import helper.ProcessHandler;
import com.sun.star.lang.XMultiServiceFactory;
import helper.BuildEnvTools;
import helper.ComplexDescGetter;
import helper.CwsDataExchangeImpl;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import share.DescEntry;
import util.PropertyName;
import util.utils;

/**
 * This Complex Test will test the UNO-API by calling dmake in <B>$MODULE</B>/qa/unoapi<p>
 * This test depends on some requirments:
 * The <B>TestJob</B> is <b>-o complex.unoapi.CheckModuleAPI::module(<CODE>MODULE</CODE>)</b><p> where <CODE>MODULE</CODE>
 * could be the following:<p>
 * <ul>
 *  <li><b>all</b>  iterates over <CODE>SRC_ROOT</CODE> and call <CODE>dmake</CODE> in all qa/unoapi folder</li><p>
 *  <li><b>$module</b>  call dmake in $module/qa/unoapi folder</li><p>
 *  <li><b>$module1,$module2,...</b>    call dmake in $module1/qa/unoapi folder then in $module2/qa/unoapi folder and so on</li><p>
 * </ul><p>
 *
 * Also you have to fill the following parameter:
 * <ul>
 *  <li><b>SHELL</b>:      fill this parameter with a shell</li>
 * </ul>
 *
 */
public class CheckModuleAPI extends ComplexTestCase
{

    private static String mSRC_ROOT = null;
    private static boolean mIsInitialized = false;
    private final static boolean mContinue = true;
    private static BuildEnvTools bet = null;

    /**
     * Initialize the test environment.
     * This method checks for all neccesarry parameter and exit if not all parameter are set.
     *
     * Further this method starts an office instance and gets the office some more time to start. This is because
     * some freshly installed offices don not have such a user tree. The office will create it on its first start,
     * but this will take some time.
     * Note: This funktionality is only reasonable with parameter <CODE>-noOffice true</CODE>
     */
    public void before()
    {

        if (!mIsInitialized)
        {
            mIsInitialized = true;

            try
            {

                bet = new BuildEnvTools(param, log);

            }
            catch (ParameterNotFoundException ex)
            {
                this.failed(ex.toString(), false);
            }

            mSRC_ROOT = bet.getSrcRoot();
        }
    }

    /**
     * let API tests run.
     */
    public void checkModule(String module)
    {
        log.println(utils.getDateTime() + ": start testing module '" + module + "'");

        log.println(utils.getDateTime() + "start new Office instance...");

        // TODO: is Office started with this program?
        final OfficeProvider officeProvider = new OfficeProvider();

        log.println(utils.getDateTime() + "Receiving the ServiceManager of the Office ");
        final XMultiServiceFactory msf = (XMultiServiceFactory) officeProvider.getManager(param);
        assure("couldnot get ServiceFarcotry", msf != null, mContinue);
        param.put("ServiceFactory", msf);

        final String sep = System.getProperty("file.separator");
        final String sUnoapi = getModulePath(module);
        final File fUnoapi = new File(sUnoapi);
        final String sMakeFile = sUnoapi + sep + "makefile.mk";
        final File fMakeFile = new File(sMakeFile);
        assure("ERROR: could not find makefile: '" + sMakeFile + "'", fMakeFile.exists(), mContinue);

        final String[] commands = getDmakeCommands(sUnoapi);

        final ProcessHandler procHdl = bet.runCommandsInEnvironmentShell(commands, fUnoapi, 0);
        log.println("exit code of dmake: " + procHdl.getExitCode());
        String test = procHdl.getOutputText();
        test += procHdl.getErrorText();
        assure("module '" + module + "' failed", verifyOutput(test), mContinue);
        log.println(utils.getDateTime() + " module '" + module + "': kill existing office...");

        // TODO: how to check if the office is not started with this process.
        boolean bNoOffice = param.getBool("NoOffice");
        if (!bNoOffice)
        {
            try
            {
                officeProvider.closeExistingOffice(param, true);
            }
            catch (java.lang.UnsatisfiedLinkError exception)
            {
                log.println("Office seems not to be running");
            }
        }
    }
    private String getQaUnoApiPath(String srcRoot, String _sModul)
    {
        File aFile = new File(srcRoot);
        if (!aFile.exists())
        {
            System.out.println("ERROR: srcRoot '" + srcRoot + "' does not exist.");
            return null;
        }
        String sModulePath = srcRoot;
        sModulePath += File.separator;
        sModulePath += _sModul;

        File aModulePath = new File(sModulePath);
        if (! aModulePath.exists())
        {
            aModulePath = new File(sModulePath + ".lnk");
            if (! aModulePath.exists())
            {
                aModulePath = new File(sModulePath + ".link");
                if (! aModulePath.exists())
                {
                    // module does not exist.
                    return null;
                }
            }
        }
        sModulePath = aModulePath.getAbsolutePath();
        sModulePath += File.separator;
        sModulePath += "qa";
        sModulePath += File.separator;
        sModulePath += "unoapi";
        File aModulePathQaUnoApi = new File(sModulePath);
        if (aModulePathQaUnoApi.exists())
        {
            return aModulePathQaUnoApi.getAbsolutePath();
        }
        return null;
    }
    private boolean doesQaUnoApiFolderExist(String srcRoot, String _sModul)
    {
        if (getQaUnoApiPath(srcRoot, _sModul) != null)
        {
            return true;
        }
        return false;
    }
/*
 private boolean doesQaUnoApiFolderExist(File srcRoot)
    {
        final FolderFilter qaFilter = new FolderFilter("qa");
        final File[] qaTree = srcRoot.listFiles(qaFilter);
        if (qaTree != null)
        {
            for (int j = 0; j < qaTree.length; j++)
            {
                final File qaFolder = qaTree[j];
                final FolderFilter apiFilter = new FolderFilter("unoapi");
                final File[] apiTree = qaFolder.listFiles(apiFilter);
                if (apiTree != null && apiTree.length > 0)
                {
                    return true;
                }
            }
        }
        return false;
    }
*/

    private String[] getAllModuleCommand()
    {
        String[] checkModules;

        final String[] names = getModulesFromSourceRoot();
        checkModules = getCheckModuleCommand(names);

        return checkModules;
    }

    private String[] getCheckModuleCommand(String[] names)
    {
        String[] checkModules;
        checkModules = new String[names.length];

        for (int i = 0; i < names.length; i++)
        {
            // if a module is not added to a cws it contains a dot in its name (forms.lnk)
            if (names[i].indexOf(".") != -1)
            {
                checkModules[i] = "checkModule(" + names[i].substring(0, names[i].indexOf(".")) + ")";
            }
            else
            {
                checkModules[i] = "checkModule(" + names[i] + ")";
            }
        }
        return checkModules;
    }

    private String[] getDmakeCommands(String sUnoapi)
    {

        String[] cmdLines = null;
        final String platform = (String) param.get(PropertyName.OPERATING_SYSTEM);
        log.println("prepare command for platform " + platform);

        if (platform.equals(PropertyName.WNTMSCI))
        {
            if (param.getBool(PropertyName.CYGWIN))
            {
                // cygwin stuff
                cmdLines = new String[]
                        {
                            "cd `cygpath '" + sUnoapi.replaceAll("\\\\", "\\\\\\\\") + "'`",
                            "dmake"
                        };
            }
            else
            {
                // old 4NT
                cmdLines = new String[]
                        {
                            "cdd " + sUnoapi,
                            "dmake"
                        };
            }
        }
        else
        {
            // unix
            cmdLines = new String[]
                    {
                        "cd " + sUnoapi,
                        "dmake"
                    };
        }
        return cmdLines;
    }

    private String[] getCwsModuleCommand()
    {
        String[] checkModules;
        final String version = (String) param.get(PropertyName.VERSION);
        String[] names = null;
        if (version.startsWith("cws_"))
        {
            try
            {
                // cws version: all added modules must be tested
                final String cws = version.substring(4, version.length());
                final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(param, log);
                final ArrayList<String> addedModules = cde.getModules();

                final ArrayList<String> moduleNames = new ArrayList<String>();
                Iterator<String> iterator = addedModules.iterator();
                while (iterator.hasNext())
                {
                    String sModuleName = iterator.next();
                    if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
                    {
                        moduleNames.add(sModuleName);
                    }
                }
                names = moduleNames.toArray(new String[0]);
            }
            catch (ParameterNotFoundException ex)
            {
                this.failed(ex.toString(), false);
            }


        }
        else
        {
            // major version: all modules must be tested
            names = getModulesFromSourceRoot();
        }
        checkModules = getCheckModuleCommand(names);

        return checkModules;
    }

    private String[] getDefinedModuleCommand(String module)
    {
        String[] checkModules = null;
        // list of modules to test: (sw,sc,sd)
        if (module.indexOf(",") != -1)
        {
            final String[] names = module.split(",");
            checkModules = new String[names.length];
            for (int i = 0; i < names.length; i++)
            {
                final String moduleName = names[i].trim();

                if (doesQaUnoApiFolderExist(mSRC_ROOT, moduleName))
                {
                    checkModules[i] = "checkModule(" + moduleName + ")";
                }
            }
        }
        else
        {
            if (doesQaUnoApiFolderExist(mSRC_ROOT, module))
            {
                checkModules = new String[]
                        {
                            "checkModule(" + module + ")"
                        };
            }
        }
        return checkModules;
    }

    private String getModulePath(String module)
    {
        String sUnoapi = getQaUnoApiPath(mSRC_ROOT, module);
        return sUnoapi;
    }

    /**
    Some modules contains more the one project. This methods translates given project parameter to the
     *  correspind module name.
     *
     * fwk -> framework
     * fwl -> framework
     * sch -> chart2
     * lnn -> lingu
     * lng -> linguistic
     * sfx -> sfx2
     * sm -> starmath
     */
    private String getTranslatedNames(String module)
    {

        final HashMap<String,String> aModuleHashMap = new HashMap<String,String>();

        aModuleHashMap.put("fwk", "framework");
        aModuleHashMap.put("fwl", "framework");
        aModuleHashMap.put("sch", "chart2");
        aModuleHashMap.put("lnn", "lingu");
        aModuleHashMap.put("lng", "linguistic");
        aModuleHashMap.put("sfx", "sfx2");
        aModuleHashMap.put("sm", "starmath");

        // it could the that the parameter looks like "fwk,fwl". This results in double "famework,framework".
        // The following loop correct this to only one "framework"

        final Set<String> keys = aModuleHashMap.keySet();
        final Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext())
        {

            final String key = iterator.next();
            final String value = aModuleHashMap.get(key);

            module = module.replaceAll(key, value);

            final int count = module.split(value).length;
            if (count > 2)
            {
                for (int i = 2; i < count; i++)
                {
                    module.replaceFirst("," + value, "");
                }

            }
        }
        return module;
    }

    private boolean verifyOutput(String output)
    {

        log.println("verify output...");
        boolean ok = false;
        final String[] outs = output.split("\n");

        for (int i = 0; i < outs.length; i++)
        {
            final String line = outs[i];
            if (line.matches("[0-9]+? of [0-9]+? tests failed"))
            {
                log.println("mached line: " + line);
                if (line.matches("0 of [0-9]+? tests failed"))
                {
                    ok = true;
                    log.println("Module passed OK");
                }
                else
                {
                    log.println("Module passed FAILED");
                }
            }
        }

        if (!ok)
        {
            log.println("ERROR: could not find '0 of [0-9]+? tests failed' in output");
        }

        return ok;
    }

    private String[] getModulesFromSourceRoot()
    {
        log.println("**** run module tests over all modules ****");

        log.println("search for qa/unoapi foldres in all modules based in ");
        log.println("'" + mSRC_ROOT + "'");

        final ArrayList<String> moduleNames = new ArrayList<String>();
        final File sourceRoot = new File(mSRC_ROOT);
        final File[] sourceTree = sourceRoot.listFiles();

        for (int i = 0; i < sourceTree.length; i++)
        {
            final File moduleName = sourceTree[i];
            String sModuleName = moduleName.getName(); // (String)moduleNames.get(i);
            if (doesQaUnoApiFolderExist(mSRC_ROOT, sModuleName))
            {
                moduleNames.add(sModuleName);
            }
        }

        final String[] names = moduleNames.toArray(new String[0]);
        return names;
    }

    /**
     * This function generates a list of modules to test and call <CODE>checkModule</CODE> for every module.
     * <p>
     *
     * @param module names to test. This could be
     * <ul>
     * <li>a comma separated list of modules like 'sw,sc,sd'</li>
     * <li>'all' to test all modules </li>
     * <li>'auto' to check only modules which are added to the ChildWorkSpace</li>
     * </ul>
     */
    public void module(String module)
    {

        String[] checkModules;
        final ComplexDescGetter desc = new ComplexDescGetter();
        DescEntry entry = null;
        module = module.trim();

        /*
        all: check all modules which contains a qa/unoapi folder
        auto: check all modules which contains a qa/unoapi folder except the module is not added
         */
        if (module.equals("all"))
        {
            checkModules = getAllModuleCommand();
        }
        else if (module.equals("auto"))
        {
            checkModules = getCwsModuleCommand();
        }
        else
        {
            module = getTranslatedNames(module);
            checkModules = getDefinedModuleCommand(module);
        }

        if (checkModules != null && checkModules.length > 0)
        {

            entry = desc.createTestDesc("complex.unoapi.CheckModuleAPI", "complex.unoapi.CheckModuleAPI", checkModules,
                    log);

            final java_complex complex = new java_complex();

            log.println("********** start test *************");
            final boolean result = complex.executeTest(param, new DescEntry[] { entry });
            log.println("********** end test *************");

            assure("CheckModuleAPI.module(" + module + ") PASSED.FAILED", result);

        }
        else
        {
            log.println("No modules containing qa/unoapi folder found => OK");
            state = true;
        }

        setUnoApiCwsStatus(state);

    }

    private void setUnoApiCwsStatus(boolean status)
    {

        if (!param.getBool(PropertyName.NO_CWS_ATTACH))
        {

            final String version = (String) param.get(PropertyName.VERSION);
            if (version.startsWith("cws_"))
            {
                try
                {

                    // cws version: all added modules must be tested
                    final String cws = version.substring(4, version.length());
                    final CwsDataExchangeImpl cde = new CwsDataExchangeImpl(param, log);
                    cde.setUnoApiCwsStatus(status);
                }
                catch (ParameterNotFoundException ex)
                {
                    log.println("ERROR: could not wirte status to EIS database: " + ex.toString());
                }
            }
        }
    }

    @Override
    public String[] getTestMethodNames()
    {
        return new String[]
                {
                    "module(all)"
                };
    }

}