AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-330457

Jin Kwon's questions

Martin Hope
Jin Kwon
Asked: 2025-04-07 13:53:34 +0800 CST

我如何覆盖 super.nested 测试类?

  • 5

假设我有一个抽象超类和一个用于测试子类的抽象测试类。

abstract class Superclass {
}

@RequiredArgsConstructor
abstract class SuperclassTest<T extends Superclass> {

    SuperclassTest(Class<T> typeClass) {
        super();
        this.typeClass = typeClass;
    }

    @Nested
    class ToStringTest {

         @Test
         void _NotBlank_NewInstance() {
             String string = newInstance().toString();
             assertThat(string).isNotBlank();
         }
    }

    final Class<T> typeClass;
}

现在,我如何覆盖嵌套的测试类/方法来禁用或修改?

class SomeTest extends SuperclassTest<Some> {

    SomeTest() {
        super(Some.class)
    }

    // How can I disable or change ToStringTest#_NotBlank_NewInstance?
}
java
  • 2 个回答
  • 23 Views
Martin Hope
Jin Kwon
Asked: 2024-10-07 11:15:14 +0800 CST

StandardOpenOption.APPEND 是否需要 StandardOpenOption.WRITE?

  • 5

根据文档,StandardOpenOption.APPEND似乎它需要StandardOpenOption.WRITE。

如果打开文件进行WRITE访问,那么字节将被写入文件末尾而不是开头。

如果该文件由其他程序打开以进行写访问,并且写入文件末尾是原子的,则它是特定于文件系统的。

这是真的吗?

我StandardOpenOption.APPEND仅测试了它,并且它有效。

@Test
void __(@TempDir final Path dir) throws Exception {
    final var path = Files.createTempFile(dir, null, null);
    final var b = ByteBuffer.allocate(12);
    try (var channel = FileChannel.open(path, StandardOpenOption.APPEND)) {
        while (b.hasRemaining()) {
            channel.write(b);
        }
        channel.force(true);
    }
    Assertions.assertEquals(Files.size(path), b.capacity());
}

这个评论是什么意思?

如果打开文件进行WRITE访问,那么字节将被写入文件末尾而不是开头。

java
  • 2 个回答
  • 26 Views
Martin Hope
Jin Kwon
Asked: 2024-09-20 13:20:56 +0800 CST

解码 System#lineSeparator 的正确字符集是什么?

  • 7

假设我们需要验证以下方法。

    /**
     * Prints {@code hello, world}, to the {@link System#out}, followed by a system dependent line separator.
     *
     * @param args command line arguments.
     */
    public static void main(String... args) {
        System.out.printf("hello, world%n"); // %n !!!!!!
    }

现在我们可以验证该方法是否打印hello, world。

    /**
     * Verifies that the {@link HelloWorld#main(String...)} method prints {@code hello, world}, to the
     * {@link System#out}, followed by a system-dependent line separator.
     *
     * @author Jin Kwon &lt;onacit_at_gmail.com&gt;
     * @see System#lineSeparator()
     */
    @DisplayName("main(args) prints 'hello, world' followed by a system-dependent line separator")
    @Test
    public void main_PrintHelloWorld_() {
        final var out = System.out;
        try {
            // --------------------------------------------------------------------------------------------------- given
            final var buffer = new ByteArrayOutputStream();
            System.setOut(new PrintStream(buffer));
            // ---------------------------------------------------------------------------------------------------- when
            HelloWorld.main();
            // ---------------------------------------------------------------------------------------------------- then
            final var actual = buffer.toByteArray();
            final var expected = ("hello, world" + System.lineSeparator()).getBytes(StandardCharsets.US_ASCII);
            Assertions.assertArrayEquals(actual, expected);
        } finally {
            System.setOut(out);
        }
    }

有疑问的部分是.getBytes(StandardCharsets.US_ASCII)。

我认为,假设系统相关的行分隔符用 进行编码并没有错US_ASCII。

Charset #defaultCharset()是否适合%n?

java
  • 1 个回答
  • 53 Views
Martin Hope
Jin Kwon
Asked: 2024-09-03 12:26:23 +0800 CST

如何在 ParameterizedTypeReference<T> 上指定 T 的实际类型?

  • 5

我有一个控制器定义了以下方法。


    Mono<EntityModel<Some>> readSome(...) {
        // works, simply.
    }

    Flux<EntityModel<Other>> readOther(...) {
        // works, simply.
    }

并且,在我的测试类中,我定义了以下有效的方法。

    // WORKS!
    static List<EntityModel<Some>> readSome(...) {
        client...
                .exchange()
                .expectStatus().isOk()
                .expectBodyList(new TypeReferences.EntityModelType<Some>() {
                })
                .returnResult()
                .getResponseBody();
    }

    // WORKS!
    static List<EntityModel<Other>> readOther(...) {
        client...
                .exchange()
                .expectStatus().isOk()
                .expectBodyList(new TypeReferences.EntityModelType<Other>() {
                })
                .returnResult()
                .getResponseBody()
    }

我自然而然地为所有类型定义了以下方法。

