Ejemplo n.º 1
0
 private static List<Character> asList(Character[] values) {
   char[] temp = new char[values.length];
   for (int i = 0; i < values.length; i++) {
     temp[i] = checkNotNull(values[i]); // checkNotNull for GWT (do not optimize).
   }
   return Chars.asList(temp);
 }
Ejemplo n.º 2
0
  static int isAnagram_lambda(String s, Matcher<Integer> match) {
    List<Integer> ints = filter(match, (count(Chars.asList(s.toCharArray()))).values());

    if ((s.length() % 2 == 0 && ints.size() == 0) || (s.length() % 2 == 1 && ints.size() == 1)) {
      return 1;
    }
    return 0;
  }
Ejemplo n.º 3
0
 @Override
 public int hashCode() {
   int result = 1;
   for (int i = start; i < end; i++) {
     result = 31 * result + Chars.hashCode(array[i]);
   }
   return result;
 }
Ejemplo n.º 4
0
 @Override
 public int lastIndexOf(Object target) {
   // Overridden to prevent a ton of boxing
   if (target instanceof Character) {
     int i = Chars.lastIndexOf(array, (Character) target, start, end);
     if (i >= 0) {
       return i - start;
     }
   }
   return -1;
 }
Ejemplo n.º 5
0
 @Override
 public int compare(char[] left, char[] right) {
   int minLength = Math.min(left.length, right.length);
   for (int i = 0; i < minLength; i++) {
     int result = Chars.compare(left[i], right[i]);
     if (result != 0) {
       return result;
     }
   }
   return left.length - right.length;
 }
 protected PushOneCommit.Result amendChange(String changeId) throws GitAPIException, IOException {
   Collections.shuffle(RANDOM);
   PushOneCommit push =
       pushFactory.create(
           db,
           admin.getIdent(),
           PushOneCommit.SUBJECT,
           PushOneCommit.FILE_NAME,
           new String(Chars.toArray(RANDOM)),
           changeId);
   return push.to(git, "refs/for/master");
 }
Ejemplo n.º 7
0
 /**
  * Web browsers do not always handle '+' characters well, use the well-supported '%20' instead.
  */
 public static String urlEncode(String in, char... skipEncode) {
   return urlEncode(in, Chars.asList(skipEncode));
 }
Ejemplo n.º 8
0
 @Override
 public boolean contains(Object target) {
   // Overridden to prevent a ton of boxing
   return (target instanceof Character)
       && Chars.indexOf(array, (Character) target, start, end) != -1;
 }
@RunWith(ConfigSuite.class)
public abstract class AbstractDaemonTest {
  @ConfigSuite.Parameter public Config baseConfig;

  @Inject protected AccountCreator accounts;

  @Inject private SchemaFactory<ReviewDb> reviewDbProvider;

  @Inject protected GerritApi gApi;

  @Inject private AcceptanceTestRequestScope atrScope;

  @Inject private IdentifiedUser.GenericFactory identifiedUserFactory;

  @Inject protected PushOneCommit.Factory pushFactory;

  protected Git git;
  protected GerritServer server;
  protected TestAccount admin;
  protected TestAccount user;
  protected RestSession adminSession;
  protected RestSession userSession;
  protected SshSession sshSession;
  protected ReviewDb db;
  protected Project.NameKey project;

  @Rule
  public TestRule testRunner =
      new TestRule() {
        @Override
        public Statement apply(final Statement base, final Description description) {
          return new Statement() {
            @Override
            public void evaluate() throws Throwable {
              boolean mem = description.getAnnotation(UseLocalDisk.class) == null;
              boolean enableHttpd =
                  description.getAnnotation(NoHttpd.class) == null
                      && description.getTestClass().getAnnotation(NoHttpd.class) == null;
              beforeTest(config(description), mem, enableHttpd);
              base.evaluate();
              afterTest();
            }
          };
        }
      };

