Exemplo n.º 1
0
  @Test
  public void testNoUserHome() {
    final Platform platform = mock(Platform.class);
    when(platform.getUserHome()).thenReturn(null);

    final ConfigDatabase config = createDatabase(platform);

    exception.expect(ConfigException.class);
    config.putGlobal("section.int", 1);
  }
Exemplo n.º 2
0
  private void setUpGeogig(GeogigCLI cli) throws Exception {
    final File userhome = tempFolder.newFolder("mockUserHomeDir");
    final File workingDir = tempFolder.newFolder("mockWorkingDir");
    tempFolder.newFolder("mockWorkingDir", ".geogig");

    final Platform platform = mock(Platform.class);
    when(platform.pwd()).thenReturn(workingDir);
    when(platform.getUserHome()).thenReturn(userhome);

    cli.setPlatform(platform);
  }
Exemplo n.º 3
0
  @Before
  public final void setUp() {
    final File userhome = tempFolder.newFolder("mockUserHomeDir");

    final File workingDir = tempFolder.newFolder("mockWorkingDir");
    tempFolder.newFolder("mockWorkingDir", ".geogig");

    final Platform platform = mock(Platform.class);
    when(platform.getUserHome()).thenReturn(userhome);
    when(platform.pwd()).thenReturn(workingDir);

    config = createDatabase(platform);
  }
Exemplo n.º 4
0
  /**
   * Parses a string with a timestamp
   *
   * @return a Long with the timestamp represented by the specified string
   */
  @Override
  protected Long _call() {
    Preconditions.checkState(string != null, "String has not been set.");

    try { // see if it is a timestamp in milisecs
      Long milis = new Long(string);
      return milis;
    } catch (NumberFormatException e) {
    }

    SimpleDateFormat formatter;
    final Platform platform = platform();
    if (string.equals("yesterday")) { // return the beginning of yesterday, not just 24h ago
      // from current time
      try {
        formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date today = new Date(platform.currentTimeMillis());
        long todayOnlyDate = formatter.parse(formatter.format(today)).getTime();
        long millisecsInOneDay = 60 * 60 * 24 * 1000;
        long yesterday = todayOnlyDate - millisecsInOneDay;
        return yesterday;
      } catch (ParseException e) {
        // shouldn't reach this
      }
    }

    if (string.equals("today")) {
      try {
        formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date today = new Date(platform.currentTimeMillis());
        long todayOnlyDate = formatter.parse(formatter.format(today)).getTime();
        return todayOnlyDate;
      } catch (ParseException e) {
        // shouldn't reach this
      }
    }

    // parse it as a git-like time reference
    String[] tokens = string.split("\\.");
    if (tokens.length % 2 != 0) {
      if (tokens[tokens.length - 1].toLowerCase().equals("ago")) {
        long currentTime = platform.currentTimeMillis();
        int i;
        for (i = 0; i < tokens.length - 1; i++) {
          try {
            double number = Double.parseDouble(tokens[i]);
            i++;
            String s = tokens[i].toLowerCase();
            if (s.endsWith("s")) {
              s = s.substring(0, s.length() - 1);
            }
            if (units.containsKey(s)) {
              currentTime -= units.get(s) * number;
            } else {
              break;
            }
          } catch (Exception e) {
            break;
          }
        }
        if (i == tokens.length - 1) {
          return currentTime;
        }
      }
    }

    // finally, try to parse it as a Date object
    try {
      long time =
          javax.xml.datatype.DatatypeFactory.newInstance()
              .newXMLGregorianCalendar(string)
              .toGregorianCalendar()
              .getTimeInMillis();
      return time;
    } catch (DatatypeConfigurationException e) {
    } catch (IllegalArgumentException e) {
    }

    throw new IllegalArgumentException("Invalid timestamp string: " + string);
  }