Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ abstract class AbstractURLClassPathHandleTest extends BaseURLClassPathHandleTest
void testGetPriority() {
assertEquals(DEFAULT_PRIORITY, handle.getPriority());
}

@Test
void testSetPriority() {
int newPriority = 42;
handle.setPriority(newPriority);
assertEquals(newPriority, handle.getPriority());
// Restore default priority
handle.setPriority(DEFAULT_PRIORITY);
assertEquals(DEFAULT_PRIORITY, handle.getPriority());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ void testDetectOnEmptySet() {
ArtifactDetector instance = new ArtifactDetector(null);
assertTrue(instance.detect(emptySet()).isEmpty());
}

@Test
void testDetectWithIncludedJdkLibraries() {
ArtifactDetector instance = new ArtifactDetector();
List<Artifact> artifacts = instance.detect(true);
assertNotNull(artifacts);
}

@Test
void testDetectWithExcludedJdkLibraries() {
ArtifactDetector instance = new ArtifactDetector();
List<Artifact> artifacts = instance.detect(false);
assertNotNull(artifacts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,44 @@ void testSize() {
deque.poll();
assertEquals(0, deque.size());
}

@Test
void testRemoveFirstOccurrenceWithMultipleOccurrences() {
TestDeque<String> multiDeque = new TestDeque<>();
multiDeque.add("A");
multiDeque.add("B");
multiDeque.add("A");
// removeFirstOccurrence delegates to remove(o) in AbstractDeque which removes the first occurrence
assertTrue(multiDeque.removeFirstOccurrence("A"));
assertEquals(2, multiDeque.size());
// "B" and the second "A" should remain, in order
Iterator<String> it = multiDeque.iterator();
assertEquals("B", it.next());
assertEquals("A", it.next());
assertFalse(it.hasNext());
}

@Test
void testOfferAndPollSequence() {
TestDeque<String> multiDeque = new TestDeque<>();
assertTrue(multiDeque.offer("X"));
assertTrue(multiDeque.offer("Y"));
assertTrue(multiDeque.offer("Z"));
// offer delegates to offerLast; poll delegates to pollFirst (FIFO)
assertEquals("X", multiDeque.poll());
assertEquals("Y", multiDeque.poll());
assertEquals("Z", multiDeque.poll());
assertNull(multiDeque.poll());
}

@Test
void testPushAndPop() {
TestDeque<String> multiDeque = new TestDeque<>();
multiDeque.push("X");
multiDeque.push("Y");
// push delegates to addFirst (LIFO)
assertEquals("Y", multiDeque.pop());
assertEquals("X", multiDeque.pop());
assertThrows(NoSuchElementException.class, () -> multiDeque.pop());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static io.microsphere.collection.Lists.ofList;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* {@link Lists} Test
Expand Down Expand Up @@ -77,4 +78,12 @@
assertEquals(emptyList(), ofList(TEST_NULL_OBJECT_ARRAY));
assertEquals(of(1, 2, 3), ofList(1, 2, 3));
}

@Test
void testOfListImmutability() {
assertThrows(UnsupportedOperationException.class, () -> ofList().add("x"));

Check warning on line 84 in microsphere-java-core/src/test/java/io/microsphere/collection/ListsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6baf1jy-UnSmeHyi&open=AZ0E6baf1jy-UnSmeHyi&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofList(1).add(2));

Check warning on line 85 in microsphere-java-core/src/test/java/io/microsphere/collection/ListsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6baf1jy-UnSmeHyj&open=AZ0E6baf1jy-UnSmeHyj&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofList(1, 2).remove(0));

Check warning on line 86 in microsphere-java-core/src/test/java/io/microsphere/collection/ListsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6baf1jy-UnSmeHyk&open=AZ0E6baf1jy-UnSmeHyk&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofList(1, 2, 3).clear());

Check warning on line 87 in microsphere-java-core/src/test/java/io/microsphere/collection/ListsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6baf1jy-UnSmeHyl&open=AZ0E6baf1jy-UnSmeHyl&pullRequest=257
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* {@link Maps} Test
Expand Down Expand Up @@ -186,4 +187,22 @@
Map map = Maps.ofMap(new Map.Entry[0]);
assertSame(emptyMap(), map);
}

