// ToDo Convert to parametric test
  private static boolean testCase(String inFile, String expectedFile) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(inFile));

    int N = Integer.parseInt(in.readLine().trim());

    int[] a = new int[N];

    for (int i = 0; i < N; i++) {
      a[i] = Integer.parseInt(in.readLine().trim());
    }

    BufferedReader expected = new BufferedReader(new FileReader(expectedFile));
    boolean error = false;

    final String filename = new File(inFile).getName();
    try {
      final int maxSum = Integer.parseInt(expected.readLine());
      final int nonCont = Integer.parseInt(expected.readLine());
      assertEquals("Error in file " + filename + ",", new Response(maxSum, nonCont), maxsum(a, N));
    } catch (AssertionError e) {
      System.out.print(HEADER);
      e.printStackTrace(System.out);
      error = true;
    }

    return error;
  }
  public static void main(String[] args) {
    int success = 0;
    int failure = 0;
    int error = 0;
    try {
      Method[] methods = TestGregorianCalendar.class.getDeclaredMethods();
      for (Method method : methods) {
        if (Modifier.isStatic(method.getModifiers()) == false
            && method.getName().startsWith("test")) {
          try {
            try {
              method.invoke(new TestGregorianCalendar());
            } catch (InvocationTargetException itex) {
              throw itex.getCause();
            }
            System.out.println(method.getName() + " SUCCEEDED");
            success++;
          } catch (AssertionError ex) {
            System.out.println(method.getName() + " FAILED");
            ex.printStackTrace(System.out);
            failure++;
          } catch (Throwable ex) {
            ex.printStackTrace();
            error++;
          }
        }
      }

    } catch (Throwable th) {
      th.printStackTrace();
    }
    System.out.println("Success: " + success);
    System.out.println("Failure: " + failure);
    System.out.println("Error:   " + error);
  }
