-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding AtomService for creating and caching new atoms.
- Loading branch information
Showing
10 changed files
with
273 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/github/moaxcp/x11client/AtomService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.github.moaxcp.x11client; | ||
|
||
import com.github.moaxcp.x11client.protocol.AtomValue; | ||
import com.github.moaxcp.x11client.protocol.xproto.Atom; | ||
import com.github.moaxcp.x11client.protocol.xproto.InternAtom; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static com.github.moaxcp.x11client.protocol.Utilities.stringToByteList; | ||
|
||
public class AtomService { | ||
private final XProtocolService protocolService; | ||
private Map<Integer,AtomValue> atomIds = new HashMap<>(); | ||
private Map<String, AtomValue> atomNames = new HashMap<>(); | ||
public AtomService(XProtocolService protocolService) { | ||
this.protocolService = protocolService; | ||
for(Atom atom : Atom.values()) { | ||
add(new AtomValue(atom.getValue(), atom.toString())); | ||
} | ||
} | ||
|
||
private void add(AtomValue atom) { | ||
atomIds.put(atom.getId(), atom); | ||
atomNames.put(atom.getName(), atom); | ||
} | ||
|
||
public AtomValue getAtom(Atom atom) { | ||
return atomIds.get(atom.getValue()); | ||
} | ||
|
||
public Optional<AtomValue> getAtom(int id) { | ||
return Optional.ofNullable(atomIds.get(id)); | ||
} | ||
|
||
public AtomValue getAtom(String name) { | ||
if(atomNames.containsKey(name)) { | ||
return atomNames.get(name); | ||
} | ||
int id = protocolService.send(InternAtom.builder().name(stringToByteList(name)).nameLen((short) name.length()).build()).getAtom(); | ||
AtomValue result = new AtomValue(id, name); | ||
add(result); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/github/moaxcp/x11client/protocol/AtomValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.moaxcp.x11client.protocol; | ||
|
||
import lombok.NonNull; | ||
import lombok.Value; | ||
|
||
/** | ||
* An atom. May be a predefined atom or an intern atom. | ||
*/ | ||
@Value | ||
public class AtomValue { | ||
int id; | ||
@NonNull | ||
String name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/com/github/moaxcp/x11client/AtomServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.github.moaxcp.x11client; | ||
|
||
import com.github.moaxcp.x11client.protocol.AtomValue; | ||
import com.github.moaxcp.x11client.protocol.xproto.Atom; | ||
import com.github.moaxcp.x11client.protocol.xproto.InternAtom; | ||
import com.github.moaxcp.x11client.protocol.xproto.InternAtomReply; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class AtomServiceTest { | ||
@Mock | ||
private XProtocolService protocolService; | ||
private AtomService service; | ||
|
||
@BeforeEach | ||
void setup() { | ||
service = new AtomService(protocolService); | ||
} | ||
|
||
@Test | ||
void getAtomId_has_predefined_atoms() { | ||
AtomValue atom = service.getAtom(Atom.BITMAP); | ||
assertThat(atom.getId()).isEqualTo(Atom.BITMAP.getValue()); | ||
assertThat(atom.getName()).isEqualTo("BITMAP"); | ||
} | ||
|
||
@Test | ||
void getAtomName_has_predefined_atoms() { | ||
AtomValue atom = service.getAtom("BITMAP"); | ||
assertThat(atom.getId()).isEqualTo(Atom.BITMAP.getValue()); | ||
assertThat(atom.getName()).isEqualTo("BITMAP"); | ||
} | ||
|
||
@Test | ||
void getAtomName_creates_new_atoms() { | ||
given(protocolService.send(any(InternAtom.class))).willReturn(InternAtomReply.builder().atom(100).build()); | ||
AtomValue atom = service.getAtom("DELETE_WINDOW"); | ||
assertThat(atom.getId()).isEqualTo(100); | ||
assertThat(atom.getName()).isEqualTo("DELETE_WINDOW"); | ||
then(protocolService).should().send(any(InternAtom.class)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/com/github/moaxcp/x11client/protocol/AtomValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.moaxcp.x11client.protocol; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class AtomValueTest { | ||
@Test | ||
void null_name_fails() { | ||
NullPointerException exception = assertThrows(NullPointerException.class, () -> new AtomValue(1, null)); | ||
assertThat(exception).hasMessage("name is marked non-null but is null"); | ||
} | ||
} |