@Test
void testOfMapOnSingleEntry() {
Map.Entry<String, Integer> entry = ofEntry("A", 1);
Map<String, Integer> map = Maps.ofMap(entry);
assertEquals(1, map.size());
assertEquals(1, map.get("A"));
assertNull(map.get("B"));
assertOfMap(map);
}

@Test
void testOfMapImmutability() {
assertThrows(UnsupportedOperationException.class, () -> ofMap().put("k", "v"));

Check warning on line 203 in microsphere-java-core/src/test/java/io/microsphere/collection/MapsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6bWn1jy-UnSmeHye&open=AZ0E6bWn1jy-UnSmeHye&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofMap("A", 1).put("B", 2));

Check warning on line 204 in microsphere-java-core/src/test/java/io/microsphere/collection/MapsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6bWn1jy-UnSmeHyf&open=AZ0E6bWn1jy-UnSmeHyf&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofMap("A", 1, "B", 2).remove("A"));

Check warning on line 205 in microsphere-java-core/src/test/java/io/microsphere/collection/MapsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6bWn1jy-UnSmeHyg&open=AZ0E6bWn1jy-UnSmeHyg&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofMap("A", 1, "B", 2, "C", 3).clear());

Check warning on line 206 in microsphere-java-core/src/test/java/io/microsphere/collection/MapsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6bWn1jy-UnSmeHyh&open=AZ0E6bWn1jy-UnSmeHyh&pullRequest=257
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static io.microsphere.collection.Sets.ofSet;
import static java.util.Collections.emptySet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* {@link Sets} Test
Expand Down Expand Up @@ -77,4 +78,12 @@
assertEquals(emptySet(), ofSet(TEST_NULL_OBJECT_ARRAY));
assertEquals(of(1, 2, 3), ofSet(1, 2, 3));
}

@Test
void testOfSetImmutability() {
assertThrows(UnsupportedOperationException.class, () -> ofSet().add("x"));

Check warning on line 84 in microsphere-java-core/src/test/java/io/microsphere/collection/SetsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6ba01jy-UnSmeHym&open=AZ0E6ba01jy-UnSmeHym&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofSet(1).add(2));

Check warning on line 85 in microsphere-java-core/src/test/java/io/microsphere/collection/SetsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6ba01jy-UnSmeHyn&open=AZ0E6ba01jy-UnSmeHyn&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofSet(1, 2).remove(1));

Check warning on line 86 in microsphere-java-core/src/test/java/io/microsphere/collection/SetsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6ba01jy-UnSmeHyo&open=AZ0E6ba01jy-UnSmeHyo&pullRequest=257
assertThrows(UnsupportedOperationException.class, () -> ofSet(1, 2, 3).clear());

Check warning on line 87 in microsphere-java-core/src/test/java/io/microsphere/collection/SetsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0E6ba01jy-UnSmeHyp&open=AZ0E6ba01jy-UnSmeHyp&pullRequest=257
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link AbstractConverter} Test
Expand Down Expand Up @@ -102,4 +103,25 @@ void testEquals() {
assertNotEquals(this.converter, StringToBooleanConverter.INSTANCE);
assertNotEquals(this.converter, ObjectToStringConverter.INSTANCE);
}

@Test
void testHashCode() {
AbstractConverter<String, String> another = createConverter();
assertEquals(this.converter.hashCode(), another.hashCode());
assertNotEquals(this.converter.hashCode(), StringToBooleanConverter.INSTANCE.hashCode());
assertNotEquals(this.converter.hashCode(), ObjectToStringConverter.INSTANCE.hashCode());
}

