Skip to content

Commit 5bd916d

Browse files
authored
Merge pull request #2683 from ControlSystemStudio/CSSTUDIO-1934
Trim pv name in PVPool
2 parents 82c7bf2 + b5c6df4 commit 5bd916d

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

core/pv/src/main/java/org/phoebus/pv/PVPool.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ public String toString()
153153
*/
154154
public static Set<String> getNameVariants(final String name, final String [] equivalent_pv_prefixes)
155155
{
156+
final String _name = name.trim();
156157
// First, look for name as given
157158
final Set<String> variants = new LinkedHashSet<>();
158-
variants.add(name);
159+
variants.add(_name);
159160
if (equivalent_pv_prefixes != null && equivalent_pv_prefixes.length > 0)
160161
{ // Optionally, if the original name is one of the equivalent types ...
161-
final TypedName typed = TypedName.analyze(name);
162+
final TypedName typed = TypedName.analyze(_name);
162163
for (String type : equivalent_pv_prefixes)
163164
if (type.equals(typed.type))
164165
{
@@ -201,15 +202,16 @@ public static Collection<String> getSupportedPrefixes()
201202
*/
202203
public static PV getPV(final String name) throws Exception
203204
{
204-
if (name.isBlank())
205+
final String _name = name.trim();
206+
if (_name.isBlank())
205207
throw new Exception("Empty PV name");
206-
final TypedName type_name = TypedName.analyze(name);
208+
final TypedName type_name = TypedName.analyze(_name);
207209
final PVFactory factory = factories.get(type_name.type);
208210
if (factory == null)
209-
throw new Exception(name + " has unknown PV type '" + type_name.type + "'");
211+
throw new Exception(_name + " has unknown PV type '" + type_name.type + "'");
210212

211-
final String core_name = factory.getCoreName(name);
212-
final ReferencedEntry<PV> ref = pool.createOrGet(core_name, () -> createPV(factory, name, type_name.name));
213+
final String core_name = factory.getCoreName(_name);
214+
final ReferencedEntry<PV> ref = pool.createOrGet(core_name, () -> createPV(factory, _name, type_name.name));
213215
logger.log(Level.CONFIG, () -> "PV '" + ref.getEntry().getName() + "' references: " + ref.getReferences());
214216
return ref.getEntry();
215217
}

core/pv/src/test/java/org/phoebus/pv/PVPoolTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,47 @@ public void equivalentPVs()
5757
assertThat(pvs, hasItem("ca://ramp"));
5858
assertThat(pvs, hasItem("pva://ramp"));
5959

60+
// Repeat to verify trimming of pv name
61+
pvs = PVPool.getNameVariants("pva://ramp\n", equivalent_pv_prefixes);
62+
assertThat(pvs.size(), equalTo(3));
63+
assertThat(pvs, hasItem("ramp"));
64+
assertThat(pvs, hasItem("ca://ramp"));
65+
assertThat(pvs, hasItem("pva://ramp"));
66+
67+
pvs = PVPool.getNameVariants("pva://ramp ", equivalent_pv_prefixes);
68+
assertThat(pvs.size(), equalTo(3));
69+
assertThat(pvs, hasItem("ramp"));
70+
assertThat(pvs, hasItem("ca://ramp"));
71+
assertThat(pvs, hasItem("pva://ramp"));
72+
73+
6074
// For loc or sim which are not in the equivalent list, pass name through
6175
pvs = PVPool.getNameVariants("loc://ramp", equivalent_pv_prefixes);
6276
assertThat(pvs.size(), equalTo(1));
6377
assertThat(pvs, hasItem("loc://ramp"));
6478

79+
// Repeat to verify trimming of pv name
80+
pvs = PVPool.getNameVariants("loc://ramp\n", equivalent_pv_prefixes);
81+
assertThat(pvs.size(), equalTo(1));
82+
assertThat(pvs, hasItem("loc://ramp"));
83+
84+
pvs = PVPool.getNameVariants("loc://ramp ", equivalent_pv_prefixes);
85+
assertThat(pvs.size(), equalTo(1));
86+
assertThat(pvs, hasItem("loc://ramp"));
87+
6588
pvs = PVPool.getNameVariants("sim://ramp", equivalent_pv_prefixes);
6689
assertThat(pvs.size(), equalTo(1));
6790
assertThat(pvs, hasItem("sim://ramp"));
91+
92+
// Repeat to verify trimming of pv name
93+
pvs = PVPool.getNameVariants("sim://ramp\n", equivalent_pv_prefixes);
94+
assertThat(pvs.size(), equalTo(1));
95+
assertThat(pvs, hasItem("sim://ramp"));
96+
97+
pvs = PVPool.getNameVariants("sim://ramp ", equivalent_pv_prefixes);
98+
assertThat(pvs.size(), equalTo(1));
99+
assertThat(pvs, hasItem("sim://ramp"));
100+
68101
}
69102

70103

core/pv/src/test/java/org/phoebus/pv/SimPVTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@ public void demoSine() throws Exception
3737
PVPool.releasePV(pv);
3838
}
3939

40+
@Test
41+
public void demoSineWithTrimming1() throws Exception
42+
{
43+
final CountDownLatch done = new CountDownLatch(3);
44+
45+
System.out.println("Awaiting " + done.getCount() + " updates...");
46+
final PV pv = PVPool.getPV("sim://sine\n");
47+
final Disposable flow = pv.onValueEvent()
48+
.subscribe(value ->
49+
{
50+
System.out.println("Received update " + value);
51+
done.countDown();
52+
});
53+
done.await();
54+
flow.dispose();
55+
PVPool.releasePV(pv);
56+
}
57+
58+
@Test
59+
public void demoSineWithTrimming2() throws Exception
60+
{
61+
final CountDownLatch done = new CountDownLatch(3);
62+
63+
System.out.println("Awaiting " + done.getCount() + " updates...");
64+
final PV pv = PVPool.getPV("sim://sine ");
65+
final Disposable flow = pv.onValueEvent()
66+
.subscribe(value ->
67+
{
68+
System.out.println("Received update " + value);
69+
done.countDown();
70+
});
71+
done.await();
72+
flow.dispose();
73+
PVPool.releasePV(pv);
74+
}
75+
4076
@Test
4177
public void demoConst() throws Exception
4278
{

0 commit comments

Comments
 (0)