Open email client from the app
1 2 |
NSString *url = [@"mailto:support@lalaland.com" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; |
Open email client from the app
1 2 |
NSString *url = [@"mailto:support@lalaland.com" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; |
Find all divs of class number_field inside a container with id finance_container
1 |
jQuery('div#finance_container div.number_field') |
to be continued …
If you need to debug request parameters for a HttpServletRequest:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@RequestMapping (value = "/promote", method = {RequestMethod.POST, RequestMethod.GET}) public ModelAndView renderPromotePage (HttpServletRequest request) { Map<String, String[]> parameters = request.getParameterMap(); for(String key : parameters.keySet()) { System.out.println(key); String[] vals = parameters.get(key); for(String val : vals) System.out.println(" -> " + val); } ModelAndView mv = new ModelAndView(); mv.setViewName("test"); return mv; } |
Often we want to test a function against a set of variables.
TestNG offers @DataProvider annotation.
My goal is to test a service class method: findByType of class StateMachineObjectService. This method accepts two ENUM arguments: SmProcess process and SmState state.
I need to feed a collection of different processes and states. Best way is to create an object array with desired combinations and feed it to test function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
@ContextConfiguration (locations = {"classpath:test-mvc-servlet.xml"}) public class StateMachineObjectServiceTest extends AbstractTestNGSpringContextTests { @Autowired private StateMachineObjectService service; @DataProvider (name = "Data-Provider-Function") public Object[][] parameterTestProvider() { return new Object[][]{ {SmProcess.ADVERTISE,SmState.INCOMPLETE}, {SmProcess.ADVERTISE,SmState.ACTIVE}, {SmProcess.ADVERTISE,SmState.DELETED}, {SmProcess.ADVERTISE,SmState.EXPIRED}, {SmProcess.ADVERTISE,SmState.ARCHIVED}, {SmProcess.ADVERTISE,SmState.INITIAL}, {SmProcess.ADVERTISE,SmState.SOLD}, {SmProcess.ADVERTISE,SmState.BUMPED}, }; } @Test (dataProvider = "Data-Provider-Function") public void findByType(SmProcess process, SmState state) { System.out.println(process.name() + " " + state.name()); StateMachineEntity entity = null; try { entity = service.findByType(process, state); //System.out.println(entity.getId() + " " + entity.getProcess().name() + " " + entity.getState().name()); } catch (RecordNotFoundException e) { fail(e.toString()); } assertEquals(entity.getProcess(), process, "returned wrong process"); assertEquals(entity.getState(), state, "returned wrong state"); } } |
Output is like:
ADVERTISE INCOMPLETE
Hibernate: select statemachi0_.id as id1_22_, statemachi0_.process as process2_22_, statemachi0_.state as state3_22_ from sm_map statemachi0_ where statemachi0_.process=0 and statemachi0_.state=0
ADVERTISE ACTIVE
Hibernate: select statemachi0_.id as id1_22_, statemachi0_.process as process2_22_, statemachi0_.state as state3_22_ from sm_map statemachi0_ where statemachi0_.process=0 and statemachi0_.state=1
…
===============================================
Custom suite
Total tests run: 8, Failures: 0, Skips: 0
===============================================
The following code will return one result based on 2 criteria:
Entity has these two attributes of type ENUM: process and state.
DB Table consist in a map between process and allowed states. Method findByType will return the correct entry based on one process and one state:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
@Service public class JpaStateMachineDaoImpl implements JpaStateMachineDao { @PersistenceContext private EntityManager em; @Override public StateMachineEntity findByType(SmProcess process, SmState state) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<StateMachineEntity> cq = cb.createQuery(StateMachineEntity.class); Root<StateMachineEntity> model = cq.from(StateMachineEntity.class); List<Predicate> predicateList = new ArrayList<Predicate>(); Predicate processPredicate = cb.equal(model.get("process"), process.ordinal()); Predicate statePredicate = cb.equal(model.get("state"), state.ordinal()); predicateList.add(processPredicate); predicateList.add(statePredicate); Predicate[] predicates = new Predicate[predicateList.size()]; predicateList.toArray(predicates); cq.where(predicates); TypedQuery<StateMachineEntity> q = em.createQuery(cq); return q.getSingleResult(); } } |