小编dzi*_*iki的帖子

检查列表是否为空的两种方法 - 差异?

我有一个简短的问题.

让我们假设我们有一个List这是一个ArrayListlist.我们想检查列表是否为空.

有什么区别(如果有的话):

if (list == null) { do something }
Run Code Online (Sandbox Code Playgroud)

if (list.isEmpty()) { do something }
Run Code Online (Sandbox Code Playgroud)

我正在研究一种古老的代码(由其他人于2007年左右编写),它正在使用这种list == null结构.但是当我们有list.isEmpty()方法时为什么要使用这种结构......

java list

12
推荐指数
3
解决办法
3万
查看次数

Jasperreports not working on Linux server without X11: "Error initializing graphic environment."

I have a huge problem. I was coding an app which is using Jasperreports. Everything was perfectly fine when I was working locally. But then, when I have dockerized my app, when I run the method that should produce an PDF file, I get an error:

Request processing failed; nested exception is net.sf.jasperreports.engine.JRRuntimeException: Error initializing graphic environment.
Run Code Online (Sandbox Code Playgroud)

I have found out that running my app with

-DJava.awt.headless=true
Run Code Online (Sandbox Code Playgroud)

the problem would solve itself. But it is not.

This is how it …

java linux headless jasper-reports docker

5
推荐指数
1
解决办法
815
查看次数

PLS-00540:尝试编译包规范时,此上下文中不支持该对象

我正在尝试编译包规范.我想包含一些类型定义并声明一个函数:

TYPE col_type AS OBJECT (
col_name VARCHAR2(50)
);

TYPE col_sub_type
IS TABLE OF
col_type;

FUNCTION get_col_tab RETURN col_sub_type;
Run Code Online (Sandbox Code Playgroud)

最后,get_col_tab功能:

FUNCTION get_col_tab RETURN col_sub_type AS
l_type  col_sub_type := col_sub_type();
BEGIN
FOR i IN (SELECT DISTINCT TABLE_NAME t_name FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = 'TABLE_1') LOOP
l_type.extend;
l_type(l_type.last) := col_type(i.t_name);
END LOOP;
RETURN l_type;
END;
Run Code Online (Sandbox Code Playgroud)

但是在编译包规范时,我收到以下错误:

PLS-00540:此上下文中不支持该对象

据我所知,我不能OBJECT在包规范中使用类型.是否有解决方法或其他方法来执行此操作?

提前致谢.

oracle plsql

4
推荐指数
1
解决办法
2549
查看次数

列表排序实现的单元测试?

我目前正在编写一个单元测试,用于检查方法是否正确对列表进行排序。

排序方法覆盖类中的compare方法,Comparator并使用 对列表进行排序Collections.sort()

这可能不是一个技术问题,但我正在寻找一种方法来使用 JUnit 的断言来检查列表是否正确排序......

该列表按它所持有的类型的内部参数排序,我称之为id. 因此,当列表包含 3 个 ID 为 3,1,2 的项目时,它会将它们排序为 1,2,3。

Long expected1 = listOfObjects.get(0).getId()
Long expected2 = listOfObjects.get(1).getId()
Long expected3 = listOfObjects.get(2).getId()
Run Code Online (Sandbox Code Playgroud)

然后在那些 Long 对象上使用断言看起来既不干净也不聪明。我正在寻找一个想法如何干净而巧妙地测试这个案例,但我没有想法......

java junit unit-testing assert

4
推荐指数
1
解决办法
4506
查看次数

在最终类中测试私有方法

我想在最终的utitlity类中测试一个私有方法.

1.班级本身:

班级签名是:

public final class SomeHelper {

