コード例 #1
0
  public void testSortByLong() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmResults<AllTypes> sortedList = testRealm.allObjects(AllTypes.class);
    sortedList.sort(FIELD_LONG, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals("Should have same size", resultList.size(), sortedList.size());
    assertEquals(TEST_DATA_SIZE, sortedList.size());
    assertEquals(
        "First excepted to be last",
        resultList.first().getColumnLong(),
        sortedList.last().getColumnLong());

    RealmResults<AllTypes> reverseList = sortedList;
    reverseList.sort(FIELD_LONG, RealmResults.SORT_ORDER_ASCENDING);
    assertEquals(TEST_DATA_SIZE, reverseList.size());
    assertEquals(
        "First excepted to be first",
        resultList.first().getColumnLong(),
        reverseList.first().getColumnLong());
    assertEquals(
        "Last excepted to be last",
        resultList.last().getColumnLong(),
        reverseList.last().getColumnLong());

    RealmResults<AllTypes> reserveSortedList = reverseList;
    reverseList.sort(FIELD_LONG, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals(TEST_DATA_SIZE, reserveSortedList.size());
  }
コード例 #2
0
 @Override
 protected void setUp() throws InterruptedException {
   RealmConfiguration realmConfig = new RealmConfiguration.Builder(getContext()).build();
   Realm.deleteRealm(realmConfig);
   testRealm = Realm.getInstance(realmConfig);
   populateTestRealm();
 }
コード例 #3
0
  private void populatePartialNullRowsForNumericTesting() {
    NullTypes nullTypes1 = new NullTypes();
    nullTypes1.setId(1);
    nullTypes1.setFieldIntegerNull(1);
    nullTypes1.setFieldFloatNull(2F);
    nullTypes1.setFieldDoubleNull(3D);
    nullTypes1.setFieldBooleanNull(true);
    nullTypes1.setFieldStringNull("4");
    nullTypes1.setFieldDateNull(new Date(12345));

    NullTypes nullTypes2 = new NullTypes();
    nullTypes2.setId(2);

    NullTypes nullTypes3 = new NullTypes();
    nullTypes3.setId(3);
    nullTypes3.setFieldIntegerNull(0);
    nullTypes3.setFieldFloatNull(0F);
    nullTypes3.setFieldDoubleNull(0D);
    nullTypes3.setFieldBooleanNull(false);
    nullTypes3.setFieldStringNull("0");
    nullTypes3.setFieldDateNull(new Date(0));

    testRealm.beginTransaction();
    testRealm.copyToRealm(nullTypes1);
    testRealm.copyToRealm(nullTypes2);
    testRealm.copyToRealm(nullTypes3);
    testRealm.commitTransaction();
  }
コード例 #4
0
  public static SatsClass copyOrUpdate(
      Realm realm, SatsClass object, boolean update, Map<RealmObject, RealmObject> cache) {
    if (object.realm != null && object.realm.getId() == realm.getId()) {
      return object;
    }
    SatsClass realmObject = null;
    boolean canUpdate = update;
    if (canUpdate) {
      Table table = realm.getTable(SatsClass.class);
      long pkColumnIndex = table.getPrimaryKey();
      long rowIndex = table.findFirstString(pkColumnIndex, object.getId());
      if (rowIndex != TableOrView.NO_MATCH) {
        realmObject = new SatsClassRealmProxy();
        realmObject.realm = realm;
        realmObject.row = table.getRow(rowIndex);
        cache.put(object, realmObject);
      } else {
        canUpdate = false;
      }
    }

    if (canUpdate) {
      return update(realm, realmObject, object, cache);
    } else {
      return copy(realm, object, update, cache);
    }
  }
コード例 #5
0
 /**
  * Test to see if a particular item that does exist in the same Realm does not exist in the result
  * set of another query.
  */
 public void testContainsSameRealmNotContained() {
   RealmResults<AllTypes> items =
       testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 1000).findAll();
   AllTypes anotherType =
       testRealm.where(AllTypes.class).greaterThan(AllTypes.FIELD_LONG, 1000).findFirst();
   assertFalse(
       "Should not be able to find item in another result list.", items.contains(anotherType));
 }
コード例 #6
0
 public void testShouldNotContainRemovedItem() {
   RealmQuery<AllTypes> query = testRealm.where(AllTypes.class).findAll().where();
   AllTypes item = testRealm.where(AllTypes.class).findFirst();
   testRealm.beginTransaction();
   item.removeFromRealm();
   testRealm.commitTransaction();
   assertFalse("Should not contain a removed item.", query.findAll().contains(item));
 }
