成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

對象克隆問題

tianren124 / 1344人閱讀

摘要:斷言不確定度,非強檢器具,參量以及附加參量都修改成功。再次執(zhí)行測試,出現(xiàn)了預(yù)期中的錯誤,證明就是對象引用的問題。

問題描述

編寫觀察者的單元測試,執(zhí)行以下測試通過。斷言不確定度,非強檢器具,參量以及附加參量都修改成功。

@Test
public void updateTest() {

    logger.debug("0. 基礎(chǔ)信息準備");

    logger.debug("構(gòu)造計量單位");
    MeasurementUnit oldMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit();

    logger.debug("1. 初始化不確定度實體");

    logger.debug("獲取基礎(chǔ)的不確定度");
    AccuracyUncertainty accuracyUncertainty1 = accuracyUncertaintyService.getOneUnSavedObject();
    AccuracyUncertainty accuracyUncertainty2 = accuracyUncertaintyService.getOneUnSavedObject();
    AccuracyUncertainty accuracyUncertainty3 = accuracyUncertaintyService.getOneUnSavedObject();
    accuracyUncertaintyService.getOneSavedObject();

    logger.debug("設(shè)置不確定度1的最小與最大單位");
    accuracyUncertainty1.setMinAccuracyUnit(oldMeasurementUnit);
    accuracyUncertainty1.setMaxAccuracyUnit(oldMeasurementUnit);

    logger.debug("設(shè)置不確定度2的最小單位");
    accuracyUncertainty2.setMinAccuracyUnit(oldMeasurementUnit);

    logger.debug("設(shè)置不確定度3的最大單位");
    accuracyUncertainty3.setMaxAccuracyUnit(oldMeasurementUnit);

    logger.debug("持久化不確定度列表");
    List accuracyUncertaintyList = new ArrayList<>();
    accuracyUncertaintyList.add(accuracyUncertainty1);
    accuracyUncertaintyList.add(accuracyUncertainty2);
    accuracyUncertaintyList.add(accuracyUncertainty3);
    accuracyUncertaintyRepository.save(accuracyUncertaintyList);

    logger.debug("2. 初始化內(nèi)嵌不確定度");
    AccuracyEmbeddable accuracyEmbeddable1 = this.getOneAccuracyEmbeddable();
    AccuracyEmbeddable accuracyEmbeddable2 = this.getOneAccuracyEmbeddable();
    AccuracyEmbeddable accuracyEmbeddable3 = this.getOneAccuracyEmbeddable();

    logger.debug("設(shè)置內(nèi)嵌不確定度1的最大與最小單位");
    accuracyEmbeddable1.setMinAccuracyUnit(oldMeasurementUnit);
    accuracyEmbeddable1.setMaxAccuracyUnit(oldMeasurementUnit);

    logger.debug("設(shè)置內(nèi)嵌不確定度2的最小單位");
    accuracyEmbeddable2.setMinAccuracyUnit(oldMeasurementUnit);

    logger.debug("設(shè)置內(nèi)嵌不確定度3的最大單位");
    accuracyEmbeddable3.setMaxAccuracyUnit(oldMeasurementUnit);

    logger.debug("3. 初始化內(nèi)嵌測量范圍");
    MeasureScaleEmbeddable measureScaleEmbeddable1 = this.getOneMeasureScaleEmbeddable();
    MeasureScaleEmbeddable measureScaleEmbeddable2 = this.getOneMeasureScaleEmbeddable();
    MeasureScaleEmbeddable measureScaleEmbeddable3 = this.getOneMeasureScaleEmbeddable();

    logger.debug("設(shè)置內(nèi)嵌測量范圍1的最大與最小單位");
    measureScaleEmbeddable1.setMinMeasureScaleUnit(oldMeasurementUnit);
    measureScaleEmbeddable1.setMaxMeasureScaleUnit(oldMeasurementUnit);

    logger.debug("設(shè)置內(nèi)嵌測量范圍2的最小單位");
    measureScaleEmbeddable2.setMinMeasureScaleUnit(oldMeasurementUnit);

    logger.debug("設(shè)置內(nèi)嵌測量范圍3的最大單位");
    measureScaleEmbeddable3.setMaxMeasureScaleUnit(oldMeasurementUnit);

    logger.debug("4. 初始化非強檢器具");
    NonMandatoryInstrument nonMandatoryInstrument1 = nonMandatoryInstrumentService.getOneUnSavedObject();
    NonMandatoryInstrument nonMandatoryInstrument2 = nonMandatoryInstrumentService.getOneUnSavedObject();
    NonMandatoryInstrument nonMandatoryInstrument3 = nonMandatoryInstrumentService.getOneUnSavedObject();
    nonMandatoryInstrumentService.getOneSavedObject();

    nonMandatoryInstrument1.setMeasureScale(measureScaleEmbeddable1);
    nonMandatoryInstrument2.setMeasureScale(measureScaleEmbeddable2);
    nonMandatoryInstrument3.setMeasureScale(measureScaleEmbeddable3);

    List nonMandatoryInstrumentList = new ArrayList<>();
    nonMandatoryInstrumentList.add(nonMandatoryInstrument1);
    nonMandatoryInstrumentList.add(nonMandatoryInstrument2);
    nonMandatoryInstrumentList.add(nonMandatoryInstrument3);

    nonMandatoryInstrumentRepository.save(nonMandatoryInstrumentList);

    logger.debug("5. 初始化參量");
    Parameter parameter1 = parameterService.getOneUnsavedObject();
    Parameter parameter2 = parameterService.getOneUnsavedObject();
    Parameter parameter3 = parameterService.getOneUnsavedObject();
    Parameter parameter4 = parameterService.getOneUnsavedObject();
    Parameter parameter5 = parameterService.getOneUnsavedObject();
    Parameter parameter6 = parameterService.getOneUnsavedObject();
    parameterService.getOneSavedObject();

    parameter1.setAccuracy(accuracyEmbeddable1);
    parameter2.setAccuracy(accuracyEmbeddable2);
    parameter3.setAccuracy(accuracyEmbeddable3);
    parameter4.setMeasureScale(measureScaleEmbeddable1);
    parameter5.setMeasureScale(measureScaleEmbeddable2);
    parameter6.setMeasureScale(measureScaleEmbeddable3);

    List parameterList = new ArrayList<>();
    parameterList.add(parameter1);
    parameterList.add(parameter2);
    parameterList.add(parameter3);
    parameterList.add(parameter4);
    parameterList.add(parameter5);
    parameterList.add(parameter6);

    parameterRepository.save(parameterList);

    logger.debug("6. 初始化附加參量");
    AdditionalParameter additionalParameter1 = additionalParameterService.getOneUnsavedObject();
    AdditionalParameter additionalParameter2 = additionalParameterService.getOneUnsavedObject();
    AdditionalParameter additionalParameter3 = additionalParameterService.getOneUnsavedObject();
    additionalParameterService.getOneSavedObject();

    additionalParameter1.setMeasureScale(measureScaleEmbeddable1);
    additionalParameter2.setMeasureScale(measureScaleEmbeddable2);
    additionalParameter3.setMeasureScale(measureScaleEmbeddable3);

    List additionalParameterList = new ArrayList<>();
    additionalParameterList.add(additionalParameter1);
    additionalParameterList.add(additionalParameter2);
    additionalParameterList.add(additionalParameter3);

    additionalParameterRepository.save(additionalParameterList);

    logger.debug("7. 初始化檢定能力");

    logger.debug("8. 初始化參量檢定能力");

    logger.debug("9. 初始化附加參量檢定能力");

    logger.debug("10. 初始化校準能力");

    logger.debug("11. 初始化參量校準能力");

    logger.debug("12. 初始化附加參量校準能力");

    logger.debug("13. 構(gòu)造新計量單位");
    MeasurementUnit newMeasurementUnit = new MeasurementUnit();
    String name = CommonService.getRandomStringByLength(10);
    newMeasurementUnit.setName(name);
    newMeasurementUnit.setSymbol("km");
    newMeasurementUnit.setMultiple((oldMeasurementUnit.getMultiple() + 10) * 2);
    newMeasurementUnit.setMeasurementUnitCategory(measurementUnitCategoryService.getOneSavedMeasurementUnitCategory());

    logger.debug("14. 更新并斷言");
    Long id = oldMeasurementUnit.getId();
    measurementUnitService.update(id, newMeasurementUnit);
    MeasurementUnit updatedMeasurementUnit = measurementUnitRepository.findOne(id);
    assertThat(updatedMeasurementUnit.getName()).isEqualTo(newMeasurementUnit.getName());
    assertThat(updatedMeasurementUnit.getSymbol()).isEqualTo(newMeasurementUnit.getSymbol());
    assertThat(updatedMeasurementUnit.getMultiple()).isEqualTo(newMeasurementUnit.getMultiple());
    assertThat(updatedMeasurementUnit.getMeasurementUnitCategory()).isEqualTo(newMeasurementUnit.getMeasurementUnitCategory());

    logger.debug("15. 斷言相關(guān)不確定度");
    List accuracyUncertainties = accuracyUncertaintyRepository.findAllByMinAccuracyUnit_IdOrMaxAccuracyUnit_Id(id, id);
    Assertions.assertThat(accuracyUncertainties.size()).isEqualTo(3);

    for (AccuracyUncertainty accuracyUncertainty : accuracyUncertainties) {
        if (accuracyUncertainty.getMinAccuracyUnit().getId().equals(id)) {
            Assertions.assertThat(accuracyUncertainty.getMinAccuracyAbsoluteValueInTest()).isEqualTo(accuracyUncertainty.getMinAccuracyValue() * newMeasurementUnit.getMultiple());
        }
        if (accuracyUncertainty.getMaxAccuracyUnit().getId().equals(id)) {
            Assertions.assertThat(accuracyUncertainty.getMaxAccuracyAbsoluteValueInTest()).isEqualTo(accuracyUncertainty.getMaxAccuracyValue() * newMeasurementUnit.getMultiple());
        }
    }

    logger.debug("16. 斷言相關(guān)非強檢器具");
    List nonMandatoryInstruments = nonMandatoryInstrumentRepository.findAllByMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id);
    Assertions.assertThat(nonMandatoryInstruments.size()).isEqualTo(3);

    for (NonMandatoryInstrument nonMandatoryInstrument : nonMandatoryInstruments) {
        MeasureScaleEmbeddable measureScaleEmbeddable = nonMandatoryInstrument.getMeasureScale();
        if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) {
            Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple());
        }
        if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) {
            Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple());
        }
    }

    logger.debug("17. 斷言相關(guān)參量");
    List parameters = parameterRepository.findAllByAccuracy_MinAccuracyUnit_IdOrAccuracy_MaxAccuracyUnit_IdOrMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id, id, id);
    Assertions.assertThat(parameters.size()).isEqualTo(6);

    for (Parameter parameter : parameters) {
        AccuracyEmbeddable accuracyEmbeddable = parameter.getAccuracy();
        MeasureScaleEmbeddable measureScaleEmbeddable = parameter.getMeasureScale();
        if (accuracyEmbeddable.getMinAccuracyUnit().getId().equals(id)) {
            Assertions.assertThat(accuracyEmbeddable.getMinAccuracyAbsoluteValueInTest()).isEqualTo(accuracyEmbeddable.getMinAccuracyValue() * newMeasurementUnit.getMultiple());
        }
        if (accuracyEmbeddable.getMaxAccuracyUnit().getId().equals(id)) {
            Assertions.assertThat(accuracyEmbeddable.getMaxAccuracyAbsoluteValueInTest()).isEqualTo(accuracyEmbeddable.getMaxAccuracyValue() * newMeasurementUnit.getMultiple());
        }
        if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) {
            Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple());
        }
        if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) {
            Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple());
        }
    }

    logger.debug("18. 斷言相關(guān)附加參量");
    List additionalParameters = additionalParameterRepository.findAllByMeasureScale_MinMeasureScaleUnit_IdOrMeasureScale_MaxMeasureScaleUnit_Id(id, id);
    Assertions.assertThat(additionalParameters.size()).isEqualTo(3);

    for (AdditionalParameter additionalParameter : additionalParameters) {
        MeasureScaleEmbeddable measureScaleEmbeddable = additionalParameter.getMeasureScale();
        if (measureScaleEmbeddable.getMinMeasureScaleUnit().getId().equals(id)) {
            Assertions.assertThat(measureScaleEmbeddable.getMinMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMinMeasureScaleValue() * newMeasurementUnit.getMultiple());
        }
        if (measureScaleEmbeddable.getMaxMeasureScaleUnit().getId().equals(id)) {
            Assertions.assertThat(measureScaleEmbeddable.getMaxMeasureScaleAbsoluteValueInTest()).isEqualTo(measureScaleEmbeddable.getMaxMeasureScaleValue() * newMeasurementUnit.getMultiple());
        }
    }
}