  private Config config(Description description) {
    GerritConfigs cfgs = description.getAnnotation(GerritConfigs.class);
    GerritConfig cfg = description.getAnnotation(GerritConfig.class);
    if (cfgs != null && cfg != null) {
      throw new IllegalStateException("Use either @GerritConfigs or @GerritConfig not both");
    }
    if (cfgs != null) {
      return ConfigAnnotationParser.parse(baseConfig, cfgs);
    } else if (cfg != null) {
      return ConfigAnnotationParser.parse(baseConfig, cfg);
    } else {
      return baseConfig;
    }
  }

  private void beforeTest(Config cfg, boolean memory, boolean enableHttpd) throws Exception {
    server = startServer(cfg, memory, enableHttpd);
    server.getTestInjector().injectMembers(this);
    admin = accounts.admin();
    user = accounts.user();
    adminSession = new RestSession(server, admin);
    userSession = new RestSession(server, user);
    initSsh(admin);
    db = reviewDbProvider.open();
    Context ctx = newRequestContext(admin);
    atrScope.set(ctx);
    sshSession = ctx.getSession();
    project = new Project.NameKey("p");
    createProject(sshSession, project.get());
    git = cloneProject(sshSession.getUrl() + "/" + project.get());
  }

  protected GerritServer startServer(Config cfg, boolean memory, boolean enableHttpd)
      throws Exception {
    return GerritServer.start(cfg, memory, enableHttpd);
  }

  private void afterTest() throws Exception {
    db.close();
    sshSession.close();
    server.stop();
  }

  protected PushOneCommit.Result createChange() throws GitAPIException, IOException {
    PushOneCommit push = pushFactory.create(db, admin.getIdent());
    return push.to(git, "refs/for/master");
  }

  private static final List<Character> RANDOM =
      Chars.asList(new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'});

  protected PushOneCommit.Result amendChange(String changeId) throws GitAPIException, IOException {
    Collections.shuffle(RANDOM);
    PushOneCommit push =
        pushFactory.create(
            db,
            admin.getIdent(),
            PushOneCommit.SUBJECT,
            PushOneCommit.FILE_NAME,
            new String(Chars.toArray(RANDOM)),
            changeId);
    return push.to(git, "refs/for/master");
  }

  protected ChangeJson.ChangeInfo getChange(String changeId, ListChangesOption... options)
      throws IOException {
    return getChange(adminSession, changeId, options);
  }

  protected ChangeJson.ChangeInfo getChange(
      RestSession session, String changeId, ListChangesOption... options) throws IOException {
    String q = options.length > 0 ? "?o=" + Joiner.on("&o=").join(options) : "";
    RestResponse r = session.get("/changes/" + changeId + q);
    assertEquals(HttpStatus.SC_OK, r.getStatusCode());
    return newGson().fromJson(r.getReader(), ChangeJson.ChangeInfo.class);
  }

  protected ChangeInfo info(String id) throws RestApiException {
    return gApi.changes().id(id).info();
  }

  protected ChangeInfo get(String id) throws RestApiException {
    return gApi.changes().id(id).get();
  }

  protected ChangeInfo get(String id, ListChangesOption... options) throws RestApiException {
    EnumSet<ListChangesOption> s = EnumSet.noneOf(ListChangesOption.class);
    s.addAll(Arrays.asList(options));
    return gApi.changes().id(id).get(s);
  }

  protected List<ChangeInfo> query(String q) throws RestApiException {
    return gApi.changes().query(q).get();
  }

  private Context newRequestContext(TestAccount account) {
    return atrScope.newContext(
        reviewDbProvider,
        new SshSession(server, admin),
        identifiedUserFactory.create(Providers.of(db), account.getId()));
  }

  protected Context setApiUser(TestAccount account) {
    return atrScope.set(newRequestContext(account));
  }

  protected static Gson newGson() {
    return OutputFormat.JSON_COMPACT.newGson();
  }

  protected RevisionApi revision(PushOneCommit.Result r) throws Exception {
    return gApi.changes().id(r.getChangeId()).current();
  }
}