コード例 #7
0
  public void testFindFirst() {
    AllTypes result = testRealm.where(AllTypes.class).findFirst();
    assertEquals(0, result.getColumnLong());
    assertEquals("test data 0", result.getColumnString());

    AllTypes none = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "smurf").findFirst();
    assertNull(none);
  }
コード例 #8
0
  public void testRemoveLastEmptyList() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    testRealm.beginTransaction();
    resultList.clear();
    assertEquals(0, resultList.size());
    resultList.removeLast();
    testRealm.commitTransaction();

    assertEquals(0, resultList.size());
  }
コード例 #9
0
  // void clear(Class<?> classSpec)
  public void testClearEmptiesTable() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    assertEquals(TEST_DATA_SIZE, resultList.size());

    testRealm.beginTransaction();
    resultList.clear();
    testRealm.commitTransaction();

    assertEquals(0, resultList.size());
  }
コード例 #10
0
 public void testWithEmptyRealmObjects() {
   testRealm.beginTransaction();
   testRealm.clear(AllTypes.class);
   testRealm.commitTransaction();
   try {
     testRealm.where(AllTypes.class).findAll().sort(FIELD_STRING);
   } catch (IllegalArgumentException e) {
     fail("Failed to sort an empty RealmResults");
   }
 }
コード例 #11
0
  private void printAppConfigurationDetails(PrintWriter pw) {
    Map<String, Realm> configs = getConfigurationDetails();
    if (configs.isEmpty()) {
      pw.println("No JAAS LoginModule registered");
      return;
    }

    pw.println("<p class=\"statline ui-state-highlight\">${Registered LoginModules}</p>");

    pw.println("<table class=\"nicetable\">");
    pw.println("<thead><tr>");
    pw.println("<th class=\"header\">${Realm}</th>");
    pw.println("<th class=\"header\">${Rank}</th>");
    pw.println("<th class=\"header\">${Control Flag}</th>");
    pw.println("<th class=\"header\">${Type}</th>");
    pw.println("<th class=\"header\">${Classname}</th>");
    pw.println("</tr></thead>");

    for (Realm r : configs.values()) {
      String realmName = r.getRealmName();
      pw.printf(
          "<tr class=\"ui-state-default\"><td>%s</td><td colspan=\"4\"></td></tr>", realmName);

      String rowClass = "odd";
      for (AppConfigurationHolder ah : r.getConfigs()) {
        LoginModuleProvider lp = ah.getProvider();
        String type = getType(lp);
        pw.printf("<tr class=\"%s ui-state-default\"><td></td><td>%d</td>", rowClass, lp.ranking());
        pw.printf("<td>%s</td>", ControlFlag.toString(lp.getControlFlag()));
        pw.printf("<td>%s</td>", type);

        pw.printf("<td>");
        pw.print(lp.getClassName());

        if (lp instanceof OsgiLoginModuleProvider) {
          ServiceReference sr = ((OsgiLoginModuleProvider) lp).getServiceReference();
          Object id = sr.getProperty(Constants.SERVICE_ID);
          pw.printf("<a href=\"${pluginRoot}/../services/%s\">(%s)</a>", id, id);
        } else if (lp instanceof ConfigLoginModuleProvider) {
          Map config = lp.options();
          Object id = config.get(Constants.SERVICE_PID);
          pw.printf("<a href=\"${pluginRoot}/../configMgr/%s\">(Details)</a>", id);
        }
        pw.printf("</td>");

        pw.println("</tr>");
        if (rowClass.equals("odd")) {
          rowClass = "even";
        } else {
          rowClass = "odd";
        }
      }
    }
    pw.println("</table>");
  }
コード例 #12
0
  public void testIsValid() {
    final RealmResults<AllTypes> results = testRealm.where(AllTypes.class).findAll();

    assertTrue(results.isValid());
    populateTestRealm(1);
    // still valid if result changed
    assertTrue(results.isValid());

    testRealm.close();
    assertFalse(results.isValid());
  }
コード例 #13
0
  public void testClearFromAdapter() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate);

    testRealm.beginTransaction();
    realmAdapter.getRealmResults().clear();
    testRealm.commitTransaction();

    assertEquals(0, realmAdapter.getCount());
    assertEquals(0, resultList.size());
  }
コード例 #14
0
  public void testRemove() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    testRealm.beginTransaction();
    resultList.remove(0);
    testRealm.commitTransaction();

    assertEquals(TEST_DATA_SIZE - 1, resultList.size());

    AllTypes allTypes = resultList.get(0);
    assertEquals(1, allTypes.getColumnLong());
  }
