Example #1
0
 protected String getHostIp() {
   try {
     IPAddr localHost = IPAddr.getLocalHost();
     return localHost.getHostAddress();
   } catch (UnknownHostException e) {
     log.error("getHostIp()", e);
     return "1.1.1.1";
   }
 }
 String getLocalIPAddr() {
   if (localAddr == null) {
     try {
       IPAddr localHost = IPAddr.getLocalHost();
       localAddr = localHost.getHostAddress();
     } catch (UnknownHostException e) {
       // shouldn't happen
       log.error("LockssServlet: getLocalHost: " + e.toString());
       return "???";
     }
   }
   return localAddr;
 }
  // Tests that don't need a servlet environment
  public void testConvertDisplayString() throws Exception {
    // test null
    Object testObj = null;
    assertEquals("", format(testObj, ColumnDescriptor.TYPE_STRING));

    // test standard numbers
    testObj = new Integer(123);
    assertEquals("123", format(testObj, ColumnDescriptor.TYPE_INT));
    testObj = new Float(123321);
    assertEquals(testObj.toString(), format(testObj, ColumnDescriptor.TYPE_FLOAT));

    // check proper 'big int' formatting
    testObj = new Long(12345678);
    assertEquals("12,345,678", format(testObj, ColumnDescriptor.TYPE_INT));

    // test string
    testObj = "test string";
    assertEquals("test string", format(testObj, ColumnDescriptor.TYPE_STRING));

    // Issue 1901: verify that there is no encoding bias
    testObj = "<>&'\"\n";
    String res = format(testObj, ColumnDescriptor.TYPE_STRING);
    assertEquals(
        "Expected \""
            + StringEscapeUtils.escapeJava(testObj.toString())
            + "\" but got \""
            + StringEscapeUtils.escapeJava(res)
            + "\"; encoding bias?",
        "<>&'\"\n",
        res);

    // test percentage
    testObj = new Double(.453);
    assertEquals("45%", format(testObj, ColumnDescriptor.TYPE_PERCENT));

    // test agreement
    testObj = new Double(.453);
    assertEquals("45.30%", format(testObj, ColumnDescriptor.TYPE_AGREEMENT));
    testObj = new Double(.999999);
    assertEquals("99.99%", format(testObj, ColumnDescriptor.TYPE_AGREEMENT));

    // test date
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2004);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DATE, 1);
    cal.set(Calendar.HOUR_OF_DAY, 15);
    cal.set(Calendar.MINUTE, 15);
    testObj = cal.getTime();
    assertEquals(
        new DisplayConverter().getTableDateFormat().format(testObj),
        format(testObj, ColumnDescriptor.TYPE_DATE));

    // test IPAddr
    testObj = IPAddr.getLocalHost();
    assertEquals(
        IPAddr.getLocalHost().getHostAddress(), format(testObj, ColumnDescriptor.TYPE_IP_ADDRESS));

    // test time interval
    long timeInt = Constants.HOUR + Constants.MINUTE;
    testObj = new Long(timeInt);
    assertEquals(
        StringUtil.timeIntervalToString(timeInt),
        format(testObj, ColumnDescriptor.TYPE_TIME_INTERVAL));

    // test unknown
    testObj = "unknown string";
    assertEquals("unknown string", format(testObj, -1));
  }