summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/automatedtests/util/DebugId.java
blob: bcf9453109ccc79340dcd0d4ee125de53da24d98 (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
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.automatedtests.util;

import java.io.Serializable;
import java.util.HashMap;

import com.itmill.toolkit.ui.Component;

public class DebugId implements Serializable {

    private static HashMap debugIds = new HashMap();

    /**
     * Generate static debug id based on package and component type. If
     * duplicate package, component type then number of instances count is
     * appended to debugId.
     * 
     * @param c
     */
    public static void set(Component c, String description) {
        String debugId = "";

        // add package name
        StackTraceElement[] st = new Throwable().fillInStackTrace()
                .getStackTrace();
        try {
            debugId += st[3].getClassName();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // add component type
        debugId += c.getClass();

        // add given description
        debugId += description;

        if (debugIds.containsKey(debugId)) {
            int count = ((Integer) debugIds.get(debugId)).intValue();
            count++;
            debugIds.put(debugId, new Integer(count));
            debugId = debugId + "-" + count;
        }

        c.setDebugId(debugId);
    }
}