示例#3
0
    @POST
    @Path("access-token")
    @Produces(MediaType.APPLICATION_JSON)
    public MyTokenResult getAccessToken(
        @FormParam("grant_type") String grantType,
        @FormParam("code") String code,
        @FormParam("redirect_uri") String redirectUri,
        @FormParam("client_id") String clientId) {
      try {
        assertEquals("authorization_code", grantType);
        assertEquals("urn:ietf:wg:oauth:2.0:oob", redirectUri);
        assertEquals(CODE, code);
        assertEquals(CLIENT_PUBLIC, clientId);
      } catch (AssertionError e) {
        e.printStackTrace();
        throw new BadRequestException(Response.status(400).entity(e.getMessage()).build());
      }

      final MyTokenResult myTokenResult = new MyTokenResult();
      myTokenResult.setAccessToken("access-token-aab999f");
      myTokenResult.setExpiresIn("3600");
      myTokenResult.setTokenType("access-token");
      myTokenResult.setRefreshToken("refresh-xyz");
      return myTokenResult;
    }
  @Test(enabled = true)
  public void testShellAccess() throws IOException {
    final String nameOfServer = "Server" + String.valueOf(new Date().getTime()).substring(6);
    serversToDeleteAfterTheTests.add(nameOfServer);

    Set<Ip> availableIps = client.getIpServices().getUnassignedIpList();
    Ip availableIp = Iterables.getLast(availableIps);

    Server createdServer =
        client
            .getServerServices()
            .addServer(
                nameOfServer, "GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "1", availableIp.getIp());
    assertNotNull(createdServer);
    assert serverLatestJobCompleted.apply(createdServer);

    // get server by name
    Set<Server> response = client.getServerServices().getServersByName(nameOfServer);
    assert (response.size() == 1);
    createdServer = Iterables.getOnlyElement(response);

    Map<String, Credentials> credsMap = client.getServerServices().getServerCredentialsList();
    LoginCredentials instanceCredentials =
        LoginCredentials.fromCredentials(credsMap.get(createdServer.getName()));
    assertNotNull(instanceCredentials);

    HostAndPort socket = HostAndPort.fromParts(createdServer.getIp().getIp(), 22);

    Predicate<HostAndPort> socketOpen = retry(new InetSocketAddressConnect(), 180, 5, SECONDS);

    socketOpen.apply(socket);

    SshClient sshClient =
        gocontext
            .utils()
            .injector()
            .getInstance(SshClient.Factory.class)
            .create(socket, instanceCredentials);
    sshClient.connect();
    String output = sshClient.exec("df").getOutput();
    assertTrue(
        output.contains("Filesystem"),
        "The output should've contained filesystem information, but it didn't. Output: " + output);
    sshClient.disconnect();

    // check that the get credentials call is the same as this
    assertEquals(
        client.getServerServices().getServerCredentials(createdServer.getId()),
        instanceCredentials);

    try {
      assertEquals(client.getServerServices().getServerCredentials(Long.MAX_VALUE), null);
    } catch (AssertionError e) {
      e.printStackTrace();
    }

    // delete the server
    client.getServerServices().deleteByName(nameOfServer);
  }
示例#5
0
    @POST
    @Path("refresh-token")
    @Produces(MediaType.APPLICATION_JSON)
    public String refreshToken(
        @FormParam("grant_type") String grantType,
        @FormParam("refresh_token") String refreshToken,
        @HeaderParam("isArray") @DefaultValue("false") boolean isArray) {
      try {
        assertEquals("refresh_token", grantType);
        assertEquals("refresh-xyz", refreshToken);
      } catch (AssertionError e) {
        e.printStackTrace();
        throw new BadRequestException(Response.status(400).entity(e.getMessage()).build());
      }

      return isArray
          ? "{\"access_token\":[\"access-token-new\"],\"expires_in\":\"3600\",\"token_type\":\"access-token\"}"
          : "{\"access_token\":\"access-token-new\",\"expires_in\":\"3600\",\"token_type\":\"access-token\"}";
    }
示例#6
0
 @GET
 @Path("authorization")
 public String authorization(
     @QueryParam("state") String state,
     @QueryParam("response_type") String responseType,
     @QueryParam("scope") String scope,
     @QueryParam("readOnly") String readOnly,
     @QueryParam("redirect_uri") String redirectUri) {
   try {
     assertEquals("code", responseType);
     assertEquals(STATE, state);
     assertEquals("urn:ietf:wg:oauth:2.0:oob", redirectUri);
     assertEquals("contact", scope);
     assertEquals("true", readOnly);
   } catch (AssertionError e) {
     e.printStackTrace();
     throw new BadRequestException(Response.status(400).entity(e.getMessage()).build());
   }
   return CODE;
 }
示例#7
0
  /** line search */
  private void backtrackingLineSearch() throws AssertionError {
    double origDirDeriv = dirDeriv();
    // if a non-descent direction is chosen, the line search will break anyway, so throw here
    // The most likely reason for this is a bug in your function's gradient computation

    try {
      assert origDirDeriv < 0;
    } catch (AssertionError ae) {
      ae.printStackTrace();
      stderr.println("!! L-BFGS chose a non-descent direction: check your gradient!");
      // System.exit(1);

      throw new AssertionError(ae.toString());
    }

    double alpha = 1.0;
    double backoff = 0.5;
    if (iter == 1) {
      double normDir = Math.sqrt(ArrayUtils.dot(dir, dir));
      alpha = (1 / normDir);
      backoff = 0.1;

      if (DEBUG || verbose) {
        stderr.println("alpha:" + alpha);
        stderr.println("normDir:" + normDir);
      }
    }

    double oldValue = value;
    boolean first = true;
    while (true) {

      getNextPoint(alpha);
      value = evalL1();

      if (DEBUG || verbose) {
        stderr.println("alpha:" + alpha);
        stderr.println("value:" + value);

        stderr.println("oldValue:" + oldValue);
        stderr.println("C1:" + C1);
        stderr.println("origDirDeriv:" + origDirDeriv);

        stderr.println("alpha:" + alpha);

        assert !Double.isNaN(value);
        assert !Double.isNaN(origDirDeriv);
      }

      if (!first && value <= (oldValue + C1 * origDirDeriv * alpha)) {
        break;
      }

      first = false;

      if (!quiet) {
        stderr.print(".");
      }

      alpha *= backoff;
    }

    if (!quiet) {
      stderr.println();
    }
  }