コード例 #15
0
  protected void setUp() throws Exception {
    super.setUp();
    Realm.deleteRealmFile(getContext());
    testRealm = Realm.getInstance(getContext());

    testRealm.beginTransaction();
    for (int i = 0; i < TEST_DATA_SIZE; ++i) {
      AllTypes allTypes = testRealm.createObject(AllTypes.class);
      allTypes.setColumnString("test data " + i);
    }
    testRealm.commitTransaction();
  }
コード例 #16
0
  public void testRemoveFromAdapter() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate);

    testRealm.beginTransaction();
    realmAdapter.getRealmResults().remove(0);
    testRealm.commitTransaction();
    assertEquals(TEST_DATA_SIZE - 1, realmAdapter.getCount());

    resultList = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "test data 0").findAll();
    assertEquals(0, resultList.size());
  }
コード例 #17
0
ファイル: RealmConfig.java プロジェクト: winsx/Payara
  public static void createRealms(String defaultRealm, List<AuthRealm> realms, String configName) {
    assert (realms != null);

    String goodRealm = null; // need at least one good realm

    for (AuthRealm aRealm : realms) {
      String realmName = aRealm.getName();
      String realmClass = aRealm.getClassname();
      assert (realmName != null);
      assert (realmClass != null);

      try {
        List<Property> realmProps = aRealm.getProperty();
        /*V3 Commented ElementProperty[] realmProps =
        aRealm.getElementProperty();*/
        Properties props = new Properties();
        for (Property realmProp : realmProps) {
          props.setProperty(realmProp.getName(), realmProp.getValue());
        }
        Realm.instantiate(realmName, realmClass, props, configName);
        if (logger.isLoggable(Level.FINE)) {
          logger.fine("Configured realm: " + realmName);
        }

        if (goodRealm == null) {
          goodRealm = realmName;
        }
      } catch (Exception e) {
        logger.log(Level.WARNING, SecurityLoggerInfo.realmConfigDisabledError, realmName);
        logger.log(Level.WARNING, SecurityLoggerInfo.securityExceptionError, e);
      }
    }

    // done loading all realms, check that there is at least one
    // in place and that default is installed, or change default
    // to the first one loaded (arbitrarily).

    if (goodRealm == null) {
      logger.severe(SecurityLoggerInfo.noRealmsError);

    } else {
      try {
        Realm.getInstance(defaultRealm);
      } catch (Exception e) {
        defaultRealm = goodRealm;
      }
      Realm.setDefaultRealm(defaultRealm);
      if (logger.isLoggable(Level.FINE)) {
        logger.fine("Default realm is set to: " + defaultRealm);
      }
    }
  }
コード例 #18
0
 public boolean methodWrongThread(final Method method)
     throws ExecutionException, InterruptedException {
   final RealmResults<AllTypes> allTypeses = testRealm.where(AllTypes.class).findAll();
   testRealm.beginTransaction();
   ExecutorService executorService = Executors.newSingleThreadExecutor();
   Future<Boolean> future =
       executorService.submit(
           new Callable<Boolean>() {
             @Override
             public Boolean call() throws Exception {
               try {
                 switch (method) {
                   case METHOD_MIN:
                     allTypeses.min(FIELD_FLOAT);
                     break;
                   case METHOD_MAX:
                     allTypeses.max(FIELD_FLOAT);
                     break;
                   case METHOD_SUM:
                     allTypeses.sum(FIELD_FLOAT);
                     break;
                   case METHOD_AVG:
                     allTypeses.average(FIELD_FLOAT);
                     break;
                   case METHOD_SORT:
                     allTypeses.sort(FIELD_FLOAT);
                     break;
                   case METHOD_WHERE:
                     allTypeses.where();
                     break;
                   case METHOD_REMOVE:
                     allTypeses.remove(0);
                     break;
                   case METHOD_REMOVE_LAST:
                     allTypeses.removeLast();
                     break;
                   case METHOD_CLEAR:
                     allTypeses.clear();
                     break;
                 }
                 return false;
               } catch (IllegalStateException ignored) {
                 return true;
               }
             }
           });
   Boolean result = future.get();
   testRealm.cancelTransaction();
   return result;
 }