@Test
void testResolvePriority() {
// When resolvePriority() returns a non-null value, getPriority() returns that value
AbstractConverter<String, Integer> converter = new AbstractConverter<String, Integer>() {
@Override
protected Integer doConvert(String source) {
return Integer.parseInt(source);
}
};
// The priority is derived from the type hierarchy and must be negative
assertTrue(converter.getPriority() < 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.microsphere.convert.multiple;

import io.microsphere.convert.StringConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static java.lang.Integer.MAX_VALUE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link StringToIterableConverter} Test
*
* <p>Tests the abstract {@link StringToIterableConverter} behavior using
* {@link StringToCollectionConverter} as the concrete implementation.</p>
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see StringToIterableConverter
* @see StringToCollectionConverter
* @since 1.0.0
*/
class StringToIterableConverterTest {

private StringToCollectionConverter converter;

@BeforeEach
void setUp() {
converter = new StringToCollectionConverter();
}

@Test
void testAccept() {
assertTrue(converter.accept(String.class, Collection.class));
assertTrue(converter.accept(String.class, List.class));
assertTrue(converter.accept(String.class, Set.class));
assertFalse(converter.accept(String.class, String.class));
assertFalse(converter.accept(null, null));
}

@Test
void testConvert() {
Collection<Integer> result = (Collection<Integer>) converter.convert("1,2,3", Collection.class, Integer.class);
assertNotNull(result);
assertEquals(3, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(2));
assertTrue(result.contains(3));

Collection<String> strResult = (Collection<String>) converter.convert("a,b", Collection.class, String.class);
assertNotNull(strResult);
assertEquals(2, strResult.size());
assertTrue(strResult.contains("a"));
assertTrue(strResult.contains("b"));
}

@Test
void testConvertOnNoStringConverter() {
// No StringConverter registered for Object.class → returns null
assertNull(converter.convert(new String[]{"a"}, 1, Collection.class, Object.class));
}

@Test
void testConvertOnNonCollectionIterable() {
// When createMultiValue returns a non-Collection Iterable, the elements are not added;

Check warning on line 89 in microsphere-java-core/src/test/java/io/microsphere/convert/multiple/StringToIterableConverterTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This block of commented-out lines of code should be removed.

See more on https://sonarcloud.io/project/issues?id=microsphere-projects_microsphere-java&issues=AZ0Ee675mtjZY35jACiA&open=AZ0Ee675mtjZY35jACiA&pullRequest=257
// the iterable is returned as-is once a StringConverter is found for the element type.
StringToIterableConverter<Iterable> iterableConverter = new StringToIterableConverter<Iterable>() {
@Override
protected Iterable createMultiValue(int size, Class<?> multiValueType) {
return Collections::emptyIterator;
}
};

Object result = iterableConverter.convert(new String[]{"1", "2"}, 2, Iterable.class, Integer.class);
assertNotNull(result);
}

@Test
void testGetStringConverter() {
// Integer has a registered StringConverter
Optional<StringConverter> intConverter = converter.getStringConverter(Integer.class);
assertTrue(intConverter.isPresent());

// Object.class has no registered StringConverter
Optional<StringConverter> objectConverter = converter.getStringConverter(Object.class);
assertFalse(objectConverter.isPresent());
}

@Test
void testGetSupportedType() {
assertEquals(Collection.class, converter.getSupportedType());
}

@Test
void testGetPriority() {
// Collection has 1 interface level above Iterable, so level=1 → priority = MAX_VALUE - 1
assertEquals(MAX_VALUE - 1, converter.getPriority());
}

@Test
void testGetSourceType() {
assertEquals(String.class, converter.getSourceType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static io.microsphere.util.ArrayUtils.ofArray;
import static io.microsphere.util.ClassLoaderUtils.getResource;
import static io.microsphere.util.ExceptionUtils.wrap;
import static io.microsphere.collection.Lists.ofList;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.copy;
import static java.nio.file.Files.write;
Expand Down Expand Up @@ -265,4 +266,51 @@ private void async(ThrowableAction task) {
}
}

@Test
void testIsStartedBeforeStart() {
StandardFileWatchService fileWatchService = new StandardFileWatchService();
assertFalse(fileWatchService.isStarted());
}

@Test
void testCloseWithoutStart() throws Exception {
StandardFileWatchService fileWatchService = new StandardFileWatchService();
assertFalse(fileWatchService.isStarted());
fileWatchService.close();
assertFalse(fileWatchService.isStarted());
}

@Test
void testWatchWithMultipleListeners() throws Exception {
AtomicReference<File> fileReference = new AtomicReference<>();

try (StandardFileWatchService fileWatchService = new StandardFileWatchService()) {

FileChangedListener listener1 = new FileChangedListener() {
@Override
public void onFileCreated(FileChangedEvent event) {
fileReference.set(event.getFile());
}
};

FileChangedListener listener2 = new LoggingFileChangedListener();

fileWatchService.watch(this.testDir, ofList(listener1, listener2), CREATED);

fileWatchService.start();

assertTrue(fileWatchService.isStarted());

File testFile = createRandomFile(testDir);

while (!testFile.equals(fileReference.get())) {
// spin
}

fileWatchService.stop();

assertFalse(fileWatchService.isStarted());
}
}

}
Loading
Loading