/**
 * 獲取一個精度內(nèi)嵌實體
 */
private AccuracyEmbeddable getOneAccuracyEmbeddable() {
    logger.debug("獲取計量單位");
    MeasurementUnit otherMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit();

    logger.debug("構(gòu)造精度內(nèi)嵌實體");
    AccuracyEmbeddable accuracyEmbeddable = new AccuracyEmbeddable();
    accuracyEmbeddable.setMinAccuracyValue(1.0f);
    accuracyEmbeddable.setMinAccuracyUnit(otherMeasurementUnit);
    accuracyEmbeddable.setMaxAccuracyValue(10.0f);
    accuracyEmbeddable.setMaxAccuracyUnit(otherMeasurementUnit);

    return accuracyEmbeddable;
}

/**
 * 獲取內(nèi)嵌測量范圍
 */
private MeasureScaleEmbeddable getOneMeasureScaleEmbeddable() {
    logger.debug("獲取計量單位");
    MeasurementUnit otherMeasurementUnit = measurementUnitService.getOneSavedMeasurementUnit();

    logger.debug("構(gòu)造測量范圍內(nèi)嵌實體");
    MeasureScaleEmbeddable measureScaleEmbeddable = new MeasureScaleEmbeddable();
    measureScaleEmbeddable.setMinMeasureScaleValue(1.0f);
    measureScaleEmbeddable.setMinMeasureScaleUnit(otherMeasurementUnit);
    measureScaleEmbeddable.setMaxMeasureScaleValue(10.0f);
    measureScaleEmbeddable.setMaxMeasureScaleUnit(otherMeasurementUnit);

    return measureScaleEmbeddable;
}