コード例 #19
0
 public static void insertOrUpdate(
     Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) {
   Table table = realm.getTable(some.test.Booleans.class);
   long tableNativePtr = table.getNativeTablePointer();
   BooleansColumnInfo columnInfo =
       (BooleansColumnInfo) realm.schema.getColumnInfo(some.test.Booleans.class);
   some.test.Booleans object = null;
   while (objects.hasNext()) {
     object = (some.test.Booleans) objects.next();
     if (!cache.containsKey(object)) {
       long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1);
       cache.put(object, rowIndex);
       Table.nativeSetBoolean(
           tableNativePtr,
           columnInfo.doneIndex,
           rowIndex,
           ((BooleansRealmProxyInterface) object).realmGet$done());
       Table.nativeSetBoolean(
           tableNativePtr,
           columnInfo.isReadyIndex,
           rowIndex,
           ((BooleansRealmProxyInterface) object).realmGet$isReady());
       Table.nativeSetBoolean(
           tableNativePtr,
           columnInfo.mCompletedIndex,
           rowIndex,
           ((BooleansRealmProxyInterface) object).realmGet$mCompleted());
       Table.nativeSetBoolean(
           tableNativePtr,
           columnInfo.anotherBooleanIndex,
           rowIndex,
           ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean());
     }
   }
 }
コード例 #20
0
 public static long insertOrUpdate(
     Realm realm, some.test.Booleans object, Map<RealmModel, Long> cache) {
   Table table = realm.getTable(some.test.Booleans.class);
   long tableNativePtr = table.getNativeTablePointer();
   BooleansColumnInfo columnInfo =
       (BooleansColumnInfo) realm.schema.getColumnInfo(some.test.Booleans.class);
   long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1);
   cache.put(object, rowIndex);
   Table.nativeSetBoolean(
       tableNativePtr,
       columnInfo.doneIndex,
       rowIndex,
       ((BooleansRealmProxyInterface) object).realmGet$done());
   Table.nativeSetBoolean(
       tableNativePtr,
       columnInfo.isReadyIndex,
       rowIndex,
       ((BooleansRealmProxyInterface) object).realmGet$isReady());
   Table.nativeSetBoolean(
       tableNativePtr,
       columnInfo.mCompletedIndex,
       rowIndex,
       ((BooleansRealmProxyInterface) object).realmGet$mCompleted());
   Table.nativeSetBoolean(
       tableNativePtr,
       columnInfo.anotherBooleanIndex,
       rowIndex,
       ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean());
   return rowIndex;
 }
コード例 #21
0
 public static some.test.Booleans copyOrUpdate(
     Realm realm,
     some.test.Booleans object,
     boolean update,
     Map<RealmModel, RealmObjectProxy> cache) {
   if (object instanceof RealmObjectProxy
       && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null
       && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().threadId
           != realm.threadId) {
     throw new IllegalArgumentException(
         "Objects which belong to Realm instances in other threads cannot be copied into this Realm instance.");
   }
   if (object instanceof RealmObjectProxy
       && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null
       && ((RealmObjectProxy) object)
           .realmGet$proxyState()
           .getRealm$realm()
           .getPath()
           .equals(realm.getPath())) {
     return object;
   }
   RealmObjectProxy cachedRealmObject = cache.get(object);
   if (cachedRealmObject != null) {
     return (some.test.Booleans) cachedRealmObject;
   } else {
     return copy(realm, object, update, cache);
   }
 }
コード例 #22
0
 public static void insertOrUpdate(
     Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) {
   Table table = realm.getTable(some.test.Simple.class);
   long tableNativePtr = table.getNativeTablePointer();
   SimpleColumnInfo columnInfo =
       (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class);
   some.test.Simple object = null;
   while (objects.hasNext()) {
     object = (some.test.Simple) objects.next();
     if (!cache.containsKey(object)) {
       long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1);
       cache.put(object, rowIndex);
       String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name();
       if (realmGet$name != null) {
         Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name);
       } else {
         Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex);
       }
       Table.nativeSetLong(
           tableNativePtr,
           columnInfo.ageIndex,
           rowIndex,
           ((SimpleRealmProxyInterface) object).realmGet$age());
     }
   }
 }
コード例 #23
0
 @SuppressWarnings("cast")
 public static some.test.Simple createUsingJsonStream(Realm realm, JsonReader reader)
     throws IOException {
   some.test.Simple obj = realm.createObject(some.test.Simple.class);
   reader.beginObject();
   while (reader.hasNext()) {
     String name = reader.nextName();
     if (name.equals("name")) {
       if (reader.peek() == JsonToken.NULL) {
         reader.skipValue();
         ((SimpleRealmProxyInterface) obj).realmSet$name(null);
       } else {
         ((SimpleRealmProxyInterface) obj).realmSet$name((String) reader.nextString());
       }
     } else if (name.equals("age")) {
       if (reader.peek() == JsonToken.NULL) {
         reader.skipValue();
         throw new IllegalArgumentException("Trying to set non-nullable field 'age' to null.");
       } else {
         ((SimpleRealmProxyInterface) obj).realmSet$age((int) reader.nextInt());
       }
     } else {
       reader.skipValue();
     }
   }
   reader.endObject();
   return obj;
 }
