1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.jemmy.control;
  24 
  25 import java.util.Map;
  26 import org.jemmy.Rectangle;
  27 import org.jemmy.action.GetAction;
  28 import org.jemmy.env.Environment;
  29 import org.testng.annotations.AfterClass;
  30 import org.testng.annotations.AfterMethod;
  31 import org.testng.annotations.BeforeClass;
  32 import org.testng.annotations.BeforeMethod;
  33 import org.testng.annotations.Test;
  34 
  35 import static org.testng.Assert.assertEquals;
  36 import static org.testng.Assert.fail;
  37 
  38 
  39 /**
  40  *
  41  * @author shura
  42  */
  43 public class PropertiesTest {
  44 
  45     public PropertiesTest() {
  46     }
  47 
  48     @BeforeClass
  49     public static void setUpClass() throws Exception {
  50     }
  51 
  52     @AfterClass
  53     public static void tearDownClass() throws Exception {
  54     }
  55 
  56     @BeforeMethod
  57     public void setUp() {
  58     }
  59 
  60     @AfterMethod
  61     public void tearDown() {
  62     }
  63 
  64     @Test
  65     public void testNoException() {
  66         Wrap<?> wrap = new TestWrap(false);
  67         Map props = wrap.getProperties();
  68         assertEquals(props.get("x"), 10);
  69         assertEquals(props.get("X"), 10);
  70         assertEquals(props.get("getX"), 10.);
  71         props = wrap.getPropertiesQiuet();
  72         assertEquals(props.get("x"), 10);
  73         assertEquals(props.get("X"), 10);
  74         assertEquals(props.get("getX"), 10.);
  75     }
  76 
  77     @Test
  78     public void testException() {
  79         Wrap<?> wrap = new TestWrap(true);
  80         try {
  81             wrap.getProperties();
  82             fail("No exception thrown");
  83         } catch (RuntimeException e) {
  84             Map props = wrap.getPropertiesQiuet();
  85             assertEquals(props.get("x"), 10);
  86             assertEquals(props.get("getX"), 10.);
  87         }
  88     }
  89 
  90     @MethodProperties("getX")
  91     @FieldProperties("x")
  92     class TestWrap extends Wrap<Rectangle> {
  93 
  94         boolean throwException;
  95 
  96         public TestWrap(boolean throwException) {
  97             super(Environment.getEnvironment(), new Rectangle(10, 20, 30, 40));
  98             this.throwException = throwException;
  99         }
 100 
 101         @Property("X")
 102         public int getX() {
 103             if (throwException) {
 104                 throw new RuntimeException("Exception while getting a property");
 105             }
 106             return new GetAction<Integer>() {
 107 
 108                 @Override
 109                 public void run(Object... parameters) throws Exception {
 110                     setResult(getControl().x);
 111                 }
 112             }.dispatch(getEnvironment());
 113         }
 114 
 115         @Override
 116         public Rectangle getScreenBounds() {
 117             return getControl();
 118         }
 119     }
 120 }