測試通過了,但是并不是我想要的。

因為我在執(zhí)行該測試時,還沒有寫修改附加參量的方法。本測試是期待不通過的。

分析

具體內(nèi)部怎么運行的不知道,但是應(yīng)該是前面的引用修改狀態(tài),后面的就跟著變了。

之前為了少寫幾行代碼,非強檢器具、參量、附加參量這三個用的是同一個測量范圍對象,不知道具體的執(zhí)行過程,但是猜想應(yīng)該是這東西的問題。

嘗試寫了一下克隆方法,每次都克隆一個新對象。

網(wǎng)上寫的克隆方法,總覺得不好,最后還要強轉(zhuǎn)。

在實體中寫的克隆對象的方法,就是new一個然后返回去唄。

public AccuracyEmbeddable cloneAccuracyEmbeddable() {
    AccuracyEmbeddable accuracyEmbeddable = new AccuracyEmbeddable();
    accuracyEmbeddable.setMinAccuracyValue(this.minAccuracyValue);
    accuracyEmbeddable.setMinAccuracyUnit(this.minAccuracyUnit);
    accuracyEmbeddable.setMaxAccuracyValue(this.maxAccuracyValue);
    accuracyEmbeddable.setMaxAccuracyUnit(this.maxAccuracyUnit);
    accuracyEmbeddable.prePersist();
    return accuracyEmbeddable;
}

