/** Tests the gauge accessor methods. */
  public void testAccessors() {
    Gauge gauge = new Gauge(null, false, POSITIVE_INT, 0);

    assertTrue("Should be noninteractive", !gauge.isInteractive());
    assertEquals("Maxvalues don't match", POSITIVE_INT, gauge.getMaxValue());
    assertEquals("Labels don't match", null, gauge.getLabel());

    gauge.setLabel(label);
    assertEquals("Labels don't match", label, gauge.getLabel());

    gauge.setValue(NEGATIVE_INT);
    assertEquals("Values mismatch", 0, gauge.getValue());

    gauge.setValue(POSITIVE_INT * 2);
    assertEquals("Values mismatch", POSITIVE_INT, gauge.getValue());

    gauge.setValue(0);
    try {
      gauge.setMaxValue(NEGATIVE_INT);
      fail("1. IllegalArgumentException expected");
    } catch (IllegalArgumentException iae) {
    }

    gauge.setMaxValue(Gauge.INDEFINITE);

    try {
      gauge.setValue(NEGATIVE_INT);
      fail("2. IllegalArgumentException expected");
    } catch (IllegalArgumentException iae) {
    }

    try {
      gauge.setValue(Gauge.CONTINUOUS_IDLE);
      gauge.setValue(Gauge.INCREMENTAL_IDLE);
      gauge.setValue(Gauge.CONTINUOUS_RUNNING);
      gauge.setValue(Gauge.INCREMENTAL_UPDATING);
    } catch (IllegalArgumentException iae) {
      fail("3. IllegalArgumentException thrown " + iae.getMessage());
    }

    try {
      gauge.setValue(POSITIVE_INT);
      fail("4. IllegalArgumentException expected");
    } catch (IllegalArgumentException iae) {
    }

    // Test minimum bounds, non-interactive:
    gauge.setLabel(null);
    assertTrue("Minimum height was zero or negative, case 1.", gauge.getMinimumHeight() > 0);
    // print("non-interactive minimum height: "
    //        + gauge.getMinimumHeight());
    assertTrue("Minimum width was zero or negative, case 1.", gauge.getMinimumWidth() > 0);
    // print("non-interactive minimum width: "
    //        + gauge.getMinimumWidth());

    // Minimum bounds, interactive:
    Gauge gauge2 = new Gauge(null, true, POSITIVE_INT, 0);
    assertTrue("Minimum height was zero or negative, case 2.", gauge2.getMinimumHeight() > 0);
    // print("interactive minimum height: "
    //        + gauge2.getMinimumHeight());
    assertTrue("Minimum width was zero or negative, case 2.", gauge2.getMinimumWidth() > 0);
    // print("interactive minimum width: "
    //        + gauge2.getMinimumWidth());

    // Minimum bounds, non-interactive with label:
    Gauge gauge3 = new Gauge("label", false, POSITIVE_INT, 0);
    assertTrue("Minimum height was zero or negative, case 3.", gauge3.getMinimumHeight() > 0);
    // print("labeled non-interactive minimum height: "
    //        + gauge3.getMinimumHeight());
    assertTrue("Minimum width was zero or negative, case 3.", gauge3.getMinimumWidth() > 0);
    // print("labeled non-interactive minimum width: "
    //        + gauge3.getMinimumWidth());

    // Minimum bounds, interactive with label:
    Gauge gauge4 = new Gauge("label", true, POSITIVE_INT, 0);
    assertTrue("Minimum height was zero or negative, case 4.", gauge4.getMinimumHeight() > 0);
    // print("labeled interactive minimum height: "
    //        + gauge4.getMinimumHeight());
    assertTrue("Minimum width was zero or negative, case 4.", gauge4.getMinimumWidth() > 0);
    // print("labeled interactive minimum width: "
    //        + gauge4.getMinimumWidth());
  }
示例#2
0
  private void fromSpecification() {
    // String caName = new String("cn=ca_name,ou=ou_name,o=org_name,c=ie");
    String caName = "c=US,st=CA,l=Santa Clara,o=dummy CA,ou=JCT,cn=thehost";
    String[] caNames = new String[1];
    String stringToSign = new String("JSR 177 Approved");
    String userPrompt =
        new String(
            "Please insert the security element "
                + "issued by bank ABC"
                + "for the application XYZ.");
    byte[] byteArrayToSign = new byte[8];
    byte[] authSignature;
    byte[] signSignature;

    caNames[0] = caName;

    try {
      // Generate a formatted authentication signature that includes the
      // content that was signed in addition to the certificate.
      // Selection of the key is implicit in selection of the certificate,
      // which is selected through the caNames parameter.
      // If the appropriate key is not found in any of the security
      // elements present in the device, the implementation may guide
      // the user to insert an alternative security element using
      // the securityElementPrompt parameter.
      authSignature =
          CMSMessageSignatureService.authenticate(
              byteArrayToSign,
              CMSMessageSignatureService.SIG_INCLUDE_CERTIFICATE
                  | CMSMessageSignatureService.SIG_INCLUDE_CONTENT,
              caNames,
              userPrompt);

      // Generate a formatted signature that includes the
      // content that was signed in addition to the certificate.
      // Selection of the key is implicit in selection of the certificate,
      // which is selected through the caNames parameter.
      // If the appropriate key is not found in any of the
      // security elements present in the device, the implementation
      // may guide the user to insert an alternative
      // security element using the securityElementPrompt parameter.
      signSignature =
          CMSMessageSignatureService.sign(
              stringToSign,
              CMSMessageSignatureService.SIG_INCLUDE_CERTIFICATE
                  | CMSMessageSignatureService.SIG_INCLUDE_CONTENT,
              caNames,
              userPrompt);
    } catch (IllegalArgumentException iae) {
      // Perform error handling
      iae.printStackTrace();
    } catch (CMSMessageSignatureServiceException ce) {
      if (ce.getReason() == ce.CRYPTO_FORMAT_ERROR) {
        System.out.println("Error formatting signature.");
      } else {
        System.out.println(ce.getMessage());
      }
    } catch (Exception e) {
      System.out.println("Other exception: " + e);
    }
  }