/** * Test method for {@link * edu.depaul.se491.models.BaseModel#BaseModel(edu.depaul.se491.daos.DAOFactory, * edu.depaul.se491.beans.CredentialsBean)}. */ @Test public void testBaseModel() { BaseModel model = new BaseModel(null, null); assertNotNull(model); assertNull(model.getResponseStatus()); assertEquals("", model.getResponseMessage()); }
@SuppressWarnings("unchecked") public static <X> X setNestedValue(BaseModel model, List<String> paths, Object value) { int index = paths.size() - 1; String path = paths.get(index); paths.remove(index); BaseModel m = getNestedValue(model, paths); m.set(path, value); return (X) m.get(path); }
@Test public void testGetDAOFactory() { BaseModel model = new BaseModel(null, null); assertNull(model.getDAOFactory()); model = new BaseModel(daoFactory, null); assertNotNull(model.getDAOFactory()); assertSame(daoFactory, model.getDAOFactory()); }
@Test public void testExceptions() { BaseModel model = new BaseModel( new TestDAOFactory(new ExceptionConnectionFactory()), new CredentialsBean("admin", "password")); assertNull(model.getLoggedinAccount()); assertEquals(Response.Status.INTERNAL_SERVER_ERROR, model.getResponseStatus()); }
@Override public Object intercept( final Object proxy, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable { final String methodName = method.getName(); if (logger.isDebugEnabled()) { logger.debug( "Intercept ModelInterceptor : " + method.getName() + " " + methodProxy.getSuperName()); } if (methodName.startsWith("set")) { final Field property = FieldUtils.getField(model.getClass(), methodName.substring(3)); if (property != null && (args != null && args.length == 1) && !property.isAnnotationPresent(Transient.class)) { final String propertyName = property.getName().toUpperCase(); final BaseModel baseModel = (BaseModel) model; final Set<String> m$aop = baseModel.getM$aop(); if (!m$aop.contains(propertyName)) { if (!createdByInstance) { // 如果是用类名创建代理时,只要是赋过值,就算变更 baseModel.addM$aop(propertyName); } else { final Object originValue = FieldUtils.readField(model, property); if (!ObjectUtils.isEquals(originValue, args[0])) { baseModel.addM$aop(propertyName); } } } } } if ("finalize".equals(methodName)) { model = null; return methodProxy.invokeSuper(proxy, args); } else { return MethodUtils.invokeMethod(model, method, args); } }
@Override public void load(JSONObject object) throws JSONException { super.load(object); name = getString(object, "name"); JSONObject topic = object.getJSONArray("topics").getJSONObject(0); numberOfOpenSuggestions = topic.getInt("open_suggestions_count"); numberOfVotesAllowed = topic.getInt("votes_allowed"); categories = deserializeList(topic, "categories", Category.class); if (categories == null) categories = new ArrayList<Category>(); }
@SuppressWarnings("unchecked") public static <X> X getNestedValue(BaseModel model, List<String> paths) { Object obj = model.get(paths.get(0)); if (paths.size() == 1) { return (X) obj; } else if (obj != null && obj instanceof BaseModel) { List<String> tmp = new ArrayList<String>(paths); tmp.remove(0); return (X) getNestedValue((BaseModel) obj, tmp); } return null; }
@Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeString(name); out.writeString(type); out.writeString(mode); out.writeInt(avg); out.writeInt(numAnswers); out.writeParcelableArray(options.toArray(new QuestionOption[options.size()]), 0); out.writeParcelableArray(choices.toArray(new Choice[choices.size()]), 0); out.writeParcelableArray(answerAverages.toArray(new AnswerAverage[answerAverages.size()]), 0); }
/** {@inheritDoc} */ public void fetch( final MembershipCriteriaVerificationRequest inRequest, final boolean inUseClientCacheIfAvailable) { final EventBus eventBus = Session.getInstance().getEventBus(); final String criterion = inRequest.getMembershipCriteria().getCriteria(); if (inRequest.isGroup()) { super.callReadAction( "groupLookup", new GroupLookupRequest(criterion), new OnSuccessCommand<Boolean>() { public void onSuccess(final Boolean wasFound) { sendSuccessEvent(inRequest.getMembershipCriteria(), wasFound); } }, new OnFailureCommand() { public void onFailure(final Throwable inEx) { eventBus.notifyObservers(new MembershipCriteriaVerificationFailureEvent()); } }, inUseClientCacheIfAvailable); } else { super.callReadAction( "personLookupOrg", new PersonLookupRequest(criterion, MAX_RESULTS), new OnSuccessCommand<ArrayList<PersonModelView>>() { public void onSuccess(final ArrayList<PersonModelView> people) { sendSuccessEvent(inRequest.getMembershipCriteria(), !people.isEmpty()); } }, new OnFailureCommand() { public void onFailure(final Throwable inEx) { eventBus.notifyObservers(new MembershipCriteriaVerificationFailureEvent()); } }, inUseClientCacheIfAvailable); } }
@Test public void testGetLoggedinAccount() { BaseModel model = new BaseModel(daoFactory, null); assertNull(model.getLoggedinAccount()); assertEquals(Response.Status.BAD_REQUEST, model.getResponseStatus()); model = new BaseModel(daoFactory, new CredentialsBean()); assertNull(model.getLoggedinAccount()); assertEquals(Response.Status.BAD_REQUEST, model.getResponseStatus()); model = new BaseModel(daoFactory, new CredentialsBean("randomUsername", "password")); assertNull(model.getLoggedinAccount()); assertEquals(Response.Status.NOT_FOUND, model.getResponseStatus()); for (String username : new String[] {"admin", "manager", "employee1", "customerapp"}) { model = new BaseModel(daoFactory, new CredentialsBean(username, "wrongPassword")); assertNull(model.getLoggedinAccount()); assertEquals(Response.Status.NOT_FOUND, model.getResponseStatus()); model = new BaseModel(daoFactory, new CredentialsBean(username, "password")); assertNotNull(model.getLoggedinAccount()); } }
@Test public void testHasPermission() { BaseModel model = new BaseModel(daoFactory, new CredentialsBean("admin", "password")); assertNotNull(model.getLoggedinAccount()); for (String username : new String[] {"admin", "manager", "employee1", "customerapp"}) { model = new BaseModel(daoFactory, new CredentialsBean(username, "password")); assertNotNull(model.getLoggedinAccount()); assertFalse(model.hasPermission(new AccountRole[] {})); assertTrue(model.hasPermission(new AccountRole[] {model.getLoggedinAccount().getRole()})); } }
@Override protected void readFromParcel(Parcel in) { super.readFromParcel(in); name = in.readString(); type = in.readString(); mode = in.readString(); avg = in.readInt(); numAnswers = in.readInt(); for (Parcelable q : in.readParcelableArray(QuestionOption.class.getClassLoader())) { options.add((QuestionOption) q); ((QuestionOption) q).setQuestion(this); } for (Parcelable c : in.readParcelableArray(Choice.class.getClassLoader())) { choices.add((Choice) c); ((Choice) c).setQuestion(this); } for (Parcelable c : in.readParcelableArray(AnswerAverage.class.getClassLoader())) { answerAverages.add((AnswerAverage) c); ((AnswerAverage) c).setQuestion(this); } }
@Override protected Object readResolve() { super.readResolve(); // FJE: If the propertyMap field has something in it, it could be: // a pre-1.3b66 token that contains a HashMap<?,?>, or // a pre-1.3b78 token that actually has the CaseInsensitiveHashMap<?>. // Newer tokens will use propertyMapCI so we only need to make corrections // if the old field has data in it. if (propertyMap != null) { if (propertyMap instanceof CaseInsensitiveHashMap) { propertyMapCI = (CaseInsensitiveHashMap<Object>) propertyMap; } else { propertyMapCI = new CaseInsensitiveHashMap<Object>(); propertyMapCI.putAll(propertyMap); propertyMap.clear(); // It'll never be written out, but we should free the memory. } propertyMap = null; } // 1.3 b77 if (exposedAreaGUID == null) { exposedAreaGUID = new GUID(); } return this; }
@Override public void setRelationItems(Relation relation, List<? extends StateModel> items) { if (relation.getModel() == QuestionOption.class) { options = new ArrayList<QuestionOption>(); for (StateModel m : items) { options.add((QuestionOption) m); ((QuestionOption) m).setQuestion(this); } } else if (relation.getModel() == Choice.class) { choices = new ArrayList<Choice>(); for (StateModel m : items) { choices.add((Choice) m); ((Choice) m).setQuestion(this); } } else if (relation.getModel() == AnswerAverage.class) { answerAverages = new ArrayList<AnswerAverage>(); for (StateModel m : items) { answerAverages.add((AnswerAverage) m); ((AnswerAverage) m).setQuestion(this); } } else { super.setRelationItems(relation, items); } }
@Test public void testSetResponseMessageAndStatus() { BaseModel model = new BaseModel(null, null); model.setResponseMessage("message"); assertNotNull(model.getResponseMessage()); assertEquals("message", model.getResponseMessage()); model.setResponseStatus(Response.Status.OK); assertNotNull(model.getResponseStatus()); assertEquals(Response.Status.OK, model.getResponseStatus()); model.setResponseAndMeessageForDBError(new SQLException("exception test")); assertNotNull(model.getResponseMessage()); assertEquals(BaseModel.GENERIC_SERVER_ERR_MSG, model.getResponseMessage()); assertNotNull(model.getResponseStatus()); assertEquals(Response.Status.INTERNAL_SERVER_ERROR, model.getResponseStatus()); }
@Override public void load(JSONObject object) throws JSONException { super.load(object); name = getString(object, "name"); numberOfArticles = object.getInt("article_count"); }