コード例 #24
0
ファイル: KeyTabEntry.java プロジェクト: ronshapiro/j86
  public int entryLength() {
    int totalPrincipalLength = 0;
    String[] names = service.getNameStrings();
    for (int i = 0; i < names.length; i++) {
      try {
        totalPrincipalLength += principalSize + names[i].getBytes("8859_1").length;
      } catch (UnsupportedEncodingException exc) {
      }
    }

    int realmLen = 0;
    try {
      realmLen = realm.toString().getBytes("8859_1").length;
    } catch (UnsupportedEncodingException exc) {
    }

    int size =
        principalComponentSize
            + realmSize
            + realmLen
            + totalPrincipalLength
            + principalTypeSize
            + timestampSize
            + keyVersionSize
            + keyTypeSize
            + keySize
            + keyblock.length;

    if (DEBUG) {
      System.out.println(">>> KeyTabEntry: key tab entry size is " + size);
    }
    return size;
  }
コード例 #25
0
  public void testNullToNonNullResults() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmAdapter realmAdapter = new RealmAdapter(getContext(), null, automaticUpdate);
    realmAdapter.updateRealmResults(resultList);

    assertEquals(TEST_DATA_SIZE, realmAdapter.getCount());
  }
コード例 #26
0
 public void testSortSingleField() {
   RealmResults<AllTypes> sortedList = testRealm.allObjects(AllTypes.class);
   sortedList.sort(new String[] {FIELD_LONG}, new boolean[] {RealmResults.SORT_ORDER_DESCENDING});
   assertEquals(TEST_DATA_SIZE, sortedList.size());
   assertEquals(TEST_DATA_SIZE - 1, sortedList.first().getColumnLong());
   assertEquals(0, sortedList.last().getColumnLong());
 }
コード例 #27
0
 // Test max on empty columns
 public void testMaxValueForEmptyColumns() {
   RealmResults<NullTypes> results = testRealm.where(NullTypes.class).findAll();
   assertNull(results.max(NullTypes.FIELD_INTEGER_NOT_NULL));
   assertNull(results.max(NullTypes.FIELD_FLOAT_NOT_NULL));
   assertNull(results.max(NullTypes.FIELD_DOUBLE_NOT_NULL));
   assertNull(results.maxDate(NullTypes.FIELD_DATE_NOT_NULL));
 }
コード例 #28
0
  public void testSumGivesCorrectValue() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();

    Number sum = resultList.sum(FIELD_LONG);
    // Sum of numbers 0 to M-1: (M-1)*M/2
    assertEquals((TEST_DATA_SIZE - 1) * TEST_DATA_SIZE / 2, sum.intValue());
  }
コード例 #29
0
  public void testAvgGivesCorrectValue() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    double N = (double) TEST_DATA_SIZE;

    // Sum of numbers 1 to M: M*(M+1)/2
    // See setUp() for values of fields
    // N = TEST_DATA_SIZE

    // Type: double; a = 3.1415
    // a, a+1, ..., a+i, ..., a+N-1
    // sum = 3.1415*N + N*(N-1)/2
    // average = sum/N = 3.1415+(N-1)/2
    double average = 3.1415 + (N - 1.0) * 0.5;
    assertEquals(average, resultList.average(FIELD_DOUBLE), 0.0001);

    // Type: long
    // 0, 1, ..., N-1
    // sum = N*(N-1)/2
    // average = sum/N = (N-1)/2
    assertEquals(0.5 * (N - 1), resultList.average(FIELD_LONG), 0.0001);

    // Type: float; b = 1.234567
    // b, b+1, ..., b+i, ..., b+N-1
    // sum = b*N + N*(N-1)/2
    // average = sum/N = b + (N-1)/2
    assertEquals(1.234567 + 0.5 * (N - 1.0), resultList.average(FIELD_FLOAT), 0.0001);
  }
コード例 #30
0
  // Test average on empty columns
  public void testAvgGivesCorrectValueForEmptyColumns() {
    RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll();

    assertEquals(0d, resultList.average(NullTypes.FIELD_INTEGER_NOT_NULL), 0d);
    assertEquals(0d, resultList.average(NullTypes.FIELD_FLOAT_NOT_NULL), 0d);
    assertEquals(0d, resultList.average(NullTypes.FIELD_DOUBLE_NOT_NULL), 0d);
  }