国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

對象克隆問題

JeOam / 1781人閱讀

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

問題描述

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

@Test
public void updateTest() {

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

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

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

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

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

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

    logger.debug("設置不確定度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. 初始化內嵌不確定度");
    AccuracyEmbeddable accuracyEmbeddable1 = this.getOneAccuracyEmbeddable();
    AccuracyEmbeddable accuracyEmbeddable2 = this.getOneAccuracyEmbeddable();
    AccuracyEmbeddable accuracyEmbeddable3 = this.getOneAccuracyEmbeddable();

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

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

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

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

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

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

    logger.debug("設置內嵌測量范圍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. 構造新計量單位");
    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. 斷言相關不確定度");
    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. 斷言相關非強檢器具");
    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. 斷言相關參量");
    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. 斷言相關附加參量");
    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());
        }
    }
}

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

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

    return accuracyEmbeddable;
}

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

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

    return measureScaleEmbeddable;
}

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

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

分析

具體內部怎么運行的不知道,但是應該是前面的引用修改狀態,后面的就跟著變了。

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

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

網上寫的克隆方法,總覺得不好,最后還要強轉。

在實體中寫的克隆對象的方法,就是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方法獲取新對象并設置。

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

再次執行測試,出現了預期中的錯誤,證明就是對象引用的問題。

總結

測試驅動開發,如果我是寫完方法再寫測試,這應該是一個能通過卻有問題的測試,而我卻不知道。

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/8872.html

相關文章

  • js克隆一個對象,支持循環引用的克隆

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

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

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

    douzifly 評論0 收藏0
  • 【轉】JavaScript 對象的深度克隆

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

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

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

    BoYang 評論0 收藏0
  • Java 作者談克隆方法的實現

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

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

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

    CatalpaFlat 評論0 收藏0

發表評論

0條評論

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