示例#1
0
 private static Region asRegion(Rectangle r) {
   if (r != null) {
     return Region.create(r);
   } else {
     return null;
   }
 }
示例#2
0
 /**
  * interactive region create with given message: lets the user draw the rectangle using the mouse
  *
  * @return the region
  */
 public Region selectRegion(final String msg) {
   ScreenImage sim = userCapture(msg);
   if (sim == null) {
     return null;
   }
   Rectangle r = sim.getROI();
   return Region.create((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
 }
示例#3
0
 public static boolean testSetup() {
   Region r = Region.create(0, 0, 100, 100);
   Image img = new Image(r.getScreen().capture(r).getImage());
   Pattern p = new Pattern(img);
   Finder f = new Finder(img);
   if (null != f.find(p) && f.hasNext()) {
     org.sikuli.basics.SikuliX.popup(
         "Hallo from Java-API.testSetup\nSikuli seems to be working fine!\n\nHave fun!");
     return true;
   }
   return false;
 }
  /**
   * Tests that a local writer receives a modified version of the callback argument on an update.
   */
  public void testLocalUpdateModifiedCallbackArgument() throws CacheException {

    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object one = "ONE";
    final Object two = "TWO";

    TestCacheLoader loader =
        new TestCacheLoader() {
          public Object load2(LoaderHelper helper) throws CacheLoaderException {

            Object[] array = (Object[]) helper.getArgument();
            assertEquals(one, array[0]);
            array[0] = two;

            return value;
          }
        };

    TestCacheWriter writer =
        new TestCacheWriter() {
          public void beforeCreate2(EntryEvent event) throws CacheWriterException {}

          public void beforeUpdate2(EntryEvent event) throws CacheWriterException {

            Object[] array = (Object[]) event.getCallbackArgument();
            assertEquals(two, array[0]);
          }
        };

    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    factory.setCacheWriter(writer);
    Region region = createRegion(name, factory.create());

    region.create(key, null);
    assertFalse(loader.wasInvoked());
    assertTrue(writer.wasInvoked());

    Object[] array = new Object[] {one};
    assertEquals(value, region.get(key, array));
    assertTrue(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
  }
示例#5
0
 /**
  * creates a region on the current screen with the given coordinate/size. The coordinate is
  * translated to the current screen from its relative position on the screen it would have been
  * created normally.
  *
  * @param loc
  * @param width
  * @param height
  * @return the new region
  */
 public Region newRegion(Location loc, int width, int height) {
   return Region.create(loc.copyTo(this), width, height);
 }
  /**
   * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
   */
  private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    Region r = createRootRegion("ProxyDUnitTest", af.create());

    doCreateOtherVm();

    r.put("putkey", "putvalue1");

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check put") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("putkey"));
                assertEquals("putvalue1", r.getEntry("putkey").getValue());
                r.put("putkey", "putvalue2");
              }
            });

    assertEquals(false, r.containsKey("putkey"));
    assertEquals("putvalue2", r.get("putkey")); // netsearch

    r.invalidate("putkey");

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check invalidate") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("putkey"));
                assertEquals(null, r.getEntry("putkey").getValue());
              }
            });
    assertEquals(null, r.get("putkey")); // invalid so total miss

    r.destroy("putkey");

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check destroy") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(false, r.containsKey("putkey"));
              }
            });

    assertEquals(null, r.get("putkey")); // total miss

    r.create("createKey", "createValue1");
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check create") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("createKey"));
                assertEquals("createValue1", r.getEntry("createKey").getValue());
              }
            });
    {
      Map m = new HashMap();
      m.put("putAllKey1", "putAllValue1");
      m.put("putAllKey2", "putAllValue2");
      r.putAll(m, "putAllCallback");
    }
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check putAll") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(true, r.containsKey("putAllKey1"));
                assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
                assertEquals(true, r.containsKey("putAllKey2"));
                assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
              }
            });
    r.clear();
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check clear") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(0, r.size());
              }
            });

    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("install CacheWriter") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                AttributesMutator am = r.getAttributesMutator();
                CacheWriter cw =
                    new CacheWriterAdapter() {
                      public void beforeCreate(EntryEvent event) throws CacheWriterException {
                        throw new CacheWriterException("expected");
                      }
                    };
                am.setCacheWriter(cw);
              }
            });
    try {
      r.put("putkey", "putvalue");
      fail("expected CacheWriterException");
    } catch (CacheWriterException expected) {
    }
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check clear") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(0, r.size());
              }
            });

    assertEquals(null, r.get("loadkey")); // total miss
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("install CacheLoader") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                AttributesMutator am = r.getAttributesMutator();
                am.setCacheWriter(null); // clear csche writer
                CacheLoader cl =
                    new CacheLoader() {
                      public Object load(LoaderHelper helper) throws CacheLoaderException {
                        if (helper.getKey().equals("loadkey")) {
                          return "loadvalue";
                        } else if (helper.getKey().equals("loadexception")) {
                          throw new CacheLoaderException("expected");
                        } else {
                          return null;
                        }
                      }

                      public void close() {}
                    };
                am.setCacheLoader(cl);
              }
            });
    assertEquals("loadvalue", r.get("loadkey")); // net load
    assertEquals(null, r.get("foobar")); // total miss
    try {
      r.get("loadexception");
      fail("expected CacheLoaderException");
    } catch (CacheLoaderException expected) {
    }
    r.destroyRegion();
    getOtherVm()
        .invoke(
            new CacheSerializableRunnable("check clear") {
              public void run2() throws CacheException {
                Region r = getRootRegion("ProxyDUnitTest");
                assertEquals(null, r);
              }
            });
  }