    /** Preventing class from being instantiated */
    private SomeHelper() {
    }
Run Code Online (Sandbox Code Playgroud)

并且有私有方法本身:

private static String formatValue(BigDecimal value)
Run Code Online (Sandbox Code Playgroud)

测试已经完成了,但是之前,该方法是在没有私有构造函数的非实用非final类中.

测试已经使用@RunWith(Parameterized.class)了.

现在我得到的只是一个例外:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.some.package.util.SomeHelper
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types
Run Code Online (Sandbox Code Playgroud)

2.测试

该测试中最重要的一行是:

String result = Whitebox.invokeMethod(mValue, "formatValue", mGiven);
Run Code Online (Sandbox Code Playgroud)

有没有办法让测试工作?

java junit mockito powermock

4
推荐指数
1
解决办法
815
查看次数

BASH:百分比变化-如何计算?没有BC如何获得绝对值?

我需要计算两个值之间的百分比变化。

我在这里的代码:

echo $time1
echo $time2
pc=$(( (($time2 - $time1)/$time1 * 100) ))
echo $pc
Run Code Online (Sandbox Code Playgroud)

在控制台中带来这样的输出(带有set -xe选项)

+ echo 1800
1800
+ echo 1000
1000
+ pc=0
+ echo 0
Run Code Online (Sandbox Code Playgroud)

数学表达式中的代码似乎编写正确,但是,我得到-80左右。为什么这发生在我身上?

问题的第二部分。我无权访问,也将无权访问bc命令。据我所知,它可以给我绝对数字。

所以..如果没有bc命令-这对于IF条件是个好主意吗?

if (( (( "$pc" > 20 || (( "$pc" < -20 )); then...
Run Code Online (Sandbox Code Playgroud)

math bash arithmetic-expressions

3
推荐指数
1
解决办法
7422
查看次数

返回不同类型的方法?

我正在写一个方法.

这是层次结构树:

IProtocoll
|
|--ProtocolImpl1
|
|--ProtocolImpl2
|
|--ProtocolImpl3
Run Code Online (Sandbox Code Playgroud)

该方法本身如下所示:

public static List<IProtocol> getProtocolsByType(String type, Transaction trx) {
    Iterator<IProtocol> protocols = trx.getProtocols();
    List<IProtocol> protocolsList = new ArrayList<IProtocol>();
    while (protocols.hasNext()) {
        if (StringHeper.isEqual(protocolls.next().getProtocolType(), type) {
            protocolsList.add(protocolls.next());
        }
    }
    return protocolsList
}
Run Code Online (Sandbox Code Playgroud)

和用法示例.

List<IProtocol> list = ProtocolHelper.getrProtocolsByType("PROTOCOL1", trx)

for (IProtocol protocol : list) {
    ProtocolHelper.createProtocolType1((ProtocolImpl1) protocol)
}
Run Code Online (Sandbox Code Playgroud)

现在 - 如在类型层次结构中所见 - 这种方法可以返回3种可能性.

String type正在定义应返回什么类型的协议.trx.getProtocols()将返回包含所有3种类型协议的迭代器.

有没有办法以某种方式统一这个方法,它将返回这3种类型中的一种,而不使用以后使用该方法时不必要的转换?

java collections iterator

3
推荐指数
1
解决办法
97
查看次数

@SpringBootTest 需要数据库连接吗?

我有一些集成测试,我正在使用测试容器。但我突然意识到,当我的应用程序的带有数据库的 docker 容器关闭时,所有其他测试(不包括使用 Testcontainers 的集成测试)都会失败(甚至是contextLoads()Spring Bootinitializr 生成的测试)

我得到:

java.lang.IllegalStateException:无法在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)加载ApplicationContext

导致:org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名称为“liquibase”的bean时出错

[org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]:调用init方法失败;嵌套异常是 liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: 通信链接失败

很明显应用程序想要连接数据库,而数据库容器宕机了。

我一直在调查,但我不记得曾经需要仅为应用程序的测试/构建过程启动容器,所以这个问题对我来说是新问题。但如果有什么地方做错了,也可能是在我的AbstractDatabaseIT课堂上:

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(initializers = AbstractDatabaseIT.DockerMySqlDataSourceInitializer.class)
@Testcontainers
public abstract class AbstractDatabaseIT {

    private static final String MYSQL_IMAGE_NAME = "mysql:5.7.24";

    public static final MySQLContainer<?> mySQLContainer = new MySQLContainer<>(MYSQL_IMAGE_NAME);

    static {
        mySQLContainer.start();
    }

    public static class DockerMySqlDataSourceInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

        @Override
        public void initialize(@NotNull ConfigurableApplicationContext applicationContext) {

            Map<String, String> parameters = new HashMap<>();
            parameters.put("command", "--character-set-server=utf8");
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
                    applicationContext,
                    "spring.datasource.url=" …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-boot testcontainers

3
推荐指数
1
解决办法
5032
查看次数

Set.of(E... elements) - 它使用哪个 Set 实现?它与 new HashSet&lt;&gt;() 有何关系?

我想问 - 有什么区别

Set<CurrencyType> set1 = new HashSet<>() {{
    add("a");
    add("b");
    add("c");
}}
Run Code Online (Sandbox Code Playgroud)

Set<CurrencyType> set2 = Set.of(
    "a",
    "b",
    "c"
)
Run Code Online (Sandbox Code Playgroud)

在名为 @Test 的调试模式下MySillyTest(这个名称很快就会回来),我可以看到它set1是 的一个实例MySillyTest$1,但我认为它只是一个HashSet. set2另一方面是 的一个实例ImmutableCollections$SetN。这两者之间真正的区别是什么?java.util.Set使用什么实现Set.of()?这两者之间有性能/内存使用/CPU 使用差异吗?

java collections set hashset

2
推荐指数
1
解决办法
1654
查看次数

即使在复杂的IF语句中,RETURN是否随时都会使用该方法?

我有这样的代码:

private final Something (SomethingElse nimportant)

    List list = (List) mListOfSomething.get(SOME_ENUMS);

    if (list == null) 
// dont ask me why not list.isEmpty(), I'm not the author of this code)
        list = new ArrayList();

        if (transaction.getPartner() instanceof IAnotherSomething) {

            IAnotherSomething ias = (IAnotherSomething) transaction.getPartner();
            return ias.getContract().next();
        }

// NIMPORTANT code ommited, produces boolean "something"

        if (something) {
            // something happens
            list.add(contract)
        }

        if (!something) {
            // something else happens
            list.add(contract)
        }
    }

    return (IContract) list.get(0);
}
Run Code Online (Sandbox Code Playgroud)

我有一个脑力劳动 - 如果列表为空,(或== null)并且这个if语句(transaction.getPartner() …

java if-statement return

1
推荐指数
1
解决办法
41
查看次数

变量为null但它确实不是?

我正在使用Java + JavaFX编写一个小应用程序.

在控制器类我有我的PasswordField:

@FXML
PasswordField password;
Run Code Online (Sandbox Code Playgroud)

以及临时存储密码的字段:

private String rootPassword;
Run Code Online (Sandbox Code Playgroud)

然后我有一个方法来处理密码:

private void handlePasswordField() {
    if (!password.getText().isEmpty()) {
        this.rootPassword = password.getText();
    } else {
        Alert alert = new Alert(AlertType.WARNING);
        alert.initOwner(nbtcInstaller.getPrimaryStage());
        alert.setTitle("No password");
        alert.setHeaderText("No password provided");
        alert.setContentText("Please enter a new ROOT password");
        alert.showAndWait();
    }
}
Run Code Online (Sandbox Code Playgroud)

以及按下"安装"按钮后启动的方法:

@FXML
private void handleCommand() {
    handlePasswordField();
    doAllTheOtherStuff();
    Platform.exit();
}
Run Code Online (Sandbox Code Playgroud)

当我没有提供密码时,我会收到警报,但doAllTheOtherStuff()仍然会启动.doAllTheOtherStuff()如果没有提供密码,我该怎么做才能阻止它启动方法?

java alert javafx

1
推荐指数
1
解决办法
66
查看次数

为什么标准迭代器比 Kotlin 中的序列更快?

我是序列的新手,所以我可能做了一些(或多或少)非常错误的事情,但我有一个问题:

我写了两个函数:

fun isPrimeNumber1(number: Int): Boolean {
    if (number <= 1) return false
    for (divider in 2 .. number / 2) {
        if ( number % divider == 0 ) return false
    }
    return true
}
Run Code Online (Sandbox Code Playgroud)

fun isPrimeNumber2(number: Int): Boolean {
    if (number <= 1) return false
    return !(2 .. number / 2).asSequence().map { it }.any { number % it == 0 }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在运行一个用 编写的测试kotest,其中两个函数都接收Int.MAX_VALUEnumber.

class MyTestPrime : FunSpec({
    context("Prime numbers") { …
Run Code Online (Sandbox Code Playgroud)

optimization sequence kotlin kotest

1
推荐指数
1
解决办法
293
查看次数

Mule 4 - 流程突然停止初始化,出现有关某些神秘 yml 文件的警告,MUnit 测试开始抛出意外错误

今天我正在开发 Mule 4 应用程序,突然我的 Anypoint Studio 变得疯狂了。当我尝试运行我的应用程序时,我收到一条错误,表明应用程序使用的端口已在使用中。没有什么异常,但是当我重新启动电脑并想再次运行该应用程序时,控制台中出现了可疑信息:

信息 2021-05-25 12:40:21,056 [WrapperListener_start_runner] [处理器:;事件:] org.mule.runtime.core.internal.construct.AbstractFlowConstruct:流 ImportPendingCorrectionsFlow 尚未启动 INFO 2021-05-25 12:40:21,056 [WrapperListener_start_runner] [处理器:; 事件:] org.mule.runtime.core.internal.construct.FlowConstructLifecycleManager:启动流程:ImportInvoicesFlow INFO 2021-05-25 12:40:21,056 [WrapperListener_start_runner] [处理器:; 事件:] org.mule.runtime.core.internal.construct.FlowConstructLifecycleManager:停止流程:ImportInvoicesFlow

我的应用程序中的所有流程都有相同的消息。

然后我尝试运行现有的 MUnit 测试,该测试自一个月或更长时间以来一直正常运行,但收到​​此错误:

++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++

  • 运行测试:ImportInvoicesFlowTestWithIn CorrectVehicleCount - 测试 + +++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++ +++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++ java.lang.AssertionError: 抛出的错误 ID 与预期的不匹配。预期:<[APP:VALIDATION]> 但为:<[HTTP:CONNECTIVITY]> 在 org.mule.munit.runner.flow.TestFlow.run(TestFlow.java:313) 引起:java.io.IOException:远程已关闭 原因:java.io.IOException:远程关闭

HTTP这确实很奇怪——因为与这个流程完全无关。

最后我WARN在控制台发现了一个很奇怪的地方:

警告 2021-05-25 13:04:35,613 [Mule.app.deployer.monitor.1.thread.1] [处理器:; 事件:] com.mulesoft.agent.configuration.descriptor.YamlMuleAgentDescriptor:描述符文件 /home/xxxxxxx/apps/AnypointStudio-7.7.0-linux64/AnypointStudio/plugins/org.mule.tooling.server.4.3.0.ee_7。 3.5.202105101830/mule/conf/invoices-esb-mule4-app.yml 未找到。

我不知道这里发生了什么,我从来没有创建过这样的文件invoices-esb-mule4-app.yml。我假设这是 Mule 本身根据应用程序名称创建的某种配置文件。这里的路径很奇怪,因为我正在处理的工作区位于/home/xxxxxxx/AnypointStudio7/workspace-2/

其他可能有帮助(或没有帮助)的信息

  • 我正在开发 ApiKit SOAP 服务,但它仍然运行良好。 …

mule mule-studio anypoint-studio apikit mule4

1
推荐指数
1
解决办法
2656
查看次数