public MeasureScaleEmbeddable cloneMeasureScaleEmbeddable() {
    MeasureScaleEmbeddable measureScaleEmbeddable = new MeasureScaleEmbeddable();
    measureScaleEmbeddable.setMinMeasureScaleValue(this.minMeasureScaleValue);
    measureScaleEmbeddable.setMinMeasureScaleUnit(this.minMeasureScaleUnit);
    measureScaleEmbeddable.setMaxMeasureScaleValue(this.maxMeasureScaleValue);
    measureScaleEmbeddable.setMaxMeasureScaleUnit(this.maxMeasureScaleUnit);
    measureScaleEmbeddable.prePersist();
    return measureScaleEmbeddable;
}

然后就用clone方法獲取新對象并設(shè)置。

additionalParameter1.setMeasureScale(measureScaleEmbeddable1.cloneMeasureScaleEmbeddable());
additionalParameter2.setMeasureScale(measureScaleEmbeddable2.cloneMeasureScaleEmbeddable());
additionalParameter3.setMeasureScale(measureScaleEmbeddable3.cloneMeasureScaleEmbeddable());

再次執(zhí)行測試,出現(xiàn)了預(yù)期中的錯誤,證明就是對象引用的問題。

總結(jié)

測試驅(qū)動開發(fā),如果我是寫完方法再寫測試,這應(yīng)該是一個能通過卻有問題的測試,而我卻不知道。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72249.html

