Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / qadevOOo / runner / stats / OutProducerFactory.java
blobcb907a4903c0567b21c111401ec0b21c2133471e
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package stats;
20 import share.LogWriter;
21 import java.util.HashMap;
22 import util.DynamicClassLoader;
24 /**
25 * A factory class for creating out producers.
27 public class OutProducerFactory {
29 /**
30 * Create an out producer. The type that is created depends on the
31 * parameters given. These are:
32 * <ul>
33 * <li>DataBaseOut - If set to true, a database outproducer is created.
34 * <li>OutProducer - The value of this parameter names the class that is created.
35 * </ul>
36 * @param param Parameters of the test.
37 * @return The created out producer.
39 public static LogWriter createOutProducer(HashMap<String,Object> param) {
40 LogWriter dbOut = null;
41 boolean getDatabase = convertToBool(param.get("DataBaseOut"));
42 if (getDatabase) {
43 dbOut = createDataBaseOutProducer(param);
45 if (dbOut == null) {
46 DynamicClassLoader dcl = new DynamicClassLoader();
47 String outProducerName = (String)param.get("OutProducer");
48 if (outProducerName != null) {
49 try {
50 dbOut = (LogWriter)dcl.getInstance(outProducerName);
52 catch(IllegalArgumentException e) {
53 e.printStackTrace();
57 if (dbOut == null) {
58 dbOut = createSimpleOutProducer();
60 return dbOut;
63 /**
64 * Create a database out producer.
65 * @param param The test parameters
66 * @return The database out producer, or null if it couldn't be created.
68 private static LogWriter createDataBaseOutProducer(HashMap<String,Object> param) {
69 String dataProducerName = (String)param.get("DataBaseOutProducer");
70 if (dataProducerName == null) {
71 String testBaseName = (String)param.get("TestBase");
72 dataProducerName = testBaseName.substring(testBaseName.indexOf('_')+1);
73 dataProducerName = "stats." + makeFirstCharUpperCase(dataProducerName)
74 + "DataBaseOutProducer";
76 DynamicClassLoader dcl = new DynamicClassLoader();
77 LogWriter dbOut = null;
78 try {
79 dbOut = (LogWriter)dcl.getInstance(dataProducerName,
80 new Class[]{HashMap.class}, new Object[]{param});
82 catch(IllegalArgumentException e) {
83 e.printStackTrace();
85 return dbOut;
88 /**
89 * As a fallback, create a simple out producer, if all else failed.
90 * @return A simple out producer, writing to the screen.
92 private static LogWriter createSimpleOutProducer() {
93 return new SimpleOutProducer();
96 private static boolean convertToBool(Object val) {
97 if(val != null) {
98 if ( val instanceof String ) {
99 String sVal = (String)val;
100 if ( sVal.equalsIgnoreCase("true") || sVal.equalsIgnoreCase("yes") ) {
101 return true;
105 else if (val instanceof Boolean) {
106 return ((Boolean)val).booleanValue();
109 return false;
113 * Make the first character to an upper case char.
114 * @param name The String to change
115 * @return The String with an upper case first char.
117 private static String makeFirstCharUpperCase(String name) {
118 return name.substring(0,1).toUpperCase() + name.substring(1);