Clover coverage report - CRISPY - 1.1.1
Coverage timestamp: Mi Nov 15 2006 13:09:46 CET
file stats: LOC: 408   Methods: 20
NCLOC: 282   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Util.java 97,5% 93,8% 95% 95,1%
coverage coverage
 1    /*
 2    * Created on 08.03.2005
 3    *
 4    */
 5    package net.sf.crispy.util;
 6   
 7    import java.io.FileInputStream;
 8    import java.io.FileNotFoundException;
 9    import java.io.IOException;
 10    import java.io.InputStream;
 11    import java.net.InetAddress;
 12    import java.net.Socket;
 13    import java.util.Collection;
 14    import java.util.Date;
 15    import java.util.Enumeration;
 16    import java.util.Iterator;
 17    import java.util.Map;
 18    import java.util.Properties;
 19    import java.util.TreeMap;
 20    import java.util.Map.Entry;
 21    import java.util.logging.LogManager;
 22    import java.util.logging.Logger;
 23   
 24    import javax.swing.JFrame;
 25    import javax.swing.JScrollPane;
 26    import javax.swing.JTree;
 27    import javax.swing.tree.DefaultMutableTreeNode;
 28    import javax.xml.namespace.QName;
 29   
 30    import net.sf.crispy.InvocationException;
 31   
 32    /**
 33    * Utility (helper) class.
 34    *
 35    * @author Linke
 36    *
 37    */
 38    public class Util {
 39   
 40    /**
 41    * Find the deepest Exception by nested Exception.
 42    * Example:
 43    *
 44    * <pre>
 45    * Exception e0 = new Exception ();
 46    * Exception e1 = new Exception (e0);
 47    *
 48    * Exception eFind = findDeepestThrowable(e1);
 49    * The result: eFind == e0
 50    * </pre>
 51    *
 52    * @param pvThrowable Root-Exception.
 53    * @return The deepest Exception.
 54    */
 55  59 public static Throwable findDeepestThrowable(Throwable pvThrowable) {
 56  59 if (pvThrowable.getCause() != null) {
 57  22 Throwable lvThrowable = pvThrowable.getCause();
 58  22 return findDeepestThrowable(lvThrowable);
 59    } else {
 60  37 return pvThrowable;
 61    }
 62    }
 63   
 64    /**
 65    * Test to free port.
 66    *
 67    * @param pvPort tested port
 68    * @return <code>true</code>, if the port is free, else <code>false</code>.
 69    */
 70  23 public static boolean isPortFree (int pvPort) {
 71  23 try {
 72  23 InetAddress lvAddress = InetAddress.getLocalHost();
 73  23 Socket lvSocket = new Socket(lvAddress, pvPort);
 74  3 lvSocket.isBound();
 75  3 return false;
 76    } catch (Exception e) {
 77  20 return true;
 78    }
 79    }
 80   
 81    /**
 82    * Separate from url the url and the port: <code>localhost:8080 --> http://localhost and 8080</code>
 83    * @param pvUrlAndPort
 84    * @return String-Array with pos 0 = URL and pos 1 = the Port
 85    */
 86  5 public static String[] separateHostAndPort (String pvUrlAndPort) {
 87  5 if (pvUrlAndPort == null) {
 88  1 return null;
 89    } else {
 90  4 String s[] = pvUrlAndPort.split(":");
 91  4 if (s.length != 2) {
 92  1 throw new IllegalArgumentException(pvUrlAndPort + " is a not separatable URL. Expected a String with two ':', how by http://localhost:8080.");
 93    }
 94  3 String lvUrl = s[0];
 95  3 String lvPort = s[1];
 96    // test whether port is a integer
 97  3 Integer.parseInt(lvPort);
 98  2 s = new String[] {lvUrl, lvPort};
 99  2 return s;
 100    }
 101    }
 102   
 103    /**
 104    * Cut a String on the end of a input-String.
 105    * Example: Input-String <code>rmi://localhost:1099/</code>, Cutting-String <code>/</code>, New-String <code>rmi://localhost:1099</code>.
 106    *
 107    * @param pvString Input-String
 108    * @param pvCuttingString Cutting-String
 109    * @return New String, without Cutting-String.
 110    */
 111  149 public static String cutString(final String pvString, final String pvCuttingString) {
 112  149 if ((pvString != null) && (pvCuttingString != null) && (pvString.endsWith(pvCuttingString))) {
 113  1 return pvString.substring(0, (pvString.length() - pvCuttingString.length()));
 114    } else {
 115  148 return pvString;
 116    }
 117   
 118    }
 119   
 120    /**
 121    * Create a object with the reflection API.
 122    *
 123    * @param pvObjectString String from the object.
 124    * @return Created object.
 125    * @throws Exception
 126    */
 127  1279 public static Object createObject (String pvObjectString) throws Exception {
 128  1279 if (pvObjectString == null) {
 129  473 return null;
 130    } else {
 131  806 Class clazz = Class.forName(pvObjectString);
 132  797 return clazz.newInstance();
 133    }
 134    }
 135   
 136    /**
 137    * Test as search class is exist in the class array.
 138    *
 139    * @param pvClassArray class array
 140    * @param pvSearchClass search class
 141    * @return if the class is finding than <code>true</code>, else <code>false</code>
 142    */
 143  5 public static boolean isClassInArray (Class[] pvClassArray, Class pvSearchClass) {
 144  3 if ((pvClassArray == null) || (pvSearchClass == null)) { return false; }
 145  2 for (int i = 0; i < pvClassArray.length; i++) {
 146  4 boolean equals = pvClassArray[i].equals(pvSearchClass);
 147  1 if (equals == true) { return true; }
 148    }
 149  1 return false;
 150    }
 151   
 152  2 public static String printArray(Object[] pvArray) {
 153  2 StringBuffer sb = new StringBuffer();
 154  2 if (pvArray == null) {
 155  1 return "Array is NULL";
 156    } else {
 157  1 for (int i = 0; i < pvArray.length; i++) {
 158  1 sb.append(pvArray[i]).append(" -> ").append(pvArray[i].getClass().getName()).append("\n");
 159    }
 160  1 return sb.toString();
 161    }
 162    }
 163   
 164  3 public static String array2String (Object pvStrArray[]) {
 165  1 if (pvStrArray == null) { return null; }
 166  2 StringBuffer ret = new StringBuffer();
 167  2 for (int i = 0; i < pvStrArray.length; i++) {
 168  4 ret.append(pvStrArray[i]).append(" ; ");
 169    }
 170  2 return ret.toString();
 171    }
 172   
 173   
 174  2 public static void printMapInfo(Map pvMap) {
 175  2 if (pvMap == null) {
 176  1 System.out.println("Util.printMapInfo: Map ist null");
 177    } else {
 178  1 Iterator it = pvMap.entrySet().iterator();
 179  1 while (it.hasNext()) {
 180  1 Map.Entry lvMapEntry = (Entry) it.next();
 181  1 System.out.println(lvMapEntry.getKey() + " = " + lvMapEntry.getValue() + " (" + lvMapEntry.getValue().getClass().getName() + ")");
 182    }
 183    }
 184    }
 185   
 186  2 public static String getClassNameWithOutPackage(final Class pvClass) {
 187  2 if (pvClass == null) {
 188  1 return null;
 189    } else {
 190  1 return getClassNameWithOutPackage(pvClass.getName());
 191    }
 192    }
 193   
 194  13 public static String getClassNameWithOutPackage(final String pvClassName) {
 195  13 if (pvClassName == null) {
 196  1 return null;
 197    } else {
 198  12 String lvNames[] = pvClassName.split("\\.");
 199  12 return lvNames[(lvNames.length - 1)];
 200    }
 201    }
 202   
 203    /**
 204    * Reverse the package fro class. For example:
 205    * <code>test.crispy.example.model.Kunde</code> -> <code>model.example.crispy.test</code>.
 206    * By a class without a package, is the return value a empty String.
 207    *
 208    * @param pvClass
 209    * @return reverse string.
 210    */
 211  54 public static String reverse(Class pvClass) {
 212  1 if (pvClass == null) { return null; }
 213  53 String lvClassName = pvClass.getName();
 214  53 String lvParts[] = lvClassName.split("\\.");
 215  53 String lvRet = "";
 216  53 int l = lvParts.length -1;
 217  53 for (int i = l; i > 0; i--) {
 218  178 lvRet = lvRet + lvParts[i-1] + ".";
 219    }
 220  53 lvRet = lvRet.substring(0, (lvRet.length() - 1));
 221  53 return lvRet;
 222    }
 223   
 224    /**
 225    * Create a <code>javax.xml.namespace.QName</code> by the class. For example
 226    * class: <code>test.crispy.example.model.Kunde</code> ->
 227    * LocalPart: <code>test.crispy.example.model.Kunde</code> and
 228    * NamespaceURI: <code>model.example.crispy.test</code>.
 229    *
 230    * @param pvClass The class for create a QName.
 231    * @return The created QName object.
 232    */
 233  53 public static QName createQNameByClass(Class pvClass) {
 234  1 if (pvClass == null) { return null; }
 235  52 return new QName(Util.reverse(pvClass), pvClass.getName());
 236    }
 237   
 238    /**
 239    * Get from the properites all properties, that begins with the prefix and sorted this
 240    * properties to the property keys.
 241    *
 242    * @param pvProperties all properties
 243    * @param pvPrefix filter = prefix from property key
 244    * @return sorted Map from all properties with correct prefix.
 245    */
 246  451 public static Map getAllPropertiesByPrefixAndSort(Properties pvProperties, String pvPrefix) {
 247  451 Enumeration enumeration = pvProperties.keys();
 248  451 Map lvSorterMap = new TreeMap();
 249  451 while (enumeration.hasMoreElements()) {
 250  1327 Object lvPropertyObject = enumeration.nextElement();
 251  1327 if ( ! (lvPropertyObject.getClass().getName().equals(String.class.getName()))) {
 252  1 throw new InvocationException("The property must be a String, but is: "
 253    + lvPropertyObject.getClass().getName()
 254    + " --> with value (key): " + lvPropertyObject);
 255    }
 256  1326 String lvKey = (String) lvPropertyObject;
 257   
 258  1326 if (lvKey.startsWith(pvPrefix)) {
 259  226 String lvProxyInterceptorStr = pvProperties.getProperty(lvKey);
 260  226 lvSorterMap.put(lvKey, lvProxyInterceptorStr);
 261    }
 262    }
 263  450 return lvSorterMap;
 264    }
 265   
 266   
 267  5 public static byte[] inputStream2ByteArray(InputStream is) throws IOException {
 268  5 int BUFFER_SIZE = 8192;
 269  5 byte[] buffer = new byte[BUFFER_SIZE];
 270  5 byte[] content = new byte[0];
 271  5 int length;
 272  ? while ((length = is.read(buffer)) > 0) {
 273  5 byte[] tmp = content;
 274  5 content = new byte[tmp.length + length];
 275  5 System.arraycopy(tmp, 0, content, 0, tmp.length);
 276  5 System.arraycopy(buffer, 0, content, tmp.length, length);
 277    }
 278  5 return content;
 279    }
 280   
 281   
 282  0 public static void visualisationTree(Object pvObject) {
 283  0 DefaultMutableTreeNode lvRoot = new DefaultMutableTreeNode("ROOT");
 284  0 createTree(pvObject, lvRoot);
 285  0 JFrame lvFrame = new JFrame("Crispy tree visualisation ...");
 286  0 JTree lvTree = new JTree(lvRoot);
 287  0 lvFrame.getContentPane().add(new JScrollPane(lvTree));
 288  0 lvFrame.setVisible(true);
 289  0 lvFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 290  0 lvFrame.show();
 291  0 lvFrame.pack();
 292   
 293    }
 294   
 295   
 296  7 public static void createTree (Object pvObject, DefaultMutableTreeNode pvRoot) {
 297  7 if (pvObject != null) {
 298  6 Class clazz = pvObject.getClass();
 299  6 if (pvObject instanceof Collection) {
 300  1 createTreeByCollection((Collection) pvObject, pvRoot, null);
 301    }
 302  5 else if (pvObject instanceof Map) {
 303  3 createTreeByMap((Map) pvObject, pvRoot, null);
 304    }
 305  2 else if ((clazz.equals(Integer.class))
 306    || (clazz.equals(Double.class))
 307    || (clazz.equals(Boolean.class))
 308    || (clazz.equals(String.class))
 309    || (clazz.equals(Date.class))) {
 310  1 pvRoot.add(new DefaultMutableTreeNode (pvObject));
 311    }
 312    else {
 313  1 pvRoot.add(new DefaultMutableTreeNode ("-- Ungueltiger Typ: " + clazz + ": " + pvObject));
 314    }
 315    }
 316    }
 317   
 318  11 public static String map2String (Map pvMap, String pvPropertyName) {
 319  1 if (pvMap == null) { return null; }
 320  10 StringBuffer sb = null;
 321  10 if (pvPropertyName != null) {
 322  2 sb = new StringBuffer(pvPropertyName + ": ");
 323    } else {
 324  8 sb = new StringBuffer();
 325    }
 326  10 StringBuffer sbClazz = new StringBuffer();
 327   
 328  10 Iterator it = pvMap.entrySet().iterator();
 329  10 while (it.hasNext()) {
 330  19 Map.Entry lvMapEntry = (Entry) it.next();
 331  19 Object key = lvMapEntry.getKey();
 332  19 Object value = lvMapEntry.getValue();
 333    // if ((value instanceof Collection) || (value instanceof Map)) {
 334   
 335    // mache nichts
 336    // } else {
 337    // ???????????
 338  19 if (!(value instanceof Collection) && !(value instanceof Map)) {
 339  13 if (key.toString().compareTo("class") == 0) {
 340  2 sbClazz.append(" -> (").append(key).append(" = ").append(value).append(")");
 341    } else {
 342  11 sb.append("[").append(key).append(" = ").append(value).append("] ");
 343    }
 344    }
 345    }
 346  10 return sb.append(sbClazz).toString();
 347    }
 348   
 349  7 public static void createTreeByMap (Map pvMap, DefaultMutableTreeNode pvRoot, String pvPropertyName) {
 350  7 if (pvMap != null) {
 351  6 DefaultMutableTreeNode child = new DefaultMutableTreeNode (map2String(pvMap, pvPropertyName));
 352  6 pvRoot.add(child);
 353   
 354  6 Iterator it = pvMap.entrySet().iterator();
 355  6 while (it.hasNext()) {
 356  9 Map.Entry lvMapEntry = (Entry) it.next();
 357  9 if (lvMapEntry.getValue() instanceof Collection) {
 358  3 createTreeByCollection((Collection) lvMapEntry.getValue(), child, lvMapEntry.getKey().toString());
 359    }
 360  6 else if (lvMapEntry.getValue() instanceof Map) {
 361  2 createTreeByMap((Map) lvMapEntry.getValue(), child, lvMapEntry.getKey().toString());
 362    }
 363    }
 364    }
 365    }
 366   
 367  8 public static void createTreeByCollection (Collection pvCollection, DefaultMutableTreeNode pvRoot, String pvPropertyName) {
 368  8 if ((pvCollection != null) && (pvCollection.size() > 0)) {
 369  3 Iterator it = pvCollection.iterator();
 370  3 while (it.hasNext()) {
 371  6 Object value = it.next();
 372  6 if (value instanceof Collection) {
 373  1 createTreeByCollection((Collection) value, pvRoot, pvPropertyName);
 374    }
 375  5 else if (value instanceof Map) {
 376  1 createTreeByMap((Map) value, pvRoot, pvPropertyName);
 377    }
 378    }
 379    }
 380    }
 381   
 382   
 383    public static boolean initJdkLogger () {
 384    return initJdkLogger2 (Util.class.getResourceAsStream("jdk14-logging.properties"));
 385    }
 386   
 387    public static boolean initJdkLogger (String pvFileName) {
 388    try {
 389    return initJdkLogger2 (new FileInputStream(pvFileName));
 390    } catch (FileNotFoundException e) {
 391    e.printStackTrace();
 392    }
 393    return false;
 394    }
 395   
 396    public static boolean initJdkLogger2 (InputStream pvInputStream) {
 397    try {
 398    LogManager.getLogManager().readConfiguration(pvInputStream);
 399    Logger logger = Logger.global;
 400    LogManager.getLogManager().addLogger(logger);
 401    logger.finest("Jdk14Logger initialisiert ...");
 402    return true;
 403    } catch (Exception e) {
 404    e.printStackTrace();
 405    }
 406    return false;
 407    }
 408    }