相關(guān)文章

  • js克隆一個對象,支持循環(huán)引用的克隆

    摘要:判斷參數(shù)是否為待判斷的參數(shù)克隆一個對象要克隆的目標對象克隆節(jié)點,綁定事件的有問題,暫不處理克隆在當前作用域,在全局克隆其它對象,通過識別復(fù)制后的對象與原對象是否相同來決定傳不傳參數(shù),像數(shù)組是不能傳參數(shù)的使用防止對象重寫了方法支持節(jié)點克隆 (function(){ var toString=Object.prototype.toString,gObj={},cloneHelper=f...

    fai1017 評論0 收藏0
  • JavaScript對象克隆

    摘要:原始類型對象指的是字符串數(shù)值布爾值,引用類型對象指的是數(shù)組對象函數(shù)。既然對象分為這兩類,他們的復(fù)制克隆也是有差別的??偨Y(jié)根據(jù)上面的情況,另外,克隆引用對象必須采用完整克隆深度克隆,包括對象的值也是一個對象也要進行完整克隆深度克隆。 前言 之前有人問我如何克隆一個JS對象,我當時沒答上來;過后我查資料弄懂了這個問題,現(xiàn)在整理成文。 正文 JavaScript的一切實例都是對象,但他們也分...

    douzifly 評論0 收藏0
  • 【轉(zhuǎn)】JavaScript 對象的深度克隆

    摘要:在聊以下簡稱深度克隆之前,我們先來了解一下中對象的組成。克隆或者拷貝分為種淺度克隆深度克隆。淺度克隆基本類型為值傳遞,對象仍為引用傳遞。 該文轉(zhuǎn)載自http://www.cnblogs.com/zichi/p/4568150.html,有部分修改。 在聊JavaScript(以下簡稱js)深度克隆之前,我們先來了解一下js中對象的組成。在 js 中一切實例皆是對象,具體分為 原始類型 ...

    JowayYoung 評論0 收藏0
  • ES6時代,你真的會克隆對象嗎(二)

    摘要:多個窗口意味著多個全局環(huán)境,不同的全局環(huán)境擁有不同的全局對象,從而擁有不同的內(nèi)置類型構(gòu)造函數(shù)。比如,表達式會返回,因為屬性得到的僅僅是構(gòu)造函數(shù),而且是可以被手動更改的,只是返回的構(gòu)造函數(shù)的名字,它并不返回類名。 原文:ES6時代,你真的會克隆對象嗎(二) 上一篇,我們從Symbol和是否可枚舉以及屬性描述符的角度分析了ES6下怎么淺拷貝一個對象,發(fā)表在掘金和segmentfault上(...

    BoYang 評論0 收藏0
  • Java 作者談克隆方法的實現(xiàn)

    摘要:不合規(guī)的代碼示例合規(guī)解決方案參閱復(fù)制構(gòu)造函數(shù)與克隆也可以參閱應(yīng)該實現(xiàn)克隆覆蓋的類應(yīng)為并調(diào)用下面為引文翻譯談設(shè)計與作者的對話,作者首次在上發(fā)表,年月日復(fù)制構(gòu)造函數(shù)與克隆在你的書中,你建議使用復(fù)制構(gòu)造函數(shù)而不是實現(xiàn)和編寫。 今天在用 sonar 審核代碼, 偶然看到下面的提示:showImg(https://segmentfault.com/img/bVbqioZ?w=858&h=116)...

    gaomysion 評論0 收藏0
  • js對象詳解(JavaScript對象深度剖析,深度理解js對象)

    摘要:對象詳解對象深度剖析,深度理解對象這算是醞釀很久的一篇文章了。用空構(gòu)造函數(shù)設(shè)置類名每個對象都共享相同屬性每個對象共享一個方法版本,省內(nèi)存。 js對象詳解(JavaScript對象深度剖析,深度理解js對象) 這算是醞釀很久的一篇文章了。 JavaScript作為一個基于對象(沒有類的概念)的語言,從入門到精通到放棄一直會被對象這個問題圍繞。 平時發(fā)的文章基本都是開發(fā)中遇到的問題和對...

    CatalpaFlat 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<