如您所见,它只是所有类型的通用方法。

    private static <T> List<EntityModel<T>> readList(final WebTestClient client,
                                                     final Function<UriBuilder, URI> uriFunction,
                                                     @Nullable final String accept) {
        final var responseBody = client
                .get()
                .uri(uriFunction)
                .headers(h -> {
                    Optional.ofNullable(accept)
                            .map(MediaType::valueOf)
                            .map(List::of)
                            .ifPresent(h::setAccept);
                })
                .exchange()
                .expectStatus().isOk()
                .expectBodyList(new TypeReferences.EntityModelType<T>() { // NOT <T>, BUT Map
                })
                .returnResult()
                .getResponseBody();
        return Objects.requireNonNull(responseBody, "responseBody is null");
    }

现在,当我用该方法改变readSome|方法时,我看到了一种奇怪的行为。readOtherreadList

Jackson 不再将内容解析为,而是解析为LinkedHashMap。

我该如何使用Class<T>来指定TypeReferences.EntityModelType<T>?

spring-hateoas
  • 1 个回答
  • 31 Views
Martin Hope
Jin Kwon
Asked: 2024-08-05 12:19:21 +0800 CST

如何在 Mono 上调用阻塞 IO 调用

  • 5

我有类似这样的实用方法。

    public static WebClient.ResponseSpec retrieve(final String baseUrl, final Duration responseTimeout) {
        // ...
    }

    public static <T> Flux<T> retrieveBodyToFlux(final String baseUrl, final Duration responseTimeout,
                                                 final Class<T> elementClass) {
        return retrieve(baseUrl, responseTimeout)
                .bodyToFlux(elementClass);
    }

    public static Mono<Void> download(final String baseUrl, final Duration responseTimeout,
                                      final Path destination, final OpenOption... openOptions) {
        return DataBufferUtils.write(
                retrieveBodyToFlux(baseUrl, responseTimeout, DataBuffer.class),
                destination,
                openOptions
        );
    }

现在我想添加另一种方法让客户端使用临时文件。

    // Let me download the file, and you can just cononsume the file!
    public static Mono<Void> download(final String baseUrl, final Duration responseTimeout,
                                      final Consumer<? super Path> consumer) {

        // Create a temp file
        // download the URL to the file
        // accept the file to the consumer; possibly blocking IO operations
        // don't forget to delete the file!
    }

我怎样才能在没有任何阻塞呼叫警告的情况下完成这项工作?

我试过了(似乎有效),但我不知道我做了什么。这是最佳选择吗?我还有其他方法吗?

        return Mono.usingWhen(
                        Mono.fromCallable(() -> Files.createTempFile(null, null)).subscribeOn(Schedulers.boundedElastic()),
                        p -> download(baseUrl, responseTimeout, p, StandardOpenOption.WRITE)
                                .then(Mono.just(p))
                                .doOnNext(consumer).subscribeOn(Schedulers.boundedElastic()),
                        p -> Mono.fromCallable(() -> {
                            Files.delete(p);
                            return null;
                        }).publishOn(Schedulers.boundedElastic()).then()
                )
                .then();
spring-webflux
  • 1 个回答
  • 18 Views
Martin Hope
Jin Kwon
Asked: 2024-02-05 02:55:34 +0800 CST

如何获得 Instant#ofEpochSecond(?) 的最大值

  • 7

我刚刚发现 具有Instant#ofEpochSecond(epochSecond)最小值/最大值。

这里是源代码。

// Instant.java
    /**
     * The minimum supported epoch second.
     */
    private static final long MIN_SECOND = -31557014167219200L;
    /**
     * The maximum supported epoch second.
     */
    private static final long MAX_SECOND = 31556889864403199L; // << I WANT THIS VALUE!

我如何以MAX_SECOND编程方式获取?

我试图弄清楚Range,

        final var range = ChronoField.INSTANT_SECONDS.range();
        log.debug("        minimum: {}", range.getMinimum());
        log.debug(" largestMinimum: {}", range.getLargestMinimum());
        log.debug("        maximum: {}", range.getMaximum());
        log.debug("smallestMaximum: {}", range.getSmallestMaximum());

并且没有运气。

03:53:36.120 [                main] DEBUG -         minimum: -9223372036854775808
03:53:36.122 [                main] DEBUG -  largestMinimum: -9223372036854775808
03:53:36.122 [                main] DEBUG -         maximum: 9223372036854775807
03:53:36.122 [                main] DEBUG - smallestMaximum: 9223372036854775807
java
  • 1 个回答
  • 48 Views
Martin Hope
Jin Kwon
Asked: 2023-11-05 18:34:08 +0800 CST

如何随机化现有的字节数组?

  • 7

我创建了一个字节数组。

array = bytearray(random.randint(1, 8192))

# Now, how can I randomize array's elements?

现在我怎样才能随机化 的每个元素array?

就像,

// with Java
var array = new byte[ThreadLocalRandom.current().nextInt(1, 8192)];
ThreadLocalRandom.current().nextBytes(array);
python
  • 3 个回答
  • 78 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve