fix tests

This commit is contained in:
Bane Stojkovic
2025-03-24 12:05:47 +01:00
parent eafb4b1307
commit 449bce435f
4 changed files with 58 additions and 240 deletions
@@ -78,7 +78,7 @@ public class DatasheetService {
public String[] lookup(Class<?> clazz, double tolerance, String key, public String[] lookup(Class<?> clazz, double tolerance, String key,
double... approximateValues) { double... approximateValues) {
int keySize = (int) (key.chars().filter(i -> i == '|').count() + 1); int keySize = (int) (key.chars().filter(i -> i == ',').count() + 1);
Map<String, List<String[]>> classData = Map<String, List<String[]>> classData =
lookupCache.computeIfAbsent(clazz.getCanonicalName(), k -> { lookupCache.computeIfAbsent(clazz.getCanonicalName(), k -> {
@@ -90,7 +90,7 @@ public class DatasheetService {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < keySize; i++) { for (int i = 0; i < keySize; i++) {
if (i > 0) { if (i > 0) {
sb.append("|"); sb.append(",");
} }
sb.append(line[i]); sb.append(line[i]);
} }
@@ -98,6 +98,10 @@ public class DatasheetService {
})); }));
}); });
if (classData == null) {
return null;
}
List<String[]> list = classData.get(key); List<String[]> list = classData.get(key);
if (list == null) { if (list == null) {
@@ -107,7 +111,7 @@ public class DatasheetService {
outerloop: for (String[] line : list) { outerloop: for (String[] line : list) {
for (int i = 0; i < approximateValues.length; i++) { for (int i = 0; i < approximateValues.length; i++) {
String valueStr = line[keySize + i]; String valueStr = line[keySize + i];
double actualValue = Double.parseDouble(valueStr.split(" ")[0]); double actualValue = Double.parseDouble(valueStr);
if (Math.abs(actualValue - approximateValues[i]) / actualValue * 100 > tolerance) if (Math.abs(actualValue - approximateValues[i]) / actualValue * 100 > tolerance)
continue outerloop; continue outerloop;
} }
@@ -1,266 +1,80 @@
package org.diylc.presenter; package org.diylc.presenter;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class) import java.util.List;
import static org.junit.Assert.*;
public class DatasheetServiceTest { public class DatasheetServiceTest {
private DatasheetService service; private DatasheetService service;
private static final Class<?> TEST_CLASS = DatasheetServiceTest.class;
@Mock
private ClassLoader mockClassLoader;
@Before @Before
public void setUp() { public void setUp() {
service = DatasheetService.getInstance(); service = DatasheetService.getInstance();
service.clearCache(); // Clear cache before each test service.clearCache();
Thread.currentThread().setContextClassLoader(mockClassLoader);
} }
@Test @Test
public void testLoadDatasheet() throws Exception { public void testLoadDatasheet() {
// Create a mock datasheet content List<String[]> result = service.loadDatasheet(TEST_CLASS);
String datasheetContent = assertNotNull(result);
"1N4001,50V,1A,1.1V\n" + assertEquals(4, result.size());
"1N4002,100V,1A,1.1V\n" + assertArrayEquals(new String[]{"TEST", "KEY", "10.0", "20.0"}, result.get(0));
"1N4003,200V,1A,1.1V";
// Create a mock URL that returns our test content
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
// Setup the mock ClassLoader to return our mock URL
when(mockClassLoader.getResource("TestComponent.datasheet"))
.thenReturn(mockUrl);
// Test loading the datasheet
List<String[]> result = service.loadDatasheet(TestComponent.class);
// Verify results
assertNotNull("Datasheet should not be null", result);
assertEquals("Should have 3 entries", 3, result.size());
// Verify first entry
String[] firstEntry = result.get(0);
assertEquals("1N4001", firstEntry[0]);
assertEquals("50V", firstEntry[1]);
assertEquals("1A", firstEntry[2]);
assertEquals("1.1V", firstEntry[3]);
// Verify second entry
String[] secondEntry = result.get(1);
assertEquals("1N4002", secondEntry[0]);
assertEquals("100V", secondEntry[1]);
// Verify third entry
String[] thirdEntry = result.get(2);
assertEquals("1N4003", thirdEntry[0]);
assertEquals("200V", thirdEntry[1]);
} }
@Test @Test
public void testLoadDatasheetFileNotFound() { public void testLoadDatasheetFileNotFound() {
// Setup the mock ClassLoader to return null (file not found) List<String[]> result = service.loadDatasheet(String.class);
when(mockClassLoader.getResource("TestComponent.datasheet")) assertNull(result);
.thenReturn(null);
List<String[]> result = service.loadDatasheet(TestComponent.class);
assertNull("Result should be null when file is not found", result);
} }
@Test @Test
public void testLookupExactMatch() throws Exception { public void testLookupExactMatch() {
// Create a mock datasheet content String[] result = service.lookup(TEST_CLASS, 0.0, "TEST,KEY", 10.0);
String datasheetContent = assertNotNull(result);
"TEST,KEY,10.0\n" +
"TEST,KEY,20.0\n" +
"TEST,OTHER,15.0\n" +
"OTHER,KEY,10.0";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("DatasheetServiceTest.datasheet"))
.thenReturn(mockUrl);
// Create test data
String[] result = service.lookup(
DatasheetServiceTest.class, // Using test class itself as component class
0.1, // 0.1% tolerance
"TEST|KEY", // Key to look up
10.0 // Value to match
);
assertNotNull("Should find matching entry", result);
assertEquals("TEST", result[0]);
assertEquals("KEY", result[1]);
assertEquals("10.0", result[2]); assertEquals("10.0", result[2]);
} }
@Test @Test
public void testLookupWithinTolerance() throws Exception { public void testLookupWithinTolerance() {
// Create a mock datasheet content String[] result = service.lookup(TEST_CLASS, 5.0, "TEST,KEY", 10.5);
String datasheetContent = assertNotNull(result);
"TEST,KEY,10.0\n" +
"TEST,KEY,20.0\n" +
"TEST,OTHER,15.0\n" +
"OTHER,KEY,10.0";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("DatasheetServiceTest.datasheet"))
.thenReturn(mockUrl);
// Should match 10.0 with 5% tolerance when looking for 10.2
String[] result = service.lookup(
DatasheetServiceTest.class,
5.0, // 5% tolerance
"TEST|KEY",
10.2 // Should match 10.0 within 5% tolerance
);
assertNotNull("Should find matching entry within tolerance", result);
assertEquals("10.0", result[2]); assertEquals("10.0", result[2]);
} }
@Test @Test
public void testLookupNoMatch() throws Exception { public void testLookupOutsideTolerance() {
// Create a mock datasheet content String[] result = service.lookup(TEST_CLASS, 5.0, "TEST,KEY", 12.0);
String datasheetContent = assertNull(result);
"TEST,KEY,10.0\n" +
"TEST,KEY,20.0\n" +
"TEST,OTHER,15.0\n" +
"OTHER,KEY,10.0";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("DatasheetServiceTest.datasheet"))
.thenReturn(mockUrl);
// Should not match when value is outside tolerance
String[] result = service.lookup(
DatasheetServiceTest.class,
1.0, // 1% tolerance
"TEST|KEY",
15.0 // No value close enough to match
);
assertNull("Should not find matching entry", result);
} }
@Test @Test
public void testLookupNonexistentKey() throws Exception { public void testLookupNoMatch() {
// Create a mock datasheet content String[] result = service.lookup(TEST_CLASS, 0.0, "TEST,KEY", 30.0);
String datasheetContent = assertNull(result);
"TEST,KEY,10.0\n" +
"TEST,KEY,20.0\n" +
"TEST,OTHER,15.0\n" +
"OTHER,KEY,10.0";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("DatasheetServiceTest.datasheet"))
.thenReturn(mockUrl);
String[] result = service.lookup(
DatasheetServiceTest.class,
0.1,
"NONEXISTENT|KEY", // Key that doesn't exist in datasheet
10.0
);
assertNull("Should not find matching entry for nonexistent key", result);
} }
@Test @Test
public void testLookupWithTolerance() throws Exception { public void testLookupNonexistentKey() {
// Create a mock datasheet content with resistor values String[] result = service.lookup(TEST_CLASS, 0.0, "NONEXISTENT,KEY", 10.0);
String datasheetContent = assertNull(result);
"0805,1%,100,0.125W\n" +
"0805,1%,220,0.125W\n" +
"0805,1%,470,0.125W";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("TestComponent.datasheet"))
.thenReturn(mockUrl);
// Test lookup with 5% tolerance (should match 220 when looking for 225)
String[] result = service.lookup(TestComponent.class, 5.0, "0805|1%", 225.0);
assertNotNull("Should find matching entry within tolerance", result);
assertEquals("0805", result[0]);
assertEquals("1%", result[1]);
assertEquals("220", result[2]);
} }
@Test @Test
public void testLookupOutsideTolerance() throws Exception { public void testLookupWithTolerance() {
// Create a mock datasheet content String[] result = service.lookup(TEST_CLASS, 50.0, "TEST,KEY", 15.0);
String datasheetContent = assertNotNull(result);
"0805,1%,100,0.125W\n" + assertEquals("10.0", result[2]);
"0805,1%,220,0.125W\n" +
"0805,1%,470,0.125W";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("TestComponent.datasheet"))
.thenReturn(mockUrl);
// Test lookup with value outside tolerance
String[] result = service.lookup(TestComponent.class, 1.0, "0805|1%", 250.0);
assertNull("Should not find matching entry outside tolerance", result);
} }
@Test @Test
public void testLookupMultipleValues() throws Exception { public void testLookupMultipleValues() {
// Create a mock datasheet content with multiple value matches String[] result = service.lookup(TEST_CLASS, 0.0, "TEST,KEY", 10.0, 20.0);
String datasheetContent = assertNotNull(result);
"TO220,NPN,50,5,0.7\n" + // Vce, Ic, Vbe assertEquals("10.0", result[2]);
"TO220,NPN,60,8,0.7\n" + assertEquals("20.0", result[3]);
"TO220,NPN,100,10,0.7";
URL mockUrl = mock(URL.class);
when(mockUrl.openStream()).thenReturn(
new ByteArrayInputStream(datasheetContent.getBytes()));
when(mockClassLoader.getResource("TestComponent.datasheet"))
.thenReturn(mockUrl);
// Test lookup matching multiple values within tolerance
String[] result = service.lookup(TestComponent.class, 5.0, "TO220|NPN",
98.0, 9.8, 0.71);
assertNotNull("Should find matching entry for multiple values", result);
assertEquals("TO220", result[0]);
assertEquals("NPN", result[1]);
assertEquals("100", result[2]);
assertEquals("10", result[3]);
assertEquals("0.7", result[4]);
}
// Test component class
private static class TestComponent {
// Empty class just for testing
} }
} }
@@ -0,0 +1,4 @@
TEST,KEY,10.0,20.0
TEST,KEY,20.0,30.0
TEST,OTHER,15.0,25.0
OTHER,KEY,10.0,20.0
@@ -1,4 +0,0 @@
TEST,KEY,10.0 mm
TEST,KEY,20.0 mm
TEST,OTHER,15.0 mm
OTHER,KEY,10.0 mm